UNPKG

2 kBJavaScriptView Raw
1/** @module types */
2
3/** Checks **correctly** if the passed type is or not a **number**
4 * @argument {*} num any possible data type
5 * @returns {boolean} indicating if is a number or not
6 * @example
7 * isNumber(null) // -> false note isNaN(null) will return false instead
8 * @see [typesTest.js](https://github.com/nerac/keyu/blob/master/test/typesTest.js)
9 * @method
10 */
11const isNumber = num => !isNaN(num) && num !== null;
12
13/** Checks **correctly** if the passed type is or not an **object**
14 * @argument {*} num any possible data type
15 * @returns {boolean} indicating if it's an object or not
16 * @example
17 * isObject(null) // -> false note that typeof will return true
18 * isObject({}) // -> true
19 * isObject(new Error()) // -> true
20 * @see [typesTest.js](https://github.com/nerac/keyu/blob/master/test/typesTest.js)
21 * @method
22 */
23const isObject = obj => typeof obj === 'object' && !Array.isArray(obj) && obj !== null;
24
25/** Checks if the passed type is a non defined value, either `undefined` or `null`
26 * @argument {*} num any possible data type
27 * @returns {boolean} indicating if it's an non defined
28 * @example
29 * isNil(null) // -> true
30 * isNil(3) // -> false
31 * isNil() // -> true
32 * isNil(undefined) // -> true
33 * @see [typesTest.js](https://github.com/nerac/keyu/blob/master/test/typesTest.js)
34 * @method
35 */
36const isNil = value => typeof value === 'undefined' || value === null;
37
38/** Returns the name of the function in which is called.
39 * @argument {String} [defaultValue] string for unknown calls.
40 * @returns {String} indicating the possible name of the function
41 * @example
42 * const hello = () => getFuncName()
43 * hello() // -> "hello"
44 * getFuncName() // -> "Unknown"
45 * getFuncName("ups") // -> "ups"
46 * @see [typesTest.js](https://github.com/nerac/keyu/blob/master/test/typesTest.js)
47 * @method
48 */
49function getFuncName(defaultValue = 'Unknown') {
50 return (getFuncName.caller && getFuncName.caller.name) || defaultValue;
51}
52
53module.exports = { isNumber, isObject, isNil, getFuncName };