mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-25 10:37:57 +00:00
Initial rebrand: @polkadot -> @pezkuwi (14 packages)
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { objectClear } from './index.js';
|
||||
|
||||
describe('objectClear', (): void => {
|
||||
it('clears an object', (): void => {
|
||||
expect(
|
||||
objectClear({ a: 1, b: 2, c: { d: 3, e: 4 } })
|
||||
).toEqual({});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name objectClear
|
||||
* @summary Removes all the keys from the input object
|
||||
*/
|
||||
export function objectClear <T> (value: Record<string, T>): Record<string, T> {
|
||||
const keys = Object.keys(value);
|
||||
|
||||
for (let i = 0, count = keys.length; i < count; i++) {
|
||||
delete value[keys[i]];
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { objectCopy } from './index.js';
|
||||
|
||||
describe('objectCopy', (): void => {
|
||||
it('makes a shallow copy of the object', (): void => {
|
||||
const a = { a: 1, b: 2, c: { d: 3, e: 4 } };
|
||||
const b = objectCopy(a);
|
||||
|
||||
expect(a).toEqual(b);
|
||||
expect(a === b).toEqual(false);
|
||||
expect(a.c === b.c).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { objectSpread } from './spread.js';
|
||||
|
||||
/**
|
||||
* @name objectCopy
|
||||
* @summary Creates a shallow clone of the input object
|
||||
*/
|
||||
export function objectCopy <T extends object> (source: T): T {
|
||||
return objectSpread({}, source);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { objectEntries } from './index.js';
|
||||
|
||||
describe('objectEntries', (): void => {
|
||||
it('extracts all entries', (): void => {
|
||||
const o = { a: 1, b: 2, c: 3 };
|
||||
const entries = objectEntries(o);
|
||||
|
||||
expect(entries).toEqual([['a', 1], ['b', 2], ['c', 3]]);
|
||||
|
||||
for (const [k, v] of entries) {
|
||||
expect(k).toBeDefined();
|
||||
expect(v).toBeDefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
type Entries<T> = { [K in keyof T]: [K, T[K]] }[keyof T][];
|
||||
|
||||
/**
|
||||
* @name objectEntries
|
||||
* @summary A version of Object.entries that is typed for TS
|
||||
*/
|
||||
export function objectEntries<T extends object> (obj: T): Entries<T> {
|
||||
return Object.entries(obj) as Entries<T>;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { objectClear } from './clear.js';
|
||||
export { objectCopy } from './copy.js';
|
||||
export { objectEntries } from './entries.js';
|
||||
export { objectKeys } from './keys.js';
|
||||
export { objectProperties, objectProperty } from './property.js';
|
||||
export { objectSpread } from './spread.js';
|
||||
export { objectValues } from './values.js';
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { objectKeys } from './index.js';
|
||||
|
||||
describe('objectKeys', (): void => {
|
||||
it('extracts all keys', (): void => {
|
||||
const o = { a: 1, b: 2, c: 3 };
|
||||
const keys = objectKeys(o);
|
||||
|
||||
expect(keys).toEqual(['a', 'b', 'c']);
|
||||
|
||||
for (const k of keys) {
|
||||
expect(o[k]).toBeDefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name objectKeys
|
||||
* @summary A version of Object.keys that is typed for TS
|
||||
*/
|
||||
export function objectKeys<T extends object, K extends Extract<keyof T, string>> (value: T): K[] {
|
||||
return Object.keys(value) as K[];
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perf } from '../test/index.js';
|
||||
import { objectProperties, objectProperty } from './index.js';
|
||||
|
||||
describe('objectProperty/objectProperties', (): void => {
|
||||
it('sets a property', (): void => {
|
||||
const test: { a?: unknown } = {};
|
||||
|
||||
expect(test.a).toEqual(undefined);
|
||||
|
||||
objectProperty(test, 'a', () => 2);
|
||||
|
||||
expect(Object.prototype.hasOwnProperty.call(test, 'a')).toBe(true);
|
||||
expect('a' in test).toEqual(true);
|
||||
expect(test.a).toEqual(2);
|
||||
});
|
||||
|
||||
it('does not override an existing property (record)', (): void => {
|
||||
const test = { a: 1 };
|
||||
|
||||
expect(test.a).toEqual(1);
|
||||
|
||||
objectProperty(test, 'a', () => 2);
|
||||
|
||||
expect(test.a).toEqual(1);
|
||||
});
|
||||
|
||||
it('does not override an existing property (class)', (): void => {
|
||||
class Test {
|
||||
b = 1;
|
||||
|
||||
get a () {
|
||||
return 1 + 0;
|
||||
}
|
||||
}
|
||||
|
||||
const test = new Test();
|
||||
|
||||
expect(test.a).toEqual(1);
|
||||
expect(test.b).toEqual(1);
|
||||
|
||||
objectProperty(test, 'a', () => 2);
|
||||
objectProperty(test, 'b', () => 2);
|
||||
|
||||
expect(test.a).toEqual(1);
|
||||
expect(test.b).toEqual(1);
|
||||
});
|
||||
|
||||
it('does not override an existing property (inherited)', (): void => {
|
||||
class Parent extends Map<string, unknown> {
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
this.set('a', 1);
|
||||
}
|
||||
|
||||
get a (): unknown {
|
||||
return this.get('a');
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
this.set('b', 1);
|
||||
}
|
||||
|
||||
get b (): unknown {
|
||||
return this.get('b');
|
||||
}
|
||||
}
|
||||
|
||||
const test = new Child() as unknown as { a: number; b: number; c?: number };
|
||||
|
||||
expect(test.a).toEqual(1);
|
||||
expect(test.b).toEqual(1);
|
||||
expect(test.c).toEqual(undefined);
|
||||
|
||||
objectProperties(test, ['a', 'b', 'c'], () => 2);
|
||||
|
||||
expect(test.a).toEqual(1);
|
||||
expect(test.b).toEqual(1);
|
||||
expect(test.c).toEqual(2);
|
||||
});
|
||||
|
||||
it('works with this used in classes', (): void => {
|
||||
class Test extends Map<string, unknown> {
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
this.set('a', 1);
|
||||
|
||||
objectProperty(this, 'a', (k) => this.get(k));
|
||||
}
|
||||
}
|
||||
|
||||
const test = new Test() as unknown as { a: number };
|
||||
|
||||
expect(test.a).toEqual(1);
|
||||
});
|
||||
|
||||
it('works with this used in classes (multiples)', (): void => {
|
||||
class Test extends Map<string, unknown> {
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
this.set('a', 1);
|
||||
this.set('b', 2);
|
||||
|
||||
objectProperties(this, ['a', 'b'], (k) => this.get(k));
|
||||
}
|
||||
}
|
||||
|
||||
const test = new Test() as unknown as { a: number; b: number };
|
||||
|
||||
expect(test.a).toEqual(1);
|
||||
expect(test.b).toEqual(2);
|
||||
});
|
||||
|
||||
it('calls back with the key name (single)', (): void => {
|
||||
const test: { a?: number } = {};
|
||||
const getter = jest.fn(() => 123);
|
||||
|
||||
objectProperty(test, 'a', getter);
|
||||
|
||||
expect(getter).not.toHaveBeenCalled();
|
||||
expect(test.a).toEqual(123);
|
||||
expect(getter).toHaveBeenCalledWith('a', 0, expect.objectContaining({}));
|
||||
});
|
||||
|
||||
it('calls back with the key name & index (numtiple)', (): void => {
|
||||
const test: { a?: number; b?: number; c?: number } = {};
|
||||
const getter = jest.fn(() => 123);
|
||||
|
||||
objectProperties(test, ['a', 'b', 'c'], getter);
|
||||
|
||||
expect(getter).not.toHaveBeenCalled();
|
||||
expect(test.b).toEqual(123);
|
||||
expect(getter).toHaveBeenCalledWith('b', 1, expect.objectContaining({}));
|
||||
});
|
||||
|
||||
perf('objectProperties (obj)', 50_000, [[]], () => objectProperties({}, ['foo', 'bar', 'baz'], (k) => k));
|
||||
perf('objectProperties (map)', 50_000, [[]], () => objectProperties(new Map(), ['foo', 'bar', 'baz'], (k) => k));
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name objectProperty
|
||||
* @summary Assign a get property on the input object
|
||||
*/
|
||||
export function objectProperty <S> (that: object, key: string, getter: (key: string, index: number, self: S) => unknown, getName?: (key: string, index: number) => string, index = 0): void {
|
||||
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
|
||||
// will retuirn false if the property is set in the parent class
|
||||
// - isUndefined(...) - this may yield a false positive when the property is there, but not set.
|
||||
// Additionally, on pre-defined getters it may make a call
|
||||
// - key in that - Does not need to be combined with either of the above and checks the full chain
|
||||
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: function (): unknown {
|
||||
return getter(key, index, this as S);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @name objectProperties
|
||||
* @summary Assign get properties on the input object
|
||||
*/
|
||||
export function objectProperties <S> (that: object, keys: string[], getter: (key: string, index: number, self: S) => unknown, getName?: (key: string, index: number) => string): void {
|
||||
for (let i = 0, count = keys.length; i < count; i++) {
|
||||
objectProperty(that, keys[i], getter, getName, i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perf } from '../test/index.js';
|
||||
import { objectSpread } from './index.js';
|
||||
|
||||
describe('objectSpread', (): void => {
|
||||
it('spreads obj sources', (): void => {
|
||||
expect(
|
||||
objectSpread({}, { a: 1, b: 2, c: 3 }, { d: 4, e: 5, f: 6 })
|
||||
).toEqual({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 });
|
||||
});
|
||||
|
||||
it('spreads map sources', (): void => {
|
||||
expect(
|
||||
objectSpread({}, new Map<string, number>([['a', 1], ['b', 2], ['c', 3]]), new Map<string, number>([['d', 4], ['e', 5], ['f', 6]]))
|
||||
).toEqual({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 });
|
||||
});
|
||||
|
||||
it('spreads an object from multiple sources', (): void => {
|
||||
expect(
|
||||
objectSpread({ a: 1 }, { b: 2 }, { a: 2, c: { d: 3, e: 4 } }, new Map<string, unknown>([['f', 5], ['g', 6], ['h', { i: 7, k: 8 }]]))
|
||||
).toEqual({ a: 2, b: 2, c: { d: 3, e: 4 }, f: 5, g: 6, h: { i: 7, k: 8 } });
|
||||
});
|
||||
|
||||
it('spreads an object from multiple sources (with null & undefined)', (): void => {
|
||||
expect(
|
||||
objectSpread({ a: 1 }, null, { b: 2 }, undefined, { a: 2, c: { d: 3, e: 4 } })
|
||||
).toEqual({ a: 2, b: 2, c: { d: 3, e: 4 } });
|
||||
});
|
||||
|
||||
perf('objectSpread (obj)', 350_000, [[{}, { a: 1, b: 2, c: 3 }, { d: 4, e: 5, f: 6 }]], objectSpread);
|
||||
perf('objectSpread (map)', 350_000, [[{}, new Map<string, number>([['a', 1], ['b', 2], ['c', 3]]), new Map<string, number>([['d', 4], ['e', 5], ['f', 6]])]], objectSpread);
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name objectSpread
|
||||
* @summary Concats all sources into the destination
|
||||
* @description Spreads object properties while maintaining object integrity
|
||||
*/
|
||||
export function objectSpread <T extends object> (dest: object, ...sources: (object | undefined | null)[]): T {
|
||||
const filterProps = new Set(['__proto__', 'constructor', 'prototype']);
|
||||
|
||||
for (let i = 0, count = sources.length; i < count; i++) {
|
||||
const src = sources[i];
|
||||
|
||||
if (src) {
|
||||
if (typeof (src as Map<string, unknown>).entries === 'function') {
|
||||
for (const [key, value] of (src as Map<string, unknown>).entries()) {
|
||||
if (!filterProps.has(key)) {
|
||||
(dest as Record<string, unknown>)[key] = value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Create a clean copy of the source object
|
||||
const sanitizedSrc = Object.create(null) as Record<string, unknown>;
|
||||
|
||||
for (const [key, value] of Object.entries(src)) {
|
||||
if (!filterProps.has(key)) {
|
||||
sanitizedSrc[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(dest, sanitizedSrc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dest as T;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { objectValues } from './index.js';
|
||||
|
||||
describe('objectValues', (): void => {
|
||||
it('extracts all values', (): void => {
|
||||
const o = { a: 1, b: 2, c: 3 };
|
||||
const values = objectValues(o);
|
||||
|
||||
expect(values).toEqual([1, 2, 3]);
|
||||
|
||||
for (const v of values) {
|
||||
expect(v).toBeDefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name objectValues
|
||||
* @summary A version of Object.values that is typed for TS
|
||||
*/
|
||||
export function objectValues<T extends object> (obj: T): T[keyof T][] {
|
||||
return Object.values(obj) as T[keyof T][];
|
||||
}
|
||||
Reference in New Issue
Block a user