object{Entries, Keys, Values} (#1169)

* object{Entries, Keys, Values}

* Use objectKeys as applicable

* tighten schema defs

* Linting issue snuck in
This commit is contained in:
Jaco
2021-11-08 10:09:26 +02:00
committed by GitHub
parent d55b48566b
commit 6d923da266
14 changed files with 120 additions and 6 deletions
+1
View File
@@ -5,6 +5,7 @@
Changes:
- Add `stringPascalCase` utility
- Add strongly typed `object{Entries, Keys, Values}` utilities
## 7.8.2 Nov 7, 2021
+3 -3
View File
@@ -1,11 +1,11 @@
// Copyright 2017-2021 @polkadot/networks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { KnownSubstrate, Ss58Registry } from './types';
import type { Ss58Registry } from './types';
import fs from 'fs';
import { stringify } from '@polkadot/util';
import { objectKeys, stringify } from '@polkadot/util';
import { fetch } from '@polkadot/x-fetch';
import { substrateRegistry } from './defaults';
@@ -52,7 +52,7 @@ describe('check latest Substrate ss58 registry', (): void => {
});
it('has the same values as the original', (): void => {
const fields = Object.keys(original.schema) as (keyof KnownSubstrate)[];
const fields = objectKeys(original.schema);
const errors = original.registry
.map((n): [string, string[]] => {
const other = allNetworks.find(({ prefix }) => prefix === n.prefix);
+1 -1
View File
@@ -37,5 +37,5 @@ export interface Network extends SubstrateNetwork {
export interface Ss58Registry {
registry: KnownSubstrate[];
specification: string;
schema: Record<string, string>;
schema: Record<keyof KnownSubstrate, string>;
}
+4
View File
@@ -1,6 +1,10 @@
// Copyright 2017-2021 @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);
+4
View File
@@ -3,6 +3,10 @@
import { objectSpread } from './spread';
/**
* @name objectCopy
* @summary Creates a shallow clone of the input object
*/
export function objectCopy <T extends object> (source: T): T {
return objectSpread({}, source);
}
+18
View File
@@ -0,0 +1,18 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { objectEntries } from '.';
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();
}
});
});
+14
View File
@@ -0,0 +1,14 @@
// Copyright 2017-2021 @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>;
}
+3
View File
@@ -3,5 +3,8 @@
export { objectClear } from './clear';
export { objectCopy } from './copy';
export { objectEntries } from './entries';
export { objectKeys } from './keys';
export { objectProperty, objectProperties } from './property';
export { objectSpread } from './spread';
export { objectValues } from './values';
+17
View File
@@ -0,0 +1,17 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { objectKeys } from '.';
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();
}
});
});
+10
View File
@@ -0,0 +1,10 @@
// Copyright 2017-2021 @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[];
}
+8
View File
@@ -3,6 +3,10 @@
import { isUndefined } from '../is/undefined';
/**
* @name objectProperty
* @summary Assign a get property on the input object
*/
export function objectProperty (that: object, key: string, getter: (k: string) => unknown): void {
// We use both the hasOwnProperty as well as isUndefined checks here, since it may be set
// in inherited classes and _Own_ properties refers to the class only, not only parents
@@ -13,6 +17,10 @@ export function objectProperty (that: object, key: string, getter: (k: string) =
}
}
/**
* @name objectProperties
* @summary Assign get properties on the input object
*/
export function objectProperties (that: object, keys: string[], getter: (k: string, i: number) => unknown): void {
for (let i = 0; i < keys.length; i++) {
objectProperty(that, keys[i], (k) => getter(k, i));
+8 -2
View File
@@ -1,17 +1,23 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { objectKeys } from './keys';
/**
* @name objectSpread
* @summary Concats all sources into the destination
*/
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);
const keys = objectKeys(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];
dest[key] = src[key];
}
}
}
+17
View File
@@ -0,0 +1,17 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { objectValues } from '.';
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();
}
});
});
+12
View File
@@ -0,0 +1,12 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
type Values<T extends object> = T[keyof T][];
/**
* @name objectValues
* @summary A version of Object.values that is typed for TS
*/
export function objectValues<T extends object> (obj: T): Values<T> {
return Object.values(obj) as Values<T>;
}