Created
February 19, 2019 14:42
-
-
Save chrisgfortes/f421848e0d7dbb15902229b9fd2edca5 to your computer and use it in GitHub Desktop.
Util functions from lodash in pure JS:: _.isObject, _.isString, _.isNumber, _.isFunction, _.isBoolean, _.isArray, _.isRegExp
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
/** | |
* util-types could be used to replace some methods of lodash such as: | |
* _.isObject | |
* _.isString | |
* _.isNumber | |
* _.isFunction | |
* _.isBoolean | |
* _.isArray | |
* _.isRegExp | |
*/ | |
/** | |
* getType receive a value and return the type of the value (e.g [] => Array || {} => Object) | |
* @param {any} value - Value that will be checked the type | |
* @return {string} return the type of value (e.g. String, Object, Array, ...) | |
*/ | |
export const getType = (value) => Object.prototype.toString.call(value).match(/^\[object\s(.*)\]$/)[1]; | |
/** | |
* isObject check the value for Object type | |
* @param {any} value - A value that will be checked if is an object | |
* @return {boolean} return true when the value is an object | |
*/ | |
export const isObject = (value) => getType(value) === 'Object'; | |
/** | |
* isString check the value for String type | |
* @param {any} value - A value that will be checked if is a String | |
* @return {boolean} return true when the value is a string | |
*/ | |
export const isString = (value) => getType(value) === 'String'; | |
/** | |
* isNumber check the value for Number type | |
* @param {any} value - A value that will be checked if is a Number | |
* @return {boolean} return true when the value is a Number | |
*/ | |
export const isNumber = (value) => getType(value) === 'Number'; | |
/** | |
* isFunction check the value for Function type | |
* @param {any} value - A value that will be checked if is a Function | |
* @return {boolean} return true when the value is a Function | |
*/ | |
export const isFunction = (value) => getType(value) === 'Function'; | |
/** | |
* isBoolean check the value for Boolean type | |
* @param {any} value - A value that will be checked if is a Boolean | |
* @return {boolean} return true when the value is a Boolean | |
*/ | |
export const isBoolean = (value) => getType(value) === 'Boolean'; | |
/** | |
* isArray check the value for Array type | |
* @param {any} value - A value that will be checked if is an Array | |
* @return {boolean} return true when the value is an Array | |
*/ | |
export const isArray = (value) => Array.isArray(value); | |
/** | |
* isRegExp check the value for RegExp type | |
* @param {any} value - A value that will be checked if is a RegExp | |
* @return {boolean} return true when the value is a RegExp | |
*/ | |
export const isRegExp = (value) => getType(value) === 'RegExp'; | |
export default { | |
getType, | |
isObject, | |
isString, | |
isNumber, | |
isFunction, | |
isBoolean, | |
isArray | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment