UNPKG

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