Skip to content

Instantly share code, notes, and snippets.

@snakeneedy
Created May 21, 2020 05:22
Show Gist options
  • Save snakeneedy/04f6ce92d358b61207d2ad2b42f072c7 to your computer and use it in GitHub Desktop.
Save snakeneedy/04f6ce92d358b61207d2ad2b42f072c7 to your computer and use it in GitHub Desktop.
Type utility in JS.
const getTypeName = (o) => {
if (o === undefined) return 'undefined';
if (o === null) return 'null';
return o.constructor.name;
};
const isUndefined = (o) => (o === undefined);
const isNull = (o) => (o === null);
const isString = (o) => (getTypeName(o) === 'String');
const isNumber = (o) => (getTypeName(o) === 'Number');
const isInteger = (o) => (Number.isInteger(o));
const isBoolean = (o) => (getTypeName(o) === 'Boolean');
const isArray = (o) => (Array.isArray(o));
const isPureObject = (o) => (getTypeName(o) === 'Object');
const isSymbol = (o) => (getTypeName(o) === 'Symbol');
export default {
getTypeName,
isUndefined,
isNull,
isString,
isNumber,
isInteger,
isBoolean,
isArray,
isPureObject,
isSymbol,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment