Shared helper function for bn/bi min/max compares (#1527)
* Shared helper function for bn/bi min/max compares * min/max test
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
|
||||
/** @internal */
|
||||
export function createCmp <T> (cmp: (a: T, b: T) => boolean): (...items: T[]) => T {
|
||||
return (...items: T[]): T => {
|
||||
assert(items.length >= 1, 'Must provide one or more arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
if (cmp(items[i], result)) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
@@ -25,6 +25,6 @@ describe('nMax', (): void => {
|
||||
it('fails when no items are available', (): void => {
|
||||
expect(
|
||||
() => nMax()
|
||||
).toThrow(/Must provide one or more bigint arguments/);
|
||||
).toThrow(/Must provide one or more arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,6 +19,6 @@ describe('nMin', (): void => {
|
||||
it('fails when no items are available', (): void => {
|
||||
expect(
|
||||
() => nMin()
|
||||
).toThrow(/Must provide one or more bigint arguments/);
|
||||
).toThrow(/Must provide one or more arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { createCmp } from './helpers';
|
||||
|
||||
/** @internal */
|
||||
function gt (a: bigint, b: bigint): boolean {
|
||||
@@ -13,23 +13,6 @@ function lt (a: bigint, b: bigint): boolean {
|
||||
return a < b;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function createCmp (cmp: (a: bigint, b: bigint) => boolean): (...items: bigint[]) => bigint {
|
||||
return (...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 (cmp(items[i], result)) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
|
||||
@@ -25,6 +25,6 @@ describe('bnMax', (): void => {
|
||||
it('fails when no items are available', (): void => {
|
||||
expect(
|
||||
() => bnMax()
|
||||
).toThrow(/Must provide one or more BN arguments/);
|
||||
).toThrow(/Must provide one or more arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,4 +15,10 @@ describe('bnMin', (): void => {
|
||||
bnMin(new BN(1))
|
||||
).toEqual(new BN(1));
|
||||
});
|
||||
|
||||
it('fails when no items are available', (): void => {
|
||||
expect(
|
||||
() => bnMin()
|
||||
).toThrow(/Must provide one or more arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { createCmp } from '../bi/helpers';
|
||||
import { BN } from './bn';
|
||||
|
||||
/** @internal */
|
||||
function createCmp (cmp: (a: BN, b: BN) => BN): (...items: BN[]) => BN {
|
||||
return (...items: BN[]): BN => {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
function gt (a: BN, b: BN): boolean {
|
||||
return a.gt(b);
|
||||
}
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = cmp(result, items[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
/** @internal */
|
||||
function lt (a: BN, b: BN): boolean {
|
||||
return a.lt(b);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +27,7 @@ function createCmp (cmp: (a: BN, b: BN) => BN): (...items: BN[]) => BN {
|
||||
* bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
|
||||
* ```
|
||||
*/
|
||||
export const bnMax = createCmp(BN.max);
|
||||
export const bnMax = createCmp(gt);
|
||||
|
||||
/**
|
||||
* @name bnMin
|
||||
@@ -47,4 +42,4 @@ export const bnMax = createCmp(BN.max);
|
||||
* bnMin([new BN(1), new BN(3), new BN(2)]).toString(); // => '1'
|
||||
* ```
|
||||
*/
|
||||
export const bnMin = createCmp(BN.min);
|
||||
export const bnMin = createCmp(lt);
|
||||
|
||||
Reference in New Issue
Block a user