From 9c03ec82cad87b6883ff4049d5d4ab66c58272fb Mon Sep 17 00:00:00 2001 From: kwingram25 Date: Tue, 7 May 2019 18:13:41 +0200 Subject: [PATCH] Add BN min/max functions (#401) * Add BN min/max * fixes * array 2+ type * assert * rv hardcoding --- packages/util/src/bn/index.ts | 2 ++ packages/util/src/bn/max.spec.ts | 15 +++++++++++++++ packages/util/src/bn/max.ts | 32 ++++++++++++++++++++++++++++++++ packages/util/src/bn/min.spec.ts | 15 +++++++++++++++ packages/util/src/bn/min.ts | 31 +++++++++++++++++++++++++++++++ packages/util/src/types.ts | 7 +++++++ 6 files changed, 102 insertions(+) create mode 100644 packages/util/src/bn/max.spec.ts create mode 100644 packages/util/src/bn/max.ts create mode 100644 packages/util/src/bn/min.spec.ts create mode 100644 packages/util/src/bn/min.ts diff --git a/packages/util/src/bn/index.ts b/packages/util/src/bn/index.ts index 66f723c5d..7d70ddecc 100644 --- a/packages/util/src/bn/index.ts +++ b/packages/util/src/bn/index.ts @@ -7,6 +7,8 @@ */ export { default as bnFromHex } from './fromHex'; +export { default as bnMax } from './max'; +export { default as bnMin } from './min'; export { default as bnToBn } from './toBn'; export { default as bnToHex } from './toHex'; export { default as bnToU8a } from './toU8a'; diff --git a/packages/util/src/bn/max.spec.ts b/packages/util/src/bn/max.spec.ts new file mode 100644 index 000000000..cf4c16f99 --- /dev/null +++ b/packages/util/src/bn/max.spec.ts @@ -0,0 +1,15 @@ +// Copyright 2017-2019 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import BN from 'bn.js'; + +import { bnMax } from '.'; + +describe('bnMax', () => { + it('finds BN maximum', () => { + expect( + bnMax(new BN(1), new BN(2), new BN(3)) + ).toEqual(new BN(3)); + }); +}); diff --git a/packages/util/src/bn/max.ts b/packages/util/src/bn/max.ts new file mode 100644 index 000000000..2e05e31dd --- /dev/null +++ b/packages/util/src/bn/max.ts @@ -0,0 +1,32 @@ +// Copyright 2017-2019 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import BN from 'bn.js'; + +import assert from '../assert'; +import { BnList } from '../types'; + +/** + * @name max + * @summary Finds and returns the highest value in an array of BNs. + * @example + *
+ * + * ```javascript + * import BN from 'bn.js'; + * import { max } from '@polkadot/util'; + * + * max([new BN(1), new BN(3), new BN(2)]).toString(); // => '3' + * ``` + */ +export default function max ( + ...items: BnList +): BN { + assert( + items && items.length >= 2, + 'Must provide two or more BN arguments' + ); + + return items.reduce((acc: BN, val: BN) => BN.max(acc, val), items[0]); +} diff --git a/packages/util/src/bn/min.spec.ts b/packages/util/src/bn/min.spec.ts new file mode 100644 index 000000000..158eac90f --- /dev/null +++ b/packages/util/src/bn/min.spec.ts @@ -0,0 +1,15 @@ +// Copyright 2017-2019 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import BN from 'bn.js'; + +import { bnMin } from '.'; + +describe('bnMin', () => { + it('finds BN minimum', () => { + expect( + bnMin(new BN(1), new BN(2), new BN(3)) + ).toEqual(new BN(1)); + }); +}); diff --git a/packages/util/src/bn/min.ts b/packages/util/src/bn/min.ts new file mode 100644 index 000000000..a9b71fc49 --- /dev/null +++ b/packages/util/src/bn/min.ts @@ -0,0 +1,31 @@ +// Copyright 2017-2019 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import BN from 'bn.js'; + +import assert from '../assert'; +import { BnList } from '../types'; +/** + * @name min + * @summary Finds and returns the smallest value in an array of BNs. + * @example + *
+ * + * ```javascript + * import BN from 'bn.js'; + * import { min } from '@polkadot/util'; + * + * min([new BN(1), new BN(3), new BN(2)]).toString(); // => '1' + * ``` + */ +export default function min ( + ...items: BnList +): BN { + assert( + items && items.length >= 2, + 'Must provide two or more BN arguments' + ); + + return items.reduce((acc: BN, val: BN) => BN.min(acc, val), items[0]); +} diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 3dd36e1a1..9cc7828a7 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import BN from 'bn.js'; + export interface ExtErrorInterface { code: number; data: any; @@ -24,3 +26,8 @@ export interface ToBnOptions { isLe?: boolean; isNegative?: boolean; } + +export type BnList = { + 0: BN, + 1: BN +} & Array;