biggystring
Advanced tools
Comparing version 1.1.10 to 2.0.0
@@ -6,6 +6,6 @@ /** | ||
var MAX_DECIMALS = 10; | ||
// const MAX_DECIMALS = 10 | ||
function isHex(x) { | ||
if (x.startsWith('0x') || x.toLowerCase().includes('a') || x.toLowerCase().includes('b') || x.toLowerCase().includes('c') || x.toLowerCase().includes('d') || x.toLowerCase().includes('e') || x.toLowerCase().includes('f')) { | ||
if (x.startsWith('0x') || x.startsWith('-0x') || x.toLowerCase().includes('a') || x.toLowerCase().includes('b') || x.toLowerCase().includes('c') || x.toLowerCase().includes('d') || x.toLowerCase().includes('e') || x.toLowerCase().includes('f')) { | ||
return true; | ||
@@ -31,6 +31,2 @@ } else { | ||
function trimEnd(val) { | ||
// if (val === '0') { | ||
// return val | ||
// } | ||
// Remove starting zeros if there are any | ||
@@ -340,10 +336,27 @@ var out = val.replace(/^0+/, ''); | ||
function max(x1, y1) { | ||
var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; | ||
function abs(x1) { | ||
var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10; | ||
var _floatShifts11 = floatShifts(x1, y1), | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
var _floatShifts11 = floatShifts(x1, '0'), | ||
x = _floatShifts11.x, | ||
y = _floatShifts11.y, | ||
shift = _floatShifts11.shift; | ||
var xBase = isHex(x1) ? 16 : 10; | ||
x = cropHex(x); | ||
var xBN = new BN(x, xBase); | ||
var out = xBN.abs(xBN).toString(base); | ||
out = addDecimal(out, shift); | ||
return base === 10 ? out : '0x' + out; | ||
} | ||
function max(x1, y1) { | ||
var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; | ||
var _floatShifts12 = floatShifts(x1, y1), | ||
x = _floatShifts12.x, | ||
y = _floatShifts12.y, | ||
shift = _floatShifts12.shift; | ||
var xBase = isHex(x) ? 16 : 10; | ||
@@ -365,44 +378,2 @@ var yBase = isHex(y) ? 16 : 10; | ||
function divf(x, y) { | ||
var shift = log10(y); | ||
return intToFixed(x, shift); | ||
} | ||
function mulf(x, y) { | ||
var shift = log10(y); | ||
return fixedToInt(x, shift); | ||
} | ||
function fixedToInt(n, multiplier) { | ||
var x = void 0; | ||
if (typeof n === 'number') { | ||
x = n.toString(); | ||
} else if (typeof n === 'string') { | ||
x = n; | ||
} else { | ||
throw new Error('Invalid input format'); | ||
} | ||
var pos = x.indexOf('.'); | ||
if (pos !== -1) { | ||
// Make sure there is only one '.' | ||
var x2 = x.substr(0, pos) + x.substr(pos + 1); | ||
var lastPos = x2.indexOf('.'); | ||
if (lastPos !== -1) { | ||
throw new Error('Invalid fixed point number. Contains more than one decimal point'); | ||
} | ||
} else { | ||
pos = x.length - 1; | ||
} | ||
var numZerosAdd = multiplier - (x.length - pos - 1); | ||
if (numZerosAdd < 0) { | ||
throw new Error('Multiplier too small to create integer'); | ||
} | ||
var out = x.replace('.', ''); | ||
out = addZeros(out, numZerosAdd); | ||
return out; | ||
} | ||
function toFixed(x1) { | ||
@@ -413,41 +384,35 @@ var minPrecision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; | ||
validate(x1); | ||
var x = trimEnd(x1); | ||
var negative = false; | ||
var out = ''; | ||
var x = x1; | ||
if (x.includes('-')) { | ||
negative = true; | ||
// Remove any leading '-' signs | ||
x = x.replace(/^-+/, ''); | ||
} | ||
x = trimEnd(x); | ||
// Number of decimal places number has | ||
var decimalPos = x.indexOf('.'); | ||
if (decimalPos === -1) { | ||
var out = x + '.' + addZeros('', minPrecision); | ||
out = x + '.' + addZeros('', minPrecision); | ||
// Remove trailing "." if there is one | ||
out = out.replace(/\.+$/, ''); | ||
return out; | ||
} else { | ||
var numDecimals = x.length - decimalPos - 1; | ||
if (numDecimals > maxPrecision) { | ||
return x.substr(0, x.length - (numDecimals - maxPrecision)); | ||
out = x.substr(0, x.length - (numDecimals - maxPrecision)); | ||
} else if (numDecimals < minPrecision) { | ||
return x + addZeros('', minPrecision - numDecimals); | ||
out = x + addZeros('', minPrecision - numDecimals); | ||
} else { | ||
return x; | ||
out = x; | ||
} | ||
} | ||
} | ||
function intToFixed(x, divisor) { | ||
if (x.length <= divisor) { | ||
var leftZeros = divisor - x.length; | ||
var out = '.'; | ||
for (var n = 0; n < leftZeros; n++) { | ||
out += '0'; | ||
} | ||
return parseFloat(out + x.substr(0, MAX_DECIMALS)); | ||
} else { | ||
var cropRight = divisor - MAX_DECIMALS; | ||
if (cropRight < 0) cropRight = 0; | ||
var cropLeft = x.length - cropRight; | ||
var _out3 = x.substr(0, cropLeft); | ||
var decimalPos = x.length - divisor; | ||
_out3 = _out3.substr(0, decimalPos) + '.' + _out3.substr(decimalPos); | ||
return parseFloat(_out3); | ||
if (negative) { | ||
out = '-' + out; | ||
} | ||
return out; | ||
} | ||
@@ -468,5 +433,5 @@ | ||
var bns = { add: add, sub: sub, mul: mul, div: div, gt: gt, lt: lt, gte: gte, lte: lte, eq: eq, mulf: mulf, divf: divf, min: min, max: max, log10: log10, toFixed: toFixed }; | ||
var bns = { add: add, sub: sub, mul: mul, div: div, gt: gt, lt: lt, gte: gte, lte: lte, eq: eq, min: min, max: max, log10: log10, toFixed: toFixed, abs: abs }; | ||
export { add, sub, mul, div, gt, lt, gte, lte, eq, mulf, divf, min, max, log10, toFixed, bns }; | ||
export { add, sub, mul, div, gt, lt, gte, lte, eq, min, max, log10, toFixed, abs, bns }; | ||
//# sourceMappingURL=index.es.js.map |
120
lib/index.js
@@ -10,6 +10,6 @@ 'use strict'; | ||
var MAX_DECIMALS = 10; | ||
// const MAX_DECIMALS = 10 | ||
function isHex(x) { | ||
if (x.startsWith('0x') || x.toLowerCase().includes('a') || x.toLowerCase().includes('b') || x.toLowerCase().includes('c') || x.toLowerCase().includes('d') || x.toLowerCase().includes('e') || x.toLowerCase().includes('f')) { | ||
if (x.startsWith('0x') || x.startsWith('-0x') || x.toLowerCase().includes('a') || x.toLowerCase().includes('b') || x.toLowerCase().includes('c') || x.toLowerCase().includes('d') || x.toLowerCase().includes('e') || x.toLowerCase().includes('f')) { | ||
return true; | ||
@@ -35,6 +35,2 @@ } else { | ||
function trimEnd(val) { | ||
// if (val === '0') { | ||
// return val | ||
// } | ||
// Remove starting zeros if there are any | ||
@@ -344,10 +340,27 @@ var out = val.replace(/^0+/, ''); | ||
function max(x1, y1) { | ||
var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; | ||
function abs(x1) { | ||
var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10; | ||
var _floatShifts11 = floatShifts(x1, y1), | ||
if (base !== 10 && base !== 16) throw new Error('Unsupported base'); | ||
var _floatShifts11 = floatShifts(x1, '0'), | ||
x = _floatShifts11.x, | ||
y = _floatShifts11.y, | ||
shift = _floatShifts11.shift; | ||
var xBase = isHex(x1) ? 16 : 10; | ||
x = cropHex(x); | ||
var xBN = new BN(x, xBase); | ||
var out = xBN.abs(xBN).toString(base); | ||
out = addDecimal(out, shift); | ||
return base === 10 ? out : '0x' + out; | ||
} | ||
function max(x1, y1) { | ||
var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; | ||
var _floatShifts12 = floatShifts(x1, y1), | ||
x = _floatShifts12.x, | ||
y = _floatShifts12.y, | ||
shift = _floatShifts12.shift; | ||
var xBase = isHex(x) ? 16 : 10; | ||
@@ -369,44 +382,2 @@ var yBase = isHex(y) ? 16 : 10; | ||
function divf(x, y) { | ||
var shift = log10(y); | ||
return intToFixed(x, shift); | ||
} | ||
function mulf(x, y) { | ||
var shift = log10(y); | ||
return fixedToInt(x, shift); | ||
} | ||
function fixedToInt(n, multiplier) { | ||
var x = void 0; | ||
if (typeof n === 'number') { | ||
x = n.toString(); | ||
} else if (typeof n === 'string') { | ||
x = n; | ||
} else { | ||
throw new Error('Invalid input format'); | ||
} | ||
var pos = x.indexOf('.'); | ||
if (pos !== -1) { | ||
// Make sure there is only one '.' | ||
var x2 = x.substr(0, pos) + x.substr(pos + 1); | ||
var lastPos = x2.indexOf('.'); | ||
if (lastPos !== -1) { | ||
throw new Error('Invalid fixed point number. Contains more than one decimal point'); | ||
} | ||
} else { | ||
pos = x.length - 1; | ||
} | ||
var numZerosAdd = multiplier - (x.length - pos - 1); | ||
if (numZerosAdd < 0) { | ||
throw new Error('Multiplier too small to create integer'); | ||
} | ||
var out = x.replace('.', ''); | ||
out = addZeros(out, numZerosAdd); | ||
return out; | ||
} | ||
function toFixed(x1) { | ||
@@ -417,41 +388,35 @@ var minPrecision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; | ||
validate(x1); | ||
var x = trimEnd(x1); | ||
var negative = false; | ||
var out = ''; | ||
var x = x1; | ||
if (x.includes('-')) { | ||
negative = true; | ||
// Remove any leading '-' signs | ||
x = x.replace(/^-+/, ''); | ||
} | ||
x = trimEnd(x); | ||
// Number of decimal places number has | ||
var decimalPos = x.indexOf('.'); | ||
if (decimalPos === -1) { | ||
var out = x + '.' + addZeros('', minPrecision); | ||
out = x + '.' + addZeros('', minPrecision); | ||
// Remove trailing "." if there is one | ||
out = out.replace(/\.+$/, ''); | ||
return out; | ||
} else { | ||
var numDecimals = x.length - decimalPos - 1; | ||
if (numDecimals > maxPrecision) { | ||
return x.substr(0, x.length - (numDecimals - maxPrecision)); | ||
out = x.substr(0, x.length - (numDecimals - maxPrecision)); | ||
} else if (numDecimals < minPrecision) { | ||
return x + addZeros('', minPrecision - numDecimals); | ||
out = x + addZeros('', minPrecision - numDecimals); | ||
} else { | ||
return x; | ||
out = x; | ||
} | ||
} | ||
} | ||
function intToFixed(x, divisor) { | ||
if (x.length <= divisor) { | ||
var leftZeros = divisor - x.length; | ||
var out = '.'; | ||
for (var n = 0; n < leftZeros; n++) { | ||
out += '0'; | ||
} | ||
return parseFloat(out + x.substr(0, MAX_DECIMALS)); | ||
} else { | ||
var cropRight = divisor - MAX_DECIMALS; | ||
if (cropRight < 0) cropRight = 0; | ||
var cropLeft = x.length - cropRight; | ||
var _out3 = x.substr(0, cropLeft); | ||
var decimalPos = x.length - divisor; | ||
_out3 = _out3.substr(0, decimalPos) + '.' + _out3.substr(decimalPos); | ||
return parseFloat(_out3); | ||
if (negative) { | ||
out = '-' + out; | ||
} | ||
return out; | ||
} | ||
@@ -472,3 +437,3 @@ | ||
var bns = { add: add, sub: sub, mul: mul, div: div, gt: gt, lt: lt, gte: gte, lte: lte, eq: eq, mulf: mulf, divf: divf, min: min, max: max, log10: log10, toFixed: toFixed }; | ||
var bns = { add: add, sub: sub, mul: mul, div: div, gt: gt, lt: lt, gte: gte, lte: lte, eq: eq, min: min, max: max, log10: log10, toFixed: toFixed, abs: abs }; | ||
@@ -484,4 +449,2 @@ exports.add = add; | ||
exports.eq = eq; | ||
exports.mulf = mulf; | ||
exports.divf = divf; | ||
exports.min = min; | ||
@@ -491,3 +454,4 @@ exports.max = max; | ||
exports.toFixed = toFixed; | ||
exports.abs = abs; | ||
exports.bns = bns; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "biggystring", | ||
"version": "1.1.10", | ||
"version": "2.0.0", | ||
"description": "Full floating point big number library using regular Javascript string", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76449
744