UNPKG

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