Socket
Socket
Sign inDemoInstall

@polkadot/util

Package Overview
Dependencies
Maintainers
2
Versions
1408
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polkadot/util - npm Package Compare versions

Comparing version 10.0.2 to 10.1.1

array/unzip.d.ts

1

array/index.d.ts

@@ -9,2 +9,3 @@ /**

export { arrayShuffle } from './shuffle';
export { arrayUnzip } from './unzip';
export { arrayZip } from './zip';

@@ -12,2 +12,3 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

export { arrayShuffle } from "./shuffle.js";
export { arrayUnzip } from "./unzip.js";
export { arrayZip } from "./zip.js";

2

array/shuffle.d.ts

@@ -5,2 +5,2 @@ /**

*/
export declare function arrayShuffle<T>(input: T[]): T[];
export declare function arrayShuffle<T>(input: readonly T[]): T[];

@@ -36,2 +36,8 @@ "use strict";

});
Object.defineProperty(exports, "arrayUnzip", {
enumerable: true,
get: function () {
return _unzip.arrayUnzip;
}
});
Object.defineProperty(exports, "arrayZip", {

@@ -54,2 +60,4 @@ enumerable: true,

var _unzip = require("./unzip");
var _zip = require("./zip");

@@ -7,17 +7,23 @@ "use strict";

exports.hexToU8a = hexToU8a;
var _hex = require("../is/hex");
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
const CHARS = '0123456789abcdef';
const UNHEX = new Array(256);
const CHR = '0123456789abcdef';
const U8 = new Array(256);
const U16 = new Array(256 * 256);
for (let i = 0; i < CHARS.length; i++) {
UNHEX[CHARS[i].charCodeAt(0)] = i;
for (let i = 0; i < CHR.length; i++) {
U8[CHR[i].charCodeAt(0) | 0] = i | 0;
if (i > 9) {
UNHEX[CHARS[i].toUpperCase().charCodeAt(0)] = i;
U8[CHR[i].toUpperCase().charCodeAt(0) | 0] = i | 0;
}
}
for (let i = 0; i < 256; i++) {
const s = i << 8;
for (let j = 0; j < 256; j++) {
U16[s | j] = U8[i] << 4 | U8[j];
}
}
/**

@@ -43,20 +49,11 @@ * @name hexToU8a

if (!value || value === '0x') {
if (!value) {
return new Uint8Array();
}
let s = 0; // we don't use hexStringPrefix here - that has substring which adds
// additional overhead. Instead we duplicate the logic, just incrementing
// the sactual string pointer, ignoring the prefix as required
if (_hex.REGEX_HEX_PREFIXED.test(value)) {
s = 2;
} else if (!_hex.REGEX_HEX_NOPREFIX.test(value)) {
throw new Error(`Expected hex value to convert, found '${value}'`);
}
const strLength = (value.length - s) / 2;
const endLength = Math.ceil(bitLength === -1 ? strLength : bitLength / 8);
let s = value.startsWith('0x') ? 2 : 0;
const decLength = Math.ceil((value.length - s) / 2);
const endLength = Math.ceil(bitLength === -1 ? decLength : bitLength / 8);
const result = new Uint8Array(endLength);
const offset = endLength > strLength ? endLength - strLength : 0;
const offset = endLength > decLength ? endLength - decLength : 0;

@@ -68,3 +65,3 @@ for (let i = offset; i < endLength; i++, s += 2) {

// also the faster operation by at least 2x with the character map above
result[i] = (UNHEX[value.charCodeAt(s)] << 4) + UNHEX[value.charCodeAt(s + 1)];
result[i] = U16[value.charCodeAt(s) << 8 | value.charCodeAt(s + 1)];
}

@@ -71,0 +68,0 @@

@@ -8,4 +8,2 @@ "use strict";

var _stripPrefix = require("./stripPrefix");
// Copyright 2017-2022 @polkadot/util authors & contributors

@@ -36,3 +34,3 @@ // SPDX-License-Identifier: Apache-2.0

const value = (0, _stripPrefix.hexStripPrefix)(_value);
const value = _value.startsWith('0x') ? _value.substring(2) : _value;
const buf = Buffer.from(value, 'hex');

@@ -39,0 +37,0 @@ const valLength = value.length / 2;

@@ -19,3 +19,3 @@ "use strict";

function isAsciiStr(str) {
const count = str.length;
const count = str.length | 0;

@@ -25,3 +25,3 @@ for (let i = 0; i < count; i++) {

if (!(b < 127 && (b >= 32 || b === 10 || b === 9 || b === 13))) {
if (b < 32 || b > 126) {
return false;

@@ -37,8 +37,8 @@ }

function isAsciiBytes(u8a) {
const count = u8a.length;
const count = u8a.length | 0;
for (let i = 0; i < count; i++) {
const b = u8a[i]; // check is inlined here, it is faster than making a call
const b = u8a[i] | 0; // check is inlined here, it is faster than making a call
if (!(b < 127 && (b >= 32 || b === 10 || b === 9 || b === 13))) {
if (b < 32 || b > 126) {
return false;

@@ -45,0 +45,0 @@ }

@@ -18,3 +18,4 @@ "use strict";

function lazyMethod(result, item, creator, getName) {
const name = getName ? getName(item) : item.toString();
let index = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
const name = getName ? getName(item, index) : item.toString();
let value;

@@ -34,3 +35,3 @@ Object.defineProperty(result, name, {

if (value === undefined) {
value = creator(item);
value = creator(item, index, this);

@@ -63,3 +64,3 @@ try {

for (let i = 0; i < items.length; i++) {
lazyMethod(result, items[i], creator, getName);
lazyMethod(result, items[i], creator, getName, i);
}

@@ -66,0 +67,0 @@

@@ -16,4 +16,5 @@ "use strict";

*/
function objectProperty(that, key, getter) {
// There are 3 approaches here -
function objectProperty(that, key, getter, getName) {
let index = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
const name = getName ? getName(key, index) : key; // There are 3 approaches here -
// - Object.prototype.hasOwnProperty.call(that, key) - this only checks the current class, i.e

@@ -24,8 +25,11 @@ // will retuirn false if the property is set in the parent class

// - key in that - Does not need to be combined with either of the above and checks the full chain
if (!(key in that)) {
Object.defineProperty(that, key, {
if (!(name in that)) {
Object.defineProperty(that, name, {
enumerable: true,
// Unlike in lazy, we always call into the upper function, i.e. this method
// does not cache old values (it is expected to be used for dynamic values)
get: () => getter(key)
get: function () {
return getter(key, index, this);
}
});

@@ -40,6 +44,6 @@ }

function objectProperties(that, keys, getter) {
function objectProperties(that, keys, getter, getName) {
for (let i = 0; i < keys.length; i++) {
objectProperty(that, keys[i], k => getter(k, i));
objectProperty(that, keys[i], getter, getName, i);
}
}

@@ -20,3 +20,9 @@ "use strict";

if (src) {
Object.assign(dest, src);
if (typeof src.entries === 'function') {
for (const [key, value] of src.entries()) {
dest[key] = value;
}
} else {
Object.assign(dest, src);
}
}

@@ -23,0 +29,0 @@ }

{
"type": "commonjs"
}
}

@@ -14,4 +14,4 @@ "use strict";

type: 'cjs',
version: '10.0.2'
version: '10.1.1'
};
exports.packageInfo = packageInfo;

@@ -18,7 +18,7 @@ "use strict";

function u8aEmpty(value) {
const len = value.length; // on smaller sizes, the byte-by-byte compare is faster than allocating
const len = value.length | 0; // on smaller sizes, the byte-by-byte compare is faster than allocating
// another object for DataView (on very large arrays the DataView is faster)
for (let i = 0; i < len; i++) {
if (value[i]) {
if (value[i] | 0) {
return false;

@@ -25,0 +25,0 @@ }

@@ -34,4 +34,4 @@ "use strict";

const dvB = new DataView(u8ab.buffer, u8ab.byteOffset);
const mod = u8aa.length % 4;
const length = u8aa.length - mod;
const mod = u8aa.length % 4 | 0;
const length = u8aa.length - mod | 0;

@@ -38,0 +38,0 @@ for (let i = 0; i < length; i += 4) {

@@ -26,16 +26,12 @@ "use strict";

function hex(value) {
const mod = value.length % 2;
const length = value.length - mod;
const dv = new DataView(value.buffer, value.byteOffset);
let result = '';
function hex(value, result) {
const mod = value.length % 2 | 0;
const length = value.length - mod | 0;
for (let i = 0; i < length; i += 2) {
// we only use getUint16 here instead of getUint32 - at least in our
// tests this is faster to execute (both long & short strings tested)
result += U16[dv.getUint16(i)];
result += U16[value[i] << 8 | value[i + 1]];
}
if (mod) {
result += U8[dv.getUint8(length)];
result += U8[value[length] | 0];
}

@@ -64,4 +60,16 @@

let isPrefixed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
const length = Math.ceil(bitLength / 8);
return `${isPrefixed ? '0x' : ''}${!value || !value.length ? '' : bitLength > 0 && value.length > length ? `${hex(value.subarray(0, length / 2))}…${hex(value.subarray(value.length - length / 2))}` : hex(value)}`;
// this is not 100% correct sinmce we support isPrefixed = false....
const empty = isPrefixed ? '0x' : '';
if (!value || !value.length) {
return empty;
} else if (bitLength > 0) {
const length = Math.ceil(bitLength / 8);
if (value.length > length) {
return `${hex(value.subarray(0, length / 2), empty)}…${hex(value.subarray(value.length - length / 2), '')}`;
}
}
return hex(value, empty);
}
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { REGEX_HEX_NOPREFIX, REGEX_HEX_PREFIXED } from "../is/hex.js";
const CHARS = '0123456789abcdef';
const UNHEX = new Array(256);
const CHR = '0123456789abcdef';
const U8 = new Array(256);
const U16 = new Array(256 * 256);
for (let i = 0; i < CHARS.length; i++) {
UNHEX[CHARS[i].charCodeAt(0)] = i;
for (let i = 0; i < CHR.length; i++) {
U8[CHR[i].charCodeAt(0) | 0] = i | 0;
if (i > 9) {
UNHEX[CHARS[i].toUpperCase().charCodeAt(0)] = i;
U8[CHR[i].toUpperCase().charCodeAt(0) | 0] = i | 0;
}
}
for (let i = 0; i < 256; i++) {
const s = i << 8;
for (let j = 0; j < 256; j++) {
U16[s | j] = U8[i] << 4 | U8[j];
}
}
/**

@@ -32,20 +40,11 @@ * @name hexToU8a

export function hexToU8a(value, bitLength = -1) {
if (!value || value === '0x') {
if (!value) {
return new Uint8Array();
}
let s = 0; // we don't use hexStringPrefix here - that has substring which adds
// additional overhead. Instead we duplicate the logic, just incrementing
// the sactual string pointer, ignoring the prefix as required
if (REGEX_HEX_PREFIXED.test(value)) {
s = 2;
} else if (!REGEX_HEX_NOPREFIX.test(value)) {
throw new Error(`Expected hex value to convert, found '${value}'`);
}
const strLength = (value.length - s) / 2;
const endLength = Math.ceil(bitLength === -1 ? strLength : bitLength / 8);
let s = value.startsWith('0x') ? 2 : 0;
const decLength = Math.ceil((value.length - s) / 2);
const endLength = Math.ceil(bitLength === -1 ? decLength : bitLength / 8);
const result = new Uint8Array(endLength);
const offset = endLength > strLength ? endLength - strLength : 0;
const offset = endLength > decLength ? endLength - decLength : 0;

@@ -57,3 +56,3 @@ for (let i = offset; i < endLength; i++, s += 2) {

// also the faster operation by at least 2x with the character map above
result[i] = (UNHEX[value.charCodeAt(s)] << 4) + UNHEX[value.charCodeAt(s + 1)];
result[i] = U16[value.charCodeAt(s) << 8 | value.charCodeAt(s + 1)];
}

@@ -60,0 +59,0 @@

// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { hexStripPrefix } from "./stripPrefix.js";
/**

@@ -19,3 +19,2 @@ * @name hexToU8a

*/
export function hexToU8a(_value, bitLength = -1) {

@@ -26,3 +25,3 @@ if (!_value) {

const value = hexStripPrefix(_value);
const value = _value.startsWith('0x') ? _value.substring(2) : _value;
const buf = Buffer.from(value, 'hex');

@@ -29,0 +28,0 @@ const valLength = value.length / 2;

@@ -9,3 +9,3 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

function isAsciiStr(str) {
const count = str.length;
const count = str.length | 0;

@@ -15,3 +15,3 @@ for (let i = 0; i < count; i++) {

if (!(b < 127 && (b >= 32 || b === 10 || b === 9 || b === 13))) {
if (b < 32 || b > 126) {
return false;

@@ -27,8 +27,8 @@ }

function isAsciiBytes(u8a) {
const count = u8a.length;
const count = u8a.length | 0;
for (let i = 0; i < count; i++) {
const b = u8a[i]; // check is inlined here, it is faster than making a call
const b = u8a[i] | 0; // check is inlined here, it is faster than making a call
if (!(b < 127 && (b >= 32 || b === 10 || b === 9 || b === 13))) {
if (b < 32 || b > 126) {
return false;

@@ -35,0 +35,0 @@ }

@@ -7,3 +7,3 @@ declare type AnyFn = (...args: unknown[]) => unknown;

*/
export declare function lazyMethod<T, K>(result: Record<string, T> | AnyFn, item: K, creator: (d: K) => T, getName?: (d: K) => string): void;
export declare function lazyMethod<T, K, S>(result: Record<string, T> | AnyFn, item: K, creator: (item: K, index: number, self: S) => T, getName?: (item: K, index: number) => string, index?: number): void;
/**

@@ -14,3 +14,3 @@ * @name lazyMethods

*/
export declare function lazyMethods<T, K>(result: Record<string, T>, items: readonly K[], creator: (v: K) => T, getName?: (m: K) => string): Record<string, T>;
export declare function lazyMethods<T, K, S>(result: Record<string, T>, items: readonly K[], creator: (item: K, index: number, self: S) => T, getName?: (item: K, index: number) => string): Record<string, T>;
export {};

@@ -9,4 +9,4 @@ // Copyright 2017-2022 @polkadot/types authors & contributors

*/
export function lazyMethod(result, item, creator, getName) {
const name = getName ? getName(item) : item.toString();
export function lazyMethod(result, item, creator, getName, index = 0) {
const name = getName ? getName(item, index) : item.toString();
let value;

@@ -26,3 +26,3 @@ Object.defineProperty(result, name, {

if (value === undefined) {
value = creator(item);
value = creator(item, index, this);

@@ -54,3 +54,3 @@ try {

for (let i = 0; i < items.length; i++) {
lazyMethod(result, items[i], creator, getName);
lazyMethod(result, items[i], creator, getName, i);
}

@@ -57,0 +57,0 @@

@@ -5,3 +5,3 @@ /**

*/
export declare function objectProperty(that: object, key: string, getter: (k: string) => unknown): void;
export declare function objectProperty<S>(that: object, key: string, getter: (key: string, index: number, self: S) => unknown, getName?: (key: string, index: number) => string, index?: number): void;
/**

@@ -11,2 +11,2 @@ * @name objectProperties

*/
export declare function objectProperties(that: object, keys: string[], getter: (k: string, i: number) => unknown): void;
export declare function objectProperties<S>(that: object, keys: string[], getter: (key: string, index: number, self: S) => unknown, getName?: (key: string, index: number) => string): void;

@@ -8,4 +8,4 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

*/
export function objectProperty(that, key, getter) {
// There are 3 approaches here -
export function objectProperty(that, key, getter, getName, index = 0) {
const name = getName ? getName(key, index) : key; // There are 3 approaches here -
// - Object.prototype.hasOwnProperty.call(that, key) - this only checks the current class, i.e

@@ -16,8 +16,11 @@ // will retuirn false if the property is set in the parent class

// - key in that - Does not need to be combined with either of the above and checks the full chain
if (!(key in that)) {
Object.defineProperty(that, key, {
if (!(name in that)) {
Object.defineProperty(that, name, {
enumerable: true,
// Unlike in lazy, we always call into the upper function, i.e. this method
// does not cache old values (it is expected to be used for dynamic values)
get: () => getter(key)
get: function () {
return getter(key, index, this);
}
});

@@ -31,6 +34,6 @@ }

export function objectProperties(that, keys, getter) {
export function objectProperties(that, keys, getter, getName) {
for (let i = 0; i < keys.length; i++) {
objectProperty(that, keys[i], k => getter(k, i));
objectProperty(that, keys[i], getter, getName, i);
}
}

@@ -13,3 +13,9 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

if (src) {
Object.assign(dest, src);
if (typeof src.entries === 'function') {
for (const [key, value] of src.entries()) {
dest[key] = value;
}
} else {
Object.assign(dest, src);
}
}

@@ -16,0 +22,0 @@ }

@@ -23,3 +23,3 @@ {

"type": "module",
"version": "10.0.2",
"version": "10.1.1",
"main": "./cjs/index.js",

@@ -66,2 +66,7 @@ "module": "./index.js",

},
"./array/unzip": {
"types": "./array/unzip.d.ts",
"require": "./cjs/array/unzip.js",
"default": "./array/unzip.js"
},
"./array/zip": {

@@ -718,10 +723,10 @@ "types": "./array/zip.d.ts",

"dependencies": {
"@babel/runtime": "^7.18.6",
"@polkadot/x-bigint": "10.0.2",
"@polkadot/x-global": "10.0.2",
"@polkadot/x-textdecoder": "10.0.2",
"@polkadot/x-textencoder": "10.0.2",
"@babel/runtime": "^7.18.9",
"@polkadot/x-bigint": "10.1.1",
"@polkadot/x-global": "10.1.1",
"@polkadot/x-textdecoder": "10.1.1",
"@polkadot/x-textencoder": "10.1.1",
"@types/bn.js": "^5.1.0",
"bn.js": "^5.2.1"
}
}
}

@@ -8,3 +8,3 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

type: 'esm',
version: '10.0.2'
version: '10.1.1'
};

@@ -11,7 +11,7 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

export function u8aEmpty(value) {
const len = value.length; // on smaller sizes, the byte-by-byte compare is faster than allocating
const len = value.length | 0; // on smaller sizes, the byte-by-byte compare is faster than allocating
// another object for DataView (on very large arrays the DataView is faster)
for (let i = 0; i < len; i++) {
if (value[i]) {
if (value[i] | 0) {
return false;

@@ -18,0 +18,0 @@ }

@@ -26,4 +26,4 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

const dvB = new DataView(u8ab.buffer, u8ab.byteOffset);
const mod = u8aa.length % 4;
const length = u8aa.length - mod;
const mod = u8aa.length % 4 | 0;
const length = u8aa.length - mod | 0;

@@ -30,0 +30,0 @@ for (let i = 0; i < length; i += 4) {

@@ -20,16 +20,12 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

function hex(value) {
const mod = value.length % 2;
const length = value.length - mod;
const dv = new DataView(value.buffer, value.byteOffset);
let result = '';
function hex(value, result) {
const mod = value.length % 2 | 0;
const length = value.length - mod | 0;
for (let i = 0; i < length; i += 2) {
// we only use getUint16 here instead of getUint32 - at least in our
// tests this is faster to execute (both long & short strings tested)
result += U16[dv.getUint16(i)];
result += U16[value[i] << 8 | value[i + 1]];
}
if (mod) {
result += U8[dv.getUint8(length)];
result += U8[value[length] | 0];
}

@@ -56,4 +52,16 @@

export function u8aToHex(value, bitLength = -1, isPrefixed = true) {
const length = Math.ceil(bitLength / 8);
return `${isPrefixed ? '0x' : ''}${!value || !value.length ? '' : bitLength > 0 && value.length > length ? `${hex(value.subarray(0, length / 2))}…${hex(value.subarray(value.length - length / 2))}` : hex(value)}`;
// this is not 100% correct sinmce we support isPrefixed = false....
const empty = isPrefixed ? '0x' : '';
if (!value || !value.length) {
return empty;
} else if (bitLength > 0) {
const length = Math.ceil(bitLength / 8);
if (value.length > length) {
return `${hex(value.subarray(0, length / 2), empty)}…${hex(value.subarray(value.length - length / 2), '')}`;
}
}
return hex(value, empty);
}

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc