@bignum/template
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -17,8 +17,12 @@ // src/index.mts | ||
} | ||
function identity(a) { | ||
return a; | ||
} | ||
function buildEvaluator(evaluateForD, evaluateForB) { | ||
const forB = evaluateForB || evaluateForD; | ||
const forD = (a, b) => evaluateForD(BigNum.valueOf(a), b); | ||
const forB = evaluateForB || forD; | ||
return (a, b) => { | ||
const na = normalize(a); | ||
const nb = normalize(b); | ||
return typeof na === "bigint" && typeof nb === "bigint" ? forB(na, nb) : evaluateForD(na, nb); | ||
return typeof na === "bigint" && typeof nb === "bigint" ? forB(na, nb) : forD(na, nb); | ||
}; | ||
@@ -36,3 +40,3 @@ } | ||
function buildOperation(opForD, opForB) { | ||
const forB = opForB || ((a) => a); | ||
const forB = opForB || ((a, ...options) => opForD(BigNum.valueOf(a), ...options)); | ||
return (a, ...options) => { | ||
@@ -46,20 +50,20 @@ const na = normalize(a); | ||
"*": buildEvaluator( | ||
(a, b) => BigNum.valueOf(a).multiply(b), | ||
(a, b) => a.multiply(b), | ||
(a, b) => a * b | ||
), | ||
"+": buildEvaluator( | ||
(a, b) => BigNum.valueOf(a).add(b), | ||
(a, b) => a.add(b), | ||
(a, b) => a + b | ||
), | ||
"-": buildEvaluator( | ||
(a, b) => BigNum.valueOf(a).subtract(b), | ||
(a, b) => a.subtract(b), | ||
(a, b) => a - b | ||
), | ||
"/": buildEvaluator((a, b) => BigNum.valueOf(a).divide(b)), | ||
"/": buildEvaluator((a, b) => a.divide(b)), | ||
"%": buildEvaluator( | ||
(a, b) => BigNum.valueOf(a).modulo(b), | ||
(a, b) => a.modulo(b), | ||
(a, b) => a % b | ||
), | ||
"**": buildEvaluator( | ||
(a, b) => BigNum.valueOf(a).pow(b), | ||
(a, b) => a.pow(b), | ||
(a, b) => a ** b | ||
@@ -90,2 +94,3 @@ ), | ||
functions: { | ||
sqrt: buildOperation((a) => a.sqrt()), | ||
abs: buildOperation( | ||
@@ -95,6 +100,6 @@ (a) => a.abs(), | ||
), | ||
trunc: buildOperation((a) => a.trunc()), | ||
round: buildOperation((a) => a.round()), | ||
floor: buildOperation((a) => a.floor()), | ||
ceil: buildOperation((a) => a.ceil()) | ||
trunc: buildOperation((a) => a.trunc(), identity), | ||
round: buildOperation((a) => a.round(), identity), | ||
floor: buildOperation((a) => a.floor(), identity), | ||
ceil: buildOperation((a) => a.ceil(), identity) | ||
} | ||
@@ -101,0 +106,0 @@ }; |
{ | ||
"name": "@bignum/template", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Write formulas with template literals.", | ||
@@ -51,3 +51,3 @@ "type": "module", | ||
"dependencies": { | ||
"@bignum/core": "^0.3.0", | ||
"@bignum/core": "^0.4.0", | ||
"@bignum/shared": "^0.1.0", | ||
@@ -54,0 +54,0 @@ "@bignum/template-compiler": "^0.1.0" |
Sorry, the diff of this file is not supported yet
12032
257
+ Added@bignum/core@0.4.0(transitive)
- Removed@bignum/core@0.3.0(transitive)
Updated@bignum/core@^0.4.0