@signpostmarv/intermediary-number 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +0,0 @@
1
- import BigNumber from "bignumber.js";
2
- import Fraction from "fraction.js";
3
- import { numeric_string } from './NumberStrings';
4
- export type input_types = BigNumber | Fraction | number | string;
5
- export type value_types = BigNumber | Fraction | numeric_string;
6
- export type type_property_types = 'BigNumber' | 'Fraction' | 'amount_string' | 'numeric_string';
@@ -1 +0,0 @@
1
- export {};
@@ -1,14 +0,0 @@
1
- import { integer_string__type, StringPassedRegExp } from './Docs.json';
2
- import BigNumber from 'bignumber.js';
3
- import Fraction from 'fraction.js';
4
- import type { operand_types } from './IntermediaryNumber';
5
- export type amount_string = StringPassedRegExp<'^\\d+(?:\\.\\d{1,6})?$'> | integer_string__type | '1' | '0';
6
- export type numeric_string = amount_string | StringPassedRegExp<'^-?(?:\\d*\\.\\d+|\\d+(?:\\.\\d+)?)$'>;
7
- export declare class NumberStrings {
8
- static amount_string(maybe: string): amount_string;
9
- static is_amount_string(maybe: unknown): maybe is amount_string;
10
- static is_numeric_string(maybe: unknown): maybe is numeric_string;
11
- static round_off(number: BigNumber | Fraction | operand_types): amount_string;
12
- private static configure;
13
- private static throw_if_not_amount_string;
14
- }
@@ -1,53 +0,0 @@
1
- import { is_string, NotAnAmountString, } from './Docs.json';
2
- import BigNumber from 'bignumber.js';
3
- import Fraction from 'fraction.js';
4
- export class NumberStrings {
5
- static amount_string(maybe) {
6
- this.throw_if_not_amount_string(maybe);
7
- return maybe;
8
- }
9
- static is_amount_string(maybe) {
10
- return (is_string(maybe)
11
- && (maybe === '0'
12
- || /^\d+(?:\.\d{1,6})?$/.test(maybe)
13
- || /^\d*(?:\.\d{1,6})$/.test(maybe)
14
- || /^\d+$/.test(maybe)));
15
- }
16
- static is_numeric_string(maybe) {
17
- return (this.is_amount_string(maybe)
18
- || (is_string(maybe)
19
- && /^-?(?:\d*\.\d+|\d+(?:\.\d+)?)$/.test(maybe)));
20
- }
21
- static round_off(number) {
22
- let result;
23
- number = ((number instanceof BigNumber)
24
- || (number instanceof Fraction))
25
- ? number
26
- : number.toBigNumberOrFraction();
27
- if (number instanceof BigNumber) {
28
- this.configure();
29
- result = number.toString();
30
- }
31
- else {
32
- result = number.valueOf().toString();
33
- }
34
- if (/\.\d{7,}$/.test(result)) {
35
- const [before, after] = result.split('.');
36
- return `${before}.${'0' === after.substring(6, 7)
37
- ? after.substring(0, 6).replace(/0+$/, '')
38
- : (parseInt(after.substring(0, 6), 10) + 1).toString().padStart(Math.min(6, after.length), '0')}`.replace(/\.$/, '');
39
- }
40
- return result;
41
- }
42
- static configure() {
43
- BigNumber.set({
44
- DECIMAL_PLACES: 7,
45
- ROUNDING_MODE: BigNumber.ROUND_HALF_CEIL,
46
- });
47
- }
48
- static throw_if_not_amount_string(maybe) {
49
- if (!this.is_amount_string(maybe)) {
50
- throw new NotAnAmountString('Not a supported amount string!');
51
- }
52
- }
53
- }
package/lib/Numbers.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import BigNumber from 'bignumber.js';
2
- import Fraction from 'fraction.js';
3
- import { math_types, operand_types } from './IntermediaryNumber';
4
- import { amount_string } from './NumberStrings';
5
- export type number_arg = BigNumber | number | amount_string;
6
- export declare class Numbers {
7
- static divide_if_not_one(left: math_types, right: Fraction, require_fraction: true): Fraction;
8
- static divide_if_not_one(left: math_types, right: Fraction, require_fraction: false): Fraction | math_types;
9
- static least_common_multiple_deferred(numbers: [
10
- (number_arg | operand_types),
11
- (number_arg | operand_types),
12
- ...(number_arg | operand_types)[]
13
- ]): (Fraction);
14
- static sum_series_fraction(a: Fraction, b: Fraction): Fraction;
15
- }
package/lib/Numbers.js DELETED
@@ -1,49 +0,0 @@
1
- import assert from 'assert';
2
- import Fraction from 'fraction.js';
3
- import { IntermediaryNumber, } from './IntermediaryNumber';
4
- export class Numbers {
5
- static divide_if_not_one(left, right, require_fraction) {
6
- const result = (0 === right.compare(1))
7
- ? left
8
- : IntermediaryNumber.reuse_or_create(left).divide(right);
9
- return require_fraction
10
- ? ((result instanceof Fraction)
11
- ? result
12
- : IntermediaryNumber.reuse_or_create(result).toFraction())
13
- : result;
14
- }
15
- static least_common_multiple_deferred(numbers) {
16
- if (2 === numbers.length) {
17
- return (IntermediaryNumber.reuse_or_create(numbers[0]).toFraction().lcm(IntermediaryNumber.reuse_or_create(numbers[1]).toFraction()));
18
- }
19
- return (numbers.map(e => IntermediaryNumber.reuse_or_create(e).toFraction()).reduce(
20
- // based on https://www.npmjs.com/package/mlcm?activeTab=code
21
- (was, is) => {
22
- return was.mul(is).abs().div(was.gcd(is));
23
- }));
24
- }
25
- static sum_series_fraction(a, b) {
26
- assert.strictEqual(b.compare(a), -1, `Expecting ${b.toString()} to be less than ${a.toString()}`);
27
- // adapted from @stdlib/math-base-tools-sum-series
28
- const tolerance = (new Fraction(1)).div((new Fraction(2)).pow(52));
29
- let counter = 10000000;
30
- const divisor = a.div(b);
31
- function calculate(number) {
32
- let previous = number;
33
- return () => {
34
- const next = previous.div(divisor);
35
- previous = next;
36
- return next;
37
- };
38
- }
39
- const generator = calculate(a);
40
- let next_term;
41
- let result = a;
42
- do {
43
- next_term = generator();
44
- result = result.add(next_term.simplify(tolerance.valueOf()));
45
- } while ((-1 === tolerance.mul(result).abs().compare(next_term.abs()))
46
- && --counter);
47
- return result;
48
- }
49
- }