Adjust base library usage of for vs reduce/map (#1151)

* Adjust base library usage of `for` vs `reduce/map`

* Additional

* Adjust
This commit is contained in:
Jaco
2021-10-31 08:27:14 +02:00
committed by GitHub
parent 7ef169585f
commit a4cefa4bfc
20 changed files with 130 additions and 87 deletions
+10 -6
View File
@@ -20,15 +20,19 @@
*/
export function arrayFlatten <T> (arrays: T[][]): T[] {
// pre-allocate based on the combined size
const output = new Array<T>(arrays.reduce((length, array) => length + array.length, 0));
let size = 0;
for (const a of arrays) {
size += a.length;
}
const output = new Array<T>(size);
let index = -1;
for (let a = 0; a < arrays.length; a++) {
const array = arrays[a];
for (const a of arrays) {
// instead of pushing, we just set the entries
for (let e = 0; e < array.length; e++) {
output[++index] = array[e];
for (let e = 0; e < a.length; e++) {
output[++index] = a[e];
}
}
+7 -1
View File
@@ -21,5 +21,11 @@ import { assert } from '../assert';
export function arrayRange (size: number, startAt = 0): number[] {
assert(size > 0, 'Expected non-zero, positive number as a range size');
return new Array(size).fill(0).map((_, i) => i + startAt);
const result = new Array<number>(size).fill(0);
for (let i = 0; i < result.length; i++) {
result[i] = i + startAt;
}
return result;
}
+6
View File
@@ -9,4 +9,10 @@ describe('bnMax', (): void => {
bnMax(new BN(1), new BN(2), new BN(3))
).toEqual(new BN(3));
});
it('finds BN maximum', (): void => {
expect(
bnMax(new BN(2), new BN(3), new BN(1))
).toEqual(new BN(3));
});
});
+1 -1
View File
@@ -6,7 +6,7 @@ import { BN, bnMin } from '.';
describe('bnMin', (): void => {
it('finds BN minimum', (): void => {
expect(
bnMin(new BN(1), new BN(2), new BN(3))
bnMin(new BN(2), new BN(1), new BN(3))
).toEqual(new BN(1));
});
});
+7 -1
View File
@@ -7,5 +7,11 @@ import { BN } from './bn';
export function checkMaxMin (type: 'max' | 'min', items: BN[]): BN {
assert(items.length >= 1, 'Must provide one or more BN arguments');
return items.reduce<BN>((a, v) => BN[type](a, v), items[0]);
let result = items[0];
for (const v of items) {
result = BN[type](result, v);
}
return result;
}
+6 -4
View File
@@ -24,11 +24,13 @@ const logTo = {
function formatOther (value: unknown): unknown {
if (value && isObject(value) && value.constructor === Object) {
return Object.keys(value).reduce((result: Record<string, unknown>, key): Record<string, unknown> => {
result[key] = loggerFormat(value[key]);
const result: Record<string, unknown> = {};
return result;
}, {});
for (const k of Object.keys(value)) {
result[k] = loggerFormat(value[k]);
}
return result;
}
return value;
+7 -1
View File
@@ -44,7 +44,13 @@ function getEntry (name: string): VersionPath[] {
}
function getVersionLength (all: { version: string }[]): number {
return all.reduce((max, { version }) => Math.max(max, version.length), 0);
let length = 0;
for (const { version } of all) {
length = Math.max(length, version.length);
}
return length;
}
/** @internal */