{address,u8a}Eq functions (#606)
* {addres,u8a}Eq functions
* Remove mnemonicToSeed
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { u8aEq } from '.';
|
||||
|
||||
describe('u8aEq', (): void => {
|
||||
it('returns false when the lengths do not match', (): void => {
|
||||
expect(
|
||||
u8aEq(
|
||||
new Uint8Array([1, 2, 3]),
|
||||
new Uint8Array([1, 2])
|
||||
)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when the contents do not match', (): void => {
|
||||
expect(
|
||||
u8aEq(
|
||||
new Uint8Array([1, 2, 3]),
|
||||
new Uint8Array([1, 2, 4])
|
||||
)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true when the contents do match', (): void => {
|
||||
expect(
|
||||
u8aEq(
|
||||
new Uint8Array([1, 2, 3]),
|
||||
new Uint8Array([1, 2, 3])
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true with equalvalent hex & Uint8array', (): void => {
|
||||
expect(
|
||||
u8aEq(
|
||||
'0x010203',
|
||||
new Uint8Array([1, 2, 3])
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aEq
|
||||
* @summary Compares two Uint8Arrays.
|
||||
* @description
|
||||
* For `UInt8Array` (or hex string) input values true if there is a match.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { u8aEq } from '@polkadot/util';
|
||||
*
|
||||
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
|
||||
* ```
|
||||
*/
|
||||
export default function u8aEq (a: Uint8Array | string, b: Uint8Array | string): boolean {
|
||||
return equals(u8aToU8a(a), u8aToU8a(b));
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
export { default as u8aConcat } from './concat';
|
||||
export { default as u8aEq } from './eq';
|
||||
export { default as u8aFixLength } from './fixLength';
|
||||
export { default as u8aSorted } from './sorted';
|
||||
export { default as u8aToBn } from './toBn';
|
||||
|
||||
@@ -2,7 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
const ALPHABET = '0123456789abcdef';
|
||||
const ALPHABET = new Array(256) as string[];
|
||||
|
||||
for (let n = 0; n < 256; ++n) {
|
||||
ALPHABET[n] = n.toString(16).padStart(2, '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aToHex
|
||||
@@ -35,10 +39,11 @@ export default function u8aToHex (value?: Uint8Array | null, bitLength = -1, isP
|
||||
return `${u8aToHex(value.subarray(0, halfLength), -1, isPrefixed)}…${u8aToHex(value.subarray(value.length - halfLength), -1, false)}`;
|
||||
}
|
||||
|
||||
// based on comments in https://stackoverflow.com/questions/40031688/javascript-arraybuffer-to-hex and
|
||||
// implementation in http://jsben.ch/Vjx2V - optimisation here suggests that a forEach loop is faster
|
||||
// than reduce as well (clocking at in 90% of the reduce speed with tweaking in the playpen above)
|
||||
return value.reduce((result, value): string => {
|
||||
return result + ALPHABET[value >> 4] + ALPHABET[value & 15];
|
||||
}, prefix);
|
||||
const result = new Array(value.length) as string[];
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
result[i] = ALPHABET[value[i]];
|
||||
}
|
||||
|
||||
return prefix + result.join('');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user