Add objectClear & objectSpread (#1157)
* Add objectClear & objectSpread * Allow null/undefined as a source
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `objectClear` and `objectSpread` utility functions
|
||||
- Sync with upstream Substrate ss58 registry
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { EncryptedJsonEncoding, Keypair, KeypairType } from '@polkadot/util
|
||||
import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta, SignOptions } from '../types';
|
||||
import type { PairInfo } from './types';
|
||||
|
||||
import { assert, u8aConcat, u8aEmpty, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, objectSpread, u8aConcat, u8aEmpty, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { blake2AsU8a, convertPublicKeyToCurve25519, convertSecretKeyToCurve25519, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclOpen, naclSeal, naclSign, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVrfSign, schnorrkelVrfVerify, secp256k1Compress, secp256k1Expand, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, signatureVerify } from '@polkadot/util-crypto';
|
||||
|
||||
import { decodePair } from './decode';
|
||||
@@ -181,7 +181,7 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P
|
||||
secretKey = new Uint8Array();
|
||||
},
|
||||
setMeta: (additional: KeyringPair$Meta): void => {
|
||||
meta = { ...meta, ...additional };
|
||||
meta = objectSpread({}, meta, additional);
|
||||
},
|
||||
sign: (message: HexString | string | Uint8Array, options: SignOptions = {}): Uint8Array => {
|
||||
assert(!isLocked(secretKey), 'Cannot sign with a locked key pair');
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import type { KeypairType } from '@polkadot/util-crypto/types';
|
||||
import type { KeyringPair$Json, KeyringPair$Meta } from '../types';
|
||||
|
||||
import { objectSpread } from '@polkadot/util';
|
||||
import { jsonEncryptFormat } from '@polkadot/util-crypto';
|
||||
|
||||
interface PairStateJson {
|
||||
@@ -12,9 +13,8 @@ interface PairStateJson {
|
||||
}
|
||||
|
||||
export function pairToJson (type: KeypairType, { address, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
|
||||
return {
|
||||
...jsonEncryptFormat(encoded, ['pkcs8', type], isEncrypted),
|
||||
return objectSpread({}, jsonEncryptFormat(encoded, ['pkcs8', type], isEncrypted), {
|
||||
address,
|
||||
meta
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { HexString, ToBn, ToBnOptions } from '../types';
|
||||
import type { BN } from './bn';
|
||||
|
||||
import { isNumber } from '../is/number';
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { bnToU8a } from './toU8a';
|
||||
|
||||
@@ -36,14 +37,14 @@ function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number
|
||||
return ZERO_STR;
|
||||
}
|
||||
|
||||
const _options = {
|
||||
isLe: false,
|
||||
isNegative: false,
|
||||
// Backwards-compatibility
|
||||
...(isNumber(arg1) ? { bitLength: arg1, isLe: arg2 } : arg1)
|
||||
};
|
||||
|
||||
return u8aToHex(bnToU8a(value, _options));
|
||||
return u8aToHex(
|
||||
bnToU8a(value, objectSpread(
|
||||
{ isLe: false, isNegative: false },
|
||||
isNumber(arg1)
|
||||
? { bitLength: arg1, isLe: arg2 }
|
||||
: arg1
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
export { bnToHex };
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { ToBn, ToBnOptions } from '../types';
|
||||
import type { BN } from './bn';
|
||||
|
||||
import { isNumber } from '../is/number';
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { bnToBn } from './toBn';
|
||||
|
||||
interface Options extends ToBnOptions {
|
||||
@@ -43,12 +44,12 @@ function createValue (valueBn: BN, byteLength: number, { isLe, isNegative }: Opt
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: Options): Uint8Array;
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false }, arg2?: boolean): Uint8Array {
|
||||
const options: Options = {
|
||||
bitLength: -1,
|
||||
isLe: true,
|
||||
isNegative: false,
|
||||
...isNumber(arg1) ? { bitLength: arg1, isLe: arg2 } : arg1
|
||||
};
|
||||
const options: Options = objectSpread(
|
||||
{ bitLength: -1, isLe: true, isNegative: false },
|
||||
isNumber(arg1)
|
||||
? { bitLength: arg1, isLe: arg2 }
|
||||
: arg1
|
||||
);
|
||||
|
||||
const valueBn = bnToBn(value);
|
||||
const byteLength = options.bitLength === -1
|
||||
|
||||
@@ -18,6 +18,7 @@ export * from './is';
|
||||
export * from './logger';
|
||||
export * from './memoize';
|
||||
export * from './number';
|
||||
export * from './object';
|
||||
export * from './promisify';
|
||||
export * from './string';
|
||||
export * from './stringify';
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
import type { Time } from './types';
|
||||
|
||||
import { objectSpread } from './object/spread';
|
||||
|
||||
const HRS = 60 * 60;
|
||||
const DAY = HRS * 24;
|
||||
|
||||
@@ -21,12 +23,12 @@ function addTime (a: Time, b: Time): Time {
|
||||
};
|
||||
}
|
||||
|
||||
const ZERO = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
|
||||
const ZERO: Time = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
|
||||
|
||||
function extractDays (milliseconds: number, hrs: number): Time {
|
||||
const days = Math.floor(hrs / 24);
|
||||
|
||||
return addTime({ ...ZERO, days }, extractTime(milliseconds - (days * DAY * 1000)));
|
||||
return addTime(objectSpread({}, ZERO, { days }), extractTime(milliseconds - (days * DAY * 1000)));
|
||||
}
|
||||
|
||||
function extractHrs (milliseconds: number, mins: number): Time {
|
||||
@@ -35,7 +37,7 @@ function extractHrs (milliseconds: number, mins: number): Time {
|
||||
if (hrs < 24) {
|
||||
const hours = Math.floor(hrs);
|
||||
|
||||
return addTime({ ...ZERO, hours }, extractTime(milliseconds - (hours * HRS * 1000)));
|
||||
return addTime(objectSpread({}, ZERO, { hours }), extractTime(milliseconds - (hours * HRS * 1000)));
|
||||
}
|
||||
|
||||
return extractDays(milliseconds, hrs);
|
||||
@@ -47,7 +49,7 @@ function extractMins (milliseconds: number, secs: number): Time {
|
||||
if (mins < 60) {
|
||||
const minutes = Math.floor(mins);
|
||||
|
||||
return addTime({ ...ZERO, minutes }, extractTime(milliseconds - (minutes * 60 * 1000)));
|
||||
return addTime(objectSpread({}, ZERO, { minutes }), extractTime(milliseconds - (minutes * 60 * 1000)));
|
||||
}
|
||||
|
||||
return extractHrs(milliseconds, mins);
|
||||
@@ -59,7 +61,7 @@ function extractSecs (milliseconds: number): Time {
|
||||
if (secs < 60) {
|
||||
const seconds = Math.floor(secs);
|
||||
|
||||
return addTime({ ...ZERO, seconds }, extractTime(milliseconds - (seconds * 1000)));
|
||||
return addTime(objectSpread({}, ZERO, { seconds }), extractTime(milliseconds - (seconds * 1000)));
|
||||
}
|
||||
|
||||
return extractMins(milliseconds, secs);
|
||||
@@ -81,7 +83,7 @@ export function extractTime (milliseconds?: number): Time {
|
||||
if (!milliseconds) {
|
||||
return ZERO;
|
||||
} else if (milliseconds < 1000) {
|
||||
return { ...ZERO, milliseconds };
|
||||
return objectSpread({}, ZERO, { milliseconds });
|
||||
}
|
||||
|
||||
return extractSecs(milliseconds);
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { ToBnOptions } from '../types';
|
||||
|
||||
import { BN } from '../bn/bn';
|
||||
import { isBoolean } from '../is/boolean';
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { hexStripPrefix } from './stripPrefix';
|
||||
|
||||
function reverse (value: string): string {
|
||||
@@ -36,16 +37,12 @@ export function hexToBn (value?: string | null, options: ToBnOptions | boolean =
|
||||
return new BN(0);
|
||||
}
|
||||
|
||||
const _options: ToBnOptions = {
|
||||
isLe: false,
|
||||
isNegative: false,
|
||||
// Backwards-compatibility
|
||||
...(
|
||||
isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options
|
||||
)
|
||||
};
|
||||
const _options: ToBnOptions = objectSpread(
|
||||
{ isLe: false, isNegative: false },
|
||||
isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options
|
||||
);
|
||||
const _value = hexStripPrefix(value);
|
||||
|
||||
// FIXME: Use BN's 3rd argument `isLe` once this issue is fixed
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { objectClear } from '.';
|
||||
|
||||
describe('objectClear', (): void => {
|
||||
it('clears an object', (): void => {
|
||||
expect(
|
||||
objectClear({ a: 1, b: 2, c: { d: 3, e: 4 } })
|
||||
).toEqual({});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export function objectClear <T> (value: Record<string, T>): Record<string, T> {
|
||||
const keys = Object.keys(value);
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
delete value[keys[i]];
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { objectClear } from './clear';
|
||||
export { objectSpread } from './spread';
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { objectSpread } from '.';
|
||||
|
||||
describe('objectSpread', (): void => {
|
||||
it('spreads an object from multiple sources', (): void => {
|
||||
expect(
|
||||
objectSpread({ a: 1 }, { b: 2 }, { a: 2, c: { d: 3, e: 4 } })
|
||||
).toEqual({ a: 2, b: 2, c: { d: 3, e: 4 } });
|
||||
});
|
||||
|
||||
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 } });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export function objectSpread <T extends object> (dest: object, ...sources: (object | undefined | null)[]): T {
|
||||
for (let i = 0; i < sources.length; i++) {
|
||||
const src = sources[i];
|
||||
|
||||
if (src) {
|
||||
const keys = Object.keys(src);
|
||||
|
||||
for (let j = 0; j < keys.length; j++) {
|
||||
const key = keys[j];
|
||||
|
||||
(dest as Record<string, unknown>)[key] = (src as Record<string, unknown>)[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dest as T;
|
||||
}
|
||||
Reference in New Issue
Block a user