Add BN min/max functions (#401)

* Add BN min/max

* fixes

* array 2+ type

* assert

* rv hardcoding
This commit is contained in:
kwingram25
2019-05-07 18:13:41 +02:00
committed by Jaco Greeff
parent d3204d9e23
commit 9c03ec82ca
6 changed files with 102 additions and 0 deletions
+2
View File
@@ -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';
+15
View File
@@ -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));
});
});
+32
View File
@@ -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
* <BR>
*
* ```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]);
}
+15
View File
@@ -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));
});
});
+31
View File
@@ -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
* <BR>
*
* ```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]);
}
+7
View File
@@ -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<BN>;