mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-04-25 09:52:12 +00:00
Add math builtins implementation (#139)
This commit is contained in:
parent
56638c8fe2
commit
f8abe728c0
@ -65,6 +65,52 @@ describe('Tests for default handler', () => {
|
||||
${'debug'} | ${'stringify'} | ${[{a: 10, b: 20}]} | ${0} | ${a10b20}}
|
||||
${'debug'} | ${'stringify'} | ${[1, 2, 3, 4]} | ${0} | ${oneTwoThreeFour}}
|
||||
|
||||
${'math'} | ${'add'}" | ${[2, 2]} | ${0} | ${4}
|
||||
${'math'} | ${'add'}" | ${[2]} | ${1} | ${"Expected 2 argument(s). Got 1"}
|
||||
|
||||
${'math'} | ${'sub'}" | ${[2, 2]} | ${0} | ${0}
|
||||
${'math'} | ${'sub'}" | ${[2, 3]} | ${0} | ${-1}
|
||||
|
||||
${'math'} | ${'mul'}" | ${[2, 2]} | ${0} | ${4}
|
||||
${'math'} | ${'mul'}" | ${[2, 0]} | ${0} | ${0}
|
||||
${'math'} | ${'mul'}" | ${[2, -1]} | ${0} | ${-2}
|
||||
|
||||
${'math'} | ${'fmul'}" | ${[10, 0.66]} | ${0} | ${6}
|
||||
${'math'} | ${'fmul'}" | ${[0.5, 0.5]} | ${0} | ${0}
|
||||
${'math'} | ${'fmul'}" | ${[100.5, 0.5]} | ${0} | ${50}
|
||||
|
||||
${'math'} | ${'div'}" | ${[2, 2]} | ${0} | ${1}
|
||||
${'math'} | ${'div'}" | ${[2, 3]} | ${0} | ${0}
|
||||
${'math'} | ${'div'}" | ${[10, 5]} | ${0} | ${2}
|
||||
|
||||
${'math'} | ${'rem'}" | ${[10, 3]} | ${0} | ${1}
|
||||
|
||||
${'math'} | ${'pow'}" | ${[2, 2]} | ${0} | ${4}
|
||||
${'math'} | ${'pow'}" | ${[2, 0]} | ${0} | ${1}
|
||||
|
||||
${'math'} | ${'log'}" | ${[2, 2]} | ${0} | ${1}
|
||||
${'math'} | ${'log'}" | ${[2, 4]} | ${0} | ${2}
|
||||
|
||||
${'cmp'} | ${'gt'}" | ${[2, 4]} | ${0} | ${false}
|
||||
${'cmp'} | ${'gte'}" | ${[2, 4]} | ${0} | ${false}
|
||||
${'cmp'} | ${'gte'}" | ${[4, 2]} | ${0} | ${true}
|
||||
${'cmp'} | ${'gte'}" | ${[2, 2]} | ${0} | ${true}
|
||||
|
||||
${'cmp'} | ${'lt'}" | ${[2, 4]} | ${0} | ${true}
|
||||
${'cmp'} | ${'lte'}" | ${[2, 4]} | ${0} | ${true}
|
||||
${'cmp'} | ${'lte'}" | ${[4, 2]} | ${0} | ${false}
|
||||
${'cmp'} | ${'lte'}" | ${[2, 2]} | ${0} | ${true}
|
||||
|
||||
${'cmp'} | ${'cmp'}" | ${[2, 4]} | ${0} | ${-1}
|
||||
${'cmp'} | ${'cmp'}" | ${[2, -4]} | ${0} | ${1}
|
||||
${'cmp'} | ${'cmp'}" | ${[2, 2]} | ${0} | ${0}
|
||||
|
||||
${'array'} | ${'sum'}" | ${[[1, 2, 3]]} | ${0} | ${6}
|
||||
${'array'} | ${'dedup'}" | ${[["a", "a", "b", "c", "a", "b", "c"]]} | ${0} | ${["a", "b", "c"]}
|
||||
${'array'} | ${'intersect'}" | ${[["a", "b", "c"], ["c", "b", "d"]]} | ${0} | ${["b", "c"]}
|
||||
${'array'} | ${'diff'}" | ${[["a", "b", "c"], ["c", "b", "d"]]} | ${0} | ${["a"]}
|
||||
${'array'} | ${'sdiff'}" | ${[["a", "b", "c"], ["c", "b", "d"]]} | ${0} | ${["a", "d"]}
|
||||
|
||||
`.test(
|
||||
//
|
||||
'$fnName with $args expected retcode: $retCode and result: $result',
|
||||
|
@ -288,79 +288,183 @@ export const builtInServices = {
|
||||
|
||||
math: {
|
||||
add: (req) => {
|
||||
return errorNotImpl('math.add');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x + y);
|
||||
},
|
||||
|
||||
sub: (req) => {
|
||||
return errorNotImpl('math.sub');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x - y);
|
||||
},
|
||||
|
||||
mul: (req) => {
|
||||
return errorNotImpl('math.mul');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x * y);
|
||||
},
|
||||
|
||||
fmul: (req) => {
|
||||
return errorNotImpl('math.fmul');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(Math.floor(x * y));
|
||||
},
|
||||
|
||||
div: (req) => {
|
||||
return errorNotImpl('math.div');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(Math.floor(x / y));
|
||||
},
|
||||
|
||||
rem: (req) => {
|
||||
return errorNotImpl('math.rem');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x % y);
|
||||
},
|
||||
|
||||
pow: (req) => {
|
||||
return errorNotImpl('math.pow');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(Math.pow(x, y));
|
||||
},
|
||||
|
||||
log: (req) => {
|
||||
return errorNotImpl('math.log');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(Math.log(y) / Math.log(x));
|
||||
},
|
||||
},
|
||||
|
||||
cmp: {
|
||||
gt: (req) => {
|
||||
return errorNotImpl('cmp.gt');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x > y);
|
||||
},
|
||||
|
||||
gte: (req) => {
|
||||
return errorNotImpl('cmp.gte');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x >= y);
|
||||
},
|
||||
|
||||
lt: (req) => {
|
||||
return errorNotImpl('cmp.lt');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x < y);
|
||||
},
|
||||
|
||||
lte: (req) => {
|
||||
return errorNotImpl('cmp.lte');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x <= y);
|
||||
},
|
||||
|
||||
cmp: (req) => {
|
||||
return errorNotImpl('cmp.cmp');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [x, y] = req.args;
|
||||
return success(x === y ? 0 : x > y ? 1 : -1);
|
||||
},
|
||||
},
|
||||
|
||||
array: {
|
||||
sum: (req) => {
|
||||
return errorNotImpl('array.sum');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 1))) {
|
||||
return err;
|
||||
}
|
||||
const [xs] = req.args;
|
||||
return success(xs.reduce((agg, cur) => agg + cur, 0));
|
||||
},
|
||||
|
||||
dedup: (req) => {
|
||||
return errorNotImpl('array.dedup');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 1))) {
|
||||
return err;
|
||||
}
|
||||
const [xs] = req.args;
|
||||
const set = new Set(xs);
|
||||
return success(Array.from(set));
|
||||
},
|
||||
|
||||
intersect: (req) => {
|
||||
return errorNotImpl('array.intersect');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [xs, ys] = req.args;
|
||||
const intersection = xs.filter((x) => ys.includes(x));
|
||||
return success(intersection);
|
||||
},
|
||||
|
||||
diff: (req) => {
|
||||
return errorNotImpl('array.diff');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [xs, ys] = req.args;
|
||||
const diff = xs.filter((x) => !ys.includes(x));
|
||||
return success(diff);
|
||||
},
|
||||
|
||||
sdiff: (req) => {
|
||||
return errorNotImpl('array.sdiff');
|
||||
let err;
|
||||
if ((err = checkForArgumentsCount(req, 2))) {
|
||||
return err;
|
||||
}
|
||||
const [xs, ys] = req.args;
|
||||
const sdiff = [
|
||||
// force new line
|
||||
...xs.filter((y) => !ys.includes(y)),
|
||||
...ys.filter((x) => !xs.includes(x)),
|
||||
];
|
||||
return success(sdiff);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const checkForArgumentsCount = (req, count: number) => {
|
||||
if (req.args.length !== count) {
|
||||
return error(`Expected ${count} argument(s). Got ${req.args.length}`);
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user