Category: new feature for "castArray"
instead of:
const castArray = val => (Array.isArray(val) ? val : [val]);
more complete:
const castArray = val => Array.isArray(val) ? val : val == null ? [] : [val];
examples:
castArray('foo'); // ['foo']
castArray([1]); // [1]
castArray(null); // []
castArray(undefined); // []
so includes case of "null" or undefined values which results in an empty array. Also no need of parenthesis in code.
I have to include this because 90% of cases you castArray(theunexpectedresultofthingelse()).map(...) and is useful in resulting data null or undefined from theunexpect...()
undefined means there is nothing there, essentially forcing us to cast to something. null however is a valid value, so what if you want to create an array of a single element with the value null? I think the behavior should remain as-is, but we could add a method that casts null into undefined and vice versa which could work in combination with this for people that want to have this behavior or something? Probably not a popular opinion. Let's see what others think.
Array indexes that contain null or undefined values are perfectly valid in code. Often you want either a lack of data but not pointer(null) or a lack of pointer(undefined) so that you know if something has gone wrong with an intermediate step. I use that sad fact often when I'm using collections that need to be collated or fetched.
Otherwise there would be no need for the === behavior regarding null and undefined. We want those behaviors kept same.
I'm closing this issue since we want to preserve comparison code and empty array indexes as expected and due to lack of activity
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.