Flatten calls (#1200)

This commit is contained in:
Jaco
2021-11-18 08:48:15 +02:00
committed by GitHub
parent 93fb633ea6
commit 6f0aa35098
5 changed files with 69 additions and 72 deletions
+26 -28
View File
@@ -5,33 +5,6 @@ import type { HexString } from '../types';
import { u8aToU8a } from './toU8a';
function compare (a: Uint8Array, b: Uint8Array): number {
let i = 0;
while (true) {
const overA = i >= a.length;
const overB = i >= b.length;
if (overA && overB) {
// both ends reached
return 0;
} else if (overA) {
// a has no more data, b has data
return -1;
} else if (overB) {
// b has no more data, a has data
return 1;
} else if (a[i] !== b[i]) {
// the number in this index doesn't match
return a[i] > b[i]
? 1
: -1;
}
i++;
}
}
/**
* @name u8aCmp
* @summary Compares two Uint8Arrays for sorting.
@@ -49,5 +22,30 @@ function compare (a: Uint8Array, b: Uint8Array): number {
* ```
*/
export function u8aCmp (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): number {
return compare(u8aToU8a(a), u8aToU8a(b));
const u8aa = u8aToU8a(a);
const u8ab = u8aToU8a(b);
let i = 0;
while (true) {
const overA = i >= u8aa.length;
const overB = i >= u8ab.length;
if (overA && overB) {
// both ends reached
return 0;
} else if (overA) {
// a has no more data, b has data
return -1;
} else if (overB) {
// b has no more data, a has data
return 1;
} else if (u8aa[i] !== u8ab[i]) {
// the number in this index doesn't match
return u8aa[i] > u8ab[i]
? 1
: -1;
}
i++;
}
}