Skip to content

Commit 0e342bb

Browse files
committed
add all and any methods
1 parent 5efe6ec commit 0e342bb

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

isType.mjs.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,39 @@ const TYPES = {
2323
weakset: { class: WeakSet },
2424
};
2525

26+
const TYPE_TEST_PROPERTY_NAMES = [
27+
"array",
28+
"bigint",
29+
"boolean",
30+
"date",
31+
"defined",
32+
"empty",
33+
"error",
34+
"falsy",
35+
"function",
36+
"infinite",
37+
"map",
38+
"nan",
39+
"nonempty",
40+
"null",
41+
"nullish",
42+
"number",
43+
"numberish",
44+
"object",
45+
"objectish",
46+
"primitive",
47+
"promise",
48+
"real",
49+
"regex",
50+
"set",
51+
"string",
52+
"symbol",
53+
"truthy",
54+
"undefined",
55+
"weakmap",
56+
"weakset"
57+
];
58+
2659
/**
2760
* A collection of boolean properties indicating the type of the given value.
2861
* @class TypeTest
@@ -103,10 +136,21 @@ class TypeTest {
103136
/**
104137
* Determine the type of a value. The returned object includes boolean properties to quickly test against specific types or for specific states (e.g., 'empty').
105138
* @param {*} value - The value to be tested.
106-
* @returns {TypeTest}
139+
* @returns {TypeTest} - Plus methods `all` and `any`.
107140
*/
108141
function is(value){
109-
return new TypeTest(value);
142+
143+
const typeTest = new TypeTest(value);
144+
145+
const tester = (propName)=>typeTest[propName];
146+
typeTest.all = (...propNames)=>propNames.every(tester);
147+
typeTest.any = (...propNames)=>propNames.some(tester);
148+
149+
return typeTest;
110150
}
111151

152+
TYPE_TEST_PROPERTY_NAMES.forEach((propName)=>{
153+
is[propName] = propName;
154+
});
155+
112156
export { is as default };

0 commit comments

Comments
 (0)