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

easy-expression

v0.2.1

Published

A expression interpreter for JavaScript

Downloads

6

Readme

EasyExpression

可定制化表达式解释器

功能

基本功能

  • 字面量: number string
  • 组合嵌套: ( )
  • 二元运算符 + - * /
  • 函数: 无预设函数

可定制化功能

  • 可选字面量
  • 字面量捕获
  • 注入常量
  • 标识符捕获
  • 注入函数
  • 运算符重载
  • 自定义二元运算符

开始使用

安装


# 使用npm安装
npm install easy-expression --save

# 使用yarn安装
yarn add easy-expression

表达式解析配置


import compile from 'easy-expression';

const runner = compile('2 AND 2 IS MULTIPLE(2, 2)', {
  // 配置二元运算符
  operators: [
    {
      token: 'AND',
      precedence: 12
    },
    {
      token: 'IS',
      precedence: 11
    }
  ]
});

表达式执行配置


const result = runner.run({
  // 运算符执行器
  operatorHandlers: {
    'AND'(num1, num2) {
      return num1 && num2;
    },
    'IS'(num1, num2) {
      return num1 === num2;
    }
  },
  // 函数执行器
  functionHandlers: {
    MULTIPLE(num1, num2) {
      return num1 * num2;
    }
  }
});

console.log(result); // true

API

// compile
// 解析表达式字符串为抽象语法树,返回执行器

declare function compile(
  expression: string, // 表达式字符串 
  languageConfig?: LanguageConfigParams, // 语法解析配置
): Runner; // 执行器
// Runner
// 表达式执行器

declare class Runner {
    evalStack: Array<ExpressionNode> | null; // 抽象语法树执行栈
    run(runtimeConfig: RuntimeConfigParams): any; // 执行抽象语法树并返回结果
}
// config
// 配置

// 表达式解析配置
declare interface LanguageConfigParams {
    literals?: boolean | Array<'number' | 'string'>; // 配置字面量解析,true-默认全部, false-不解析字面量, array-选择解析数字或字符串,默认['number', 'string']
    operators?: boolean | Array<{token: string, precedence: number}>; // 配置运算符,true-使用默认运算符,false-无运算符,array-指定运算符和运算符优先级,默认 + - * /
    allowFunction?: boolean; // 是否支持函数语法,默认true
}

// 表达式执行配置
declare interface RuntimeConfigParams {
    constants?: {[key: string]: any}; // 常量,非运算符及函数的运算符将视为常量
    constantsHandler?: ((token: string) => any) | null; // 常量捕获器,按自定义逻辑解析常量的值,优先级低于constants
	functionHandlers?: {[key: string]: (...params: any[]) => any}; // 函数执行器
	literalHandler?: (literal: string) => any; // 字面量捕获器,按自定义逻辑解析字面量
	operatorHandlers?: {[key: string]: (...params: any[]) => any} // 运算符执行器
}
// ExpressionNode
// 表达式抽象语法树节点

// 字面量节点
declare type LiteralNode = {
  type: 'literal'; // 类型
  value: string; // 值
  startPos: string; // 节点开始位置
  endPos: string; // 节点结束位置
}

// 标识符节点
declare type IdentNode = {
  type: 'ident';
  value: string;
  startPos: string;
  endPos: string;
};

// 二元运算节点
declare type BinaryNode = {
  type: 'binary';
  left: ExpressionNode; // 左侧节点
  right: ExpressionNode; // 右侧节点
  op: string; // 运算符
  startPos: string;
  endPos: string;
}

// 函数调用节点
declare type CallNode = {
  type: 'call';
  func: string; // 函数
  args: Array<ExpressionNode>; // 参数节点
  startPos: string;
  endPos: string;
}

// 表达式语法树节点
declare type ExpressionNode = LiteralNode | IdentNode | CallNode | BinaryNode;