Add u8aCmp (sorting determination) (#1015)
* Add u8aCmp (sorting determination) * Update packages/util/src/u8a/cmp.ts * Update packages/util/src/u8a/cmp.ts * Update packages/util/src/u8a/cmp.ts * Use u8aCmp in u8aEq
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aCmp } from '.';
|
||||
|
||||
describe('u8aCmp', (): void => {
|
||||
it('has the same result as localCompare (equal length)', (): void => {
|
||||
const a = 'test abc';
|
||||
const b = 'test def';
|
||||
|
||||
expect(
|
||||
u8aCmp(a, b)
|
||||
).toEqual(a.localeCompare(b));
|
||||
expect(
|
||||
u8aCmp(b, a)
|
||||
).toEqual(b.localeCompare(a));
|
||||
});
|
||||
|
||||
it('has the same result as localCompare (diff length)', (): void => {
|
||||
const a = 'test';
|
||||
const b = 'test abc';
|
||||
|
||||
expect(
|
||||
u8aCmp(a, b)
|
||||
).toEqual(a.localeCompare(b));
|
||||
expect(
|
||||
u8aCmp(b, a)
|
||||
).toEqual(b.localeCompare(a));
|
||||
});
|
||||
|
||||
it('compares between string and Uint8Array', (): void => {
|
||||
expect(
|
||||
u8aCmp(
|
||||
'0x12345678',
|
||||
new Uint8Array([0x12, 0x34, 0x56, 0x78])
|
||||
)
|
||||
).toEqual(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
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.
|
||||
* @description
|
||||
* For `UInt8Array` (or hex string) input values returning -1, 0 or +1
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { u8aCmp } from '@polkadot/util';
|
||||
*
|
||||
* u8aCmp(new Uint8Array([0x67, 0x65]), new Uint8Array([0x68, 0x65])); // -1
|
||||
* u8aCmp(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // 0
|
||||
* u8aCmp(new Uint8Array([0x69, 0x65]), new Uint8Array([0x68, 0x65])); // +1
|
||||
* ```
|
||||
*/
|
||||
export function u8aCmp (a: Uint8Array | string, b: Uint8Array | string): number {
|
||||
return compare(u8aToU8a(a), u8aToU8a(b));
|
||||
}
|
||||
@@ -1,32 +1,23 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aCmp } from './cmp';
|
||||
import { u8aToU8a } from './toU8a';
|
||||
|
||||
function equals (a: Uint8Array, b: Uint8Array): boolean {
|
||||
if (a.length !== b.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (a[i] !== b[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return (a.length === b.length) && (u8aCmp(a, b) === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aEq
|
||||
* @summary Compares two Uint8Arrays.
|
||||
* @summary Compares two Uint8Arrays for equality.
|
||||
* @description
|
||||
* For `UInt8Array` (or hex string) input values true if there is a match.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { { u8aEq } from '@polkadot/util';
|
||||
* import { u8aEq } from '@polkadot/util';
|
||||
*
|
||||
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
|
||||
* ```
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
* @summary Utility methods to convert to and from `Uint8Array` objects
|
||||
*/
|
||||
|
||||
export { u8aCmp } from './cmp';
|
||||
export { u8aConcat } from './concat';
|
||||
export { u8aEq } from './eq';
|
||||
export { u8aFixLength } from './fixLength';
|
||||
|
||||
@@ -1,28 +1,22 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isUndefined } from '../is/undefined';
|
||||
import { u8aCmp } from './cmp';
|
||||
|
||||
/**
|
||||
* @name u8aSorted
|
||||
* @summary Sorts an array of Uint8Arrays
|
||||
* @description
|
||||
* For input `UInt8Array[]` return the sorted result
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { u8aSorted} from '@polkadot/util';
|
||||
*
|
||||
* u8aSorted([new Uint8Array([0x69]), new Uint8Array([0x68])]); // [0x68, 0x69]
|
||||
* ```
|
||||
*/
|
||||
export function u8aSorted (u8as: Uint8Array[]): Uint8Array[] {
|
||||
return u8as.sort((a, b): number => {
|
||||
let i = 0;
|
||||
|
||||
while (true) {
|
||||
if (isUndefined(a[i]) && isUndefined(b[i])) {
|
||||
return 0;
|
||||
} else if (isUndefined(a[i])) {
|
||||
return -1;
|
||||
} else if (isUndefined(b[i])) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const cmp = a[i] - b[i];
|
||||
|
||||
if (cmp !== 0) {
|
||||
return cmp;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
});
|
||||
return u8as.sort(u8aCmp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user