8000 1. remvoe eval 2. curry math functions · TYPECASTINGSG/rpscript-api-basic@808c46e · GitHub
[go: up one dir, main page]

Skip to content

Commit 808c46e

Browse files
committed
1. remvoe eval 2. curry math functions
1 parent ebd1ff8 commit 808c46e

File tree

6 files changed

+56
-124
lines changed

6 files changed

+56
-124
lines changed

libs/mathjs/math.min.js

-56
This file was deleted.

libs/mathjs/math.min.map

-1
This file was deleted.

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"publish": "tsc && npm publish"
2323
},
2424
"dependencies": {
25-
"rpscript-interface": "^0.6.1"
25+
"rpscript-interface": "^0.6.2"
2626
},
2727
"devDependencies": {
2828
"@types/chai": "^4.1.4",

src/index.ts

+15-49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {RpsContext,RpsModule,rpsAction} from 'rpscript-interface';
1+
import {RpsContext,RpsModule,rpsAction,R} from 'rpscript-interface';
22
import { EventEmitter } from 'events';
3-
import * as math from '../libs/mathjs/math.min';
43

54
/** Basic utility for rpscript. Contain basic operation such as condition, event listening, variable assignment, terminal print etc.
65
* @namespace Basic
@@ -159,39 +158,6 @@ export default class RPSBasic {
159158
});
160159
}
161160

162-
/**
163-
* @function eval
164-
* @memberof Basic
165-
* @example
166-
* ;wait for 5 second
167-
* eval '1 + 2'
168-
*
169-
* @param {string} alegbra The alegbra to apply.
170-
* @returns {*} result of the calculation.
171-
* @summary Evaluate a mathematical equation.
172-
*
173-
*/
174-
@rpsAction({verbName:'eval'})
175-
async evaluate (ctx:RpsContext,opts:Object, expression:string, ...args:any[]) : Promise<any>{
176-
let retFn = opts['function'];
177-
let expr = math.compile(expression);
178-
179-
let objArg = this.argMapToObj(args);
180-
var that = this;
181-
182-
let lateFn = function (...fnargs:any[]) {
183-
let allArgs = args.concat(fnargs);
184-
let objArg = that.argMapToObj(allArgs);
185-
186-
return expr.eval(objArg);
187-
}
188-
< 9E88 /div>
189-
if(retFn===true) return lateFn;
190-
else if(retFn===false) return expr.eval(objArg);
191-
else if(objArg) return expr.eval(objArg);
192-
else return lateFn;
193-
}
194-
195161
/**
196162
* @function abs
197163
* @memberof Basic
@@ -204,8 +170,8 @@ export default class RPSBasic {
204170
*
205171
*/
206172
@rpsAction({verbName:'abs'})
207-
async abs (ctx:RpsContext,opts:{}, num:number) : Promise<number>{
208-
return Math.abs(num);
173+
async abs (ctx:RpsContext,opts:{}, ...args:number[]) : Promise<number|Function>{
174+
return R.apply(R.curry(Math.abs),args);
209175
}
210176
/**
211177
* @function ceil
@@ -219,8 +185,8 @@ async abs (ctx:RpsContext,opts:{}, num:number) : Promise<number>{
219185
*
220186
*/
221187
@rpsAction({verbName:'ceil'})
222-
async ceil (ctx:RpsContext,opts:{}, num:number) : Promise<number>{
223-
return Math.ceil(num);
188+
async ceil (ctx:RpsContext,opts:{}, ...args:number[]) : Promise<number|Function>{
189+
return R.apply(R.curry(Math.ceil),args);
224190
}
225191
/**
226192
* @function max
@@ -233,9 +199,9 @@ async ceil (ctx:RpsContext,opts:{}, num:number) : Promise<number>{
233199
* @returns {number} number.
234200
*
235201
*/
236-
@rpsAction({verbName:'max'})
202+
@rpsAction({verbName:'math-max'})
237203
async max (ctx:RpsContext,opts:{}, ...num:number[]) : Promise<number>{
238-
return Math.max.apply(this,num);
204+
return R.apply(R.curryN(2,Math.max),num);
239205
}
240206
/**
241207
* @function min
@@ -248,9 +214,9 @@ async max (ctx:RpsContext,opts:{}, ...num:number[]) : Promise<number>{
248214
* @returns {number} number.
249215
*
250216
*/
251-
@rpsAction({verbName:'min'})
217+
@rpsAction({verbName:'math-min'})
252218
async min (ctx:RpsContext,opts:{}, ...num:number[]) : Promise<number>{
253-
return Math.min.apply(this,num);
219+
return R.apply(R.curryN(2,Math.min),num);
254220
}
255221
/**
256222
* @function floor
@@ -264,8 +230,8 @@ async min (ctx:RpsContext,opts:{}, ...num:number[]) : Promise<number>{
264230
*
265231
*/
266232
@rpsAction({verbName:'floor'})
267-
async floor (ctx:RpsContext,opts:{}, num:number) : Promise<number>{
268-
return Math.floor(num);
233+
async floor (ctx:RpsContext,opts:{}, ...num:number[]) : Promise<number>{
234+
return R.apply(R.curry(Math.floor),num);
269235
}
270236
/**
271237
* @function power
@@ -280,8 +246,8 @@ async floor (ctx:RpsContext,opts:{}, num:number) : Promise<number>{
280246
*
281247
*/
282248
@rpsAction({verbName:'pow'})
283-
async power (ctx:RpsContext,opts:{}, x:number, y:number) : Promise<number>{
284-
return Math.pow(x,y);
249+
async power (ctx:RpsContext,opts:{}, ...args:number[]) : Promise<number|Function>{
250+
return R.apply(R.curry(Math.pow),args);
285251
}
286252
/**
287253
* @function random
@@ -309,7 +275,7 @@ async random (ctx:RpsContext,opts:{}) : Promise<number>{
309275
*/
310276
@rpsAction({verbName:'round'})
311277
async round (ctx:RpsContext,opts:{},num:number) : Promise<number>{
312-
return Math.round(num);
278+
return R.apply(R.curry(Math.round),num);
313279
}
314280
/**
315281
* @function trunc
@@ -323,7 +289,7 @@ async round (ctx:RpsContext,opts:{},num:number) : Promise<number>{
323289
*/
324290
@rpsAction({verbName:'trunc'})
325291
async trunc (ctx:RpsContext,opts:{},num:number) : Promise<number>{
326-
return Math.trunc(num);
292+
return R.apply(R.curry(Math.trunc),num);
327293
}
328294

329295
private argMapToObj (args:any[]) : Object{

test/index.ts

+37-14
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ m.describe('Basic', () => {
3333
expect(output).to.be.equals('Hello');
3434
});
3535

36-
m.it('should do calculation', async function () {
37-
let basic = new RPSBasic;
38-
let context = new RpsContext;
36+
// m.it('should do calculation', async function () {
37+
// let basic = new RPSBasic;
38+
// let context = new RpsContext;
3939

40-
let output = await basic.evaluate(context,{},'a + b',5,4);
41-
expect(output).to.be.equal(9);
40+
// let output = await basic.evaluate(context,{},'a + b',5,4);
41+
// expect(output).to.be.equal(9);
4242

43-
output = await basic.evaluate(context,{function:true},'a + b',5);
44-
expect(output(4)).to.be.equal(9);
43+
// output = await basic.evaluate(context,{function:true},'a + b',5);
44+
// expect(output(4)).to.be.equal(9);
4545

46-
output = await basic.evaluate(context,{},'a + 4');
47-
expect(output(10)).to.be.equal(14);
46+
// output = await basic.evaluate(context,{},'a + 4');
47+
// expect(output(10)).to.be.equal(14);
4848

49-
output = await b 1241 asic.evaluate(context,{},'9 + 4');
50-
expect(output(1)).to.be.equal(13);
49+
// output = await basic.evaluate(context,{},'9 + 4');
50+
// expect(output(1)).to.be.equal(13);
5151

52-
output = await basic.evaluate(context,{function:false},'9 + 4');
53-
expect(output).to.be.equal(13);
54-
});
52+
// output = await basic.evaluate(context,{function:false},'9 + 4');
53+
// expect(output).to.be.equal(13);
54+
// });
5555

5656
m.it('should perform maths operation', async function () {
5757
let basic = new RPSBasic;
@@ -61,5 +61,28 @@ m.describe('Basic', () => {
6161
expect(output).to.be.equal(1.23);
6262
});
6363

64+
m.it('should call power with varying argument number', async function () {
65+
let basic = new RPSBasic;
66+
let context = new RpsContext;
67+
68+
let output:any = await basic.power(context,{},2,3);
69+
expect(output).to.be.equal(8);
70+
71+
output = await basic.power(context,{},2);
72+
expect(output(3)).to.be.equal(8);
73+
74+
output = await basic.power(context,{});
75+
expect(output(2,3)).to.be.equal(8);
76+
77+
output = await basic.min(context,{});
78+
expect(output(5,6)).to.be.equal(5);
79+
80+
output = await basic.min(context,{},9);
81+
expect(output(3)).to.be.equal(3);
82+
83+
output = await basic.min(context,{},9,7,5,1);
84+
expect(output).to.be.equal(1);
85+
});
86+
6487

6588
})

0 commit comments

Comments
 (0)
0