-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,048 additions
and
1,468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as method from "./method"; | ||
|
||
declare global { | ||
interface Array<T> { | ||
nest(id?: any, link?: string): object[]; | ||
} | ||
} | ||
|
||
/** | ||
* Given a flat array of objects linked to one another, it will nest them recursively | ||
* @memberof Array | ||
* @param {*} id | ||
* @param {string} link | ||
* @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 } | ||
* ]; | ||
* comments.nest(); // [{ id: 1, parent_id: null, children: [...] }] | ||
*/ | ||
Array.prototype.nest = function(id, link): any { | ||
return method(this, id, link); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const method = (arr: any[], id: any = null, link: string = "parent_id"): any[] => | ||
arr.filter((item) => item[link] === id) | ||
.map((item) => ({ ...item, children: method(arr, item.id) })); | ||
|
||
export = method; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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 }, | ||
]; | ||
|
||
expect(comments.nest()).toEqual([ | ||
{ | ||
id: 1, | ||
parent_id: null, | ||
children: [ | ||
{ | ||
id: 2, | ||
parent_id: 1, | ||
children: [ | ||
{ | ||
id: 4, | ||
parent_id: 2, | ||
children: [ | ||
{ | ||
id: 5, | ||
parent_id: 4, | ||
children: [], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 3, | ||
parent_id: 1, | ||
children: [], | ||
}, | ||
], | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const filter = (arr: any[], key: string | undefined, fn: any) => { | ||
if (key) { | ||
const keys = key.split("."); | ||
const reducer = (item: any) => keys.reduce((prev, curr) => prev[curr], item); | ||
|
||
return arr.filter((item) => fn(reducer(item))); | ||
} | ||
|
||
return arr.filter(fn); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.