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

exp-parser

v1.0.1

Published

expression parser

Downloads

5

Readme

npm version Build Status Coverage Status

Expression Parser(exp-parser)

##安装

npm install --save exp-parser

##问题

  1. 为啥要山寨一个表达式解析器?
  2. 表达式解析器能做神马事情?
  3. 怎么自制表达式解析器?

##回答

  1. 因为好玩,因为市面上好像还没有一款独立的表达式解析器,如果有的话,体积估计也不小,我打算做一个较为轻量级,同时扩展性较强的表达式解析器,功能与angular1.x的指令表达式解析器一致,最重要是的可扩展,提供内核级别的扩展性
  2. 类似于angular的指令表达式,你可以集成在模板引擎中,主要用于解析组件的属性,组件于组件间传值计算,过滤,话说。。。。这其实就是将视图逻辑从后端中释放出来。
  3. 本人其实就是个初学者,原来一直想搞个表达式解析器,苦于无从下手,后来找到了jsep这个表达式AST生成器,它能自动解析表达式,生成一个json结构的抽象语法树,所以,后面我仅仅只需要深度遍历这棵树就行了,然后边遍历边计算,注意是后序遍历,就这样一层一层的剥开计算结果的心。其实真心没啥技术含量,我做这个仅仅只是为了好玩,又或者说后面自己需要的时候也不需要各种找东西,麻烦。。。。

##API文档

###解析

import ExpressionParser form 'expression-parser'

let parser = new ExpressionParser({//注入一个全局上下文,比如将window注入进去,不过不建议这样做,如果方法被拦截,可能会发生危险,尽量是将闭包内受保护的对象注入其中
	
})

parser.parse('2 * 2') // 4

parser.parse('a * b',{ //变量求值
	a:4,
	b:5
}) // 20

parser.parse('fuck(a,b)',{//函数求值
	fuck(x,y){
		return x * y;
	},
	a:5,
	b:6
}) // 30

parser.parse('a * b|fuck:34:cc',{//作用域注入
	a:5,
	b:6,
	cc:7
},{//过滤器注入
	fuck(x,y){
		return (input)=>{
			return (input - x) / y;
		}
	}
})

###扩展


const {BINARY_EXP} = ExpressionParser.expressionTypes;


ExpressionParser.injectExpHandler(BINARY_EXP,(tree)=>{

})

//添加一个优先级为10的二进制操作符^
ExpressionParser.addBinaryOp("^", 10);

//添加一个一元操作符@
ExpressionParser.addUnaryOp('@');

//移除一个二进制操作符
ExpressionParser.removeBinaryOp(">>>");

//移除一个一元操作符
ExpressionParser.removeUnaryOp("~");