Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardalan committed Jul 31, 2018
2 parents 07098a6 + 6e7dbfd commit 9aa1068
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 29 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ node_js:
script:
- npm run codecov

cache:
directories:
- "node_modules"

notifications:
email:
on_success: never
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT",
Expand Down
20 changes: 10 additions & 10 deletions src/array/nest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import * as method from "./method";

declare global {
interface Array<T> {
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);
};
6 changes: 4 additions & 2 deletions src/array/nest/method.ts
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 11 additions & 11 deletions src/array/nest/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
],
Expand All @@ -34,7 +34,7 @@ describe("Array.prototype.nest", () => {
},
{
id: 3,
parent_id: 1,
comment_id: 1,
children: [],
},
],
Expand Down

0 comments on commit 9aa1068

Please sign in to comment.