@aux4/facilitator

2.0.0 • Public • Published

💬 facilitator

Natural language as a programming language

Installation

npm install --global facilitator

Usage

Command-line

test.txt

set "firstName" to "John"
set "lastName" to "Doe"
print "Hello {{firstName}} {{lastName}}"
facilitator execute test.txt

See all commands

Node.js

Create custom expression

import Facilitator, { install } from "facilitator";

const facilitator = new Facilitator();
install(facilitator);

facilitator.register("say hello to {firstName} {lastName}", (firstName, lastName) => {
  console.log(`Hello ${firstName} ${lastName}!`);
});

const script = `
say hello to "John" "Doe"
`;

facilitator.exec(script, { currentDate: new Date() });

Output:

Hello John Doe

Use optional variable with custom regex

To make the variable optional you can include the ? as suffix of the variable. e.g.: {count?}

By default the variables are represented by quoted text (e.g.: "value"), you can redefine the variable, the third parameter of register receives an object with the regular expression to represent the variable:

{
  count: /\d+/
}
import Facilitator, { install } from "facilitator";

const facilitator = new Facilitator();
install(facilitator);

facilitator.register("increment\\s*{count?}", (count, context) => {
  let value = context.count || parseInt(count);
  value += 1;
  context.count = value;
  console.log("increment", value);
}, {
  count: /\d+/
});

const script = `
increment 5
increment
increment
increment
increment
`;

facilitator.exec(script, {});

Output:

increment 6
increment 7
increment 8
increment 9
increment 10

Package Sidebar

Install

npm i @aux4/facilitator

Weekly Downloads

63

Version

2.0.0

License

Apache-2.0

Unpacked Size

28.2 kB

Total Files

8

Last publish

Collaborators

  • aux4-dev