Skip to content

Instantly share code, notes, and snippets.

@theadam
Last active January 4, 2017 05:05
Show Gist options
  • Save theadam/e98c7a5295d3e940114ee87b7c273fe2 to your computer and use it in GitHub Desktop.
Save theadam/e98c7a5295d3e940114ee87b7c273fe2 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
// Try out by visiting: https://esnextb.in/?gist=e98c7a5295d3e940114ee87b7c273fe2
/*
* Recursively flattens each item in the array.
*/
function flatten(array) {
if (array.length === 0) return array;
const [head, ...tail] = array;
if (Array.isArray(head)) return flatten(head).concat(flatten(tail));
// If the head is a single item, it just becomes the head of the result.
return [head].concat(flatten(tail));
}
// TESTS
import test from 'tape';
import tapeDom from 'tape-dom'
// Show results in the DOM;
tapeDom(test);
test('empty array', function (t) {
t.deepEqual(
[],
flatten([]),
'an empty array should equal a flattened empty array',
);
t.end();
});
test('already flat array', function (t) {
t.deepEqual(
flatten([1, 2, 3]),
[1, 2, 3],
'a flat array should be the same after being flattened',
);
t.end();
});
test('nested array', function (t) {
t.deepEqual(
flatten([[[1], [[2, [3]]], 4, [[[5]]]]]),
[1, 2, 3, 4, 5],
'a nested array should be flattened',
);
t.end();
});
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"tape": "4.6.3",
"tape-dom": "0.0.12",
"babel-runtime": "6.20.0"
}
}
'use strict';
var _toArray2 = require('babel-runtime/helpers/toArray');
var _toArray3 = _interopRequireDefault(_toArray2);
var _tape = require('tape');
var _tape2 = _interopRequireDefault(_tape);
var _tapeDom = require('tape-dom');
var _tapeDom2 = _interopRequireDefault(_tapeDom);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Try out by visiting: https://esnextb.in/?gist=e98c7a5295d3e940114ee87b7c273fe2
/*
* Recursively flattens each item in the array.
*/
function flatten(array) {
if (array.length === 0) return array;
var _array = (0, _toArray3.default)(array);
var head = _array[0];
var tail = _array.slice(1);
if (Array.isArray(head)) return flatten(head).concat(flatten(tail));
// If the head is a single item, it just becomes the head of the result.
return [head].concat(flatten(tail));
}
// TESTS
// Show results in the DOM;
(0, _tapeDom2.default)(_tape2.default);
(0, _tape2.default)('empty array', function (t) {
t.deepEqual([], flatten([]), 'an empty array should equal a flattened empty array');
t.end();
});
(0, _tape2.default)('already flat array', function (t) {
t.deepEqual(flatten([1, 2, 3]), [1, 2, 3], 'a flat array should be the same after being flattened');
t.end();
});
(0, _tape2.default)('nested array', function (t) {
t.deepEqual(flatten([[[1], [[2, [3]]], 4, [[[5]]]]]), [1, 2, 3, 4, 5], 'a nested array should be flattened');
t.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment