Skip to content

Commit ee90c24

Browse files
authored
Merge pull request #61 from lambdalisue/fix-isobjectof
🐛 Fix `isObjectOf` for non pure object predication
2 parents bd9a951 + 14a83f7 commit ee90c24

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

is/factory.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { FlatType } from "../_typeutil.ts";
22
import type { Predicate, PredicateType } from "./type.ts";
3-
import { isOptional, isOptionalOf, isReadonlyOf } from "./annotation.ts";
3+
import { isOptionalOf, isReadonlyOf } from "./annotation.ts";
44
import {
55
isAny,
66
isArray,
@@ -506,18 +506,12 @@ export function isObjectOf<
506506
// deno-lint-ignore no-explicit-any
507507
return isStrictOf(isObjectOf(predObj)) as any;
508508
}
509-
const requiredKeys = Object.entries(predObj)
510-
.filter(([_, v]) => !isWithOptional(v))
511-
.map(([k]) => k);
512509
return setPredicateFactoryMetadata(
513510
(x: unknown): x is ObjectOf<T> => {
514-
if (!isRecord(x)) return false;
515-
// Check required keys
516-
const s = new Set(Object.keys(x));
517-
if (requiredKeys.some((k) => !s.has(k))) return false;
511+
if (x == null || typeof x !== "object") return false;
518512
// Check each values
519513
for (const k in predObj) {
520-
if (!predObj[k](x[k])) return false;
514+
if (!predObj[k]((x as T)[k])) return false;
521515
}
522516
return true;
523517
},
@@ -529,13 +523,6 @@ type WithOptional =
529523
| WithMetadata<GetMetadata<ReturnType<typeof isOptionalOf>>>
530524
| { optional: true }; // For backward compatibility
531525

532-
function isWithOptional<T extends Predicate<unknown>>(
533-
pred: T,
534-
): pred is T & WithOptional {
535-
// deno-lint-ignore no-explicit-any
536-
return isOptional(pred) || (pred as any).optional === true;
537-
}
538-
539526
type ObjectOf<T extends Record<PropertyKey, Predicate<unknown>>> = FlatType<
540527
// Non optional
541528
& {

is/factory_test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import { assertType } from "https://deno.land/[email protected]/testing/types.ts";
99
import { type Equal, stringify } from "./_testutil.ts";
1010
import { type Predicate } from "./type.ts";
1111
import { isOptionalOf } from "./annotation.ts";
12-
import { isArray, isBoolean, isNumber, isString, isUndefined } from "./core.ts";
12+
import {
13+
isArray,
14+
isBoolean,
15+
isFunction,
16+
isNumber,
17+
isString,
18+
isUndefined,
19+
} from "./core.ts";
1320
import is, {
1421
isArrayOf,
1522
isInstanceOf,
@@ -612,6 +619,13 @@ Deno.test("isObjectOf<T>", async (t) => {
612619
"Specify `{ strict: true }` and object have an unknown property",
613620
);
614621
});
622+
await t.step("returns true on T instance", () => {
623+
const date = new Date();
624+
const predObj = {
625+
getFullYear: isFunction,
626+
};
627+
assertEquals(isObjectOf(predObj)(date), true, "Value is not an object");
628+
});
615629
await testWithExamples(
616630
t,
617631
isObjectOf({ a: (_: unknown): _ is unknown => false }),

0 commit comments

Comments
 (0)