operation-strint
v1.1.1
Published
String-based integer arithmetic utilities for JavaScript
Maintainers
Readme
operation-strint
String-based integer arithmetic helpers for JavaScript and TypeScript.
operation-strint works on base-10 integers stored as strings, which makes it useful when values may exceed JavaScript's safe integer range. The package now ships as an ES module and includes built-in TypeScript declarations.
Install
npm install operation-strintUsage
import { sum, minus, multiply, divby2, pow, photo } from 'operation-strint';
sum('12983479272', '323984729837');
// '336968209109'
minus('10001', '2');
// '9999'
multiply('30', '1023');
// '30690'
divby2('121');
// '60.5'
pow('2', '5');
// '32'
photo('JHGAJHFJD000000');
// 'JHGAJHFJD000001'You can also import the full namespace:
import op from 'operation-strint';
op.sum('9', '3');
// '12'TypeScript
import { sum, type ValidationMessage } from 'operation-strint';
const result: string = sum('12', '3');
const validation: ValidationMessage = 'Numbers cannot be empty';API
All arithmetic inputs are expected to be strings containing decimal digits unless noted otherwise.
sum(a, b, removeLeadingZeros = true): stringminus(a, b, removeLeadingZeros = true): stringmultiply(a, b, removeLeadingZeros = true): stringmultiplybysum(a, b, removeLeadingZeros = true): stringdivby2(value, removeLeadingZeros = true): stringpow(base, exponent, removeLeadingZeros = true): stringfact(value): stringphoto(value): stringIsMoreThan(a, b, removeLeadingZeros = true): boolean | ValidationMessageCSRange: { sbyte; byte; short; ushort; int; uint; long; ulong }numinbase10: string[]snumpat: RegExp
Behavior Notes
- Empty inputs return
Numbers cannot be emptyin the validated arithmetic helpers. - Non-digit input returns
Numbers must be numbersin the validated arithmetic helpers. - Leading zeros are removed by default for arithmetic operations.
- Negative numbers and decimal values are not supported.
Examples Covered by Tests
import * as op from 'operation-strint';
op.sum('0012', '3');
// '15'
op.sum('323928374982384729837', '323928374982384729837');
// '647856749964769459674'
op.minus('100000', '1');
// '99999'
op.multiply('123', '4');
// '492'
op.divby2('12000001');
// '6000000.5'
op.pow('4', '0');
// '1'
op.photo('9999');
// '0000'
op.multiplybysum('12', '3');
// '36'
op.fact('5');
// '120'License
MIT
