Skip to content

Commit

Permalink
use named imports from mathjs to reduce bundle size (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenkan committed Apr 8, 2024
1 parent 65d5b83 commit a6949da
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/exports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './ready';
export * from './math';

export * from './keri/app/habery';
export * from './keri/app/controller';
Expand Down
11 changes: 5 additions & 6 deletions src/keri/core/tholder.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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' });
}
});
});
Expand Down Expand Up @@ -159,7 +158,7 @@ export class Tholder {

private _processWeighted(thold: Array<Array<Fraction>>) {
for (const clause of thold) {
if (Number(math.sum(clause)) < 1) {
if (Number(sum(clause)) < 1) {
throw new Error(
'Invalid sith clause: ' +
thold +
Expand All @@ -178,7 +177,7 @@ export class Tholder {
}

private weight(w: string): Fraction {
return math.fraction(w);
return fraction(w);
}

private _satisfy_numeric(indices: any[]) {
Expand Down
3 changes: 0 additions & 3 deletions src/math.ts

This file was deleted.

45 changes: 20 additions & 25 deletions test/core/tholder.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a6949da

Please sign in to comment.