Skip to content

feat[isStrictOf]: discover symbol properties in pred.predObj #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions is/__snapshots__/object_of_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ snapshot[`isObjectOf<T> > returns properly named predicate function 3`] = `
})
})"
`;

snapshot[`isObjectOf<T> > with symbol properties > returns properly named predicate function 1`] = `
"isObjectOf({
a: isNumber,
b: isString,
Symbol(s): isBoolean
})"
`;

snapshot[`isObjectOf<T> > with symbol properties > returns properly named predicate function 2`] = `
"isObjectOf({
Symbol(a): isObjectOf({
Symbol(b): isObjectOf({Symbol(c): isBoolean})
})
})"
`;
16 changes: 16 additions & 0 deletions is/__snapshots__/strict_of_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ snapshot[`isStrictOf<T> > returns properly named predicate function 3`] = `
}))
}))"
`;

snapshot[`isStrictOf<T> > with symbol properties > returns properly named predicate function 1`] = `
"isStrictOf(isObjectOf({
a: isNumber,
Symbol(b): isString,
Symbol(c): isBoolean
}))"
`;

snapshot[`isStrictOf<T> > with symbol properties > returns properly named predicate function 2`] = `
"isStrictOf(isObjectOf({
a: isStrictOf(isObjectOf({
Symbol(b): isStrictOf(isObjectOf({Symbol(c): isBoolean}))
}))
}))"
`;
5 changes: 4 additions & 1 deletion is/object_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export function isObjectOf<
Array.isArray(x)
) return false;
// Check each values
return Object.keys(predObj).every((k) => predObj[k]((x as T)[k]));
return [
...Object.keys(predObj),
...Object.getOwnPropertySymbols(predObj),
].every((k) => predObj[k]((x as T)[k]));
},
"isObjectOf",
predObj,
Expand Down
116 changes: 116 additions & 0 deletions is/object_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,120 @@ Deno.test("isObjectOf<T>", async (t) => {
});
},
);

await t.step("with symbol properties", async (t) => {
const s = Symbol("s");
const predObj = {
a: is.Number,
b: is.String,
[s]: is.Boolean,
};

await t.step("returns properly named predicate function", async (t) => {
await assertSnapshot(t, isObjectOf(predObj).name);
await assertSnapshot(
t,
isObjectOf({
[Symbol("a")]: isObjectOf({
[Symbol("b")]: isObjectOf({ [Symbol("c")]: is.Boolean }),
}),
}).name,
);
});

await t.step("returns true on T object", () => {
assertEquals(isObjectOf(predObj)({ a: 0, b: "a", [s]: true }), true);
assertEquals(
isObjectOf(predObj)({ a: 0, b: "a", [s]: true, d: "ignored" }),
true,
"Undefined properties are ignored",
);
assertEquals(
isObjectOf(predObj)({
a: 0,
b: "a",
[s]: true,
[Symbol("t")]: "ignored",
}),
true,
"Undefined symbol properties are ignored",
);
assertEquals(
isObjectOf(predObj)(
Object.assign(() => void 0, { a: 0, b: "a", [s]: true }),
),
true,
"Function are treated as an object",
);
});

await t.step("returns false on non T object", () => {
assertEquals(isObjectOf(predObj)("a"), false, "Value is not an object");
assertEquals(
isObjectOf(predObj)({ a: 0, b: "a", [s]: "" }),
false,
"Object have a different type symbol property",
);
assertEquals(
isObjectOf(predObj)({ a: 0, b: "a" }),
false,
"Object does not have symbol property",
);
const arrayWithSymbolProp = ["ignored"];
// deno-lint-ignore no-explicit-any
(arrayWithSymbolProp as any)[s] = true;
assertEquals(
isObjectOf({ [s]: is.Boolean })(arrayWithSymbolProp),
false,
"Value is not an object",
);
});

await t.step("predicated type is correct", () => {
const a = Symbol("a");
const b = Symbol("b");
const c = Symbol("c");
const d = Symbol("d");
const e = Symbol("e");
const f = Symbol("f");
const predObj2 = {
[a]: as.Readonly(as.Optional(is.String)),
[b]: as.Optional(as.Readonly(is.String)),
[c]: as.Readonly(is.String),
[d]: as.Optional(is.String),
[e]: as.Unreadonly(as.Unoptional(as.Readonly(as.Optional(is.String)))),
[f]: as.Unoptional(as.Unreadonly(as.Optional(as.Readonly(is.String)))),
};
const x: unknown = {};

if (isObjectOf(predObj)(x)) {
assertType<
Equal<
typeof x,
{
a: number;
b: string;
[s]: boolean;
}
>
>(true);
}

if (isObjectOf(predObj2)(x)) {
assertType<
Equal<
typeof x,
{
readonly [a]?: string;
readonly [b]?: string;
readonly [c]: string;
[d]?: string;
[e]: string;
[f]: string;
}
>
>(true);
}
});
});
});
11 changes: 7 additions & 4 deletions is/strict_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ export function isStrictOf<
):
& Predicate<T>
& IsPredObj<P> {
const s = new Set(Object.keys(pred.predObj));
const s = new Set(getKeys(pred.predObj));
return rewriteName(
(x: unknown): x is T => {
if (!pred(x)) return false;
// deno-lint-ignore no-explicit-any
const ks = Object.keys(x as any);
return ks.length <= s.size && ks.every((k) => s.has(k));
const ks = new Set(getKeys(x));
return ks.difference(s).size === 0;
},
"isStrictOf",
pred,
) as Predicate<T> & IsPredObj<P>;
}

function getKeys(o: Record<PropertyKey, unknown>) {
return [...Object.keys(o), ...Object.getOwnPropertySymbols(o)];
}
108 changes: 108 additions & 0 deletions is/strict_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,112 @@ Deno.test("isStrictOf<T>", async (t) => {
}
});
});

await t.step("with symbol properties", async (t) => {
const b = Symbol("b");
const c = Symbol("c");
const predObj = {
a: is.Number,
[b]: is.UnionOf([is.String, is.Undefined]),
[c]: as.Optional(is.Boolean),
};
await t.step("returns properly named predicate function", async (t) => {
await assertSnapshot(
t,
isStrictOf(
is.ObjectOf({ a: is.Number, [b]: is.String, [c]: is.Boolean }),
)
.name,
);
await assertSnapshot(
t,
isStrictOf(
is.ObjectOf({
a: isStrictOf(
is.ObjectOf({
[b]: isStrictOf(is.ObjectOf({ [c]: is.Boolean })),
}),
),
}),
).name,
);
});

await t.step("returns true on T object", () => {
assertEquals(
isStrictOf(is.ObjectOf(predObj))({ a: 0, [b]: "a", [c]: true }),
true,
);
assertEquals(
isStrictOf(is.ObjectOf(predObj))({ a: 0, [b]: "a" }),
true,
"Object does not have an optional property",
);
assertEquals(
isStrictOf(is.ObjectOf(predObj))({ a: 0, [b]: "a", [c]: undefined }),
true,
"Object has `undefined` as value of optional property",
);
});

await t.step("returns false on non T object", () => {
assertEquals(
isStrictOf(is.ObjectOf(predObj))({ a: 0, [b]: "a", [c]: "" }),
false,
"Object have a different type property",
);
assertEquals(
isStrictOf(is.ObjectOf(predObj))({ a: 0, [b]: "a", [c]: null }),
false,
"Object has `null` as value of optional property",
);
assertEquals(
isStrictOf(is.ObjectOf(predObj))({
a: 0,
[b]: "a",
[c]: true,
d: "invalid",
}),
false,
"Object have an unknown property",
);
assertEquals(
isStrictOf(is.ObjectOf(predObj))({
a: 0,
[b]: "a",
[c]: true,
[Symbol("d")]: "invalid",
}),
false,
"Object have an unknown symbol property",
);
assertEquals(
isStrictOf(is.ObjectOf(predObj))({
a: 0,
[b]: "a",
d: "invalid",
}),
false,
"Object have the same number of properties but an unknown property exists",
);
assertEquals(
isStrictOf(is.ObjectOf(predObj))({
a: 0,
[b]: "a",
[Symbol("d")]: "invalid",
}),
false,
"Object have the same number of properties but an unknown symbol property exists",
);
});

await t.step("predicated type is correct", () => {
const a: unknown = { a: 0, [b]: "a" };
if (isStrictOf(is.ObjectOf(predObj))(a)) {
assertType<
Equal<typeof a, { a: number; [b]: string | undefined; [c]?: boolean }>
>(true);
}
});
});
});