Skip to content

Commit

Permalink
refactor: move BigInt math to core package, add sum(bigint[])
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Sep 23, 2021
1 parent d36dd5c commit 41f3e03
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Expand Up @@ -14,7 +14,7 @@
"tscNoEmit": "shx echo typescript --noEmit command not implemented yet",
"cleanup": "shx rm -rf dist node_modules",
"lint": "eslint --ignore-path ../../.eslintignore \"**/*.ts\"",
"test": "shx echo No tests in this package"
"test": "jest -c ./test/jest.config.js"
},
"devDependencies": {
"shx": "^0.3.3"
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/BigIntMath.ts
@@ -0,0 +1,8 @@
export const BigIntMath = {
abs(x: bigint): bigint {
return x < 0n ? -x : x;
},
sum(arr: bigint[]): bigint {
return arr.reduce((result, num) => result + num, 0n);
}
};
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Expand Up @@ -3,3 +3,4 @@ export * as Cardano from './Cardano';
export * from './Genesis';
export * from './Provider';
export * from './Transaction';
export * from './BigIntMath';
7 changes: 7 additions & 0 deletions packages/core/test/.eslintrc.js
@@ -0,0 +1,7 @@
module.exports = {
"extends": ["../../../test/.eslintrc.js"],
"parserOptions": {
"project": "./tsconfig.json",
"tsconfigRootDir": __dirname
}
}
12 changes: 12 additions & 0 deletions packages/core/test/BigIntMath.test.ts
@@ -0,0 +1,12 @@
import { BigIntMath } from '@src/BigIntMath';

describe('BigIntMath', () => {
describe('abs', () => {
it('positive', () => expect(BigIntMath.abs(1n)).toBe(1n));
it('negative', () => expect(BigIntMath.abs(-1n)).toBe(1n));
});
describe('sum', () => {
it('empty', () => expect(BigIntMath.sum([])).toBe(0n));
it('non-empty', () => expect(BigIntMath.sum([-1n, 5n])).toBe(4n));
});
});
11 changes: 11 additions & 0 deletions packages/core/test/jest.config.js
@@ -0,0 +1,11 @@
const { pathsToModuleNameMapper } = require('ts-jest/utils')
const { compilerOptions } = require('./tsconfig')

module.exports = {
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
preset: 'ts-jest',
transform: {
"^.+\\.test.ts?$": "ts-jest"
},
testTimeout: 120000
}
10 changes: 10 additions & 0 deletions packages/core/test/tsconfig.json
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@src/*": ["../src/*"]
}
},
"references": [{ "path": "../src" }]
}
@@ -1,12 +1,14 @@
/* eslint-disable prettier/prettier */
import { Schema } from '@cardano-ogmios/client';
import { Math as BigIntMath } from '../lib/BigInt';
import { BigIntMath } from '@cardano-sdk/core';

const throwIfNegative = (value: bigint | number): void => {
if (value < 0) {
throw new Error('The value provided cannot be applied as it will result in a negative balance');
}
};

// eslint-disable-next-line sonarjs/cognitive-complexity
export const applyValue = (balance: Schema.Value, value: Schema.Value, spending = false): Schema.Value => {
const coins = balance.coins + (spending ? -Math.abs(value.coins) : value.coins);
throwIfNegative(coins);
Expand All @@ -20,9 +22,7 @@ export const applyValue = (balance: Schema.Value, value: Schema.Value, spending
balanceToApply.assets[assetId] =
balance.assets[assetId] !== undefined
? balance.assets[assetId] + (spending ? -BigIntMath.abs(qty) : qty)
: spending
? -BigIntMath.abs(qty)
: qty;
: (spending ? -BigIntMath.abs(qty) : qty);
throwIfNegative(balanceToApply.assets[assetId]);
}
}
Expand Down
5 changes: 0 additions & 5 deletions packages/golden-test-generator/src/lib/BigInt.ts

This file was deleted.

0 comments on commit 41f3e03

Please sign in to comment.