diff --git a/.travis.yml b/.travis.yml index 87740706..b9b75fd0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,10 +12,6 @@ node_js: script: - npm run codecov -cache: - directories: - - "node_modules" - notifications: email: on_success: never diff --git a/CHANGELOG.md b/CHANGELOG.md index 926531ea..4a6f3a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [v0.12.1](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.12.1) *(2018-07-31)* + +**Fixed bugs:** + +- `Array.prototype.nest` + - fixed finding children of children bug + ## [v0.12.0](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.12.0) *(2018-07-26)* **Implemented enhancements:** diff --git a/package-lock.json b/package-lock.json index f432b7c5..5645ff31 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "prototyped.js", - "version": "0.12.0", + "version": "0.12.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 119a46b0..e1fea19f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prototyped.js", - "version": "0.12.0", + "version": "0.12.1", "description": "Common typescript ready prototypes available in both es5 and es6", "author": "Ardalan Amini ", "license": "MIT", diff --git a/src/array/nest/index.ts b/src/array/nest/index.ts index e7e9496c..78daec48 100644 --- a/src/array/nest/index.ts +++ b/src/array/nest/index.ts @@ -2,26 +2,26 @@ import * as method from "./method"; declare global { interface Array { - nest(id?: any, link?: string): object[]; + nest(link?: string, key?: string): object[]; } } /** * Given a flat array of objects linked to one another, it will nest them recursively * @memberof Array - * @param {*} id * @param {string} link + * @param {string} id * @returns {Object[]} * @example * const comments = [ - * { id: 1, parent_id: null }, - * { id: 2, parent_id: 1 }, - * { id: 3, parent_id: 1 }, - * { id: 4, parent_id: 2 }, - * { id: 5, parent_id: 4 } + * { id: 1, comment_id: null }, + * { id: 2, comment_id: 1 }, + * { id: 3, comment_id: 1 }, + * { id: 4, comment_id: 2 }, + * { id: 5, comment_id: 4 } * ]; - * comments.nest(); // [{ id: 1, parent_id: null, children: [...] }] + * comments.nest("comment_id"); // [{ id: 1, comment_id: null, children: [...] }] */ -Array.prototype.nest = function(id, link): any { - return method(this, id, link); +Array.prototype.nest = function(link, key): any { + return method(this, link, key); }; diff --git a/src/array/nest/method.ts b/src/array/nest/method.ts index 86d90c28..a2b24053 100644 --- a/src/array/nest/method.ts +++ b/src/array/nest/method.ts @@ -1,5 +1,7 @@ -const method = (arr: any[], id: any = null, link: string = "parent_id"): any[] => +const filter = (arr: any[], link: string, key: string, id: any = null): any[] => arr.filter((item) => item[link] === id) - .map((item) => ({ ...item, children: method(arr, item.id) })); + .map((item) => ({ ...item, children: filter(arr, link, key, item[key]) })); + +const method = (arr: any[], link: string = "parent_id", key: string = "id"): any[] => filter(arr, link, key); export = method; diff --git a/src/array/nest/test.ts b/src/array/nest/test.ts index 00b67bb6..5412b3f1 100644 --- a/src/array/nest/test.ts +++ b/src/array/nest/test.ts @@ -3,29 +3,29 @@ import "./index"; describe("Array.prototype.nest", () => { test("Array.prototype.nest", () => { const comments = [ - { id: 1, parent_id: null }, - { id: 2, parent_id: 1 }, - { id: 3, parent_id: 1 }, - { id: 4, parent_id: 2 }, - { id: 5, parent_id: 4 }, + { id: 1, comment_id: null }, + { id: 2, comment_id: 1 }, + { id: 3, comment_id: 1 }, + { id: 4, comment_id: 2 }, + { id: 5, comment_id: 4 }, ]; - expect(comments.nest()).toEqual([ + expect(comments.nest("comment_id")).toEqual([ { id: 1, - parent_id: null, + comment_id: null, children: [ { id: 2, - parent_id: 1, + comment_id: 1, children: [ { id: 4, - parent_id: 2, + comment_id: 2, children: [ { id: 5, - parent_id: 4, + comment_id: 4, children: [], }, ], @@ -34,7 +34,7 @@ describe("Array.prototype.nest", () => { }, { id: 3, - parent_id: 1, + comment_id: 1, children: [], }, ],