From 9c1fd06c2813d686fb32411f30aa7362fdfbeb22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lenksj=C3=B6?= <5889538+lenkan@users.noreply.github.com> Date: Mon, 8 Apr 2024 10:47:33 +0200 Subject: [PATCH] use named imports from mathjs to reduce bundle size (#235) --- src/exports.ts | 1 - src/keri/core/tholder.ts | 11 +++++----- src/math.ts | 3 --- test/core/tholder.test.ts | 45 +++++++++++++++++---------------------- 4 files changed, 25 insertions(+), 35 deletions(-) delete mode 100644 src/math.ts diff --git a/src/exports.ts b/src/exports.ts index 7561737f..839f508a 100644 --- a/src/exports.ts +++ b/src/exports.ts @@ -1,5 +1,4 @@ export * from './ready'; -export * from './math'; export * from './keri/app/habery'; export * from './keri/app/controller'; diff --git a/src/keri/core/tholder.ts b/src/keri/core/tholder.ts index 4221df25..29234d5e 100644 --- a/src/keri/core/tholder.ts +++ b/src/keri/core/tholder.ts @@ -1,7 +1,6 @@ import { BexDex, Matter, NumDex } from './matter'; import { CesrNumber } from './number'; -import { Fraction } from 'mathjs'; -import { math } from '../../index'; +import { Fraction, format, sum, fraction } from 'mathjs'; export class Tholder { private _weighted: boolean = false; @@ -45,9 +44,9 @@ export class Tholder { let sith = this.thold.map((clause: Fraction[]) => { return clause.map((c) => { if (0 < Number(c) && Number(c) < 1) { - return math.format(c, { fraction: 'ratio' }); + return format(c, { fraction: 'ratio' }); } else { - return math.format(c, { fraction: 'decimal' }); + return format(c, { fraction: 'decimal' }); } }); }); @@ -159,7 +158,7 @@ export class Tholder { private _processWeighted(thold: Array>) { for (const clause of thold) { - if (Number(math.sum(clause)) < 1) { + if (Number(sum(clause)) < 1) { throw new Error( 'Invalid sith clause: ' + thold + @@ -178,7 +177,7 @@ export class Tholder { } private weight(w: string): Fraction { - return math.fraction(w); + return fraction(w); } private _satisfy_numeric(indices: any[]) { diff --git a/src/math.ts b/src/math.ts deleted file mode 100644 index d5e60e2f..00000000 --- a/src/math.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { create, all } from 'mathjs'; -const config = {}; -export const math = create(all, config); diff --git a/test/core/tholder.test.ts b/test/core/tholder.test.ts index 5efced45..765242b5 100644 --- a/test/core/tholder.test.ts +++ b/test/core/tholder.test.ts @@ -1,6 +1,6 @@ import { strict as assert } from 'assert'; +import { fraction } from 'mathjs'; import { Tholder } from '../../src/keri/core/tholder'; -import { math } from '../../src'; describe('THolder', () => { it('should hold thresholds', async () => { @@ -66,11 +66,11 @@ describe('THolder', () => { assert.equal(tholder.size, 5); assert.deepStrictEqual(tholder.thold, [ [ - math.fraction('1/2'), - math.fraction('1/2'), - math.fraction('1/4'), - math.fraction('1/4'), - math.fraction('1/4'), + fraction('1/2'), + fraction('1/2'), + fraction('1/4'), + fraction('1/4'), + fraction('1/4'), ], ]); assert.equal(tholder.satisfy([0, 1]), true); @@ -96,13 +96,8 @@ describe('THolder', () => { ['1/3', '1/3', '1/3', '1/3'], ]); assert.deepStrictEqual(tholder.thold, [ - [math.fraction(1, 2), math.fraction(1, 2), math.fraction(1, 2)], - [ - math.fraction(1, 3), - math.fraction(1, 3), - math.fraction(1, 3), - math.fraction(1, 3), - ], + [fraction(1, 2), fraction(1, 2), fraction(1, 2)], + [fraction(1, 3), fraction(1, 3), fraction(1, 3), fraction(1, 3)], ]); assert.equal(tholder.satisfy([0, 2, 3, 5, 6]), true); assert.equal(tholder.satisfy([1, 2, 3, 4, 5]), true); @@ -122,13 +117,13 @@ describe('THolder', () => { ]); assert.deepStrictEqual(tholder.thold, [ [ - math.fraction(1, 2), - math.fraction(1, 2), - math.fraction(1, 4), - math.fraction(1, 4), - math.fraction(1, 4), + fraction(1, 2), + fraction(1, 2), + fraction(1, 4), + fraction(1, 4), + fraction(1, 4), ], - [math.fraction(1, 1), math.fraction(1, 1)], + [fraction(1, 1), fraction(1, 1)], ]); assert.equal(tholder.satisfy([1, 2, 3, 5]), true); assert.equal(tholder.satisfy([0, 1, 6]), true); @@ -155,13 +150,13 @@ describe('THolder', () => { ); assert.deepStrictEqual(tholder.thold, [ [ - math.fraction(1, 2), - math.fraction(1, 2), - math.fraction(1, 4), - math.fraction(1, 4), - math.fraction(1, 4), + fraction(1, 2), + fraction(1, 2), + fraction(1, 4), + fraction(1, 4), + fraction(1, 4), ], - [math.fraction(1, 1), math.fraction(1, 1)], + [fraction(1, 1), fraction(1, 1)], ]); assert.equal(tholder.satisfy([1, 2, 3, 5]), true); assert.equal(tholder.satisfy([0, 1, 6]), true);