Created
May 21, 2020 05:22
-
-
Save snakeneedy/04f6ce92d358b61207d2ad2b42f072c7 to your computer and use it in GitHub Desktop.
Type utility in JS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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