Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pooriamo committed Aug 15, 2024
1 parent 486a4b2 commit 26849c3
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
This small package converts a one dimensional array into a tree based on the elements' relationship in an efficient way with "O(n)".
This small package converts a one dimensional array into a tree based on the elements' relationship in an efficient way.

#### Installation:
### Installation:
```
npm install array-nest
// OR
yarn add array-nest
```

The usage is:
`nest(array: array, options?: {})`
`nest(array, options)`

#### Example:
### Example:

```
```ts
import nest from 'array-nest';

const tree = nest([
Expand All @@ -28,8 +28,8 @@ const tree = nest([
console.log(tree);
```

#### The expected result:
```
#### The result:
```ts

[
{
Expand Down Expand Up @@ -81,9 +81,29 @@ console.log(tree);
]
```

Supported `options` parameters:
### Overriding the default values:
```ts
import nest from 'array-nest';

const tree = nest([
{ code: 1, parent_code: '', name: 'A' },
{ code: 2, parent_code: 1, name: 'B' },
{ code: 3, parent_code: 2, name: 'C' },
{ code: 4, parent_code: '', name: 'D' },
{ code: 5, parent_code: 1, name: 'E' },
{ code: 6, parent_code: 4, name: 'F' },
{ code: 7, parent_code: 6, name: 'G' },
], {
idKey: 'code',
parentKey: 'parent_code',
rootParentsId: '',
childrenKey: 'subtree'
});
```

### Supported `options`:

- __idKey__: What is the equivalent `id` identifier in the input array. _default: 'id'_
- __parentKey__: What is the equivalent `parent_id` identifier in the input array. _default: 'parent_id'_
- __childrenKey__: What should the `children` property be named in the resulting array. _default: 'children'_
- __rootParentsId__: What is the `id` of the root entries (The entries which have no parents themselves) _default: null_
- __idKey__: The equivalent of `id` key in the input array. _default: 'id'_
- __parentKey__: The equivalent of `parent_id` key in the input array. _default: 'parent_id'_
- __childrenKey__: What the nested property should be named in the resulting array. _default: 'children'_
- __rootParentsId__: The `id` of the root entries (The entries which have no parents themselves) _default: null_

0 comments on commit 26849c3

Please sign in to comment.