mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-10-28 13:29:52 +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:
commit
ec06da0ebc
687 files changed
+48096
No files matched your search
@@ -0,0 +1,119 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { getRandomValues } from '@pezkuwi/x-randomvalues';
|
||||
|
||||
import { arrayRange } from '../array/index.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { stringCamelCase, stringUpperFirst } from './index.js';
|
||||
|
||||
const SEPS = [' ', '_', '-', '.', ','];
|
||||
const words = 'bring|brisk|broccoli|broken|bronze|broom|brother|brown|brush|bubble|buddy|budget|buffalo|build|bulb|bulk|bullet|bundle|bunker|burden'.split('|');
|
||||
|
||||
function randomAsNumber (): number {
|
||||
const rand = getRandomValues(new Uint8Array(4));
|
||||
|
||||
return rand.reduce((v, t) => (v * t) + v, 0);
|
||||
}
|
||||
|
||||
const randomWords = arrayRange(256).map((): [string] => {
|
||||
let result = '';
|
||||
|
||||
for (let i = 0; i < 6; i++) {
|
||||
let word = words[randomAsNumber() % words.length];
|
||||
|
||||
if (randomAsNumber() % 2) {
|
||||
word = stringUpperFirst(word);
|
||||
}
|
||||
|
||||
if (i === 0) {
|
||||
result = word;
|
||||
} else {
|
||||
result += `${SEPS[randomAsNumber() % SEPS.length]}${word}`;
|
||||
}
|
||||
}
|
||||
|
||||
return [result];
|
||||
});
|
||||
|
||||
describe('stringCamelCase', (): void => {
|
||||
it('works correctly', (): void => {
|
||||
expect(
|
||||
stringCamelCase('Snake_case-...SomethingSomething spaced')
|
||||
).toBe('snakeCaseSomethingSomethingSpaced');
|
||||
});
|
||||
|
||||
it('works correctly for String (class', (): void => {
|
||||
expect(
|
||||
stringCamelCase(String('Foo_bar-baz---test-_ spaced....Extra'))
|
||||
).toBe('fooBarBazTestSpacedExtra');
|
||||
});
|
||||
|
||||
it('adjusts all-uppercase', (): void => {
|
||||
expect(
|
||||
stringCamelCase('DEDUP_KEY_PREFIX')
|
||||
).toEqual('dedupKeyPrefix');
|
||||
expect(
|
||||
stringCamelCase(String('DEDUP_KEY_PREFIX'))
|
||||
).toEqual('dedupKeyPrefix');
|
||||
expect(
|
||||
stringCamelCase('SOMETHING')
|
||||
).toEqual('something');
|
||||
expect(
|
||||
stringCamelCase('NMap')
|
||||
).toEqual('nMap');
|
||||
});
|
||||
|
||||
it('adjusts all-uppercase + digits', (): void => {
|
||||
expect(
|
||||
stringCamelCase('SS58 PreFIX')
|
||||
).toEqual('ss58PreFIX');
|
||||
expect(
|
||||
stringCamelCase('SS58Prefix')
|
||||
).toEqual('ss58Prefix');
|
||||
expect(
|
||||
stringCamelCase('UUID64')
|
||||
).toEqual('uuid64');
|
||||
expect(
|
||||
stringCamelCase('BLAKE2B')
|
||||
).toEqual('blake2b');
|
||||
expect(
|
||||
stringCamelCase('NFTOrder')
|
||||
).toEqual('nftOrder');
|
||||
expect(
|
||||
stringCamelCase('EVM')
|
||||
).toEqual('evm');
|
||||
expect(
|
||||
stringCamelCase('A')
|
||||
).toEqual('a');
|
||||
expect(
|
||||
stringCamelCase('A1')
|
||||
).toEqual('a1');
|
||||
expect(
|
||||
stringCamelCase('A1b')
|
||||
).toEqual('a1b');
|
||||
expect(
|
||||
stringCamelCase('A1B')
|
||||
).toEqual('a1b');
|
||||
expect(
|
||||
stringCamelCase('RawVRFOutput')
|
||||
).toEqual('rawVRFOutput');
|
||||
});
|
||||
|
||||
it('adjusts with leading _', (): void => {
|
||||
expect(
|
||||
stringCamelCase('_reserved')
|
||||
).toEqual('reserved');
|
||||
});
|
||||
|
||||
it('adjusts with minimal chars per part', (): void => {
|
||||
expect(
|
||||
stringCamelCase('_a_b_c_def')
|
||||
).toEqual('aBCDef');
|
||||
});
|
||||
|
||||
perf('stringCamelCase (random)', 100_000, randomWords, stringCamelCase);
|
||||
perf('stringCamelCase (NFTOrder)', 100_000, [['NFTOrder']], stringCamelCase);
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyString } from '../types.js';
|
||||
|
||||
export const CC_TO_UP = new Array<string>(256);
|
||||
export const CC_TO_LO = new Array<string>(256);
|
||||
|
||||
for (let i = 0, count = CC_TO_UP.length; i < count; i++) {
|
||||
CC_TO_LO[i] = String.fromCharCode(i).toLowerCase();
|
||||
CC_TO_UP[i] = String.fromCharCode(i).toUpperCase();
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function formatAllCaps (w: string): string {
|
||||
return w.slice(0, w.length - 1).toLowerCase() + CC_TO_UP[w.charCodeAt(w.length - 1)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* Inspired by https://stackoverflow.com/a/2970667
|
||||
*
|
||||
* This is not as optimal as the original SO answer (we split into per-word),
|
||||
* however it does pass the tests (which the SO version doesn't) and is still
|
||||
* a major improvement over the original camelcase npm package -
|
||||
*
|
||||
* camelcase: 20.88 μs/op
|
||||
* this: 1.00 μs/op
|
||||
*
|
||||
* Caveat of this: only Ascii, but acceptable for the intended usecase
|
||||
*/
|
||||
function converter (format: (w: string, i: number) => string): (value: AnyString) => string {
|
||||
return (value: AnyString): string => {
|
||||
const parts = value
|
||||
// replace all separators (including consequtive) with spaces
|
||||
.replace(/[-_., ]+/g, ' ')
|
||||
// we don't want leading or trailing spaces
|
||||
.trim()
|
||||
// split into words
|
||||
.split(' ');
|
||||
let result = '';
|
||||
|
||||
for (let i = 0, count = parts.length; i < count; i++) {
|
||||
const w = parts[i];
|
||||
|
||||
// apply the formatting
|
||||
result += format(
|
||||
/^[\dA-Z]+$/.test(w)
|
||||
// all full uppercase + letters are changed to lowercase
|
||||
? w.toLowerCase()
|
||||
// all consecutive capitals + letters are changed to lowercase
|
||||
// e.g. UUID64 -> uuid64, while preserving splits, eg. NFTOrder -> nftOrder
|
||||
: w.replace(/^[\dA-Z]{2,}[^a-z]/, formatAllCaps),
|
||||
i
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @name stringCamelCase
|
||||
* @summary Convert a dash/dot/underscore/space separated Ascii string/String to camelCase
|
||||
*/
|
||||
export const stringCamelCase = /*#__PURE__*/ converter((w, i) =>
|
||||
// lowercase for first letter/first word, else uppercase first, rest unchanged
|
||||
(i ? CC_TO_UP[w.charCodeAt(0)] : CC_TO_LO[w.charCodeAt(0)]) + w.slice(1)
|
||||
);
|
||||
|
||||
/**
|
||||
* @name stringPascalCase
|
||||
* @summary Convert a dash/dot/underscore/space separated Ascii string/String to PascalCase
|
||||
*/
|
||||
export const stringPascalCase = /*#__PURE__*/ converter((w) =>
|
||||
// uppercase the first character, leave the rest unchanged
|
||||
CC_TO_UP[w.charCodeAt(0)] + w.slice(1)
|
||||
);
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Utility methods to convert to work with `string` values
|
||||
*/
|
||||
|
||||
export { stringCamelCase, stringPascalCase } from './camelCase.js';
|
||||
export { stringLowerFirst, stringUpperFirst } from './lowerFirst.js';
|
||||
export { stringShorten } from './shorten.js';
|
||||
export { stringToHex } from './toHex.js';
|
||||
export { stringToU8a } from './toU8a.js';
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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 { stringLowerFirst } from './index.js';
|
||||
|
||||
describe('stringLowerFirst', (): void => {
|
||||
it("lowers the first letter if it's a capital letter", (): void => {
|
||||
expect(
|
||||
stringLowerFirst('ABC')
|
||||
).toBe('aBC');
|
||||
});
|
||||
|
||||
it("lowers the first letter if it's a capital letter (String)", (): void => {
|
||||
expect(
|
||||
stringLowerFirst(String('ABC'))
|
||||
).toBe('aBC');
|
||||
});
|
||||
|
||||
it("lowers the first letter if it's a lowercase letter", (): void => {
|
||||
expect(
|
||||
stringLowerFirst('abc')
|
||||
).toBe('abc');
|
||||
});
|
||||
|
||||
it('returns undefined as empty', (): void => {
|
||||
expect(
|
||||
stringLowerFirst()
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
it('returns null as empty', (): void => {
|
||||
expect(
|
||||
stringLowerFirst(null)
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
perf('stringLowerFirst', 1_000_000, [['HelloWorldSomething']], stringLowerFirst);
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyString } from '../types.js';
|
||||
|
||||
import { CC_TO_LO, CC_TO_UP } from './camelCase.js';
|
||||
|
||||
/** @internal */
|
||||
function converter (map: readonly string[]): (value?: AnyString | null) => string {
|
||||
return (value?: AnyString | null): string =>
|
||||
value
|
||||
? map[value.charCodeAt(0)] + value.slice(1)
|
||||
: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @name stringLowerFirst
|
||||
* @summary Lowercase the first letter of a string
|
||||
* @description
|
||||
* Lowercase the first letter of a string
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { stringLowerFirst } from '@pezkuwi/util';
|
||||
*
|
||||
* stringLowerFirst('ABC'); // => 'aBC'
|
||||
* ```
|
||||
*/
|
||||
export const stringLowerFirst = /*#__PURE__*/ converter(CC_TO_LO);
|
||||
|
||||
/**
|
||||
* @name stringUpperFirst
|
||||
* @summary Uppercase the first letter of a string
|
||||
* @description
|
||||
* Lowercase the first letter of a string
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { stringUpperFirst } from '@pezkuwi/util';
|
||||
*
|
||||
* stringUpperFirst('abc'); // => 'Abc'
|
||||
* ```
|
||||
*/
|
||||
export const stringUpperFirst = /*#__PURE__*/ converter(CC_TO_UP);
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringPascalCase } from './index.js';
|
||||
|
||||
describe('stringPascalCase', (): void => {
|
||||
it('works correctly', (): void => {
|
||||
expect(
|
||||
stringPascalCase('Snake_case-something spaced')
|
||||
).toBe('SnakeCaseSomethingSpaced');
|
||||
});
|
||||
|
||||
it('works correctly for String (class)', (): void => {
|
||||
expect(
|
||||
stringPascalCase(String('foo_bar-- __baz Bob'))
|
||||
).toBe('FooBarBazBob');
|
||||
});
|
||||
|
||||
it('adjusts all-uppercase + digits', (): void => {
|
||||
expect(
|
||||
stringPascalCase('UUID64')
|
||||
).toEqual('Uuid64');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringShorten } from './index.js';
|
||||
|
||||
describe('stringShorten', (): void => {
|
||||
it('returns the value as-is when <= maxLength', (): void => {
|
||||
expect(
|
||||
stringShorten('0123456789', 4)
|
||||
).toEqual('0123456789');
|
||||
});
|
||||
|
||||
it('returns the shortened value when > maxLength', (): void => {
|
||||
expect(
|
||||
stringShorten('0123456789', 3)
|
||||
).toEqual('012…789');
|
||||
});
|
||||
|
||||
it('returns the shortened value when > maxLength (String)', (): void => {
|
||||
expect(
|
||||
stringShorten(String('0123456789'), 3)
|
||||
).toEqual('012…789');
|
||||
});
|
||||
|
||||
it('returns the value as-is when <= maxLength (defaults)', (): void => {
|
||||
expect(
|
||||
stringShorten('012345', 4)
|
||||
).toEqual('012345');
|
||||
});
|
||||
|
||||
it('returns the shortened value when > maxLength (defaults)', (): void => {
|
||||
expect(
|
||||
stringShorten('01234567890123456789')
|
||||
).toEqual('012345…456789');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyString } from '../types.js';
|
||||
|
||||
/**
|
||||
* @name stringShorten
|
||||
* @summary Returns a string with maximum length
|
||||
* @description
|
||||
* Checks the string against the `prefixLength`, if longer than double this, shortens it by placing `..` in the middle of it
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { stringShorten } from '@pezkuwi/util';
|
||||
*
|
||||
* stringShorten('1234567890', 2); // => 12..90
|
||||
* ```
|
||||
*/
|
||||
export function stringShorten (value: AnyString, prefixLength = 6): string {
|
||||
return value.length <= 2 + 2 * prefixLength
|
||||
? value.toString()
|
||||
: `${value.substring(0, prefixLength)}…${value.slice(-prefixLength)}`;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringToHex } from './index.js';
|
||||
|
||||
describe('hexToString', (): void => {
|
||||
it('converts an empty to ""', (): void => {
|
||||
expect(
|
||||
stringToHex()
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts to a hex from string', (): void => {
|
||||
expect(
|
||||
stringToHex('hello')
|
||||
).toEqual('0x68656c6c6f');
|
||||
});
|
||||
|
||||
it('converts to a hex from String', (): void => {
|
||||
expect(
|
||||
stringToHex(String('hello'))
|
||||
).toEqual('0x68656c6c6f');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyString, HexString } from '../types.js';
|
||||
|
||||
import { u8aToHex } from '../u8a/toHex.js';
|
||||
import { stringToU8a } from './toU8a.js';
|
||||
|
||||
/**
|
||||
* @name stringToHex
|
||||
* @summary Creates a hex string from a utf-8 string
|
||||
* @description
|
||||
* String input values return the actual encoded hex value.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { stringToHex } from '@pezkuwi/util';
|
||||
*
|
||||
* stringToU8a('hello'); // 0x68656c6c6f
|
||||
* ```
|
||||
*/
|
||||
export function stringToHex (value?: AnyString): HexString {
|
||||
return u8aToHex(
|
||||
stringToU8a(value)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringToU8a } from '../index.js';
|
||||
|
||||
const RUSS_HELLO = new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33]);
|
||||
|
||||
describe('stringToU8a', (): void => {
|
||||
it('decodes to an empty string for undefined/null', (): void => {
|
||||
expect(
|
||||
stringToU8a()
|
||||
).toEqual(new Uint8Array());
|
||||
expect(
|
||||
stringToU8a(null)
|
||||
).toEqual(new Uint8Array());
|
||||
});
|
||||
|
||||
it('encodes the string correctly', (): void => {
|
||||
expect(
|
||||
stringToU8a('Привет, мир!')
|
||||
).toEqual(RUSS_HELLO);
|
||||
});
|
||||
|
||||
it('encodes the string correctly (String)', (): void => {
|
||||
expect(
|
||||
stringToU8a(String('Привет, мир!'))
|
||||
).toEqual(RUSS_HELLO);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyString } from '../types.js';
|
||||
|
||||
import { TextEncoder } from '@pezkuwi/x-textencoder';
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
/**
|
||||
* @name stringToU8a
|
||||
* @summary Creates a Uint8Array object from a utf-8 string.
|
||||
* @description
|
||||
* String input values return the actual encoded `UInt8Array`. `null` or `undefined` values returns an empty encoded array.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { stringToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
|
||||
* ```
|
||||
*/
|
||||
export function stringToU8a (value?: AnyString | null): Uint8Array {
|
||||
return value
|
||||
? encoder.encode(value.toString())
|
||||
: new Uint8Array();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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 { stringUpperFirst } from './index.js';
|
||||
|
||||
describe('stringUpperFirst', (): void => {
|
||||
it("uppers the first letter if it's a capital letter", (): void => {
|
||||
expect(
|
||||
stringUpperFirst('ABC')
|
||||
).toBe('ABC');
|
||||
});
|
||||
|
||||
it("uppers the first letter if it's a lowercase letter", (): void => {
|
||||
expect(
|
||||
stringUpperFirst('abc')
|
||||
).toBe('Abc');
|
||||
});
|
||||
|
||||
it("uppers the first letter if it's a lowercase letter (String)", (): void => {
|
||||
expect(
|
||||
stringUpperFirst(String('abc'))
|
||||
).toBe('Abc');
|
||||
});
|
||||
|
||||
it('returns undefined as empty', (): void => {
|
||||
expect(
|
||||
stringUpperFirst()
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
it('returns null as empty', (): void => {
|
||||
expect(
|
||||
stringUpperFirst(null)
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
perf('stringUpperFirst', 1_000_000, [['helloWorldSomething']], stringUpperFirst);
|
||||
});
|
||||
Reference in new issue
Block a user