npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

exprml-ts

v0.0.5

Published

The ExprML interpreter for TypeScript and JavaScript.

Downloads

118

Readme

exprml-ts

exprml-ts is a TypeScript and JavaScript library implementing an ExprML interpreter. The ExprML is a programming language that can evaluate expressions represented in JSON (and JSON-compatible YAML).

The ExprML language specification is available at https://github.com/exprml/exprml-language .

Installation

npm install @bufbuild/[email protected] exprml-ts

Examples

Evaluate the expression an expression represented in the YAML format.

import {
    DecodeInputSchema,
    Decoder,
    EncodeInputSchema,
    Encoder,
    EvaluateInputSchema,
    Evaluator,
    ParseInputSchema,
    Parser
} from "exprml-ts";
import {create} from "@bufbuild/protobuf";

// Decode the source string to a JSON value of JavaScript.
const decodeResult = new Decoder()
    .decode(create(DecodeInputSchema, {text: "cat: ['`Hello`', '`, `', '`ExprML`', '`!`']"}));
// Parse an AST from the JSON value.
const parseResult = new Parser()
    .parse(create(ParseInputSchema, {value: decodeResult.value}));
// Evaluate expression of the AST as a JSON value.
const evaluateResult = new Evaluator()
    .evaluateExpr(create(EvaluateInputSchema, {expr: parseResult.expr}));
// Encode the JSON value to a string.
const encodeResult = new Encoder()
    .encode(create(EncodeInputSchema, {value: evaluateResult.value}));

console.log(encodeResult.text);
// => Hello, ExprML!

Call JavaScript function from ExprML.

import {
    Config,
    DecodeInputSchema,
    Decoder,
    EncodeInputSchema,
    Encoder,
    EvaluateInputSchema, EvaluateOutput, EvaluateOutputSchema,
    Evaluator, Expr_Path,
    ParseInputSchema,
    Parser, strValue, Value, ValueSchema
} from "exprml-ts";
import {create} from "@bufbuild/protobuf";

const decodeResult = new Decoder()
    .decode(create(DecodeInputSchema, {text: "cat: ['`Hello`', '`, `', '`ExprML`', '`!`']"}));

const parseResult = new Parser()
    .parse(create(ParseInputSchema, {value: decodeResult.value}));

const evaluator = new Evaluator(new Config({
    extension: new Map([
        // Define an extension function named $hello, which takes an argument $name and returns a greeting string.
        ["$hello", (path: Expr_Path, args: Record<string, Value>): EvaluateOutput => {
            const name = args["$name"];
            return create(EvaluateOutputSchema, {
                value: create(ValueSchema, strValue(`Hello, ${name.str}!`)),
            });
        }],
    ]),
}));
const evaluateResult = evaluator.evaluateExpr(create(EvaluateInputSchema, {expr: parseResult.expr}));

const encodeResult = new Encoder().encode(create(EncodeInputSchema, {value: evaluateResult.value}));
console.log(encodeResult.text);
// => Hello, Extension!

Hook PHP functions before and after each evaluation of nested expressions.

import {
    Config,
    DecodeInputSchema,
    Decoder,
    EvaluateInput,
    EvaluateInputSchema, EvaluateOutput,
    Evaluator, format,
    ParseInputSchema,
    Parser, ValueSchema
} from "exprml-ts";
import {create, toJsonString} from "@bufbuild/protobuf";

const decodeResult = new Decoder()
    .decode(create(DecodeInputSchema, {text: "cat: ['`Hello`', '`, `', '`ExprML`', '`!`']"}));

const parseResult = new Parser()
    .parse(create(ParseInputSchema, {value: decodeResult.value}));


const evaluator = new Evaluator(new Config({
    /* Hook a function before the evaluation of each expression. */
    beforeEvaluate: (input: EvaluateInput) => {
        console.log(`before:\t${format(input.expr!.path!)}`);
    },
    /* Hook a function after the evaluation of each expression. */
    afterEvaluate: (input: EvaluateInput, output: EvaluateOutput) => {
        console.log(`after:\t${format(input.expr!.path!)} --> ${toJsonString(ValueSchema, output.value!)}`);
    }
}));

evaluator.evaluateExpr(create(EvaluateInputSchema, {expr: parseResult.expr}));
// =>
// before: /
// before: /cat/0
// after:  /cat/0 --> {"type":"STR","str":"Hello"}
// before: /cat/1
// after:  /cat/1 --> {"type":"STR","str":", "}
// before: /cat/2
// after:  /cat/2 --> {"type":"STR","str":"ExprML"}
// before: /cat/3
// after:  /cat/3 --> {"type":"STR","str":"!"}
// after:  / --> {"type":"STR","str":"Hello, ExprML!"}