Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

biggystring

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

biggystring - npm Package Compare versions

Comparing version 4.1.3 to 4.2.0

36

lib/src/index.d.ts
/**
* Created by paul on 7/25/17.
*/
declare function add(x1: string, y1: string, base?: number): string;
declare function mul(x1: string, y1: string, base?: number): string;
declare function sub(x1: string, y1: string, base?: number): string;
declare function div(x1: string, y1: string, precision?: number, base?: number): string;
declare function lt(x1: string, y1: string): boolean;
declare function lte(x1: string, y1: string): boolean;
declare function gt(x1: string, y1: string): boolean;
declare function gte(x1: string, y1: string): boolean;
declare function eq(x1: string, y1: string): boolean;
declare function min(x1: string, y1: string, base?: number): string;
declare function abs(x1: string, base?: number): string;
declare function max(x1: string, y1: string, base?: number): string;
declare const floor: (x1: string, precision: number) => string;
declare const ceil: (x1: string, precision: number) => string;
declare const round: (x1: string, precision: number) => string;
declare function toFixed(x1: string, minPrecision?: number, maxPrecision?: number): string;
declare function log10(x: string): number;
export { add, sub, mul, div, gt, lt, gte, lte, eq, min, max, log10, toFixed, abs, floor, ceil, round, };
export declare function add(x1: string | number, y1: string | number, base?: number): string;
export declare function mul(x1: string | number, y1: string | number, base?: number): string;
export declare function sub(x1: string | number, y1: string | number, base?: number): string;
export declare function div(x1: string | number, y1: string | number, precision?: number, base?: number): string;
export declare function lt(x1: string | number, y1: string | number): boolean;
export declare function lte(x1: string | number, y1: string | number): boolean;
export declare function gt(x1: string | number, y1: string | number): boolean;
export declare function gte(x1: string | number, y1: string | number): boolean;
export declare function eq(x1: string | number, y1: string | number): boolean;
export declare function min(x1: string | number, y1: string | number, base?: number): string;
export declare function abs(x1: string | number, base?: number): string;
export declare function max(x1: string | number, y1: string | number, base?: number): string;
export declare const floor: (x1: string | number, precision: number) => string;
export declare const ceil: (x1: string | number, precision: number) => string;
export declare const round: (x1: string | number, precision: number) => string;
export declare function toBns(n: number | string): string;
export declare function toFixed(x1: string | number, minPrecision?: number, maxPrecision?: number): string;
export declare function log10(x: string): number;

