UNPKG

1.07 kBJavaScriptView Raw
1import { u8aToU8a } from './toU8a.js';
2/**
3 * @name u8aEq
4 * @summary Compares two Uint8Arrays for equality.
5 * @description
6 * For `UInt8Array` (or hex string) input values true if there is a match.
7 * @example
8 * <BR>
9 *
10 * ```javascript
11 * import { u8aEq } from '@polkadot/util';
12 *
13 * u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
14 * ```
15 */
16export function u8aEq(a, b) {
17 const u8aa = u8aToU8a(a);
18 const u8ab = u8aToU8a(b);
19 if (u8aa.length === u8ab.length) {
20 const dvA = new DataView(u8aa.buffer, u8aa.byteOffset);
21 const dvB = new DataView(u8ab.buffer, u8ab.byteOffset);
22 const mod = (u8aa.length % 4) | 0;
23 const length = (u8aa.length - mod) | 0;
24 for (let i = 0; i < length; i += 4) {
25 if (dvA.getUint32(i) !== dvB.getUint32(i)) {
26 return false;
27 }
28 }
29 for (let i = length; i < u8aa.length; i++) {
30 if (u8aa[i] !== u8ab[i]) {
31 return false;
32 }
33 }
34 return true;
35 }
36 return false;
37}
\No newline at end of file