Skip to content

Commit

Permalink
feat(utils): add pick fn, improve omit
Browse files Browse the repository at this point in the history
  • Loading branch information
tsa96 committed Dec 20, 2024
1 parent e9433a8 commit 1463a3f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
1 change: 1 addition & 0 deletions libs/util-fn/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './kebab-case';
export * from './deep-equal';
export * from './extract-prefix-from-map-name';
export * from './leaderboard-key';
export * from './pick';
export * from './omit';
export * from './magic';
export * from './approx-eq';
1 change: 1 addition & 0 deletions libs/util-fn/src/omit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('omit function', () => {
});

it('should return the same object if the key does not exist', () => {
// @ts-expect-error - testing invalid keys
expect(omit({ a: 1, b: 2, c: 3 }, 'd')).toEqual({ a: 1, b: 2, c: 3 });
});

Expand Down
33 changes: 24 additions & 9 deletions libs/util-fn/src/omit.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
export function omit<T extends object>(
obj: T,
keys: string | string[]
): Partial<T> {
const result = {};
for (const key of Object.keys(obj)) {
if (typeof keys === 'string') {
if (keys !== key) {
export function omit<Obj extends object, Keys extends keyof Obj>(
obj: Obj,
keys: Keys
): Omit<Obj, Keys>;

export function omit<Obj extends object, Keys extends Array<keyof Obj>>(
obj: Obj,
keys: Keys
): Omit<Obj, Keys[number]>;

export function omit<
Obj extends object,
Keys extends keyof Obj | Array<keyof Obj>
>(
obj: Obj,
keys: string | Array<keyof Obj>
): Omit<Obj, Keys extends Array<keyof Obj> ? Keys[number] : Keys> {
const result: any = {};

for (const key of Object.keys(obj) as Array<keyof Obj>) {
if (Array.isArray(keys)) {
if (!keys.includes(key)) {
result[key] = obj[key];
}
} else {
if (!keys.includes(key)) {
if (keys !== key) {
result[key] = obj[key];
}
}
}

return result;
}
56 changes: 56 additions & 0 deletions libs/util-fn/src/pick.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { pick } from './pick';

describe('pick', () => {
it('returns an object with only the specified key when a single key is provided', () => {
const obj = { a: 1, b: 2, c: 3 };
const result = pick(obj, 'a');
expect(result).toEqual({ a: 1 });
});

it('returns an object with only the specified keys when an array of keys is provided', () => {
const obj = { a: 1, b: 2, c: 3 };
const result = pick(obj, ['a', 'c']);
expect(result).toEqual({ a: 1, c: 3 });
});

it('returns an empty object when none of the specified keys are present', () => {
const obj = { a: 1, b: 2, c: 3 };
// @ts-expect-error - intentionally testing non-existent keys
const result = pick(obj, ['d', 'e']);
expect(result).toEqual({});
});

it('returns an empty object when the input object is empty', () => {
const obj = {};
// @ts-expect-error - intentionally testing non-existent keys
const result = pick(obj, ['a', 'b']);
expect(result).toEqual({});
});

it('returns an empty object when the keys array is empty', () => {
const obj = { a: 1, b: 2, c: 3 };
const result = pick(obj, []);
expect(result).toEqual({});
});

it('returns an empty object when the key string is empty', () => {
const obj = { a: 1, b: 2, c: 3 };
// @ts-expect-error - intentionally testing non-existent keys
const result = pick(obj, '');
expect(result).toEqual({});
});

it('returns an object with only the specified key when the key is present multiple times', () => {
// @ts-expect-error - intentionally testing duplicate keys
const obj = { a: 1, b: 2, a: 3 };
const result = pick(obj, 'a');
expect(result).toEqual({ a: 3 });
});

it('returns an object with only the specified keys when the keys are present multiple times', () => {
// @ts-expect-error - intentionally testing duplicate keys
const obj = { a: 1, b: 2, a: 3, c: 4, c: 5 };
const result = pick(obj, ['a', 'c']);
expect(result).toEqual({ a: 3, c: 5 });
});
});
33 changes: 33 additions & 0 deletions libs/util-fn/src/pick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export function pick<Obj extends object, Keys extends keyof Obj>(
obj: Obj,
keys: Keys
): Pick<Obj, Keys>;

export function pick<Obj extends object, Keys extends Array<keyof Obj>>(
obj: Obj,
keys: Keys
): Pick<Obj, Keys[number]>;

export function pick<
Obj extends object,
Keys extends keyof Obj | Array<keyof Obj>
>(
obj: Obj,
keys: Keys
): Pick<Obj, Keys extends Array<keyof Obj> ? Keys[number] : Keys> {
const result: any = {};

for (const key of Object.keys(obj) as Array<keyof Obj>) {
if (Array.isArray(keys)) {
if (keys.includes(key)) {
result[key] = obj[key];
}
} else {
if (keys === key) {
result[key] = obj[key];
}
}
}

return result;
}

0 comments on commit 1463a3f

Please sign in to comment.