Git Product home page Git Product logo

ctxexp-parser's Introduction

Ctxexp Parser

上下文表达式解析器

在不能动态执行 JS 语言的环境(微信小程序)里执行类 JS 的调用函数。

Feature

  • 轻量包,未压缩 16k,压缩 4k
  • 单元测试 >85% 的代码覆盖
  • 使用状态机编写词法分析
  • 动态执行表达式
  • 调用方法支持传入 Number +1 -1 1.1
  • 调用方法支持传入 String
  • 调用方法支持传入 Callback ([参数])=><上下文表达式>
  • 支持传入多个参数
  • 支持嵌套调用
  • 语法错误检查
  • 支持链式调用、链式访问
  • 支持数组的访问

Tip

  • 不支持在表达式中使用空格 如 $.fn(1, 2)
  • 回调函数只支持简单的箭头函数

Install

npm i ctxexp-parser
# yarn add ctxexp-parser

Uses

const { CtxexpParser } = require("ctxexp-parser");
// import { CtxexpParser } from "ctxexp-parser";

const $ = {
  a: "hello",
  fn(str, str2) {
    return str + str2;
  },
  runs(...cbs) {
    return cbs.map((cb, index) => cb(index));
  },
  Type(i) {
    return ["A", "D", "C"][i];
  },
};

const exp = `$.a`;
const res = new CtxexpParser($, exp).exec();
console.log(res); // hello

const exp1 = `$.fn($.a," cxtexp")`;
const res1 = new CtxexpParser($, exp1).exec();
console.log(res1); // hello cxtexp

const exp2 = `$.runs((i)=>$.Type(i)).join("")`;
const res2 = new CtxexpParser($, exp2).exec();
console.log(res2); // A

查看更多例子

State

state

About

MIT License

ctxexp-parser's People

Contributors

wumacoder avatar

Stargazers

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

Watchers

 avatar  avatar

Forkers

zhizouvip

ctxexp-parser's Issues

refactor stater

Description

使用类似于xstate库去重构状态机。

Target

  • 增加代码可读性
  • 减少代码量

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.