Skip to content

Commit

Permalink
refactor[isObjectOf]: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Milly committed Aug 11, 2024
1 parent 05cebb7 commit 64ff84c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions is/object_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import type { Predicate } from "../type.ts";
export function isObjectOf<
T extends Record<PropertyKey, Predicate<unknown>>,
>(predObj: T): Predicate<ObjectOf<T>> & IsPredObj<T> {
const preds: readonly [key: PropertyKey, pred: Predicate<unknown>][] = [
...Object.keys(predObj),
...Object.getOwnPropertySymbols(predObj),
].map((k) => [k, predObj[k]]);
return annotate(
rewriteName(
(x: unknown): x is ObjectOf<T> => {
Expand All @@ -47,11 +51,7 @@ export function isObjectOf<
typeof x !== "object" && typeof x !== "function" ||
Array.isArray(x)
) return false;
// Check each values
return [
...Object.keys(predObj),
...Object.getOwnPropertySymbols(predObj),
].every((k) => predObj[k]((x as T)[k]));
return preds.every(([k, pred]) => pred((x as T)[k]));
},
"isObjectOf",
predObj,
Expand Down

0 comments on commit 64ff84c

Please sign in to comment.