Last active
August 24, 2023 02:59
-
-
Save lewdev/7565b1cfc98d1ddc5eb5d7b9c87378cf to your computer and use it in GitHub Desktop.
lodash `isEmpty` returns true for numbers
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
| <h2>isObject returns true for arrays?!?!?</h2> | |
| <p id=o></p> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> | |
| <script> | |
| const tests = [{}, [], "z", 1]; | |
| o.innerHTML = tests.map(a => `${JSON.stringify(a)}, isArray=${_.isArray(a)}, isObject=${_.isObject(a)}`).join`<br><br>`; | |
| /* Yields: | |
| {}, isArray=false, isObject=true | |
| [], isArray=true, isObject=true | |
| "z", isArray=false, isObject=false | |
| 1, isArray=false, isObject=false | |
| */ | |
| </script> |
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
| <p id=o></p> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> | |
| <script> | |
| const { isNil, isNumber, isEmpty } = _; | |
| const isEmptyStr = s => isNil(s) || (!isNumber(s) && isEmpty(s.trim())); | |
| o.innerHTML = [ | |
| 0, | |
| 1, | |
| 123, | |
| "", | |
| " ", | |
| " ", | |
| "asdf", | |
| "1234" | |
| ].map(a => `<div>isEmptyStr(${a}) = ${isEmptyStr(a)}</div>`).join``; | |
| /* Yields: | |
| isEmptyStr(0) = false | |
| isEmptyStr(1) = false | |
| isEmptyStr(123) = false | |
| isEmptyStr() = true | |
| isEmptyStr( ) = true | |
| isEmptyStr( ) = true | |
| isEmptyStr(asdf) = false | |
| isEmptyStr(1234) = false | |
| */ | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment