1 | # list
|
2 | > Lightweight and context-free array operations
|
3 |
|
4 | ## install
|
5 | ```sh
|
6 | npm install list
|
7 | ```
|
8 |
|
9 | ## usage
|
10 | ```js
|
11 | const { append, insert, remove, locate } = require('list')
|
12 | ```
|
13 |
|
14 | ### `append(list, item)`
|
15 | Adds `item` to the end of `list` and returns `list`.
|
16 | ```js
|
17 | > append(['foo', 'bar'], 'baz')
|
18 | ['foo', 'bar', 'baz']
|
19 | ```
|
20 |
|
21 | ### `insert(list, index, item)`
|
22 | Inserts `item` at `index` in `list`. The item in `list` located at `index` and all subsequent items are shifted by one index to accommodate `item`.
|
23 | ```js
|
24 | > insert(['foo', 'baz'], 1, 'bar')
|
25 | ['foo', 'bar', 'baz']
|
26 | ```
|
27 | To prepend to a list, insert at index `0`.
|
28 | ```js
|
29 | > insert(['bar', 'baz'], 0, 'foo')
|
30 | ['foo', 'bar', 'baz']
|
31 | ```
|
32 | `list` is not modified if `index` is less than `0` or greater than or equal to `list.length`. Therefore, `insert` cannot be used to add items to the end of a list - use `append` instead.
|
33 |
|
34 | ### `remove(list, index)`
|
35 | Removes the item located at `index` from `list`. Returns the item if it exists, otherwise `undefined`.
|
36 | ```js
|
37 | > remove(['foo', 'bar', 'baz'], 1)
|
38 | 'bar'
|
39 | ```
|
40 | ```js
|
41 | > remove(['foo', 'bar', 'baz'], 'qux')
|
42 | undefined
|
43 | ```
|
44 |
|
45 | ### `locate(list, item)`
|
46 | Finds the first index where `list[index] === item`, or `undefined` if no item is found.
|
47 | ```js
|
48 | > locate(['foo', 'bar', 'baz'], 'baz')
|
49 | 2
|
50 | ```
|
51 | ```js
|
52 | > locate(['foo', 'bar', 'baz'], 'qux')
|
53 | undefined
|
54 | ```
|
55 |
|
56 | ## special thanks
|
57 | - [leo](https://github.com/leo) - for donating the package name
|
58 |
|
59 | ## license
|
60 | [MIT](https://opensource.org/licenses/MIT) © [Brandon Semilla](https://git.io/semibran)
|