GithubHelp home page GithubHelp logo

gajus / pianola Goto Github PK

View Code? Open in Web Editor NEW
20.0 2.0 1.0 44 KB

A declarative function composition and evaluation engine.

License: Other

JavaScript 95.81% Nearley 4.19%
declarative evaluator pipe-operator

pianola's Introduction

Pianola

Travis build status Coveralls NPM version Canonical Code Style Twitter Follow

A declarative function composition and evaluation engine.

Use cases

  • Surgeon uses Pianola to extract information from HTML documents using a declarative API.

Configuration

Name Type Description Default value
bindle Object (optional) A user-defined object that is passed to every subroutine.
handleResult ResultHandlerType (optional) A function invoked after each subroutine with the result of the current subroutine and the subject value used to execute the subroutine. Use this method to throw an error. Return
subroutines $PropertyType<UserConfigurationType, 'subroutines'> User defined subroutines. See subroutines. N/A

Subroutines

A subroutine is a function used to advance the evaluator, e.g.

x('foo | bar baz', 'qux');

In the above example, Pianola expression uses two subroutines: foo and bar.

  • foo subroutine is invoked without values.
  • bar subroutine is executed with 1 value ("baz").

Subroutines are executed in the order in which they are defined – the result of the last subroutine is passed on to the next one. The first subroutine receives the value used to start the evaluator.

Multiple subroutines can be written as an array. The following example is equivalent to the earlier example.

x([
  'foo',
  'bar baz'
], 'qux');

Note:

These functions are called subroutines to emphasise the cross-platform nature of the declarative API.

Defining subroutines

Subroutines are defined using the subroutines configuration.

A subroutine is a function. A subroutine function is invoked with the following parameters:

Parameter name
Subject value, i.e. value used to start the evaluator or result of the parent subroutine.
An array of parameter values used in the expression.
Bindle. See subroutines configuration.

Example:

const x = pianola({
  subroutines: {
    mySubourtine: (subjectValue, [firstParameterValue, secondParameterValue]) => {
      console.log(subjectValue, firstParameterValue, secondParameterValue);

      return parseInt(subjectValue, 10) + 1;
    }
  }
});

x('mySubourtine foo bar | mySubourtine baz qux', 0);

The above example prints:

0 "foo" "bar"
1 "baz" "qux"

Inline subroutines

Subroutines can be inlined by adding a function to the instructions array, e.g.

x([
  'foo',
  'bar',
  (subjectValue) => {
    return subjectValue.toUpperCase();
  },
], 'qux');

Sentinels

FinalResultSentinel

FinalResultSentinel is used to signal the final value, i.e. it will interrupt the routine and return the value used to construct an instance of FinalResultSentinel.

Example:

import pianola, {
  FinalResultSentinel
} from 'pianola';

const x = pianola({
  subroutines: {
    a: () => {
      return new FinalResultSentinel(null);
    },
    b: () => {
      throw new Error('This method is never invoked.');
    }
  }
});

x('a | b', 0);

Expression reference

Pianola subroutines are described using expressions.

An expression is defined using the following pseudo-grammar:

subroutines ->
    subroutines _ ">|" _ subroutine
  | subroutines _ "|" _ subroutine
  | subroutine

subroutine ->
    subroutineName " " parameters
  | subroutineName

subroutineName ->
  [a-zA-Z0-9\-_]:+

parameters ->
    parameters " " parameter
  | parameter

Example:

x('foo bar baz', 'qux');

In this example,

  • Pianola expression evaluator (x) is invoked with foo bar baz expression and qux starting value.
  • The expression tells the expression evaluator to run foo subroutine with parameter values "bar" and "baz".
  • The expression evaluator runs foo subroutine with parameter values "bar" and "baz" and a subject value "qux".

Multiple subroutines can be combined using an array:

x([
  'foo bar baz',
  'corge grault garply'
], 'qux');

In this example,

  • Pianola expression evaluator (x) is invoked with two expressions (foo bar baz and corge grault garply).
  • The first subroutine is executed with the subject value "qux".
  • The second subroutine is executed with a value that is the result of the parent subroutine.

The result of the query is the result of the last subroutine.

Read define subroutines documentation for broader explanation of the role of the parameter values and the subject value.

The pipeline operator (|)

Multiple subroutines can be combined using the pipeline operator (|).

The pipeline operator tells expression evaluator to pass the result of the previous subroutine to the next subroutine. If the subroutine on the left-hand side returns an array, then the receiving subroutine is called for every value in the array.

The following examples are equivalent:

x([
  'foo bar baz',
  'qux quux quuz'
]);

x([
  'foo bar baz | foo bar baz'
]);

x('foo bar baz | foo bar baz');

The aggregate pipeline operator (>|)

Multiple subroutines can be combined using the aggregate pipeline operator (>|).

The pipeline operator tells expression evaluator to pass the result of the previous subroutine to the next subroutine. If the subroutine on the left-hand side returns an array, then the receiving subroutine is called with an array of values.

The following examples are equivalent:

x('foo >| bar');

Cookbook

Unless redefined, all examples assume the following initialisation:

import pianola from 'pianola';

/**
 * @param configuration {@see https://github.com/gajus/pianola#configuration}
 */
const x = pianola();

Map multiple results

When a subroutine results multiple results, then the rest of the expression is evaluated for each of the result.

const foo = () => {
  return [
    1,
    2,
    3
  ];
};

const bar = (value) => {
  if (value === 1) {
    return 'one';
  }

  if (value === 2) {
    return 'two';
  }

  if (value === 3) {
    return 'three';
  }
};

const x = pianola({
  subroutines: {
    bar,
    foo
  }
});

x('foo | bar');

// [
//   'one',
//   'two',
//   'three'
// ]

Name results

Use a QueryChildrenType object (a plain object whose values are Pianola expressions) to name the results.

const foo = (subjectValue, [name]) => {
  return name;
};

const x = pianola({
  subroutines: {
    foo
  }
});

x([
  {
    foo0: 'foo corge',
    foo1: 'foo grault',
    foo2: 'foo garply'
  }
]);

// [
//   {
//     name: 'corge'
//   },
//   {
//     name: 'grault'
//   },
//   {
//     name: 'garply'
//   }
// ]

Error handling

Pianola throws the following errors to indicate a predictable error state. All Pianola errors can be imported. Use instanceof operator to determine the error type.

Name Description
NotFoundError Used to indicate that a resource is not found, e.g. when a subroutine is not found.
PianolaError A generic error. All other Pianola errors extend from PianolaError.

Logging

This package is using roarr logger to log the program's state.

Export ROARR_LOG=true environment variable to enable log printing to stdout.

Use roarr-cli program to pretty-print the logs.

pianola's People

Contributors

gajus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rsercano

pianola's Issues

Async/Await Support

Nevermind, it seems this already works! Was just testing wrong.
Thanks for this gajus!

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


The push permission to the Git repository is required.

semantic-release cannot push the version tag to the branch master on remote Git repository with URL https://github.com/gajus/pianola.

Please refer to the authentication configuration documentation to configure the Git credentials on your CI environment and make sure the repositoryUrl is configured with a valid Git URL.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Collate, combine, spread ... operator

I'm using surgeon in this example, but the idea should be good for pianola.

Given

<body>
  <a href="/relative1">Relative 1</a>
  <a href="/relative2">Relative 2</a>
  <a href="/relative1">Relative1 Duplicate</a>
  <a href="http://www.example.com/relative1">Absolute a duplicate of Relative1 after resolve</a>
</body>

What I want to do is return an array with resolved urls and remove duplicates.

uniqueAbsoluteLinks: sa 'body a:not([href^="#"]'|sa href|resolve http://www.example.com/ ... uniq

Result:

{
  links: [
    'http://www.example.com/relative1',
    'http://www.example.com/relative2'
  ]
}

What happens here is that

  • 'sa href' is executed once per link element
  • resolve is executed once per returned href string
  • all the resolved href strings are combined to an array
  • and uniq is executed once on the whole array

Another alternative syntax would be parentheses, but its not as pretty:

uniqueAbsoluteLinks: sa 'uniq(body a:not([href^="#"]'|sa href|resolve)

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


The push permission to the Git repository is required.

semantic-release cannot push the version tag to the branch master on remote Git repository with URL https://github.com/gajus/pianola.

Please refer to the authentication configuration documentation to configure the Git credentials on your CI environment and make sure the repositoryUrl is configured with a valid Git URL.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Add ability to parse parameters containing quotes

e.g.

foo bar[" "]baz

should be parsed as:

subroutine: foo
  - bar["
  - "]baz

When there is a couple of quotes, then the content between the quotes should make a single entity, i.e.

  • foo "a b" matches a single group a b.
  • foo "a b matches two groups "a and b.
  • foo a b" matches two groups a and b".
  • foo 'a b' matches a single group a b.
  • foo 'a b matches two groups 'a and b.
  • foo a b' matches two groups a and b'.
  • foo a b matches two groups a and b.
  • foo "a" 'b' matches two groups a and b.

The following tests are expected to be passing:

9a81fdf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.