diff --git a/index.js b/index.js index 0cb4ea4..569fb73 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,29 @@ -/*! - * arr-flatten - * - * Copyright (c) 2014-2017, Jon Schlinkert. - * Released under the MIT License. - */ +/** + * Hello Jon. + * + * I saw the issue with the deep nested array and I came up with this. + * Let me know what you think. The only caveat I can see is that if there + * are any characters with square brackets, they will be affected. + * + * Cheers! + **/ 'use strict'; -module.exports = function (arr) { - return flat(arr, []); -}; +module.exports = (arr) => { + /* if not an array, then do not proceed */ + if (!Array.isArray(arr)) { + throw new Error("- requires 'arr' argument to be of array type."); + } + /* ok. next */ + else { + /* stringify the entire entry */ + let stringifiedArray = JSON.stringify(arr); -function flat(arr, res) { - var i = 0, cur; - var len = arr.length; - for (; i < len; i++) { - cur = arr[i]; - Array.isArray(cur) ? flat(cur, res) : res.push(cur); + /* remove all square brackets */ + let stringWithoutSquareBrackets = stringifiedArray.split("[").join("").split("]").join(""); + + /* now return the value like this */ + return JSON.parse(`[${stringWithoutSquareBrackets}]`); } - return res; -} +};