New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

lambda-math

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lambda-math - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

tests/add_tests.js

6

package.json
{
"name": "lambda-math",
"version": "0.0.3",
"version": "0.0.4",
"description": "Pseudo lambda expressions for JS arbitrary-precision arithmetic operations.",

@@ -25,3 +25,7 @@ "main": "src/index.js",

"mocha": "^7.1.2"
},
"dependencies": {
"bignumber.js": "^9.0.0",
"uuid": "^8.0.0"
}
}

2

README.md

@@ -5,4 +5,2 @@ # Lambda math

**NOTE: Under development. Most of the functionality is still not working.**
## Example

@@ -9,0 +7,0 @@

@@ -0,4 +1,23 @@

const uuidv4 = require('uuid').v4;
const BigNumber = require('bignumber.js');
const resultsStore = {};
let λ_call_count = -1;
function λ() {
const func = arguments[0];
const funcArgsSet = [];
let repeatLastFuncTimes = 1;
let c1;
const CALL_ID = uuidv4();
resultsStore[CALL_ID] = 0;
λ_call_count += 1;
λ[λ_call_count] = new BigNumber(0);
if (!(func instanceof Function)) {
throw new TypeError('λ: 1st param should be a function!');
}
if (

@@ -13,25 +32,156 @@ (func !== add) &&

if (Array.isArray(arguments[1]) === false) {
throw new TypeError('λ: 2nd param should be an array!');
}
funcArgsSet.push(arguments[1]);
c1 = 2;
while (typeof arguments[c1] !== 'undefined') {
if (typeof arguments[c1] === 'number') {
repeatLastFuncTimes = Math.floor(arguments[c1]);
if (Number.isNaN(repeatLastFuncTimes) || repeatLastFuncTimes <= 0) {
throw new Error('λ: Repeat last function call times should be a positive number!');
}
if (typeof arguments[c1 + 1] !== 'undefined') {
throw new Error('λ: Repeat last function call times should be the last parameter!');
}
} else if (Array.isArray(arguments[c1])) {
funcArgsSet.push(arguments[c1]);
} else {
throw new TypeError('λ: 3rd, 4th, ... params can be either an array or a number!');
}
c1 += 1;
}
funcArgsSet.forEach(function (args) {
[args[0], args[1]].forEach((param) => {
if (
(typeof param === 'undefined' || param === null || Number.isNaN(param) === true) ||
(typeof param !== 'number' && param.constructor !== BigNumber && param !== _)
) {
throw new TypeError(`Error! Array item must be a number, a BigNumber, or "_".`);
}
});
});
funcArgsSet.forEach(function (args, idx) {
if (idx === funcArgsSet.length - 1 && repeatLastFuncTimes >= 2) {
return;
}
args.forEach(function (arg, idx) {
if (args[idx] === _) {
args[idx] = resultsStore[CALL_ID];
}
});
resultsStore[CALL_ID] = func(args[0], args[1]);
});
if (repeatLastFuncTimes >= 2) {
const args = [];
for (c1 = 0; c1 < repeatLastFuncTimes; c1 += 1) {
args[0] = funcArgsSet[funcArgsSet.length - 1][0];
args[1] = funcArgsSet[funcArgsSet.length - 1][1];
args.forEach(function (arg, idx) {
if (args[idx] === _) {
args[idx] = resultsStore[CALL_ID];
}
});
resultsStore[CALL_ID] = func(args[0], args[1]);
}
}
λ[λ_call_count] = resultsStore[CALL_ID];
λ[λ_call_count].number = resultsStore[CALL_ID].toNumber();
return λ;
}
function _() {
λ.reset = function () {
let prop;
for (prop in resultsStore) {
if (Object.prototype.hasOwnProperty.call(resultsStore, prop)) {
resultsStore[prop] = undefined;
delete resultsStore[prop];
}
}
while (λ_call_count >= 0) {
λ[λ_call_count] = undefined;
delete λ[λ_call_count];
λ_call_count -= 1;
}
};
function _(callId) {
return resultsStore[callId];
}
function add() {
function verifyFuncParams(funcName, x, y) {
[x, y].forEach((param, idx) => {
if (
(typeof param === 'undefined' || param === null || Number.isNaN(param) === true) ||
(typeof param !== 'number' && param.constructor !== BigNumber)
) {
const paramName = (idx === 0) ? 'First' : 'Second';
throw new TypeError(`${funcName}: Error! ${paramName} param must be a number or a BigNumber.`);
}
});
}
function sub() {
function convertFuncParams(x, y) {
if (typeof x === 'number') {
x = new BigNumber(x);
}
if (typeof y === 'number') {
y = new BigNumber(y);
}
return {x, y};
}
function mul() {
function add(x, y) {
verifyFuncParams('add', x, y);
({x, y} = convertFuncParams(x, y));
return x.plus(y);
}
function div() {
function sub(x, y) {
verifyFuncParams('sub', x, y);
({x, y} = convertFuncParams(x, y));
return x.minus(y);
}
function mul(x, y) {
verifyFuncParams('mul', x, y);
({x, y} = convertFuncParams(x, y));
return x.times(y);
}
function div(x, y) {
verifyFuncParams('div', x, y);
({x, y} = convertFuncParams(x, y));
return x.div(y);
}
module.exports = {

@@ -38,0 +188,0 @@ λ: λ,

@@ -0,1 +1,2 @@

const BigNumber = require('bignumber.js');
const expect = require('chai').expect;

@@ -15,2 +16,6 @@

describe('lambda', function () {
beforeEach(function () {
λ.reset();
});
it('should be defined', function () {

@@ -20,36 +25,461 @@ expect(λ).to.not.be.undefined;

it('should throw if called without params', function () {
expect(function () { λ(); }).to.throw(TypeError, 'λ: Illegal function passed as 1st param!');
});
describe('params', function () {
it('should throw if called without params', function () {
expect(function () { λ(); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should throw if called with wrong function as 1st param', function () {
expect(function () { λ(test_func); }).to.throw(TypeError, 'λ: Illegal function passed as 1st param!');
});
describe('1st param', function () {
it('should throw if called with wrong function as first param', function () {
expect(function () { λ(test_func); }).to.throw(TypeError, 'λ: Illegal function passed as 1st param!');
});
it('should be callable with "add" as 1st param', function () {
expect(function () { λ(add); }).to.not.throw();
});
describe('wrong types', function () {
it('should throw if first param is "string"', function () {
expect(function () { λ('test', []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should be callable with "sub" as 1st param', function () {
expect(function () { λ(sub); }).to.not.throw();
});
it('should throw if first param is "object"', function () {
expect(function () { λ({}, []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should be callable with "mul" as 1st param', function () {
expect(function () { λ(mul); }).to.not.throw();
});
it('should throw if first param is "null"', function () {
expect(function () { λ(null, []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should be callable with "div" as 1st param', function () {
expect(function () { λ(div); }).to.not.throw();
it('should throw if first param is "undefined"', function () {
expect(function () { λ(undefined, []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should throw if first param is "NaN"', function () {
expect(function () { λ(NaN, []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should throw if first param is "number" 0', function () {
expect(function () { λ(0, []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should throw if first param is "number" 1', function () {
expect(function () { λ(1, []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should throw if first param is "boolean" false', function () {
expect(function () { λ(false, []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should throw if first param is "boolean" true', function () {
expect(function () { λ(true, []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
it('should throw if first param is "BigNumber"', function () {
expect(function () { λ(new BigNumber(1), []); }).to.throw(TypeError, 'λ: 1st param should be a function!');
});
});
describe('correct arithmetic function', function () {
it('should be callable with "add" as first param', function () {
expect(function () { λ(add, [1, 1]); }).to.not.throw();
});
it('should be callable with "sub" as first param', function () {
expect(function () { λ(sub, [1, 1]); }).to.not.throw();
});
it('should be callable with "mul" as first param', function () {
expect(function () { λ(mul, [1, 1]); }).to.not.throw();
});
it('should be callable with "div" as first param', function () {
expect(function () { λ(div, [1, 1]); }).to.not.throw();
});
});
});
describe('2nd param', function () {
describe('wrong types', function () {
it('should throw if second param is "string"', function () {
expect(function () { λ(add, 'test'); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "object"', function () {
expect(function () { λ(add, {}); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "null"', function () {
expect(function () { λ(add, null); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "undefined"', function () {
expect(function () { λ(add, undefined); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "NaN"', function () {
expect(function () { λ(add, NaN); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "number" 0', function () {
expect(function () { λ(add, 0); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "number" 1', function () {
expect(function () { λ(add, 1); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "boolean" false', function () {
expect(function () { λ(add, false); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "boolean" true', function () {
expect(function () { λ(add, true); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
it('should throw if second param is "BigNumber"', function () {
expect(function () { λ(add, new BigNumber(1)); }).to.throw(TypeError, 'λ: 2nd param should be an array!');
});
});
describe('right type', function () {
it('should not throw if second param is "array"', function () {
expect(function () { λ(add, [1, 1]); }).to.not.throw();
});
});
});
describe('3rd param', function () {
describe('wrong types', function () {
it('should throw if third param is "string"', function () {
expect(function () { λ(add, [], 'test'); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if third param is "object"', function () {
expect(function () { λ(add, [], {}); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if third param is "null"', function () {
expect(function () { λ(add, [], null); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if third param is "NaN"', function () {
expect(function () { λ(add, [], NaN); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if third param is "number" -1', function () {
expect(function () { λ(add, [], -1); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if third param is "number" 0', function () {
expect(function () { λ(add, [], 0); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if third param is "boolean" false', function () {
expect(function () { λ(add, [], false); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if third param is "boolean" true', function () {
expect(function () { λ(add, [], true); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if third param is "BigNumber"', function () {
expect(function () { λ(add, [], new BigNumber(1)); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
});
describe('correct types', function () {
it('should not throw if third param is "undefined"', function () {
expect(function () { λ(add, [1, 1], undefined); }).to.not.throw();
});
it('should not throw if third param is a positive number', function () {
expect(function () { λ(add, [1, 1], 2); }).to.not.throw();
});
it('should not throw if third param is an array', function () {
expect(function () { λ(add, [1, 1], [1, 1]); }).to.not.throw();
});
});
describe('repeat last function call times', function () {
it('should throw if another "number" is passed after', function () {
expect(function () { λ(add, [], 1, 1); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
it('should throw if another "array" is passed after', function () {
expect(function () { λ(add, [], 1, []); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
it('should throw if another "object" is passed after', function () {
expect(function () { λ(add, [], 1, {}); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
});
});
describe('4th param', function () {
describe('wrong types', function () {
it('should throw if fourth param is "string"', function () {
expect(function () { λ(add, [], [], 'test'); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fourth param is "object"', function () {
expect(function () { λ(add, [], [], {}); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fourth param is "null"', function () {
expect(function () { λ(add, [], [], null); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fourth param is "NaN"', function () {
expect(function () { λ(add, [], [], NaN); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if fourth param is "number" -1', function () {
expect(function () { λ(add, [], [], -1); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if fourth param is "number" 0', function () {
expect(function () { λ(add, [], [], 0); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if fourth param is "boolean" false', function () {
expect(function () { λ(add, [], [], false); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fourth param is "boolean" true', function () {
expect(function () { λ(add, [], [], true); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fourth param is "BigNumber"', function () {
expect(function () { λ(add, [], [], new BigNumber(1)); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
});
describe('correct types', function () {
it('should not throw if fourth param is "undefined"', function () {
expect(function () { λ(add, [1, 1], [1, 1], undefined); }).to.not.throw();
});
it('should not throw if fourth param is a positive number', function () {
expect(function () { λ(add, [1, 1], [1, 1], 2); }).to.not.throw();
});
it('should not throw if fourth param is an array', function () {
expect(function () { λ(add, [1, 1], [1, 1], [1, 1]); }).to.not.throw();
});
});
describe('repeat last function call times', function () {
it('should throw if another "number" is passed after', function () {
expect(function () { λ(add, [], [], 1, 1); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
it('should throw if another "array" is passed after', function () {
expect(function () { λ(add, [], [], 1, []); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
it('should throw if another "object" is passed after', function () {
expect(function () { λ(add, [], [], 1, {}); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
});
});
describe('5th param', function () {
describe('wrong types', function () {
it('should throw if fifth param is "string"', function () {
expect(function () { λ(add, [], [], [], 'test'); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fifth param is "object"', function () {
expect(function () { λ(add, [], [], [], {}); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fifth param is "null"', function () {
expect(function () { λ(add, [], [], [], null); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fifth param is "NaN"', function () {
expect(function () { λ(add, [], [], [], NaN); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if fifth param is "number" -1', function () {
expect(function () { λ(add, [], [], [], -1); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if fifth param is "number" 0', function () {
expect(function () { λ(add, [], [], [], 0); }).to.throw(Error, 'λ: Repeat last function call times should be a positive number!');
});
it('should throw if fifth param is "boolean" false', function () {
expect(function () { λ(add, [], [], [], false); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fifth param is "boolean" true', function () {
expect(function () { λ(add, [], [], [], true); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
it('should throw if fifth param is "BigNumber"', function () {
expect(function () { λ(add, [], [], [], new BigNumber(1)); }).to.throw(TypeError, 'λ: 3rd, 4th, ... params can be either an array or a number!');
});
});
describe('correct types', function () {
it('should not throw if fifth param is "undefined"', function () {
expect(function () { λ(add, [1, 1], [1, 1], [1, 1], undefined); }).to.not.throw();
});
it('should not throw if fifth param is a positive number', function () {
expect(function () { λ(add, [1, 1], [1, 1], [1, 1], 2); }).to.not.throw();
});
it('should not throw if fifth param is an array', function () {
expect(function () { λ(add, [1, 1], [1, 1], [1, 1], [1, 1]); }).to.not.throw();
});
});
describe('repeat last function call times', function () {
it('should throw if another "number" is passed after', function () {
expect(function () { λ(add, [], [], [], 1, 1); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
it('should throw if another "array" is passed after', function () {
expect(function () { λ(add, [], [], [], 1, []); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
it('should throw if another "object" is passed after', function () {
expect(function () { λ(add, [], [], [], 1, {}); }).to.throw(Error, 'λ: Repeat last function call times should be the last parameter!');
});
});
});
});
it('should return a callable', function () {
expect(function () { λ(add)(add); }).to.not.throw();
expect(function () { λ(add, [1, 1])(add, [1, 1]); }).to.not.throw();
});
it('used in complex scenario should not throw', function () {
expect(function () {
describe('arithmetics tests', function () {
it('0001', function () {
λ( div, [6, 2] );
expect(λ[0].toNumber()).to.equal(3);
});
it('0002', function () {
λ( div, [6, 2], [10, 5] );
expect(λ[0].toNumber()).to.equal(2);
});
it('0003', function () {
λ( div, [6, 2], [10, 5], [12, 3] );
expect(λ[0].toNumber()).to.equal(4);
});
it('0004', function () {
λ( div, [6, 2] );
λ( div, [6, 2] );
expect(λ[0].toNumber()).to.equal(3);
});
it('0005', function () {
λ( div, [6, 2], [10, 5] );
λ( div, [6, 2], [10, 5] );
expect(λ[0].toNumber()).to.equal(2);
});
it('0006', function () {
λ( div, [6, 2], [10, 5], [12, 3] );
λ( div, [6, 2], [10, 5], [12, 3] );
expect(λ[0].toNumber()).to.equal(4);
});
it('0007', function () {
λ( div, [6, 2] );
λ( div, [21, 3] );
expect(λ[1].toNumber()).to.equal(7);
});
it('0008', function () {
λ( div, [6, 2], [10, 5] );
λ( div, [21, 3], [90, 10] );
expect(λ[1].toNumber()).to.equal(9);
});
it('0009', function () {
λ( div, [6, 2], [10, 5], [12, 3] );
λ( div, [21, 3], [90, 10], [20, 2] );
expect(λ[1].toNumber()).to.equal(10);
});
/* ------------------------------------ */
it('0010', function () {
λ( div, [6, 2], [_, 1] );
expect(λ[0].toNumber()).to.equal(3);
});
it('0010', function () {
λ( div, [6, 2], [3, _] );
expect(λ[0].toNumber()).to.equal(1);
});
it('0010', function () {
λ( div, [6, 2], [3, _], [_, 2] );
expect(λ[0].toNumber()).to.equal(0.5);
});
it('0010', function () {
λ( div, [6, 2], [3, _], [4, _] );
expect(λ[0].toNumber()).to.equal(4);
});
/* ------------------------------------ */
it('0010', function () {
λ( div, [6, 2], 1 );
expect(λ[0].toNumber()).to.equal(3);
});
it('0010', function () {
λ( div, [6, 2], 2 );
expect(λ[0].toNumber()).to.equal(3);
});
it('0010', function () {
λ( div, [6, 2], 3 );
expect(λ[0].toNumber()).to.equal(3);
});
/* ------------------------------------ */
it('0010', function () {
λ( div, [48, 2], [_, 2], 1 );
expect(λ[0].toNumber()).to.equal(12);
});
it('0010', function () {
λ( div, [48, 2], [_, 2], 2 );
expect(λ[0].toNumber()).to.equal(6);
});
it('0010', function () {
λ( div, [48, 2], [_, 2], 3 );
expect(λ[0].toNumber()).to.equal(3);
});
it('0010', function () {
λ( div, [48, 2], [_, 2], 4 );
expect(λ[0].toNumber()).to.equal(1.5);
});
/* ------------------------------------ */
it('0011', function () {
λ( div, [300, 293] )
( add, [λ[0], λ[0]], [_, λ[0]], 70 );
}).to.not.throw();
expect(λ[1].toNumber()).to.equal(73.72013651877133);
});
it('0012', function () {
λ( div, [300, 293] )
( add, [λ[0], λ[0]], [_, λ[0]], 70 );
expect(λ[1].number).to.equal(73.72013651877133);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc