link-list-js
v1.0.1
Published
Simple link list
Downloads
4
Maintainers
Readme
Simple Link List
Javascript Implementation of Singly Linked List
Installation
npm install link-list-js
Example
const data = [
{
id: 1,
name: "item 1",
parent_id: 5,
},
{
id: 4,
name: "item 4",
parent_id: 2,
},
{
id: 5,
name: "item 5",
parent_id: null,
},
{
id: 2,
name: "item 2",
parent_id: 1,
},
];
const list = new LinkList(data);
Get full Array
list.toArray();
// output
result = [
{ id: 5, name: "item 5", parent_id: null },
{ id: 1, name: "item 1", parent_id: 5 },
{ id: 2, name: "item 2", parent_id: 1 },
{ id: 4, name: "item 4", parent_id: 2 },
];
Get array from item[id] = 2 to end
list.toArray(2);
// output
result = [
{ id: 2, name: "item 2", parent_id: 1 },
{ id: 4, name: "item 4", parent_id: 2 },
];
Get array from head to item[id] = 2
list.toArray(0, 2);
// output
result = [
{ id: 5, name: "item 5", parent_id: null },
{ id: 1, name: "item 1", parent_id: 5 },
{ id: 2, name: "item 2", parent_id: 1 },
];
Get array with deep = 2
list.toArray(null, null, 2);
// output
result = [
{ id: 5, name: "item 5", parent_id: null },
{ id: 1, name: "item 1", parent_id: 5 },
];
Add item to after item[id] = 4
list.add({ id: 6, name: "item 6", parent_id: 4 });
// output
result = [
{ id: 5, name: "item 5", parent_id: null },
{ id: 1, name: "item 1", parent_id: 5 },
{ id: 2, name: "item 2", parent_id: 1 },
{ id: 4, name: "item 4", parent_id: 2 },
{ id: 6, name: "item 6", parent_id: 4 },
];
Delete item item[id] = 6
list.delete(6);
// output
result = [
{ id: 5, name: "item 5", parent_id: null },
{ id: 1, name: "item 1", parent_id: 5 },
{ id: 2, name: "item 2", parent_id: 1 },
{ id: 4, name: "item 4", parent_id: 2 },
];
Update item item[id] = 4
list.update({ id: 4, name: "item 4", parent_id: 1 });
// output
result = [
{ id: 5, name: "item 5", parent_id: null },
{ id: 1, name: "item 1", parent_id: 5 },
{ id: 4, name: "item 4", parent_id: 1 },
];
Append item
list.append({ id: 10, name: "item 10" });
// output
result = [
{ id: 5, name: "item 5", parent_id: null },
{ id: 1, name: "item 1", parent_id: 5 },
{ id: 4, name: "item 4", parent_id: 1 },
{ id: 10, name: "item 10", parent_id: 4 }, // item appeded
];
Prepend item
list.prepend({ id: 11, name: "item 11" });
// output
result = [
{ id: 11, name: "item 11", parent_id: null }, // item added
{ id: 5, name: "item 5", parent_id: 11 },
{ id: 1, name: "item 1", parent_id: 5 },
{ id: 4, name: "item 4", parent_id: 1 },
{ id: 10, name: "item 10", parent_id: 4 },
];