@@ -9,133 +9,8 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.round = exports.ceil = exports.floor = exports.abs = exports.toFixed = exports.log10 = exports.max = exports.min = exports.eq = exports.lte = exports.gte = exports.lt = exports.gt = exports.div = exports.mul = exports.sub = exports.add = void 0;
exports.log10 = exports.toFixed = exports.toBns = exports.round = exports.ceil = exports.floor = exports.max = exports.abs = exports.min = exports.eq = exports.gte = exports.gt = exports.lte = exports.lt = exports.div = exports.sub = exports.mul = exports.add = void 0;
const bn_js_1 = __importDefault(require("bn.js"));
// const MAX_DECIMALS = 10
function isHex(x) {
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;
}
else {
return false;
}
}
function cropHex(x) {
return x.replace('0x', '');
}
function addZeros(val, numZeros) {
let out = val;
for (let n = 0; n < numZeros; n++) {
out += '0';
}
return out;
}
// Remove starting and trailing zeros and decimal
function trimEnd(val) {
// Remove starting zeros if there are any
let out = val.replace(/^0+/, '');
out = out.replace(/^\.+/, '0.');
if (out.includes('.')) {
// Remove trailing zeros
out = out.replace(/0+$/, '');
// Remove trailing "." if there is one
out = out.replace(/\.+$/, '');
if (out === '') {
out = '0';
}
}
if (out === '') {
out = '0';
}
return out;
}
function addDecimal(x, shift) {
if (shift === 0)
return x;
let isNegative = false;
if (x.slice(0, 1) === '-') {
isNegative = true;
x = x.slice(1);
}
let out;
if (shift > x.length) {
out = '0.' + addZeros('', shift - x.length) + x;
}
else {
out =
x.substr(0, x.length - shift) + '.' + x.substr(x.length - shift, x.length);
}
out = trimEnd(out);
if (isNegative) {
out = `-${out}`;
}
return out;
}
// Takes two floating point (base 10) numbers and finds the multiplier needed to make them both
// operable as a integer
function floatShifts(xStart, yStart, moreShift) {
let x = xStart;
let y = yStart;
validate(x, y);
let xPos = x.indexOf('.');
let yPos = y.indexOf('.');
const xHex = isHex(x);
const yHex = isHex(y);
if (xPos !== -1) {
// Remove trailing zeros
x = trimEnd(x);
xPos = x.indexOf('.');
}
if (yPos !== -1) {
// Remove trailing zeros
y = trimEnd(y);
yPos = y.indexOf('.');
}
if (xPos !== -1 || yPos !== -1 || typeof moreShift === 'number') {
if (xHex || yHex) {
throw new Error('Cannot operate on base16 float values');
}
let xShift = 0;
let yShift = 0;
if (xPos !== -1) {
xShift = x.length - xPos - 1;
}
if (yPos !== -1) {
yShift = y.length - yPos - 1;
}
const shift = xShift > yShift ? xShift : yShift;
let moreS = 0;
if (typeof moreShift === 'number') {
moreS = moreShift;
}
x = addZeros(x.replace('.', ''), shift + moreS - xShift);
y = addZeros(y.replace('.', ''), shift - yShift);
const out = { x, y, shift };
return out;
}
else {
// Both x and y are int and need no float conversion
const out = {
x,
y,
shift: 0,
};
return out;
}
}
function validate(...args) {
for (const arg of args) {
if (arg.split('.').length - 1 > 1) {
throw new Error('Invalid number: more than one decimal point');
}
if (arg.split('-').length - 1 > 1) {
throw new Error('Invalid number: more than one negative sign');
}
}
}
const SCI_NOTATION_REGEX = /^(-?\d*\.?\d*)e((?:\+|-)?\d+)$/;
// -----------------------------------------------------------------------------
// Public
// -----------------------------------------------------------------------------
function add(x1, y1, base = 10) {

@@ -309,8 +184,173 @@ if (base !== 10 && base !== 16)

exports.max = max;
exports.floor = (x1, precision) => precisionAdjust('floor', x1, precision);
exports.ceil = (x1, precision) => precisionAdjust('ceil', x1, precision);
exports.round = (x1, precision) => precisionAdjust('round', x1, precision);
function toBns(n) {
let out = typeof n === 'number' ? n.toString() : n;
// Handle scientific notation
const match = out.match(SCI_NOTATION_REGEX);
if (match != null) {
const base = match[1];
const exponent = parseInt(match[2]);
out = sciNotation(base, exponent);
}
validate(out);
return out;
}
exports.toBns = toBns;
function toFixed(x1, minPrecision = 2, maxPrecision = 8) {
let x = toBns(x1);
let negative = false;
let out = '';
if (x.includes('-')) {
negative = true;
// Remove any leading '-' signs
x = x.replace(/^-+/, '');
}
x = trimEnd(x);
// Number of decimal places number has
const decimalPos = x.indexOf('.');
if (decimalPos === -1) {
out = x + '.' + addZeros('', minPrecision);
}
else {
const numDecimals = x.length - decimalPos - 1;
if (numDecimals > maxPrecision) {
out = x.substr(0, x.length - (numDecimals - maxPrecision));
}
else if (numDecimals < minPrecision) {
out = x + addZeros('', minPrecision - numDecimals);
}
else {
out = x;
}
}
// Remove trailing "." if there is one
out = out.replace(/\.+$/, '');
if (negative && out !== '0') {
out = '-' + out;
}
return out;
}
exports.toFixed = toFixed;
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;
}
exports.log10 = log10;
// -----------------------------------------------------------------------------
// Private
// -----------------------------------------------------------------------------
function addDecimal(x, shift) {
if (shift === 0)
return x;
let isNegative = false;
if (x.slice(0, 1) === '-') {
isNegative = true;
x = x.slice(1);
}
let out;
if (shift > x.length) {
out = '0.' + addZeros('', shift - x.length) + x;
}
else {
out =
x.substr(0, x.length - shift) + '.' + x.substr(x.length - shift, x.length);
}
out = trimEnd(out);
if (isNegative) {
out = `-${out}`;
}
return out;
}
function addZeros(val, numZeros) {
let out = val;
for (let n = 0; n < numZeros; n++) {
out += '0';
}
return out;
}
function cropHex(x) {
return x.replace('0x', '');
}
// Takes two floating point (base 10) numbers and finds the multiplier needed to make them both
// operable as a integer
function floatShifts(xStart, yStart, moreShift) {
let x = toBns(xStart);
let y = toBns(yStart);
let xPos = x.indexOf('.');
let yPos = y.indexOf('.');
const xHex = isHex(x);
const yHex = isHex(y);
if (xPos !== -1) {
// Remove trailing zeros
x = trimEnd(x);
xPos = x.indexOf('.');
}
if (yPos !== -1) {
// Remove trailing zeros
y = trimEnd(y);
yPos = y.indexOf('.');
}
if (xPos !== -1 || yPos !== -1 || typeof moreShift === 'number') {
if (xHex || yHex) {
throw new Error('Cannot operate on base16 float values');
}
let xShift = 0;
let yShift = 0;
if (xPos !== -1) {
xShift = x.length - xPos - 1;
}
if (yPos !== -1) {
yShift = y.length - yPos - 1;
}
const shift = xShift > yShift ? xShift : yShift;
let moreS = 0;
if (typeof moreShift === 'number') {
moreS = moreShift;
}
x = addZeros(x.replace('.', ''), shift + moreS - xShift);
y = addZeros(y.replace('.', ''), shift - yShift);
const out = { x, y, shift };
return out;
}
else {
// Both x and y are int and need no float conversion
const out = {
x,
y,
shift: 0,
};
return out;
}
}
function isHex(x1) {
const x = toBns(x1);
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;
}
else {
return false;
}
}
function precisionAdjust(type, x1, precision = 0) {
var _a, _b;
validate(x1);
let x = toBns(x1);
let negative = false;
let out = '';
let x = x1;
if (x.includes('-')) {

@@ -365,57 +405,40 @@ negative = true;

}
const floor = (x1, precision) => precisionAdjust('floor', x1, precision);
exports.floor = floor;
const ceil = (x1, precision) => precisionAdjust('ceil', x1, precision);
exports.ceil = ceil;
const round = (x1, precision) => precisionAdjust('round', x1, precision);
exports.round = round;
function toFixed(x1, minPrecision = 2, maxPrecision = 8) {
validate(x1);
let negative = false;
let out = '';
let x = x1;
if (x.includes('-')) {
negative = true;
// Remove any leading '-' signs
x = x.replace(/^-+/, '');
function sciNotation(x1, exponent) {
const magnitude = exponent >= 0
? '1' + '0'.repeat(Math.abs(exponent)) // 5.8e7 -> 58000000
: `0.${'0'.repeat(Math.abs(exponent) - 1)}1`; // 5.8e-7 -> 0.00000058
return mul(x1, magnitude);
}
// Remove starting and trailing zeros and decimal
function trimEnd(val) {
// Remove starting zeros if there are any
let out = val.replace(/^0+/, '');
out = out.replace(/^\.+/, '0.');
if (out.includes('.')) {
// Remove trailing zeros
out = out.replace(/0+$/, '');
// Remove trailing "." if there is one
out = out.replace(/\.+$/, '');
if (out === '') {
out = '0';
}
}
x = trimEnd(x);
// Number of decimal places number has
const decimalPos = x.indexOf('.');
if (decimalPos === -1) {
out = x + '.' + addZeros('', minPrecision);
if (out === '') {
out = '0';
}
else {
const numDecimals = x.length - decimalPos - 1;
if (numDecimals > maxPrecision) {
out = x.substr(0, x.length - (numDecimals - maxPrecision));
return out;
}
function validate(...args) {
for (const arg of args) {
if (arg.split('.').length - 1 > 1) {
throw new Error(`Invalid number: more than one decimal point '${arg}'`);
}
else if (numDecimals < minPrecision) {
out = x + addZeros('', minPrecision - numDecimals);
if (arg.split('-').length - 1 > 1) {
throw new Error(`Invalid number: more than one negative sign '${arg}'`);
}
else {
out = x;
if (/[^\d.\-x]/.test(arg)) {
throw new Error(`Invalid number: non-number characters '${arg}'`);
}
}
// Remove trailing "." if there is one
out = out.replace(/\.+$/, '');
if (negative && out !== '0') {
out = '-' + out;
}
return out;
}
exports.toFixed = toFixed;
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;
}
exports.log10 = log10;
//# sourceMappingURL=index.js.map

@@ -8,48 +8,32 @@ "use strict";

const index_1 = require("../src/index");
const bns = {
add: index_1.add,
sub: index_1.sub,
mul: index_1.mul,
log10: index_1.log10,
div: index_1.div,
toFixed: index_1.toFixed,
lt: index_1.lt,
lte: index_1.lte,
gt: index_1.gt,
gte: index_1.gte,
eq: index_1.eq,
min: index_1.min,
max: index_1.max,
abs: index_1.abs,
};
describe('add', function () {
it('32 + 10 = 42', function () {
chai_1.assert.equal(bns.add('32', '10'), '42');
chai_1.assert.equal(index_1.add('32', '10'), '42');
});
it('very big num', function () {
chai_1.assert.equal(bns.add('1234000000000000000000000000000000001234', '4321000000000000000000000000000000004321'), '5555000000000000000000000000000000005555');
chai_1.assert.equal(index_1.add('1234000000000000000000000000000000001234', '4321000000000000000000000000000000004321'), '5555000000000000000000000000000000005555');
});
it('0x100 + 10 = 266', function () {
chai_1.assert.equal(bns.add('0x100', '10'), '266');
chai_1.assert.equal(index_1.add('0x100', '10'), '266');
});
it('0x100 + 0x10 = 272', function () {
chai_1.assert.equal(bns.add('0x100', '0x10'), '272');
chai_1.assert.equal(index_1.add('0x100', '0x10'), '272');
});
it('0x100 + -0x10 = 240', function () {
chai_1.assert.equal(bns.add('0x100', '-0x10'), '240');
chai_1.assert.equal(index_1.add('0x100', '-0x10'), '240');
});
it('0x100 + 0x10 = 0x110', function () {
chai_1.assert.equal(bns.add('0x100', '0x10', 16), '0x110');
chai_1.assert.equal(index_1.add('0x100', '0x10', 16), '0x110');
});
it('32.01 + 10.2 = 42.21', function () {
chai_1.assert.equal(bns.add('32.01', '10.2'), '42.21');
chai_1.assert.equal(index_1.add('32.01', '10.2'), '42.21');
});
it('very big float', function () {
chai_1.assert.equal(bns.add('100000.1234000000000000000000000000000000001234', '4321000000000000000000000000000000004321.000001'), '4321000000000000000000000000000000104321.1234010000000000000000000000000000001234');
chai_1.assert.equal(index_1.add('100000.1234000000000000000000000000000000001234', '4321000000000000000000000000000000004321.000001'), '4321000000000000000000000000000000104321.1234010000000000000000000000000000001234');
});
it('very big negative float', function () {
chai_1.assert.equal(bns.add('1000000000000000001.9876000000000000000000000000000000009876', '-001.4321000000000000000000000000000000004321'), '1000000000000000000.5555000000000000000000000000000000005555');
chai_1.assert.equal(index_1.add('1000000000000000001.9876000000000000000000000000000000009876', '-001.4321000000000000000000000000000000004321'), '1000000000000000000.5555000000000000000000000000000000005555');
});
it('resolve negative numbers in base 16', function () {
chai_1.assert.equal(bns.add('-60830', '0', 16), '-0xed9e');
chai_1.assert.equal(index_1.add('-60830', '0', 16), '-0xed9e');
});

@@ -59,27 +43,27 @@ });

it('32 - 10 = 22', function () {
chai_1.assert.equal(bns.sub('32', '10'), '22');
chai_1.assert.equal(index_1.sub('32', '10'), '22');
});
it('very big num', function () {
chai_1.assert.equal(bns.sub('9876000000000000000000000000000000009876', '4321000000000000000000000000000000004321'), '5555000000000000000000000000000000005555');
chai_1.assert.equal(index_1.sub('9876000000000000000000000000000000009876', '4321000000000000000000000000000000004321'), '5555000000000000000000000000000000005555');
});
it('0x100 1 10 = 246', function () {
chai_1.assert.equal(bns.sub('0x100', '10'), '246');
chai_1.assert.equal(index_1.sub('0x100', '10'), '246');
});
it('0x100 - 0x10 = 240', function () {
chai_1.assert.equal(bns.sub('0x100', '0x10'), '240');
chai_1.assert.equal(index_1.sub('0x100', '0x10'), '240');
});
it('0x100 - -0x10 = 272', function () {
chai_1.assert.equal(bns.sub('0x100', '-0x10'), '272');
chai_1.assert.equal(index_1.sub('0x100', '-0x10'), '272');
});
it('0x100 - 0x10 = 0xf0', function () {
chai_1.assert.equal(bns.sub('0x100', '0x10', 16), '0xf0');
chai_1.assert.equal(index_1.sub('0x100', '0x10', 16), '0xf0');
});
it('32.01 - 10.2 = 21.81', function () {
chai_1.assert.equal(bns.sub('32.01', '10.2'), '21.81');
chai_1.assert.equal(index_1.sub('32.01', '10.2'), '21.81');
});
it('very big float', function () {
chai_1.assert.equal(bns.sub('1000000000000000001.9876000000000000000000000000000000009876', '001.4321000000000000000000000000000000004321'), '1000000000000000000.5555000000000000000000000000000000005555');
chai_1.assert.equal(index_1.sub('1000000000000000001.9876000000000000000000000000000000009876', '001.4321000000000000000000000000000000004321'), '1000000000000000000.5555000000000000000000000000000000005555');
});
it('resolve negative numbers in base 16', function () {
chai_1.assert.equal(bns.sub('-60830', '0', 16), '-0xed9e');
chai_1.assert.equal(index_1.sub('-60830', '0', 16), '-0xed9e');
});

@@ -89,12 +73,12 @@ });

it('4 * 5 = 20', function () {
chai_1.assert.equal(bns.mul('4', '5'), '20');
chai_1.assert.equal(index_1.mul('4', '5'), '20');
});
it('very big num', function () {
chai_1.assert.equal(bns.mul('400000000000000000000000000', '5'), '2000000000000000000000000000');
chai_1.assert.equal(index_1.mul('400000000000000000000000000', '5'), '2000000000000000000000000000');
});
it('very small num', function () {
chai_1.assert.equal(bns.mul('1234567890123456.123', '.00000000000000000000001'), '0.00000001234567890123456123');
chai_1.assert.equal(index_1.mul('1234567890123456.123', '.00000000000000000000001'), '0.00000001234567890123456123');
});
it('resolve negative numbers in base 16', function () {
chai_1.assert.equal(bns.mul('-60830', '1', 16), '-0xed9e');
chai_1.assert.equal(index_1.mul('-60830', '1', 16), '-0xed9e');
});

@@ -104,70 +88,144 @@ });

it('-411 / 100000 = -0.00411', function () {
chai_1.assert.equal(bns.div('-411', '100000', 18), '-0.00411');
chai_1.assert.equal(index_1.div('-411', '100000', 18), '-0.00411');
});
it('20 / 5 = 4', function () {
chai_1.assert.equal(bns.div('20', '5'), '4');
chai_1.assert.equal(index_1.div('20', '5'), '4');
});
it('20.0 / 5.0 = 4', function () {
chai_1.assert.equal(bns.div('20.0', '5.0'), '4');
chai_1.assert.equal(index_1.div('20.0', '5.0'), '4');
});
it('20.0 / 5 = 4', function () {
chai_1.assert.equal(bns.div('20.0', '5'), '4');
chai_1.assert.equal(index_1.div('20.0', '5'), '4');
});
it('20 / 5.0 = 4', function () {
chai_1.assert.equal(bns.div('20', '5.0'), '4');
chai_1.assert.equal(index_1.div('20', '5.0'), '4');
});
it('10 / 3 = 3', function () {
chai_1.assert.equal(bns.div('10', '3'), '3');
chai_1.assert.equal(index_1.div('10', '3'), '3');
});
it('10 / 3 = 3.33333 (precision 5)', function () {
chai_1.assert.equal(bns.div('10', '3', 5), '3.33333');
chai_1.assert.equal(index_1.div('10', '3', 5), '3.33333');
});
it('very big num', function () {
chai_1.assert.equal(bns.div('400000000000000000000000000', '5'), '80000000000000000000000000');
chai_1.assert.equal(index_1.div('400000000000000000000000000', '5'), '80000000000000000000000000');
});
it('very big float', function () {
chai_1.assert.equal(bns.div('800000000000000000000000000.0000000000000000008', '2'), '400000000000000000000000000');
chai_1.assert.equal(index_1.div('800000000000000000000000000.0000000000000000008', '2'), '400000000000000000000000000');
});
it('very big float (precision 9, base 10)', function () {
chai_1.assert.equal(bns.div('800000000000000000000000000.000000008', '2', 9, 10), '400000000000000000000000000.000000004');
chai_1.assert.equal(index_1.div('800000000000000000000000000.000000008', '2', 9, 10), '400000000000000000000000000.000000004');
});
it('very small num', function () {
chai_1.assert.equal(bns.div('1234567890123456.123', '.00000000000000000000001'), '123456789012345612300000000000000000000');
chai_1.assert.equal(index_1.div('1234567890123456.123', '.00000000000000000000001'), '123456789012345612300000000000000000000');
});
it('Check error with hex output', function () {
chai_1.assert.throws(() => {
bns.div('10', '3', 5, 16);
index_1.div('10', '3', 5, 16);
});
});
it('resolve negative numbers in base 16', function () {
chai_1.assert.equal(bns.div('-60830', '1', 0, 16), '-0xed9e');
chai_1.assert.equal(index_1.div('-60830', '1', 0, 16), '-0xed9e');
});
});
describe('toBns', function () {
it('regular number', function () {
chai_1.assert.equal(index_1.toBns(123), '123');
});
it('scientific notation: regular numbers', function () {
const numStr = '5e0';
chai_1.assert.equal(index_1.toBns(numStr), '5');
});
it('scientific notation: big integer base', function () {
const big = 3000000000000000000000;
const bigStr = big.toString();
chai_1.assert.equal(bigStr, '3e+21');
chai_1.assert.equal(index_1.toBns(big), '3000000000000000000000');
chai_1.assert.equal(index_1.toBns(bigStr), '3000000000000000000000');
});
it('scientific notation: small integer base', function () {
const small = 0.000000000000000000003;
const smallStr = small.toString();
chai_1.assert.equal(smallStr, '3e-21');
chai_1.assert.equal(index_1.toBns(small), '0.000000000000000000003');
chai_1.assert.equal(index_1.toBns(smallStr), '0.000000000000000000003');
});
it('scientific notation: big negative base', function () {
const big = -3000000000000000000000;
const bigStr = big.toString();
chai_1.assert.equal(bigStr, '-3e+21');
chai_1.assert.equal(index_1.toBns(big), '-3000000000000000000000');
chai_1.assert.equal(index_1.toBns(bigStr), '-3000000000000000000000');
});
it('scientific notation: small negative base', function () {
const small = -0.000000000000000000003;
const smallStr = small.toString();
chai_1.assert(smallStr, '-3e-21');
chai_1.assert.equal(index_1.toBns(small), '-0.000000000000000000003');
chai_1.assert.equal(index_1.toBns(smallStr), '-0.000000000000000000003');
});
it('scientific notation: big decimal base', function () {
const decimal = 312000000000000000000000;
const decimalStr = decimal.toString();
chai_1.assert.equal(decimalStr, '3.12e+23');
chai_1.assert.equal(index_1.toBns(decimal), '312000000000000000000000');
chai_1.assert.equal(index_1.toBns(decimalStr), '312000000000000000000000');
});
it('scientific notation: small decimal base', function () {
const small = 0.000000000000000000000312;
const smallStr = small.toString();
chai_1.assert(smallStr, '3.12e-23');
chai_1.assert.equal(index_1.toBns(small), '0.000000000000000000000312');
chai_1.assert.equal(index_1.toBns(smallStr), '0.000000000000000000000312');
});
it('scientific notation: big negative decimal base', function () {
const small = -312000000000000000000000;
const smallStr = small.toString();
chai_1.assert(smallStr, '-3.12e+23');
chai_1.assert.equal(index_1.toBns(small), '-312000000000000000000000');
chai_1.assert.equal(index_1.toBns(smallStr), '-312000000000000000000000');
});
it('scientific notation: small negative decimal base', function () {
const small = -0.000000000000000000000312;
const smallStr = small.toString();
chai_1.assert(smallStr, '-3.12e-23');
chai_1.assert.equal(index_1.toBns(small), '-0.000000000000000000000312');
chai_1.assert.equal(index_1.toBns(smallStr), '-0.000000000000000000000312');
});
it('scientific notation: long decimal base', function () {
const longDecimalSciNo = '-1.2345678911121314151617181920e-12';
chai_1.assert.equal(index_1.toBns(longDecimalSciNo), '-0.000000000001234567891112131415161718192');
});
it('scientific notation: invalid numbers', function () {
chai_1.assert.throws(() => index_1.toBns('3.2.0e12'), `Invalid number: more than one decimal point '3.2.0e12'`);
chai_1.assert.throws(() => index_1.toBns('3x0e12'), `Invalid number: non-number characters '3x0e12'`);
chai_1.assert.throws(() => index_1.toBns('3e1.2'), `Invalid number: non-number characters '3e1.2'`);
});
});
describe('less than', function () {
it('15 < 20 = True', function () {
chai_1.assert.equal(bns.lt('15', '20'), true);
chai_1.assert.equal(index_1.lt('15', '20'), true);
});
it('20 < 15 = False', function () {
chai_1.assert.equal(bns.lt('20', '15'), false);
chai_1.assert.equal(index_1.lt('20', '15'), false);
});
it('20 < 20 = False', function () {
chai_1.assert.equal(bns.lt('20', '20'), false);
chai_1.assert.equal(index_1.lt('20', '20'), false);
});
it('Big num true', function () {
chai_1.assert.equal(bns.lt('4321000000000000000000000000000000004320', '4321000000000000000000000000000000004321'), true);
chai_1.assert.equal(index_1.lt('4321000000000000000000000000000000004320', '4321000000000000000000000000000000004321'), true);
});
it('Big num false', function () {
chai_1.assert.equal(bns.lt('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004321'), false);
chai_1.assert.equal(index_1.lt('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004321'), false);
});
it('Big num eq false', function () {
chai_1.assert.equal(bns.lt('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004322'), false);
chai_1.assert.equal(index_1.lt('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004322'), false);
});
it('Big float true', function () {
chai_1.assert.equal(bns.lt('1.004321000000000000000000000000000000004320', '1.004321000000000000000000000000000000004321'), true);
chai_1.assert.equal(index_1.lt('1.004321000000000000000000000000000000004320', '1.004321000000000000000000000000000000004321'), true);
});
it('Big float false', function () {
chai_1.assert.equal(bns.lt('10000000000000000.4321000000000000000000000000000000004322', '10000000000000000.4321000000000000000000000000000000004321'), false);
chai_1.assert.equal(index_1.lt('10000000000000000.4321000000000000000000000000000000004322', '10000000000000000.4321000000000000000000000000000000004321'), false);
});
it('Big float eq false', function () {
chai_1.assert.equal(bns.lt('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), false);
chai_1.assert.equal(index_1.lt('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), false);
});

@@ -177,27 +235,27 @@ });

it('15 > 20 = false', function () {
chai_1.assert.equal(bns.gt('15', '20'), false);
chai_1.assert.equal(index_1.gt('15', '20'), false);
});
it('20 > 15 = true', function () {
chai_1.assert.equal(bns.gt('20', '15'), true);
chai_1.assert.equal(index_1.gt('20', '15'), true);
});
it('20 > 20 = false', function () {
chai_1.assert.equal(bns.gt('20', '20'), false);
chai_1.assert.equal(index_1.gt('20', '20'), false);
});
it('Big num false', function () {
chai_1.assert.equal(bns.gt('4321000000000000000000000000000000004320', '4321000000000000000000000000000000004321'), false);
chai_1.assert.equal(index_1.gt('4321000000000000000000000000000000004320', '4321000000000000000000000000000000004321'), false);
});
it('Big num true', function () {
chai_1.assert.equal(bns.gt('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004321'), true);
chai_1.assert.equal(index_1.gt('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004321'), true);
});
it('Big num eq false', function () {
chai_1.assert.equal(bns.gt('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004322'), false);
chai_1.assert.equal(index_1.gt('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004322'), false);
});
it('Big float eq false', function () {
chai_1.assert.equal(bns.gt('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), false);
chai_1.assert.equal(index_1.gt('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), false);
});
it('Big float true', function () {
chai_1.assert.equal(bns.gt('1234.4321000000000000000001000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), true);
chai_1.assert.equal(index_1.gt('1234.4321000000000000000001000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), true);
});
it('Big float false', function () {
chai_1.assert.equal(bns.gt('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220001'), false);
chai_1.assert.equal(index_1.gt('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220001'), false);
});

@@ -207,27 +265,27 @@ });

it('15 <= 20 = True', function () {
chai_1.assert.equal(bns.lte('15', '20'), true);
chai_1.assert.equal(index_1.lte('15', '20'), true);
});
it('20 <= 15 = False', function () {
chai_1.assert.equal(bns.lte('20', '15'), false);
chai_1.assert.equal(index_1.lte('20', '15'), false);
});
it('20 <= 20 = true', function () {
chai_1.assert.equal(bns.lte('20', '20'), true);
chai_1.assert.equal(index_1.lte('20', '20'), true);
});
it('Big num true', function () {
chai_1.assert.equal(bns.lte('4321000000000000000000000000000000004320', '4321000000000000000000000000000000004321'), true);
chai_1.assert.equal(index_1.lte('4321000000000000000000000000000000004320', '4321000000000000000000000000000000004321'), true);
});
it('Big num false', function () {
chai_1.assert.equal(bns.lte('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004321'), false);
chai_1.assert.equal(index_1.lte('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004321'), false);
});
it('Big num eq true', function () {
chai_1.assert.equal(bns.lte('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004322'), true);
chai_1.assert.equal(index_1.lte('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004322'), true);
});
it('Big float eq true', function () {
chai_1.assert.equal(bns.lte('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), true);
chai_1.assert.equal(index_1.lte('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), true);
});
it('Big float false', function () {
chai_1.assert.equal(bns.lte('1234.4321000000000000000001000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), false);
chai_1.assert.equal(index_1.lte('1234.4321000000000000000001000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), false);
});
it('Big float true', function () {
chai_1.assert.equal(bns.lte('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220001'), true);
chai_1.assert.equal(index_1.lte('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220001'), true);
});

@@ -237,27 +295,27 @@ });

it('15 >= 20 = false', function () {
chai_1.assert.equal(bns.gte('15', '20'), false);
chai_1.assert.equal(index_1.gte('15', '20'), false);
});
it('20 >= 15 = true', function () {
chai_1.assert.equal(bns.gte('20', '15'), true);
chai_1.assert.equal(index_1.gte('20', '15'), true);
});
it('20 >= 20 = true', function () {
chai_1.assert.equal(bns.gte('20', '15'), true);
chai_1.assert.equal(index_1.gte('20', '15'), true);
});
it('Big num false', function () {
chai_1.assert.equal(bns.gte('4321000000000000000000000000000000004320', '4321000000000000000000000000000000004321'), false);
chai_1.assert.equal(index_1.gte('4321000000000000000000000000000000004320', '4321000000000000000000000000000000004321'), false);
});
it('Big num true', function () {
chai_1.assert.equal(bns.gte('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004321'), true);
chai_1.assert.equal(index_1.gte('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004321'), true);
});
it('Big num eq true', function () {
chai_1.assert.equal(bns.gte('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004322'), true);
chai_1.assert.equal(index_1.gte('4321000000000000000000000000000000004322', '4321000000000000000000000000000000004322'), true);
});
it('Big float eq true', function () {
chai_1.assert.equal(bns.gte('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), true);
chai_1.assert.equal(index_1.gte('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), true);
});
it('Big float true', function () {
chai_1.assert.equal(bns.gte('1234.4321000000000000000001000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), true);
chai_1.assert.equal(index_1.gte('1234.4321000000000000000001000000000000004322', '0000001234.43210000000000000000000000000000000043220000'), true);
});
it('Big float false', function () {
chai_1.assert.equal(bns.gte('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220001'), false);
chai_1.assert.equal(index_1.gte('1234.4321000000000000000000000000000000004322', '0000001234.43210000000000000000000000000000000043220001'), false);
});

@@ -267,22 +325,22 @@ });

it('15 == 20 = false', function () {
chai_1.assert.equal(bns.eq('15', '20'), false);
chai_1.assert.equal(index_1.eq('15', '20'), false);
});
it('20 == 15 = false', function () {
chai_1.assert.equal(bns.eq('20', '15'), false);
chai_1.assert.equal(index_1.eq('20', '15'), false);
});
it('20 == 20 = true', function () {
chai_1.assert.equal(bns.eq('20', '20'), true);
chai_1.assert.equal(index_1.eq('20', '20'), true);
});
it('00020 == 20 = true', function () {
chai_1.assert.equal(bns.eq('00020', '20'), true);
chai_1.assert.equal(index_1.eq('00020', '20'), true);
});
it('00020 == 20.000 = true', function () {
chai_1.assert.equal(bns.eq('00020', '20.000'), true);
chai_1.assert.equal(index_1.eq('00020', '20.000'), true);
});
it('00020.000000001 == 20.000 = false', function () {
chai_1.assert.equal(bns.eq('00020.000000001', '20.000'), false);
chai_1.assert.equal(index_1.eq('00020.000000001', '20.000'), false);
});
it('123456789.12345 == 0x1001 => Error', function () {
chai_1.assert.throws(() => {
bns.eq('123456789.12345', '0x1001');
index_1.eq('123456789.12345', '0x1001');
});

@@ -293,27 +351,27 @@ });

it('max(100, 1000) => 1000', function () {
chai_1.assert.equal(bns.max('100', '1000'), '1000');
chai_1.assert.equal(index_1.max('100', '1000'), '1000');
});
it('max(2000, 100) => 2000', function () {
chai_1.assert.equal(bns.max('2000', '100'), '2000');
chai_1.assert.equal(index_1.max('2000', '100'), '2000');
});
it('max(3000, 3000) => 3000', function () {
chai_1.assert.equal(bns.max('3000', '3000'), '3000');
chai_1.assert.equal(index_1.max('3000', '3000'), '3000');
});
it('max(0x100, 255) => 256', function () {
chai_1.assert.equal(bns.max('0x100', '255'), '256');
chai_1.assert.equal(index_1.max('0x100', '255'), '256');
});
it('max(255, 0x100) => 256', function () {
chai_1.assert.equal(bns.max('255', '0x100'), '256');
chai_1.assert.equal(index_1.max('255', '0x100'), '256');
});
it('max(257, 0x100, 16) => 0x101', function () {
chai_1.assert.equal(bns.max('257', '0x100', 16), '0x101');
chai_1.assert.equal(index_1.max('257', '0x100', 16), '0x101');
});
it('very big num', function () {
chai_1.assert.equal(bns.max('9876000000000000000000000000000000000002', '9876000000000000000000000000000000000001'), '9876000000000000000000000000000000000002');
chai_1.assert.equal(index_1.max('9876000000000000000000000000000000000002', '9876000000000000000000000000000000000001'), '9876000000000000000000000000000000000002');
});
it('max(100.001, 1000.0) => 1000', function () {
chai_1.assert.equal(bns.max('100.001', '1000'), '1000');
chai_1.assert.equal(index_1.max('100.001', '1000'), '1000');
});
it('max(100123.001, 1000.01) => 1000', function () {
chai_1.assert.equal(bns.max('100123.001', '1000.01'), '100123.001');
chai_1.assert.equal(index_1.max('100123.001', '1000.01'), '100123.001');
});

@@ -323,27 +381,27 @@ });

it('min(100, 1000) => 100', function () {
chai_1.assert.equal(bns.min('100', '1000'), '100');
chai_1.assert.equal(index_1.min('100', '1000'), '100');
});
it('min(2000, 100) => 100', function () {
chai_1.assert.equal(bns.min('1000', '100'), '100');
chai_1.assert.equal(index_1.min('1000', '100'), '100');
});
it('min(3000, 3000) => 3000', function () {
chai_1.assert.equal(bns.min('3000', '3000'), '3000');
chai_1.assert.equal(index_1.min('3000', '3000'), '3000');
});
it('min(0x100, 255) => 255', function () {
chai_1.assert.equal(bns.min('0x100', '255'), '255');
chai_1.assert.equal(index_1.min('0x100', '255'), '255');
});
it('min(255, 0x100) => 255', function () {
chai_1.assert.equal(bns.min('255', '0x100'), '255');
chai_1.assert.equal(index_1.min('255', '0x100'), '255');
});
it('min(257, 0x100, 16) => 0x100', function () {
chai_1.assert.equal(bns.min('257', '0x100', 16), '0x100');
chai_1.assert.equal(index_1.min('257', '0x100', 16), '0x100');
});
it('very big num', function () {
chai_1.assert.equal(bns.min('9876000000000000000000000000000000000002', '9876000000000000000000000000000000000001'), '9876000000000000000000000000000000000001');
chai_1.assert.equal(index_1.min('9876000000000000000000000000000000000002', '9876000000000000000000000000000000000001'), '9876000000000000000000000000000000000001');
});
it('min(100.001, 1000.0) => 1000', function () {
chai_1.assert.equal(bns.min('100.001', '1000'), '100.001');
chai_1.assert.equal(index_1.min('100.001', '1000'), '100.001');
});
it('min(100123.001, 1000.01) => 1000', function () {
chai_1.assert.equal(bns.min('100123.001', '1000.01'), '1000.01');
chai_1.assert.equal(index_1.min('100123.001', '1000.01'), '1000.01');
});

@@ -353,21 +411,21 @@ });

it('abs("1") => "1")', function () {
chai_1.assert.equal(bns.abs('1'), '1');
chai_1.assert.equal(index_1.abs('1'), '1');
});
it('abs("-1") => "1")', function () {
chai_1.assert.equal(bns.abs('-1'), '1');
chai_1.assert.equal(index_1.abs('-1'), '1');
});
it('abs("-1.123") => "1.123")', function () {
chai_1.assert.equal(bns.abs('-1.123'), '1.123');
chai_1.assert.equal(index_1.abs('-1.123'), '1.123');
});
it('abs("-.123456789012345") => "0.123456789012345")', function () {
chai_1.assert.equal(bns.abs('-.123456789012345'), '0.123456789012345');
chai_1.assert.equal(index_1.abs('-.123456789012345'), '0.123456789012345');
});
it('abs(".123456789012345") => "0.123456789012345")', function () {
chai_1.assert.equal(bns.abs('.123456789012345'), '0.123456789012345');
chai_1.assert.equal(index_1.abs('.123456789012345'), '0.123456789012345');
});
it('abs("-0x11") => "17")', function () {
chai_1.assert.equal(bns.abs('-0x11'), '17');
chai_1.assert.equal(index_1.abs('-0x11'), '17');
});
it('abs("-0x11", 16) => "0x11")', function () {
chai_1.assert.equal(bns.abs('-0x11', 16), '0x11');
chai_1.assert.equal(index_1.abs('-0x11', 16), '0x11');
});

@@ -377,45 +435,45 @@ });

it('toFixed("100", 2) => 100.00', function () {
chai_1.assert.equal(bns.toFixed('100', 2), '100.00');
chai_1.assert.equal(index_1.toFixed('100', 2), '100.00');
});
it('toFixed("100.123", 2) => 100.12', function () {
chai_1.assert.equal(bns.toFixed('100.123', 2), '100.123');
chai_1.assert.equal(index_1.toFixed('100.123', 2), '100.123');
});
it('toFixed("00100.123", 2) => 100.12', function () {
chai_1.assert.equal(bns.toFixed('100.123', 2), '100.123');
chai_1.assert.equal(index_1.toFixed('100.123', 2), '100.123');
});
it('toFixed("00100.12300", 2) => 100.12', function () {
chai_1.assert.equal(bns.toFixed('100.123', 2), '100.123');
chai_1.assert.equal(index_1.toFixed('100.123', 2), '100.123');
});
it('toFixed("00100.12300", 2) => 100.12', function () {
chai_1.assert.equal(bns.toFixed('100.1', 2), '100.10');
chai_1.assert.equal(index_1.toFixed('100.1', 2), '100.10');
});
it('toFixed("00100", 5) => 100.00000', function () {
chai_1.assert.equal(bns.toFixed('00100', 5), '100.00000');
chai_1.assert.equal(index_1.toFixed('00100', 5), '100.00000');
});
it('toFixed("00100.12345678", 5, 7) => 100.12345678', function () {
chai_1.assert.equal(bns.toFixed('00100.12345678', 5, 7), '100.1234567');
chai_1.assert.equal(index_1.toFixed('00100.12345678', 5, 7), '100.1234567');
});
it('toFixed("00100.12345678", 5, 8) => 100.1234568', function () {
chai_1.assert.equal(bns.toFixed('00100.12345678', 5, 8), '100.12345678');
chai_1.assert.equal(index_1.toFixed('00100.12345678', 5, 8), '100.12345678');
});
it('toFixed("00100.12345678", 5, 6) => 100.123456', function () {
chai_1.assert.equal(bns.toFixed('00100.12345678', 5, 6), '100.123456');
chai_1.assert.equal(index_1.toFixed('00100.12345678', 5, 6), '100.123456');
});
it('toFixed("1.0", 1, 6) => "1.0"', function () {
chai_1.assert.equal(bns.toFixed('1.0', 1, 6), '1.0');
chai_1.assert.equal(index_1.toFixed('1.0', 1, 6), '1.0');
});
it('toFixed("0", 0, 6) => "0"', function () {
chai_1.assert.equal(bns.toFixed('0', 0, 6), '0');
chai_1.assert.equal(index_1.toFixed('0', 0, 6), '0');
});
it('toFixed("00", 0, 6) => "0"', function () {
chai_1.assert.equal(bns.toFixed('00', 0, 6), '0');
chai_1.assert.equal(index_1.toFixed('00', 0, 6), '0');
});
it('toFixed("-1.0", 1, 6) => "-1.0"', function () {
chai_1.assert.equal(bns.toFixed('-1.0', 1, 6), '-1.0');
chai_1.assert.equal(index_1.toFixed('-1.0', 1, 6), '-1.0');
});
it('toFixed("-00100.12345678", 5, 6) => -100.123456', function () {
chai_1.assert.equal(bns.toFixed('-00100.12345678', 5, 6), '-100.123456');
chai_1.assert.equal(index_1.toFixed('-00100.12345678', 5, 6), '-100.123456');
});
it('toFixed("-00100.12345678", 0, 0) => -100', function () {
chai_1.assert.equal(bns.toFixed('-00100.12345678', 0, 0), '-100');
chai_1.assert.equal(index_1.toFixed('-00100.12345678', 0, 0), '-100');
});

@@ -425,10 +483,10 @@ });

it('100 => 2', function () {
chai_1.assert.equal(bns.log10('100'), 2);
chai_1.assert.equal(index_1.log10('100'), 2);
});
it('100000000000000000000 => 20', function () {
chai_1.assert.equal(bns.log10('100000000000000000000'), 20);
chai_1.assert.equal(index_1.log10('100000000000000000000'), 20);
});
it('100000000000000001000 => Error', function () {
chai_1.assert.throws(() => {
bns.log10('100000000000000001000');
index_1.log10('100000000000000001000');
});

@@ -438,3 +496,3 @@ });

chai_1.assert.throws(() => {
bns.log10('3000000000000000000');
index_1.log10('3000000000000000000');
});

@@ -444,3 +502,3 @@ });

chai_1.assert.throws(() => {
bns.log10('00100000000000000001000');
index_1.log10('00100000000000000001000');
});

@@ -447,0 +505,0 @@ });

{
"name": "biggystring",
"version": "4.1.3",
"version": "4.2.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

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