Skip to content

Commit 427a4b1

Browse files
committed
decomposition
1 parent 58324e9 commit 427a4b1

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

flatten.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
"use strict";
22

3-
function wrap(idxs = []) {
4-
if (Array.isArray(idxs)) {
5-
let line = "";
6-
7-
for (let i = 0; i < idxs.length; i++) {
8-
line += `[${idxs[i]}]`;
9-
}
10-
11-
return line;
12-
}
13-
14-
return `[${idxs}]`;
15-
}
3+
const wrap = require('./wrap');
164

175
function flatten(arr = []) {
186
if (!Array.isArray(arr)) {

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arr-flatten-unflatten",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "non-recursive method of flattening an array or arrays and unflattening the result",
55
"main": "index.js",
66
"author": "Quernest",
@@ -28,5 +28,8 @@
2828
},
2929
"devDependencies": {
3030
"jest": "^24.0.0"
31+
},
32+
"publishConfig": {
33+
"registry": "https://npm.pkg.github.com/"
3134
}
3235
}

unflatten.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
const unflatten = require("./unflatten");
22

33
test("unflattens object", () => {
4-
/**
5-
* json-like object
6-
*/
74
const json = {
85
"[0]": 2,
96
"[1]": 4,
@@ -15,9 +12,6 @@ test("unflattens object", () => {
1512
"[3]": 5
1613
};
1714

18-
/**
19-
* expected result
20-
*/
2115
const arr = [2, 4, [8, [2, [32, 64]], 7], 5];
2216

2317
expect(unflatten(json)).toMatchObject(arr);

wrap.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
3+
function wrap(idxs = []) {
4+
if (Array.isArray(idxs)) {
5+
let line = "";
6+
7+
for (let i = 0; i < idxs.length; i++) {
8+
line += `[${idxs[i]}]`;
9+
}
10+
11+
return line;
12+
}
13+
14+
return `[${idxs}]`;
15+
}
16+
17+
module.exports = wrap;

wrap.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const wrap = require("./wrap");
2+
3+
describe("wraps to brackets", () => {
4+
it('for single index', () => {
5+
expect(wrap(1)).toEqual('[1]');
6+
});
7+
8+
it('for array of indexes', () => {
9+
expect(wrap([1,2,3])).toEqual('[1][2][3]');
10+
});
11+
});

0 commit comments

Comments
 (0)