UNPKG

1.51 kBMarkdownView Raw
1# list
2> Lightweight and context-free array operations
3
4## install
5```sh
6npm install list
7```
8
9## usage
10```js
11const { append, insert, remove, locate } = require('list')
12```
13
14### `append(list, item)`
15Adds `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)`
22Inserts `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```
27To 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)`
35Removes 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')
42undefined
43```
44
45### `locate(list, item)`
46Finds the first index where `list[index] === item`, or `undefined` if no item is found.
47```js
48> locate(['foo', 'bar', 'baz'], 'baz')
492
50```
51```js
52> locate(['foo', 'bar', 'baz'], 'qux')
53undefined
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)