Sprinkle in readonly on array inputs (#1568)

This commit is contained in:
Jaco
2022-06-28 10:52:57 +03:00
committed by GitHub
parent 691a8e9b18
commit 3bc62adf6f
11 changed files with 35 additions and 19 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
* arrayFilter([0, void 0, true, null, false, ''], false); // [0, true, false, '']
* ```
*/
export function arrayFilter <T = unknown> (array: (T | null | undefined)[], allowNulls = true): T[] {
export function arrayFilter <T = unknown> (array: readonly (T | null | undefined)[], allowNulls = true): T[] {
return array.filter((v): v is T =>
v !== undefined &&
(allowNulls || v !== null)
+1 -1
View File
@@ -18,7 +18,7 @@
* arrayFlatten([[1, 2], [3, 4], [5]]); // [1, 2, 3, 4, 5]
* ```
*/
export function arrayFlatten <T> (arrays: T[][]): T[] {
export function arrayFlatten <T> (arrays: readonly T[][]): T[] {
// noop for the single-entry case
if (arrays.length === 1) {
return arrays[0];
+1 -1
View File
@@ -5,7 +5,7 @@
* @name arrayZip
* @description Combines 2 distinct key/value arrays into a single [K, V] array
*/
export function arrayZip <K, V> (keys: K[], values: V[]): [K, V][] {
export function arrayZip <K, V> (keys: readonly K[], values: readonly V[]): [K, V][] {
const result = new Array<[K, V]>(keys.length);
for (let i = 0; i < keys.length; i++) {
+1 -1
View File
@@ -54,7 +54,7 @@ export function lazyMethod <T, K> (result: Record<string, T> | AnyFn, item: K, c
* @description
* Creates lazy, on-demand getters for the specific values.
*/
export function lazyMethods <T, K> (result: Record<string, T>, items: K[], creator: (v: K) => T, getName?: (m: K) => string): Record<string, T> {
export function lazyMethods <T, K> (result: Record<string, T>, items: readonly K[], creator: (v: K) => T, getName?: (m: K) => string): Record<string, T> {
for (let i = 0; i < items.length; i++) {
lazyMethod(result, items[i], creator, getName);
}
+1 -1
View File
@@ -107,7 +107,7 @@ function isDebugOff (e: string, type: string): boolean {
);
}
function getDebugFlag (env: string[], type: string): boolean {
function getDebugFlag (env: readonly string[], type: string): boolean {
let flag = false;
for (const e of env) {
+1 -1
View File
@@ -6,7 +6,7 @@ import type { AnyString } from '../types';
import { CC_TO_LO, CC_TO_UP } from './camelCase';
/** @internal */
function converter (map: string[]): (value?: AnyString | null) => string {
function converter (map: readonly string[]): (value?: AnyString | null) => string {
return (value?: AnyString | null): string =>
value
? map[value.charCodeAt(0)] + value.slice(1)
+3 -3
View File
@@ -9,7 +9,7 @@ type ExecFn = (...params: any[]) => unknown;
const NUM_PAD = 16;
const PRE_PAD = 32;
function loop (count: number, inputs: unknown[][], exec: ExecFn): [number, unknown[]] {
function loop (count: number, inputs: readonly unknown[][], exec: ExecFn): [number, unknown[]] {
const start = performance.now();
const results = new Array<unknown>(inputs.length);
@@ -39,7 +39,7 @@ ${formatFixed(ops).padStart(NUM_PAD + PRE_PAD + 1)} ops/s
${formatFixed(micro).padStart(NUM_PAD + PRE_PAD + 1)} μs/op`;
}
export function perf (name: string, count: number, inputs: unknown[][], exec: ExecFn): void {
export function perf (name: string, count: number, inputs: readonly unknown[][], exec: ExecFn): void {
if (process.env.GITHUB_REPOSITORY) {
return;
}
@@ -55,7 +55,7 @@ ${`${name}:`.padStart(PRE_PAD)} ${time.toFixed(2).padStart(NUM_PAD)} ms${formatO
});
}
export function perfCmp (name: string, [first, second]: [string, string], count: number, inputs: unknown[][], exec: ExecFn): void {
export function perfCmp (name: string, [first, second]: [string, string], count: number, inputs: readonly unknown[][], exec: ExecFn): void {
if (process.env.GITHUB_REPOSITORY) {
return;
}
+2 -2
View File
@@ -22,7 +22,7 @@ import { u8aToU8a } from './toU8a';
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export function u8aConcat (...list: U8aLike[]): Uint8Array {
export function u8aConcat (...list: readonly U8aLike[]): Uint8Array {
const u8as = new Array<Uint8Array>(list.length);
let length = 0;
@@ -38,7 +38,7 @@ export function u8aConcat (...list: U8aLike[]): Uint8Array {
* @name u8aConcatStrict
* @description A strict version of [[u8aConcat]], accepting only Uint8Array inputs
*/
export function u8aConcatStrict (u8as: Uint8Array[], length = 0): Uint8Array {
export function u8aConcatStrict (u8as: readonly Uint8Array[], length = 0): Uint8Array {
let offset = 0;
if (!length) {
+1 -1
View File
@@ -22,7 +22,7 @@ import { u8aToU8a } from './toU8a';
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export function u8aConcat (...list: U8aLike[]): Uint8Array {
export function u8aConcat (...list: readonly U8aLike[]): Uint8Array {
const u8as = new Array<Uint8Array>(list.length);
for (let i = 0; i < list.length; i++) {
+18 -3
View File
@@ -4,8 +4,6 @@
import { perf } from '../test/performance';
import { u8aEmpty } from '.';
const ztest = new Uint8Array(32);
describe('u8aEmpty', (): void => {
it('returns true on zero length', (): void => {
expect(
@@ -25,5 +23,22 @@ describe('u8aEmpty', (): void => {
).toEqual(false);
});
perf('u8aEmpty (32 cmp)', 1000000, [[ztest]], u8aEmpty);
it('returns false when value is found (256)', (): void => {
const test = new Uint8Array(256);
expect(
u8aEmpty(test)
).toEqual(true);
test[128] = 1;
expect(
u8aEmpty(test)
).toEqual(false);
});
perf('u8aEmpty (32 cmp)', 1_000_000, [[new Uint8Array(32)]], u8aEmpty);
perf('u8aEmpty (64 cmp)', 500_000, [[new Uint8Array(64)]], u8aEmpty);
perf('u8aEmpty (128 cmp)', 250_000, [[new Uint8Array(128)]], u8aEmpty);
perf('u8aEmpty (256 cmp)', 125_000, [[new Uint8Array(256)]], u8aEmpty);
});
+5 -4
View File
@@ -8,10 +8,11 @@
* Checks to see if the input `Uint8Array` has zero length or contains all 0 values.
*/
export function u8aEmpty (value: Uint8Array): boolean {
// on smaller values < 64 bytes, the byte-by-byte compare is faster than
// allocating yet another object for DataView (on large buffers the DataView
// is much faster)
for (let i = 0; i < value.length; i++) {
const len = value.length;
// on smaller sizes, the byte-by-byte compare is faster than allocating
// another object for DataView (on very large arrays the DataView is faster)
for (let i = 0; i < len; i++) {
if (value[i]) {
return false;
}