types
types
Members
(static, constant) exports.PRIMITIVES :Set.<string>
When testing if a type is a primitive, it is often easier to simply verify
that with a list of known types. To make this dead simple, a modified Set
containing the typeOf
results for each of the six known JavaScript
primitive types is exported.
The modifications are such that a call to has()
, on this Set only, first
converts the supplied values to their resulting typeOf()
representations.
So, PRIMITIVES.has(4)
would be the same as PRIMITIVES.has('Number')
.
Type:
- Set.<string>
(static, constant) NULL :string
Programmatic constant defintion of the result of a call totypeOf(null)
.
Type:
- string
(static, constant) UNDEFINED :string
Programmatic constant defintion of the result of a call totypeOf(undefined)
.
Type:
- string
(inner) PRIMITIVES :Set.<String>
Create a base set containing the typeOf representations for each of the
known primitive types.
Type:
- Set.<String>
Methods
(inner) ⌾⠀extendsFrom(TestedClass, RootClass, enforceClasses) → {Boolean}
NOTE This function will not work on nodejs versions less than 6 as Reflect
is needed natively.
The instanceof keyword only works on instances of an object and not on
the class objects the instances are created from.
class A {}
class B extends A {}
let a = new A();
let b = new B();
b instanceof A; // true
a instanceof A; // true
B instanceof A; // false
Therefore the extendsFrom function checks this relationship at the class
level and not at the instance level.
import { extendsFrom } from '...'
class A {}
class B extends A {}
class C extends B {}
extendsFrom(A, A); // true
extendsFrom(B, A); // true
extendsFrom(C, A); // true
extendsFrom(C, 1); // false
extendsFrom(B, null); // false
Parameters:
Name | Type | Description |
---|---|---|
TestedClass |
function | the class of which to test heredity |
RootClass |
function | the ancestor to test for |
enforceClasses |
Boolean | if true, false by default, an additional |
Returns:
(
Boolean
)
true if the lineage exists; false otherwise
(inner) ⌾⠀isArray(obj) → {Boolean}
Returns true if the type supplied evaluates to [object Array]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isClass(obj) → {boolean}
Returns true if the supplied obj is a ECMAScript class definition. It first
checks by examining the properties of the supplied class. Secondly it checks
by searching the toString() method of the 'function' for the term class. If
either are true, then true is returned; false is returned otherwise.
NOTE Relying on this strictly, especially when used with other libraries
can cause some problems down the line, especially if the code wraps a class
instance like react-jss or other similar use cases. Use at your own peril.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | any object who's type is to be compared as a class |
Returns:
(
boolean
)
true if the obj is an ECMAScript class object; not an
instance. False otherwise.
- Source:
- types.js, line 236
- See:
- #isNativeClassByProps
- #isNativeClassByString
(inner) ⌾⠀isDate(obj) → {Boolean}
Returns true if the type supplied evaluates to [object Date]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isFunction(obj) → {Boolean}
Returns true if the type supplied evaluates to [object Function]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isNativeClassByProps(thing) → {boolean}
isNativeClass method taken from code submitted on stackoverflow. Logic and
basis for the test appears there. See URL below for follow up if desired.
Parameters:
Name | Type | Description |
---|---|---|
thing |
mixed | any type of JavaScript value to test |
Returns:
(
boolean
)
true if it is a ECMAScript class by testing properties;
false otherwise
- Source:
- types.js, line 261
- See:
- https://stackoverflow.com/questions/29093396/how-do-you-check-the-difference-between-an-ecmascript-6-class-and-function#32235645
(inner) ⌾⠀isNativeClassByString(thing) → {Boolean}
isNativeClass method taken from code submitted on stackoverflow. Logic and
basis for the test appears there. See URL below for follow up if desired.
Parameters:
Name | Type | Description |
---|---|---|
thing |
mixed | any type of JavaScript value to test |
Returns:
(
Boolean
)
true if it is a ECMAScript class by testing properties;
false otherwise
- Source:
- types.js, line 279
- See:
- https://stackoverflow.com/questions/29093396/how-do-you-check-the-difference-between-an-ecmascript-6-class-and-function#32235645
(inner) ⌾⠀isNull(obj) → {Boolean}
Returns true if the type supplied evaluates to [object Null]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isNumber(obj) → {Boolean}
Returns true if the type supplied evaluates to [object Number]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isObject(obj) → {Boolean}
Returns true if the type supplied evaluates to [object Object]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isOfType(obj) → {Boolean}
A shorthand way to test an object's declared toString type to a supplied
string or Function/Class. Realistically, this checks typeOf(obj) to both
T and T.name. If either are true, then true is returned; false otherwise.
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isPrimitive() → {Boolean}
Determines if the resulting type is one of the six types of primitives
(according to MDN; https://goo.gl/USmkUU). If it is, true will be returned;
otherwise false.
Returns:
(
Boolean
)
true if not one of Boolean, Null, Undefined, Number,
String or Symbol.
(inner) ⌾⠀isRegExp(obj) → {Boolean}
Returns true if the type supplied evaluates to [object RegExp]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isString(obj) → {Boolean}
Returns true if the type supplied evaluates to [object String]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isUndefined(obj) → {Boolean}
Returns true if the type supplied evaluates to [object Undefined]
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀isValue(obj) → {Boolean}
Returns true if the type supplied evaluates to neither [object Object]
nor [object Array]
.
Parameters:
Name | Type | Description |
---|---|---|
obj |
any | any object that can be passed to Object.prototype.toString |
Returns:
(
Boolean
)
true if it passes the test, false otherwise
(inner) ⌾⠀typeOf(object) → {string}
One common way to determine the type of class that you are working with,
in a fairly compatible manner, is to use .call or .apply on the function
toString of the Object.prototype.
Calling Object.prototype.toString.call('hello')
will yield"[object String]"
as an answer. This technique is fairly sound but is
also fairly verbose to use often. This function extracts the detected value
name from the above string; so "String" from "[object String]" and so forth.
The added advantage of using this method is that it works well with direct
name comparisons, such as typeOf("asdfas") === String.name
. The newSymbol.toStringTag
allows you to define custom values that are
reflected in this manner.
Parameters:
Name | Type | Description |
---|---|---|
object |
any | any value is acceptable here, including null and |
Returns:
(
string
)
for objects of type [object String] the value "String"
will be returned.