-1

This is no anything special about the Lodash castArray function. Is there any ways to solve this task without any external libraries, with the newest language features, but still shortly?

If you are not familiar with the task:

castArray(['abc', 'def'])
// ['abc', 'def']
castArray('abc')
// ['abc']
castArray()
// []
castArray(undefined)
// [undefined]

Is there way to do it without a type checking? Notice that I look for the shortest equivalent, ES6+.

avdotion
  • 105
  • 8
  • Click "source" in the link you posted. – str Jul 24 '19 at 10:58
  • You *changed* your requirement! The "without type checking" makes your question *quite* different and invalidates the current answer. That's [not good](https://meta.stackoverflow.com/questions/271400/edits-that-do-not-change-the-meaning-of-the-original-post-but-invalidate-posted). The `castArray(undefined) // [undefined]` seems fine, though - it's a clarification that is already in the docs but not needed attention. – VLAZ Jul 24 '19 at 11:23
  • Also, I'm not sure how you'd even solve this without type checking. You *need* to do different stuff for an array and not an array. – VLAZ Jul 24 '19 at 11:25
  • Sorry for misunderstanding, I still look for short and convenient way to do this thing. – avdotion Jul 24 '19 at 11:57

2 Answers2

4

You could take a function with a default value of an empty array and return the result of a check with either the array or a new one.

const castArray = (data = []) => Array.isArray(data) ? data : [data];

console.log(castArray(['abc', 'def']));  // ['abc', 'def']
console.log(castArray('abc'));           // ['abc']
console.log(castArray());                // []

console.log(castArray(1));               // => [1]
console.log(castArray({ 'a': 1 }));      // => [{ 'a': 1 }]
console.log(castArray('abc'));           // => ['abc']
console.log(castArray(null));            // => [null]
console.log(castArray(undefined));       // => [undefined]
console.log(castArray());                // => []

var array = [1, 2, 3];
console.log(castArray(array) === array); // => true
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Just added the examples from the page to show it performs the same as Lodash. – VLAZ Jul 24 '19 at 11:02
  • 2
    Actually, it doesn't for `castArray(undefined)`. Didn't notice that - in that case it creates an empty array. OP can decide whether that's acceptable or not but it's relatively easy to amend. – VLAZ Jul 24 '19 at 11:04
0

The above answer doesn't take in account NodeLists and other Array-like (Iterrable) objects.

A more accurate answer to castArray would be

function isIterable(value) {
       return Symbol.iterator in Object(value)
}

function getElementsAsArray(obj) {
    if (!obj) {
        return []
    }

    if (isIterable(obj) && typeof obj !== 'string') {
        return Array.from(obj)
    }

    return [obj]
}

(checking for iterable is from here Checking whether something is iterable)

Tombigel
  • 108
  • 8