asgc-operator

1.0.6 • Public • Published

欢迎使asgc-operator

asgc-operator是傲世孤尘开源的一个运算符定义工具,它内置了一些常见的数学运算(+、-、*、/、...),并且通过接口可以扩展我们自己的运算符,带来前所未有的编程体验。特点概述:

  • 使用简单 :三行代码可完成运算符定义。
  • 例程丰富 :对于本工具的使用提供了详细的示例程序。

如何使用asgc-operator

1、安装模块

npm install asgc-operator

2、引入模块

var $ = require('asgc-operator');

3、使用内置运算符进行计算

var $ = require('asgc-operator');

//加
console.log($.parse('3+2'));

//中间可以加空格
//减
console.log($.parse('3 -2'));

//乘
console.log($.parse('3 * 2'));

//除
console.log($.parse('3/2'));

//运算符可以包含多个字符
//求余
console.log($.parse('3MOD2'));
console.log($.parse('3 % 2'));

//阶乘
console.log($.parse('5!'));

//取反
console.log($.parse('!true'));

//乘方
console.log($.parse('2 ^ 3'));

3.1、输出结果

5
1
6
1.5
1
1
120
false
8

4、变量赋值、引用

var $ = require('asgc-operator');

//方法名p是parse的简写

//赋值运算
$.p('a = hello');
$.p('b = 3');

//引用变量进行运算
console.log($.p('1 + {b}'));
console.log($.p('{b} ^ 2'));

//没有运算符直接输出
console.log($.p('{a} world'));

4.1、输出结果

4
9
hello world

5、使用上下文

var $ = require('asgc-operator');

//上下文对象
var o = {
	name: '张三'
};

//设置上下文
$.ctx(o);

//为上下文添加age变量,值为20
$.p('age=20');

//输出上下文
console.log(o);

//引用上下文变量
console.log($.p('姓名: {name} , 年龄: {age}'));

5.1、输出结果

{ name: '张三', age: [Number: 20] }
姓名: 张三 , 年龄: 20

6、自定义运算符

var $ = require('asgc-operator');

//双目运算符,o是operator的简写,都可使用
$.o('binary','吃',function(a,b){
	return $.p(a) + '吃了一个' + $.p(b);
});

//为上下文添加变量
$.p('me=傲世孤尘');
$.p('JT=鸡腿');

//调用运算符计算
//不用变量
console.log($.p('张三吃梨子'));
//使用变量
console.log($.p('张三吃{JT}'));
console.log($.p('{me}吃{JT}'));

//单目运算符,操作数后置
//定义平方根
$.o('unaryAfter','sqrt',function(a){
	return Math.sqrt(a);
});

console.log($.p('sqrt 9'));

//单目运算符,操作数前置
//定义hello
$.o('unaryBefore','hello',function(a){
	return a + ' 你好!';
});

console.log($.p('aoshiguchen hello'));

6.1、输出结果

张三吃了一个梨子
张三吃了一个鸡腿
傲世孤尘吃了一个鸡腿
3
aoshiguchen 你好!

asgc-operator相关

asgc-observer

asgc-observer是傲世孤尘开源的一个面向事件、消息编程的库。

asgc-lisen

asgc-lisen是傲世孤尘开源的一个监听变量值、表达式变化的库。基于这个库,笔者已实现一个前段双向绑定的工具。类似于vue、angularjs。

###asgc-web asgc-web是傲世孤尘开源的一个nodejs web容器,mvc框架。实现了后端路由、请求分发、参数解析、前端模板渲染等等。

联系我们

笔者QQ 1052045476 nodejs交流群 527393872 java交流群 527393872 c/c++语言交流群 251975693

Readme

Keywords

none

Package Sidebar

Install

npm i asgc-operator

Weekly Downloads

0

Version

1.0.6

License

ISC

Unpacked Size

9.19 kB

Total Files

7

Last publish

Collaborators

  • aoshiguchen