Dedupes for min/max (#1297)
This commit is contained in:
@@ -9,7 +9,6 @@ export * from './consts';
|
||||
|
||||
export { nSqrt } from './sqrt';
|
||||
export { nToBigInt } from './toBigInt';
|
||||
export { nMax } from './max';
|
||||
export { nMin } from './min';
|
||||
export { nMax, nMin } from './min';
|
||||
export { nToHex } from './toHex';
|
||||
export { nToU8a } from './toU8a';
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
*/
|
||||
export function nMax (...items: bigint[]): bigint {
|
||||
assert(items.length >= 1, 'Must provide one or more bigint arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
if (items[i] > result) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -3,20 +3,40 @@
|
||||
|
||||
import { assert } from '../assert';
|
||||
|
||||
/**
|
||||
* @name nMin
|
||||
* @summary Finds and returns the lowest value in an array of bigint.
|
||||
*/
|
||||
export function nMin (...items: bigint[]): bigint {
|
||||
function gt (a: bigint, b: bigint): boolean {
|
||||
return a > b;
|
||||
}
|
||||
|
||||
function lt (a: bigint, b: bigint): boolean {
|
||||
return a < b;
|
||||
}
|
||||
|
||||
function find (items: bigint[], cmp: (a: bigint, b: bigint) => boolean): bigint {
|
||||
assert(items.length >= 1, 'Must provide one or more bigint arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
if (items[i] < result) {
|
||||
if (cmp(items[i], result)) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
*/
|
||||
export function nMax (...items: bigint[]): bigint {
|
||||
return find(items, gt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name nMin
|
||||
* @summary Finds and returns the lowest value in an array of bigint.
|
||||
*/
|
||||
export function nMin (...items: bigint[]): bigint {
|
||||
return find(items, lt);
|
||||
}
|
||||
|
||||
@@ -9,8 +9,7 @@ export * from './consts';
|
||||
|
||||
export { BN } from './bn';
|
||||
export { bnFromHex } from './fromHex';
|
||||
export { bnMax } from './max';
|
||||
export { bnMin } from './min';
|
||||
export { bnMax, bnMin } from './min';
|
||||
export { bnSqrt } from './sqrt';
|
||||
export { bnToBn } from './toBn';
|
||||
export { bnToHex } from './toHex';
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
export function checkMaxMin (type: 'max' | 'min', items: BN[]): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = BN[type](result, items[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnMax
|
||||
* @summary Finds and returns the highest value in an array of BNs.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnMax } from '@polkadot/util';
|
||||
*
|
||||
* bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
|
||||
* ```
|
||||
*/
|
||||
export function bnMax (...items: BN[]): BN {
|
||||
return checkMaxMin('max', items);
|
||||
}
|
||||
@@ -1,9 +1,37 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from './bn';
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
import { checkMaxMin } from './max';
|
||||
export function find (items: BN[], cmp: (a: BN, b: BN) => BN): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = cmp(result, items[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnMax
|
||||
* @summary Finds and returns the highest value in an array of BNs.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnMax } from '@polkadot/util';
|
||||
*
|
||||
* bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
|
||||
* ```
|
||||
*/
|
||||
export function bnMax (...items: BN[]): BN {
|
||||
return find(items, BN.max);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnMin
|
||||
@@ -19,5 +47,5 @@ import { checkMaxMin } from './max';
|
||||
* ```
|
||||
*/
|
||||
export function bnMin (...items: BN[]): BN {
|
||||
return checkMaxMin('min', items);
|
||||
return find(items, BN.min);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user