Created
January 8, 2021 02:32
-
-
Save hscstudio/157635627871640ffb5df60e9f7f7d32 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 toInt = (string) => parseInt(string) || 0 | |
console.log( | |
toInt(null), // 0 | |
toInt(undefined), // 0 | |
toInt(NaN), // 0 | |
toInt(""), // 0 | |
toInt({}), // 0 | |
toInt(0), // 0 | |
toInt([]), // 0 | |
toInt(), // 0 | |
toInt("xyz"), // 0 | |
toInt("123"), // 123 | |
toInt(123) // 123 | |
) | |
const isEmpty = (any) => !(!!any ? typeof any === 'object' ? Array.isArray(any) ? !!any.length : !!Object.keys(any).length : true : false); | |
console.log( | |
isEmpty(null), // true | |
isEmpty(undefined), // true | |
isEmpty(NaN), // true | |
isEmpty(""), // true | |
isEmpty({}), // true | |
isEmpty(0), // true | |
isEmpty([]), // true | |
isEmpty(), // true | |
isEmpty(1), // false | |
isEmpty({'a':1}), // false | |
isEmpty([1]) // false | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment