Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
/*!
* arr-flatten <https://github.com/jonschlinkert/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;
}
};