Skip to content

Commit 4eb87da

Browse files
committed
WIP on fewer-keystrokes
1 parent 0e342bb commit 4eb87da

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

isType.mjs.js

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

26-
const TYPE_TEST_PROPERTY_NAMES = [
26+
const PROPERTY_NAMES = [
2727
"array",
2828
"bigint",
2929
"boolean",
@@ -57,7 +57,7 @@ const TYPE_TEST_PROPERTY_NAMES = [
5757
];
5858

5959
/**
60-
* A collection of boolean properties indicating the type of the given value.
60+
* A collection of boolean properties set according to the type, and sometimes value, of the argument.
6161
* @class TypeTest
6262
*/
6363
class TypeTest {
@@ -115,6 +115,8 @@ class TypeTest {
115115
this.defined = !this.undefined;
116116
this.nullish = this.undefined || this.null;
117117

118+
this.false = value === false;
119+
this.true = value === true;
118120
this.falsy = !value;
119121
this.truthy = !this.falsy;
120122

@@ -133,23 +135,35 @@ class TypeTest {
133135
}
134136
}
135137

138+
class Is extends TypeTest {
139+
140+
constructor(value){
141+
142+
super(value);
143+
144+
this.type = this.toString();
145+
}
146+
147+
all(...propNames){
148+
return propNames.every(propName=>this[propName]);
149+
}
150+
151+
any(...propNames){
152+
return propNames.some(propName=>this[propName]);
153+
}
154+
}
155+
136156
/**
137157
* 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').
138158
* @param {*} value - The value to be tested.
139-
* @returns {TypeTest} - Plus methods `all` and `any`.
159+
* @returns {Is}
140160
*/
141161
function is(value){
142162

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;
163+
return new Is(value);
150164
}
151165

152-
TYPE_TEST_PROPERTY_NAMES.forEach((propName)=>{
166+
PROPERTY_NAMES.forEach((propName)=>{
153167
is[propName] = propName;
154168
});
155169

0 commit comments

Comments
 (0)