Skip to content

Commit

Permalink
refactor ♻️: omitkeys and pickkeys functions.
Browse files Browse the repository at this point in the history
- Refactor the omitKeys function to use a generic type to represent the keys to omit.
- Refactor the pickKeys function to return the correct type.
  • Loading branch information
Ryan-Zayne committed Oct 9, 2024
1 parent 347b442 commit 9e42bec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .changeset/big-spies-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@zayne-labs/toolkit": patch
---

refactor ♻️: omitkeys and pickkeys functions.

- Refactor the omitKeys function to use a generic type to represent the keys to omit.
- Refactor the pickKeys function to return the correct type.
8 changes: 5 additions & 3 deletions src/core/omitKeys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { AnyObject } from "@/type-helpers";

type OmitKeys<TKeys extends keyof TObject, TObject extends AnyObject> = Omit<TObject, TKeys>;

export const omitKeys = <TObject extends AnyObject, const TOmitArray extends Array<keyof TObject>>(
initialObject: TObject,
keysToOmit: TOmitArray
Expand All @@ -14,7 +16,7 @@ export const omitKeys = <TObject extends AnyObject, const TOmitArray extends Arr
}
}

return updatedObject as Omit<TObject, TOmitArray[number]>;
return updatedObject as OmitKeys<TOmitArray[number], TObject>;
};

export const omitKeysWithReduce = <
Expand All @@ -34,7 +36,7 @@ export const omitKeysWithReduce = <
return accumulator;
}, {});

return updatedObject as Omit<TObject, TOmitArray[number]>;
return updatedObject as OmitKeys<TOmitArray[number], TObject>;
};

export const omitKeysWithFilter = <
Expand All @@ -52,5 +54,5 @@ export const omitKeysWithFilter = <

const updatedObject = Object.fromEntries(arrayFromFilteredObject);

return updatedObject as Omit<TObject, TOmitArray[number]>;
return updatedObject as OmitKeys<TOmitArray[number], TObject>;
};
8 changes: 5 additions & 3 deletions src/core/pickKeys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { AnyObject } from "@/type-helpers";

type PickKeys<TKeys extends keyof TObject, TObject extends AnyObject> = Pick<TObject, TKeys>;

export const pickKeys = <TObject extends AnyObject, const TPickArray extends Array<keyof TObject>>(
initialObject: TObject,
keysToPick: TPickArray
Expand All @@ -14,7 +16,7 @@ export const pickKeys = <TObject extends AnyObject, const TPickArray extends Arr
}
}

return updatedObject as Pick<TObject, TPickArray[number]>;
return updatedObject as PickKeys<TPickArray[number], TObject>;
};

export const pickKeysWithReduce = <
Expand All @@ -34,7 +36,7 @@ export const pickKeysWithReduce = <
return accumulator;
}, {});

return updatedObject as Pick<TObject, TPickArray[number]>;
return updatedObject as PickKeys<TPickArray[number], TObject>;
};

export const pickKeysWithFilter = <
Expand All @@ -50,5 +52,5 @@ export const pickKeysWithFilter = <

const updatedObject = Object.fromEntries(arrayFromFilteredObject);

return updatedObject as Pick<TObject, TPickArray[number]>;
return updatedObject as PickKeys<TPickArray[number], TObject>;
};

0 comments on commit 9e42bec

Please sign in to comment.