types

types

Source:
types.js, line 31

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>
Source:
types.js, line 441

(static, constant) NULL :string

Programmatic constant defintion of the result of a call to
typeOf(null).

Type:
  • string
Source:
types.js, line 396

(static, constant) UNDEFINED :string

Programmatic constant defintion of the result of a call to
typeOf(undefined).

Type:
  • string
Source:
types.js, line 386

(inner) PRIMITIVES :Set.<String>

Create a base set containing the typeOf representations for each of the
known primitive types.

Type:
  • Set.<String>
Source:
types.js, line 406

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
runtime check for the type of the supplied Class objects will be made. If
either is not a Function, an error is thrown.

Returns:
( Boolean )

true if the lineage exists; false otherwise

Source:
types.js, line 297
See:
types#isClass

(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

Source:
types.js, line 77

(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

Source:
types.js, line 91

(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

Source:
types.js, line 63

(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

Source:
types.js, line 161

(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

Source:
types.js, line 133

(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

Source:
types.js, line 105

(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

Source:
types.js, line 220

(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.

Source:
types.js, line 189

(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

Source:
types.js, line 147

(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

Source:
types.js, line 119

(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

Source:
types.js, line 175

(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

Source:
types.js, line 205

(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 new
Symbol.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
undefined

Returns:
( string )

for objects of type [object String] the value "String"
will be returned.

Source:
types.js, line 34