diff --git a/README.md b/README.md index 89f130919..bd6dcf478 100644 --- a/README.md +++ b/README.md @@ -4671,6 +4671,7 @@ const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]); ```js + const lengthIs4 = checkProp(l => l === 4, 'length'); lengthIs4([]); // false lengthIs4([1,2,3,4]); // true @@ -6852,11 +6853,11 @@ const deepMapKeys = (obj, f) => ? obj.map(val => deepMapKeys(val, f)) : typeof obj === 'object' ? Object.keys(obj).reduce((acc, current) => { - const val = obj[current]; - acc[f(current)] = + const val = obj[current]; + acc[f(current)] = val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val); - return acc; - }, {}) + return acc; + }, {}) : obj; ``` @@ -6930,9 +6931,9 @@ const dig = (obj, target) => target in obj ? obj[target] : Object.values(obj).reduce((acc, val) => { - if (acc !== undefined) return acc; - if (typeof val === 'object') return dig(val, target); - }, undefined); + if (acc !== undefined) return acc; + if (typeof val === 'object') return dig(val, target); + }, undefined); ```
@@ -8872,18 +8873,20 @@ isNull(null); // true Checks if the given argument is a number. -Use `typeof` to check if a value is classified as a number primitive. +Use `typeof` to check if a value is classified as a number primitive. +To safeguard against `NaN`, check if `val === val` (as `NaN` has a `typeof` equal to `number` and is the only value not equal to itself). ```js -const isNumber = val => typeof val === 'number'; +const isNumber = val => typeof val === 'number' && val === val; ```
Examples ```js -isNumber('1'); // false isNumber(1); // true +isNumber('1'); // false +isNumber(NaN); // false ```
diff --git a/docs/function.html b/docs/function.html index f06e8c942..0f44926f2 100644 --- a/docs/function.html +++ b/docs/function.html @@ -146,6 +146,7 @@ console.log<

checkProp

Given a predicate function and a prop string, this curried function will then take an object to inspect by calling the property and passing it to the predicate.

Summon prop on obj, pass it to a provided predicate function and return a masked boolean.

const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]);
 
 
+
 const lengthIs4 = checkProp(l => l === 4, 'length');
 lengthIs4([]); // false
 lengthIs4([1,2,3,4]); // true
diff --git a/docs/object.html b/docs/object.html
index 91405896b..b230ccf59 100644
--- a/docs/object.html
+++ b/docs/object.html
@@ -139,11 +139,11 @@ o[1? obj.map(val => deepMapKeys(val, f))
     : typeof obj === 'object'
       ? Object.keys(obj).reduce((acc, current) => {
-        const val = obj[current];
-        acc[f(current)] =
+          const val = obj[current];
+          acc[f(current)] =
             val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val);
-        return acc;
-      }, {})
+          return acc;
+        }, {})
       : obj;
 
const obj = {
   foo: '1',
@@ -178,9 +178,9 @@ o[1in obj
     ? obj[target]
     : Object.values(obj).reduce((acc, val) => {
-      if (acc !== undefined) return acc;
-      if (typeof val === 'object') return dig(val, target);
-    }, undefined);
+        if (acc !== undefined) return acc;
+        if (typeof val === 'object') return dig(val, target);
+      }, undefined);
 
const data = {
   level1: {
     level2: {
diff --git a/docs/type.html b/docs/type.html
index 0bb0d6e66..2bfaea055 100644
--- a/docs/type.html
+++ b/docs/type.html
@@ -136,9 +136,10 @@
 isNil(undefined); // true
 

isNull

Returns true if the specified value is null, false otherwise.

Use the strict equality operator to check if the value and of val are equal to null.

const isNull = val => val === null;
 
isNull(null); // true
-

isNumber

Checks if the given argument is a number.

Use typeof to check if a value is classified as a number primitive.

const isNumber = val => typeof val === 'number';
-
isNumber('1'); // false
-isNumber(1); // true
+

isNumber

Checks if the given argument is a number.

Use typeof to check if a value is classified as a number primitive. To safeguard against NaN, check if val === val (as NaN has a typeof equal to number and is the only value not equal to itself).

const isNumber = val => typeof val === 'number' && val === val;
+
isNumber(1); // true
+isNumber('1'); // false
+isNumber(NaN); // false
 

isObject

Returns a boolean determining if the passed value is an object or not.

Uses the Object constructor to create an object wrapper for the given value. If the value is null or undefined, create and return an empty object. Οtherwise, return an object of a type that corresponds to the given value.

const isObject = obj => obj === Object(obj);
 
isObject([1, 2, 3, 4]); // true
 isObject([]); // true
diff --git a/snippets/checkProp.md b/snippets/checkProp.md
index ddc49eee2..7f553f68d 100644
--- a/snippets/checkProp.md
+++ b/snippets/checkProp.md
@@ -11,6 +11,7 @@ const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]);
 ```js
 
 
+
 const lengthIs4 = checkProp(l => l === 4, 'length');
 lengthIs4([]); // false
 lengthIs4([1,2,3,4]); // true
diff --git a/snippets/deepMapKeys.md b/snippets/deepMapKeys.md
index c736420ee..970453bdc 100644
--- a/snippets/deepMapKeys.md
+++ b/snippets/deepMapKeys.md
@@ -13,11 +13,11 @@ const deepMapKeys = (obj, f) =>
     ? obj.map(val => deepMapKeys(val, f))
     : typeof obj === 'object'
       ? Object.keys(obj).reduce((acc, current) => {
-        const val = obj[current];
-        acc[f(current)] =
+          const val = obj[current];
+          acc[f(current)] =
             val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val);
-        return acc;
-      }, {})
+          return acc;
+        }, {})
       : obj;
 ```
 
diff --git a/snippets/dig.md b/snippets/dig.md
index 737130b6e..c97559833 100644
--- a/snippets/dig.md
+++ b/snippets/dig.md
@@ -10,9 +10,9 @@ const dig = (obj, target) =>
   target in obj
     ? obj[target]
     : Object.values(obj).reduce((acc, val) => {
-      if (acc !== undefined) return acc;
-      if (typeof val === 'object') return dig(val, target);
-    }, undefined);
+        if (acc !== undefined) return acc;
+        if (typeof val === 'object') return dig(val, target);
+      }, undefined);
 ```
 
 ```js
diff --git a/test/_30s.js b/test/_30s.js
index 22aeaaf6b..baf6a0b37 100644
--- a/test/_30s.js
+++ b/test/_30s.js
@@ -243,11 +243,11 @@ const deepMapKeys = (obj, f) =>
     ? obj.map(val => deepMapKeys(val, f))
     : typeof obj === 'object'
       ? Object.keys(obj).reduce((acc, current) => {
-        const val = obj[current];
-        acc[f(current)] =
+          const val = obj[current];
+          acc[f(current)] =
             val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val);
-        return acc;
-      }, {})
+          return acc;
+        }, {})
       : obj;
 const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
 const defer = (fn, ...args) => setTimeout(fn, 1, ...args);
@@ -270,9 +270,9 @@ const dig = (obj, target) =>
   target in obj
     ? obj[target]
     : Object.values(obj).reduce((acc, val) => {
-      if (acc !== undefined) return acc;
-      if (typeof val === 'object') return dig(val, target);
-    }, undefined);
+        if (acc !== undefined) return acc;
+        if (typeof val === 'object') return dig(val, target);
+      }, undefined);
 const digitize = n => [...`${n}`].map(i => parseInt(i));
 const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
 const drop = (arr, n = 1) => arr.slice(n);
@@ -604,7 +604,7 @@ const isLowerCase = str => str === str.toLowerCase();
 const isNegativeZero = val => val === 0 && 1 / val === -Infinity;
 const isNil = val => val === undefined || val === null;
 const isNull = val => val === null;
-const isNumber = val => typeof val === 'number';
+const isNumber = val => typeof val === 'number' && val === val;
 const isObject = obj => obj === Object(obj);
 const isObjectLike = val => val !== null && typeof val === 'object';
 const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object;