Last active
March 10, 2022 05:16
-
-
Save dfkaye/e24b35e5cdc66b010cf95766f37ed4ca to your computer and use it in GitHub Desktop.
no-value: check whether value translates to null, undefined, NaN, or an empty string.
This file contains 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
// 2 Aug 2019 | |
// Check whether value translates to null, undefined, NaN, or an empty string. | |
// Comparable to the is-empty.js gist at https://gist.github.com/dfkaye/f87272098c4e19e4d55a6a18515aefce | |
// One major difference is that noValue asserts that [ '' ] has no value (true) | |
// whereas isEmpty asserts [ '' ] is *not* empty (false) due to { 0: ''}. | |
// Examples: | |
// false is a value. | |
// [] and [''] have noValue. | |
// {"":""} and {} have noValue. -- added 6 April 2020 | |
// {"empty": ""} has noValue -- 17 April 2020 | |
// {"": "not empty"} has a value -- 17 April 2020 | |
function noValue(o) { | |
return !String(o == null || o !== o | |
? '' | |
: Array.isArray(o) | |
? o | |
: typeof o == 'object' | |
? Object.values(o) // values, not keys | |
: o).length; | |
} | |
var tests = [ | |
// false | |
0, | |
[0], | |
['0'], | |
{"": "not empty"}, | |
1, | |
'2', | |
true, | |
false, | |
' ', | |
// true | |
null, | |
undefined, | |
NaN, | |
'', | |
[], | |
[''], | |
{}, | |
{"":""}, | |
{"empty": ""} | |
]; | |
var results = tests.map(test => { | |
var label = test === undefined ? 'undefined' : test !== test ? 'NaN' : test; | |
return { test: label, result: noValue(test) }; | |
}); | |
console.log(JSON.stringify(results, null, 2)); | |
/* | |
[ | |
{ | |
"test": 0, | |
"result": false | |
}, | |
{ | |
"test": [ | |
0 | |
], | |
"result": false | |
}, | |
{ | |
"test": [ | |
"0" | |
], | |
"result": false | |
}, | |
{ | |
"test": { | |
"": "not empty" | |
}, | |
"result": false | |
}, | |
{ | |
"test": 1, | |
"result": false | |
}, | |
{ | |
"test": "2", | |
"result": false | |
}, | |
{ | |
"test": true, | |
"result": false | |
}, | |
{ | |
"test": false, | |
"result": false | |
}, | |
{ | |
"test": " ", | |
"result": false | |
}, | |
{ | |
"test": null, | |
"result": true | |
}, | |
{ | |
"test": "undefined", | |
"result": true | |
}, | |
{ | |
"test": "NaN", | |
"result": true | |
}, | |
{ | |
"test": "", | |
"result": true | |
}, | |
{ | |
"test": [], | |
"result": true | |
}, | |
{ | |
"test": [ | |
"" | |
], | |
"result": true | |
}, | |
{ | |
"test": {}, | |
"result": true | |
}, | |
{ | |
"test": { | |
"": "" | |
}, | |
"result": true | |
}, | |
{ | |
"test": { | |
"empty": "" | |
}, | |
"result": true | |
} | |
] | |
*/ |
This file contains 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
// 30 July 2019 | |
// wip | |
function go(arg) { | |
var n = arg * 2; | |
console.log(n === n); | |
return n === n ? n : { arg, n }; | |
} | |
/* test it out */ | |
console.log( | |
go(2), | |
go('2'), | |
go([1]), | |
go([0.1]), | |
go([]), | |
go({}) | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment