Skip to content

Instantly share code, notes, and snippets.

@hscstudio
Created January 8, 2021 02:32
Show Gist options
  • Save hscstudio/157635627871640ffb5df60e9f7f7d32 to your computer and use it in GitHub Desktop.
Save hscstudio/157635627871640ffb5df60e9f7f7d32 to your computer and use it in GitHub Desktop.
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