Initial commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
# [←](../../Readme.md) Огляд Object утиліт
|
||||
|
||||
### # keys\<T extends object>(object: T): (keyof T)[]
|
||||
|
||||
Типізована обгортка над `Object.keys` — повертає масив ключів зі збереженням типу.
|
||||
|
||||
```ts
|
||||
keys({ a: 1, b: 2 }); // → ['a', 'b']
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### # pick(names: string[], obj: Record\<string, unknown>): object
|
||||
|
||||
Повертає новий об'єкт лише з вказаними ключами.
|
||||
|
||||
```ts
|
||||
pick(['a', 'c'], { a: 1, b: 2, c: 3 }); // → { a: 1, c: 3 }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### # omit(excludeObjectKeys: string[], obj: Record\<string, unknown>): object
|
||||
|
||||
Повертає новий об'єкт без вказаних ключів.
|
||||
|
||||
```ts
|
||||
omit(['b', 'c'], { a: 1, b: 2, c: 3 }); // → { a: 1 }
|
||||
```
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './keys';
|
||||
export * from './pick';
|
||||
export * from './omit';
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Return an array of keys from an object.
|
||||
*
|
||||
* @param object - The object from which to extract the keys.
|
||||
* @returns An array of keys from the object.
|
||||
*
|
||||
* @example
|
||||
* const obj = { a: 1, b: 2, c: 3 };
|
||||
* const result = keys(obj);
|
||||
* // result: ['a', 'b', 'c']
|
||||
*/
|
||||
export function keys<T extends object>(object: T): (keyof T)[] {
|
||||
return Object.keys(object) as (keyof T)[];
|
||||
}
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Omit specified keys from an object.
|
||||
*
|
||||
* @param excludeObjectKeys - An array of keys to exclude from the object.
|
||||
* @param obj - The object from which to omit the keys.
|
||||
* @returns The new object with the specified keys omitted.
|
||||
*
|
||||
* @example
|
||||
* const input = { a: 1, b: 2, c: 3 };
|
||||
* const excludedKeys = ['b', 'c'];
|
||||
* const output = omit(excludedKeys, input);
|
||||
* // output: { a: 1 }
|
||||
*/
|
||||
export function omit(excludeObjectKeys: string[], obj: Record<string, unknown>): object {
|
||||
const result: Record<string, unknown> = {};
|
||||
const index: Record<string, unknown> = {};
|
||||
let idx = 0;
|
||||
const length = excludeObjectKeys.length;
|
||||
|
||||
while (idx < length) {
|
||||
index[excludeObjectKeys[idx]] = 1;
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
for (const prop in obj) {
|
||||
if (!Object.prototype.hasOwnProperty.call(index, prop)) result[prop] = obj[prop];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Pick specified keys from an object.
|
||||
*
|
||||
* @param names - An array of keys to pick from the object.
|
||||
* @param obj - The object from which to pick the keys.
|
||||
* @returns The new object with the specified keys picked.
|
||||
*
|
||||
* @example
|
||||
* const input = { a: 1, b: 2, c: 3 };
|
||||
* const pickedKeys = ['a', 'c'];
|
||||
* const output = pick(pickedKeys, input);
|
||||
* // output: { a: 1, c: 3 }
|
||||
*/
|
||||
export function pick(names: string[], obj: Record<string, unknown>): object {
|
||||
const result: Record<string, unknown> = {};
|
||||
let idx = 0;
|
||||
|
||||
while (idx < names.length) {
|
||||
if (names[idx] in obj) result[names[idx]] = obj[names[idx]];
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user