Add BN min/max functions (#401)
* Add BN min/max * fixes * array 2+ type * assert * rv hardcoding
This commit is contained in:
@@ -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';
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
@@ -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]);
|
||||
}
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
@@ -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]);
|
||||
}
|
||||
@@ -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>;
|
||||
|
||||
Reference in New Issue
Block a user