## Type

### getType

Returns the native type of a value.

Returns lowercased constructor name of value, `"undefined"` or `"null"` if value is `undefined` or `null`.

    const getType = v =>
      v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();

Show examples

    getType(new Set([1, 2, 3])); // 'set'

### is

Checks if the provided value is of the specified type.

Ensure the value is not `undefined` or `null` using `Array.includes()`, and compare the `constructor` property on the value with `type` to check if the provided value is of the specified `type`.

    const is = (type, val) => ![, null].includes(val) && val.constructor === type;

Show examples

    is(Array, [1]); // true
    is(ArrayBuffer, new ArrayBuffer()); // true
    is(Map, new Map()); // true
    is(RegExp, /./g); // true
    is(Set, new Set()); // true
    is(WeakMap, new WeakMap()); // true
    is(WeakSet, new WeakSet()); // true
    is(String, ''); // true
    is(String, new String('')); // true
    is(Number, 1); // true
    is(Number, new Number(1)); // true
    is(Boolean, true); // true
    is(Boolean, new Boolean(true)); // true

### isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use the spread operator (`...`) to check if the provided argument is iterable inside a `try... catch` block and the comma operator (`,`) to return the appropriate value.

    const isArrayLike = val => {
      try {
        return [...val], true;
      } catch (e) {
        return false;
      }
    };

Show examples

    isArrayLike(document.querySelectorAll('.className')); // true
    isArrayLike('abc'); // true
    isArrayLike(null); // false

### isBoolean

Checks if the given argument is a native boolean element.

Use `typeof` to check if a value is classified as a boolean primitive.

    const isBoolean = val => typeof val === 'boolean';

Show examples

    isBoolean(null); // false
    isBoolean(false); // true

### isEmpty

Returns true if the a value is an empty object, collection, map or set, has no enumerable properties or is any type that is not considered a collection.

Check if the provided value is `null` or if its `length` is equal to `0`.

    const isEmpty = val => val == null || !(Object.keys(val) || val).length;

Show examples

    isEmpty(new Map()); // true
    isEmpty(new Set()); // true
    isEmpty([]); // true
    isEmpty({}); // true
    isEmpty(''); // true
    isEmpty([1, 2]); // false
    isEmpty({ a: 1, b: 2 }); // false
    isEmpty('text'); // false
    isEmpty(123); // true - type is not considered a collection
    isEmpty(true); // true - type is not considered a collection

### isFunction

Checks if the given argument is a function.

Use `typeof` to check if a value is classified as a function primitive.

    const isFunction = val => typeof val === 'function';

Show examples

    isFunction('x'); // false
    isFunction(x => x); // true

### isNil

Returns `true` if the specified value is `null` or `undefined`, `false` otherwise.

Use the strict equality operator to check if the value and of `val` are equal to `null` or `undefined`.

    const isNil = val => val === undefined || val === null;

Show examples

    isNil(null); // true
    isNil(undefined); // true

### isNull

Returns `true` if the specified value is `null`, `false` otherwise.

Use the strict equality operator to check if the value and of `val` are equal to `null`.

    const isNull = val => val === null;

Show examples

    isNull(null); // true

### isNumber

Checks if the given argument is a number.

Use `typeof` to check if a value is classified as a number primitive.

    const isNumber = val => typeof val === 'number';

Show examples

    isNumber('1'); // false
    isNumber(1); // true

### isObject

Returns a boolean determining if the passed value is an object or not.

Uses the `Object` constructor to create an object wrapper for the given value. If the value is `null` or `undefined`, create and return an empty object. Οtherwise, return an object of a type that corresponds to the given value.

    const isObject = obj => obj === Object(obj);

Show examples

    isObject([1, 2, 3, 4]); // true
    isObject([]); // true
    isObject(['Hello!']); // true
    isObject({ a: 1 }); // true
    isObject({}); // true
    isObject(true); // false

### isObjectLike

Checks if a value is object-like.

Check if the provided value is not `null` and its `typeof` is equal to `'object'`.

    const isObjectLike = val => val !== null && typeof val === 'object';

Show examples

    isObjectLike({}); // true
    isObjectLike([1, 2, 3]); // true
    isObjectLike(x => x); // false
    isObjectLike(null); // false

### isPlainObject

Checks if the provided value is an object created by the Object constructor.

Check if the provided value is truthy, use `typeof` to check if it is an object and `Object.constructor` to make sure the constructor is equal to `Object`.

    const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object;

Show examples

    isPlainObject({ a: 1 }); // true
    isPlainObject(new Map()); // false

### isPrimitive

Returns a boolean determining if the passed value is primitive or not.

Use `Array.includes()` on an array of type strings which are not primitive, supplying the type using `typeof`. Since `typeof null` evaluates to `'object'`, it needs to be directly compared.

    const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;

Show examples

    isPrimitive(null); // true
    isPrimitive(50); // true
    isPrimitive('Hello!'); // true
    isPrimitive(false); // true
    isPrimitive(Symbol()); // true
    isPrimitive([]); // false

### isPromiseLike

Returns `true` if an object looks like a `Promise`, `false` otherwise.

Check if the object is not `null`, its `typeof` matches either `object` or `function` and if it has a `.then` property, which is also a `function`.

    const isPromiseLike = obj =>
      obj !== null &&
      (typeof obj === 'object' || typeof obj === 'function') &&
      typeof obj.then === 'function';

Show examples

    isPromiseLike({
      then: function() {
        return '';
      }
    }); // true
    isPromiseLike(null); // false
    isPromiseLike({}); // false

### isString

Checks if the given argument is a string.

Use `typeof` to check if a value is classified as a string primitive.

    const isString = val => typeof val === 'string';

Show examples

    isString('10'); // true

### isSymbol

Checks if the given argument is a symbol.

Use `typeof` to check if a value is classified as a symbol primitive.

    const isSymbol = val => typeof val === 'symbol';

Show examples

    isSymbol(Symbol('x')); // true

### isUndefined

Returns `true` if the specified value is `undefined`, `false` otherwise.

Use the strict equality operator to check if the value and of `val` are equal to `undefined`.

    const isUndefined = val => val === undefined;

Show examples

    isUndefined(undefined); // true

### isValidJSON

Checks if the provided argument is a valid JSON.

Use `JSON.parse()` and a `try... catch` block to check if the provided argument is a valid JSON.

    const isValidJSON = obj => {
      try {
        JSON.parse(obj);
        return true;
      } catch (e) {
        return false;
      }
    };

Show examples

    isValidJSON('{"name":"Adam","age":20}'); // true
    isValidJSON('{"name":"Adam",age:"20"}'); // false
    isValidJSON(null); // true