Skip to content

feat[isRecordObjectOf]: checks own symbol key properties #110

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 2 commits into from
Aug 10, 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
4 changes: 4 additions & 0 deletions is/__snapshots__/record_object_of_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ snapshot[`isRecordObjectOf<T> > returns properly named predicate function 2`] =
snapshot[`isRecordObjectOf<T, K> > returns properly named predicate function 1`] = `"isRecordObjectOf(isNumber, isString)"`;
snapshot[`isRecordObjectOf<T, K> > returns properly named predicate function 2`] = `"isRecordObjectOf((anonymous), isString)"`;
snapshot[`isRecordObjectOf<T, K> > with symbol properties > returns properly named predicate function 1`] = `"isRecordObjectOf(isNumber, isSymbol)"`;
snapshot[`isRecordObjectOf<T, K> > with symbol properties > returns properly named predicate function 2`] = `"isRecordObjectOf((anonymous), isSymbol)"`;
6 changes: 5 additions & 1 deletion is/record_object_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export function isRecordObjectOf<T, K extends PropertyKey = PropertyKey>(
return rewriteName(
(x: unknown): x is Record<K, T> => {
if (!isRecordObject(x)) return false;
for (const k in x) {
const keys = [
...Object.keys(x),
...Object.getOwnPropertySymbols(x),
];
for (const k of keys) {
if (!pred(x[k])) return false;
if (predKey && !predKey(k)) return false;
}
Expand Down
121 changes: 121 additions & 0 deletions is/record_object_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@ Deno.test("isRecordObjectOf<T>", async (t) => {
assertType<Equal<typeof a, Record<PropertyKey, number>>>(true);
}
});

await t.step("checks only object's own properties", async (t) => {
await t.step("returns true on T record", () => {
assertEquals(
isRecordObjectOf(is.Number)(
Object.assign(Object.create({ p: "ignore" }), { a: 0 }),
),
true,
);
assertEquals(
isRecordObjectOf(is.String)(
Object.assign(Object.create({ p: 0 /* ignore */ }), { a: "a" }),
),
true,
);
assertEquals(
isRecordObjectOf(is.Boolean)(
Object.assign(Object.create({ p: "ignore" }), { a: true }),
),
true,
);
});
});

await t.step("with symbol properties", async (t) => {
const a = Symbol("a");
await t.step("returns true on T record", () => {
assertEquals(isRecordObjectOf(is.Number)({ [a]: 0 }), true);
assertEquals(isRecordObjectOf(is.String)({ [a]: "a" }), true);
assertEquals(isRecordObjectOf(is.Boolean)({ [a]: true }), true);
});

await t.step("returns false on non T record", () => {
assertEquals(isRecordObjectOf(is.String)({ [a]: 0 }), false);
assertEquals(isRecordObjectOf(is.Number)({ [a]: "a" }), false);
assertEquals(isRecordObjectOf(is.String)({ [a]: true }), false);
});
});
});

Deno.test("isRecordObjectOf<T, K>", async (t) => {
Expand Down Expand Up @@ -64,4 +102,87 @@ Deno.test("isRecordObjectOf<T, K>", async (t) => {
assertType<Equal<typeof a, Record<string, number>>>(true);
}
});

await t.step("checks only object's own properties", async (t) => {
const s = Symbol("s");
await t.step("returns true on T record", () => {
assertEquals(
isRecordObjectOf(is.Number, is.String)(
Object.assign(Object.create({ p: "ignore" }), { a: 0 }),
),
true,
);
assertEquals(
isRecordObjectOf(is.String, is.String)(
Object.assign(Object.create({ p: 0 /* ignore */ }), { a: "a" }),
),
true,
);
assertEquals(
isRecordObjectOf(is.Boolean, is.String)(
Object.assign(Object.create({ p: "ignore" }), { a: true }),
),
true,
);
assertEquals(
isRecordObjectOf(is.String, is.Number)(
Object.assign(Object.create({ p: "ignore" }), {/* empty */}),
),
true,
"No own properties",
);
assertEquals(
isRecordObjectOf(is.String, is.String)(
Object.assign(Object.create({ [s]: "ignore" }), {/* empty */}),
),
true,
"No own properties",
);
});
});

await t.step("with symbol properties", async (t) => {
const a = Symbol("a");
await t.step("returns properly named predicate function", async (t) => {
await assertSnapshot(t, isRecordObjectOf(is.Number, is.Symbol).name);
await assertSnapshot(
t,
isRecordObjectOf((_x): _x is string => false, is.Symbol).name,
);
});

await t.step("returns true on T record", () => {
assertEquals(isRecordObjectOf(is.Number, is.Symbol)({ [a]: 0 }), true);
assertEquals(isRecordObjectOf(is.String, is.Symbol)({ [a]: "a" }), true);
assertEquals(
isRecordObjectOf(is.Boolean, is.Symbol)({ [a]: true }),
true,
);
});

await t.step("returns false on non T record", () => {
assertEquals(isRecordObjectOf(is.String, is.Symbol)({ [a]: 0 }), false);
assertEquals(isRecordObjectOf(is.Number, is.Symbol)({ [a]: "a" }), false);
assertEquals(
isRecordObjectOf(is.String, is.Symbol)({ [a]: true }),
false,
);
});

await t.step("returns false on non K record", () => {
assertEquals(isRecordObjectOf(is.Number, is.String)({ [a]: 0 }), false);
assertEquals(isRecordObjectOf(is.String, is.String)({ [a]: "a" }), false);
assertEquals(
isRecordObjectOf(is.Boolean, is.String)({ [a]: true }),
false,
);
});

await t.step("predicated type is correct", () => {
const a: unknown = { a: 0 };
if (isRecordObjectOf(is.Number, is.Symbol)(a)) {
assertType<Equal<typeof a, Record<symbol, number>>>(true);
}
});
});
});