From da5912731fca297e8bc8f60474b0eb9ba39c2d37 Mon Sep 17 00:00:00 2001 From: Milly Date: Thu, 8 Aug 2024 16:44:42 +0900 Subject: [PATCH 1/3] test[isObjectOf]: remove invalid assert message --- is/object_of_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/is/object_of_test.ts b/is/object_of_test.ts index 7379377..374d587 100644 --- a/is/object_of_test.ts +++ b/is/object_of_test.ts @@ -64,7 +64,7 @@ Deno.test("isObjectOf", async (t) => { const predObj = { getFullYear: is.Function, }; - assertEquals(isObjectOf(predObj)(date), true, "Value is not an object"); + assertEquals(isObjectOf(predObj)(date), true); }); await t.step("predicated type is correct", () => { From e01ab011c9f07e1022dae76d49c92c4f9e6a6b9b Mon Sep 17 00:00:00 2001 From: Milly Date: Thu, 8 Aug 2024 17:00:55 +0900 Subject: [PATCH 2/3] fix[isObjectOf]: no discover prototype properties Fixes #92 --- is/object_of.ts | 5 +---- is/object_of_test.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/is/object_of.ts b/is/object_of.ts index 6a7633c..16defd6 100644 --- a/is/object_of.ts +++ b/is/object_of.ts @@ -48,10 +48,7 @@ export function isObjectOf< Array.isArray(x) ) return false; // Check each values - for (const k in predObj) { - if (!predObj[k]((x as T)[k])) return false; - } - return true; + return Object.keys(predObj).every((k) => predObj[k]((x as T)[k])); }, "isObjectOf", predObj, diff --git a/is/object_of_test.ts b/is/object_of_test.ts index 374d587..6efeb35 100644 --- a/is/object_of_test.ts +++ b/is/object_of_test.ts @@ -98,4 +98,46 @@ Deno.test("isObjectOf", async (t) => { >(true); } }); + + await t.step("if 'predObj' has prototype properties", async (t) => { + const prototypeObj = { + a: is.Number, + b: is.Boolean, + }; + // deno-lint-ignore ban-types + const predObj2 = Object.assign(Object.create(prototypeObj) as {}, { + c: is.String, + }); + + await t.step("returns true on T object that omits prototype", () => { + assertEquals(isObjectOf(predObj2)({ c: "a" }), true); + assertEquals( + isObjectOf(predObj2)({ c: "a", d: "ignored" }), + true, + ); + assertEquals( + isObjectOf(predObj2)(Object.assign(() => void 0, { c: "a" })), + true, + ); + }); + + await t.step("returns false on non T object that omits prototype", () => { + assertEquals(isObjectOf(predObj2)("a"), false, "Value is not an object"); + assertEquals( + isObjectOf(predObj2)({ a: 0, b: true, c: 1 }), + false, + "Object have a different type property", + ); + assertEquals( + isObjectOf(predObj2)({ a: 0, b: true }), + false, + "Object does not have one property", + ); + assertEquals( + isObjectOf({ 0: is.String })(["a"]), + false, + "Value is not an object", + ); + }); + }); }); From 0ec8074bbe9089b5b37e923af0a75f794f058a68 Mon Sep 17 00:00:00 2001 From: Milly Date: Thu, 8 Aug 2024 18:29:47 +0900 Subject: [PATCH 3/3] test[isObjectOf]: clarify target var name and assertion messages --- is/object_of_test.ts | 90 +++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/is/object_of_test.ts b/is/object_of_test.ts index 6efeb35..4aadc5c 100644 --- a/is/object_of_test.ts +++ b/is/object_of_test.ts @@ -31,12 +31,14 @@ Deno.test("isObjectOf", async (t) => { assertEquals( isObjectOf(predObj)({ a: 0, b: "a", c: true, d: "ignored" }), true, + "Undefined properties are ignored", ); assertEquals( isObjectOf(predObj)( Object.assign(() => void 0, { a: 0, b: "a", c: true }), ), true, + "Function are treated as an object", ); }); @@ -99,45 +101,55 @@ Deno.test("isObjectOf", async (t) => { } }); - await t.step("if 'predObj' has prototype properties", async (t) => { - const prototypeObj = { - a: is.Number, - b: is.Boolean, - }; - // deno-lint-ignore ban-types - const predObj2 = Object.assign(Object.create(prototypeObj) as {}, { - c: is.String, - }); + await t.step( + "does not affect prototype properties of 'predObj'", + async (t) => { + const prototypeObj = { + a: is.Number, + b: is.Boolean, + }; + // deno-lint-ignore ban-types + const predObj = Object.assign(Object.create(prototypeObj) as {}, { + c: is.String, + }); - await t.step("returns true on T object that omits prototype", () => { - assertEquals(isObjectOf(predObj2)({ c: "a" }), true); - assertEquals( - isObjectOf(predObj2)({ c: "a", d: "ignored" }), - true, - ); - assertEquals( - isObjectOf(predObj2)(Object.assign(() => void 0, { c: "a" })), - true, - ); - }); + await t.step("returns true on T object", () => { + assertEquals(isObjectOf(predObj)({ c: "a" }), true); + assertEquals( + isObjectOf(predObj)({ a: "ignored", b: "ignored", c: "a" }), + true, + "Predicates defined in the prototype are ignored", + ); + assertEquals( + isObjectOf(predObj)({ c: "a", d: "ignored" }), + true, + "Undefined properties are ignored", + ); + assertEquals( + isObjectOf(predObj)(Object.assign(() => void 0, { c: "a" })), + true, + "Function are treated as an object", + ); + }); - await t.step("returns false on non T object that omits prototype", () => { - assertEquals(isObjectOf(predObj2)("a"), false, "Value is not an object"); - assertEquals( - isObjectOf(predObj2)({ a: 0, b: true, c: 1 }), - false, - "Object have a different type property", - ); - assertEquals( - isObjectOf(predObj2)({ a: 0, b: true }), - false, - "Object does not have one property", - ); - assertEquals( - isObjectOf({ 0: is.String })(["a"]), - false, - "Value is not 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: true, c: 1 }), + false, + "Object have a different type property", + ); + assertEquals( + isObjectOf(predObj)({ a: 0, b: true }), + false, + "Object does not have one property", + ); + assertEquals( + isObjectOf({ 0: is.String })(["a"]), + false, + "Value is not an object", + ); + }); + }, + ); });