23 Different Values vs. Underscore.js is* FunctionsBy craigshoemaker
A common task in any code is determining “what is what”. In this case Underscore.js has a number of functions to help you determine between, objects, arrays, undefined, null values and a whole lot more. I’m calling the collection of those functions the ‘is*’ functions as they are named isEmpty, isElement, isArray and so on.
Taking a Look at the Results
For the most part the functions behave against values just as you might expect, but there are a few anomalies which warrant some further explanation.
The isEmpty function seems to return some false positives. Reviewing the output, an empty object, empty array and empty string are correctly considered empty by isEmpty returning true, but there are some other values that don’t really make sense. For instance it may seem odd that integers, floating point numbers, regular expressions are considered empty by this function.
The isEmpty function is only appropriate for use with objects an arrays. According to the developers, you shouldn’t use it on strings or numbers and apparently Booleans, Dates, Regular Expressions, empty arguments or functions either
Arguments are Arguments, Not Arrays
If you haven’t had the chance I hope you have an opportunity to check out my course Underscore.js Fundamentals where I demonstrate how underscore simplifies working with objects, arrays and functions and how you can improve the expressiveness and performance of your code using Underscore.js.
About the Author
- New course: Underscore.js Fundamentals
- TypeScript Grammar – Part 2
- My Clojure journey: pure functions
- Video: Returning Real Values from Mock Objects with Moq
- Domain Driven Design in C# – implementing immutable value objects
- TypeScript Classes and Interfaces – Part 3
- 5 Examples of Microsoft Access Date Functions