biggystring
Advanced tools
Comparing version 1.0.3 to 1.0.4
312
lib/index.js
@@ -0,1 +1,5 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/** | ||
@@ -20,133 +24,185 @@ * Created by paul on 7/25/17. | ||
const bns = { | ||
add: (x, y, base = 10) => { | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
const out = xBN.add(yBN).toString(base); | ||
return base === 10 ? out : '0x' + out; | ||
}, | ||
mul: (x, y, base = 10) => { | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
const out = xBN.mul(yBN).toString(base); | ||
return base === 10 ? out : '0x' + out; | ||
}, | ||
sub: (x, y, base = 10) => { | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
const out = xBN.sub(yBN).toString(base); | ||
return base === 10 ? out : '0x' + out; | ||
}, | ||
div: (x, y, base = 10) => { | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
const out = xBN.div(yBN).toString(base); | ||
return base === 10 ? out : '0x' + out; | ||
}, | ||
lt: (x, y) => { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.lt(yBN); | ||
}, | ||
lte: (x, y) => { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.lte(yBN); | ||
}, | ||
gt: (x, y) => { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.gt(yBN); | ||
}, | ||
gte: (x, y) => { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.gte(yBN); | ||
}, | ||
eq: (x, y) => { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.eq(yBN); | ||
}, | ||
fixedToInt: (n, multiplier) => { | ||
const x = n.toString(); | ||
const pos = x.indexOf('.'); | ||
if (pos === -1) { | ||
throw new Error('Invalid fixed point number'); | ||
} | ||
// Make sure there is only one '.' | ||
const lastPos = x.indexOf('.'); | ||
if (lastPos !== pos) { | ||
throw new Error('Invalid fixed point number. Contains more than one decimal point'); | ||
} | ||
const addZeros = multiplier - (x.length - pos - 1); | ||
if (addZeros < 0) { | ||
throw new Error('Multiplier too small to create integer'); | ||
} | ||
let out = x.replace('.', ''); | ||
for (let n = 0; n < addZeros; n++) { | ||
function add(x, y, base = 10) { | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
const out = xBN.add(yBN).toString(base); | ||
return base === 10 ? out : '0x' + out; | ||
} | ||
function mul(x, y, base = 10) { | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
const out = xBN.mul(yBN).toString(base); | ||
return base === 10 ? out : '0x' + out; | ||
} | ||
function sub(x, y, base = 10) { | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
const out = xBN.sub(yBN).toString(base); | ||
return base === 10 ? out : '0x' + out; | ||
} | ||
function div(x, y, base = 10) { | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
const out = xBN.div(yBN).toString(base); | ||
return base === 10 ? out : '0x' + out; | ||
} | ||
function lt(x, y) { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.lt(yBN); | ||
} | ||
function lte(x, y) { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.lte(yBN); | ||
} | ||
function gt(x, y) { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.gt(yBN); | ||
} | ||
function gte(x, y) { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.gte(yBN); | ||
} | ||
function eq(x, y) { | ||
const xBase = isHex(x) ? 16 : 10; | ||
const yBase = isHex(y) ? 16 : 10; | ||
x = cropHex(x); | ||
y = cropHex(y); | ||
const xBN = new BN(x, xBase); | ||
const yBN = new BN(y, yBase); | ||
return xBN.eq(yBN); | ||
} | ||
function divf(x, y) { | ||
const shift = log10(y); | ||
return intToFixed(x, shift); | ||
} | ||
function mulf(x, y) { | ||
const shift = log10(y); | ||
return fixedToInt(x, shift); | ||
} | ||
function fixedToInt(n, multiplier) { | ||
let x; | ||
if (typeof n === 'number') { | ||
x = n.toString(); | ||
} else if (typeof n === 'string') { | ||
x = n; | ||
} else { | ||
throw new Error('Invalid input format'); | ||
} | ||
const pos = x.indexOf('.'); | ||
if (pos === -1) { | ||
throw new Error('Invalid fixed point number'); | ||
} | ||
// Make sure there is only one '.' | ||
const lastPos = x.indexOf('.'); | ||
if (lastPos !== pos) { | ||
throw new Error('Invalid fixed point number. Contains more than one decimal point'); | ||
} | ||
const addZeros = multiplier - (x.length - pos - 1); | ||
if (addZeros < 0) { | ||
throw new Error('Multiplier too small to create integer'); | ||
} | ||
let out = x.replace('.', ''); | ||
for (let n = 0; n < addZeros; n++) { | ||
out += '0'; | ||
} | ||
return out; | ||
} | ||
function intToFixed(x, divisor) { | ||
if (x.length <= divisor) { | ||
const leftZeros = divisor - x.length; | ||
let out = '.'; | ||
for (let n = 0; n < leftZeros; n++) { | ||
out += '0'; | ||
} | ||
return out; | ||
}, | ||
intToFixed: (x, divisor) => { | ||
if (x.length <= divisor) { | ||
const leftZeros = divisor - x.length; | ||
let out = '.'; | ||
for (let n = 0; n < leftZeros; n++) { | ||
out += '0'; | ||
} | ||
return parseFloat(out + x.substr(0, MAX_DECIMALS)); | ||
} else { | ||
let cropRight = divisor - MAX_DECIMALS; | ||
if (cropRight < 0) cropRight = 0; | ||
let cropLeft = x.length - cropRight; | ||
let out = x.substr(0, cropLeft); | ||
const decimalPos = x.length - divisor; | ||
out = out.substr(0, decimalPos) + '.' + out.substr(decimalPos); | ||
return parseFloat(out); | ||
} | ||
return parseFloat(out + x.substr(0, MAX_DECIMALS)); | ||
} else { | ||
let cropRight = divisor - MAX_DECIMALS; | ||
if (cropRight < 0) cropRight = 0; | ||
let cropLeft = x.length - cropRight; | ||
let out = x.substr(0, cropLeft); | ||
const decimalPos = x.length - divisor; | ||
out = out.substr(0, decimalPos) + '.' + out.substr(decimalPos); | ||
return parseFloat(out); | ||
} | ||
}; | ||
} | ||
module.exports = bns; | ||
function log10(x) { | ||
if (!(x.match(/^[0-1]+$/g) !== null)) { | ||
throw new Error('InvalidLogInputValue: Must be a power of 10'); | ||
} | ||
if (!x.startsWith('1')) { | ||
throw new Error('InvalidLogInputValue: Must not have leading zeros'); | ||
} | ||
if ((x.match(/1/g) || []).length > 1) { | ||
throw new Error('InvalidLogInputValue: Must be power of 10.'); | ||
} | ||
return (x.match(/0/g) || []).length; | ||
} | ||
const bns = { add, sub, mul, div, gt, lt, gte, lte, eq, mulf, divf }; | ||
exports.add = add; | ||
exports.sub = sub; | ||
exports.mul = mul; | ||
exports.div = div; | ||
exports.gt = gt; | ||
exports.lt = lt; | ||
exports.gte = gte; | ||
exports.lte = lte; | ||
exports.eq = eq; | ||
exports.mulf = mulf; | ||
exports.divf = divf; | ||
exports.bns = bns; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "biggystring", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Wrapper for bn.js to directly operate on strings as big numbers", | ||
"main": "lib/index.js", | ||
"main": "./lib/index.js", | ||
"module": "./lib/index.es.js", | ||
"keywords": [ | ||
"big", | ||
"number" | ||
"number", | ||
"string" | ||
], | ||
@@ -29,5 +31,7 @@ "author": "Airbitz, Inc.", | ||
"eslint-plugin-flowtype": "^2.34.1", | ||
"standard": "^10.0.2", | ||
"flow-bin": "^0.49.1", | ||
"mocha": "^3.1.2", | ||
"flow-bin": "^0.49.1" | ||
"rollup": "^0.43.1", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"standard": "^10.0.2" | ||
}, | ||
@@ -42,3 +46,3 @@ "standard": { | ||
"flow": "flow", | ||
"babel": "babel src/ -d lib/", | ||
"rollup": "rollup -c", | ||
"build": "npm run lint && npm run prepare && npm run flow", | ||
@@ -48,5 +52,5 @@ "cover": "nyc --reporter=lcov --reporter=text --reporter=html --extension .js npm test", | ||
"lint": "standard '*.js' 'src/**/*.js'", | ||
"prepare": "npm run babel", | ||
"prepare": "npm run rollup", | ||
"test": "npm run build && mocha" | ||
} | ||
} |
@@ -35,6 +35,1 @@ # biggystring | ||
### Utility conversions | ||
console.log(bs.fixedToInt(12345.678, 10) // => '123456780000000' | ||
console.log(bs.fixedToInt(12345.67890, 3) // => Error | ||
console.log(bs.intToFixed('123456789012345', 8) // => 1234567.89012345 |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
36667
6
359
9
35
1