Skip to content
/ list Public

📜 Lightweight and context-free array operations

License

Notifications You must be signed in to change notification settings

semibran/list

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

list

Lightweight and context-free array operations

usage

NPM

// ES6:
const { append, insert, remove, locate } = require('list')
// ES5:
var append = require('list/append')

append(list, item)

Adds item to the end of list and returns list.

> append(['foo', 'bar'], 'baz')
['foo', 'bar', 'baz']

insert(list, index, item)

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.

> insert(['foo', 'baz'], 1, 'bar')
['foo', 'bar', 'baz']

To prepend to a list, insert at index 0.

> insert(['bar', 'baz'], 0, 'foo')
['foo', 'bar', 'baz']

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.

remove(list, index)

Removes the item located at index from list. Returns the item if it exists, otherwise undefined.

> remove(['foo', 'bar', 'baz'], 1)
'bar'
> remove(['foo', 'bar', 'baz'], 'qux')
undefined

locate(list, item)

Finds the first index where list[index] === item, or undefined if no item is found.

> locate(['foo', 'bar', 'baz'], 'baz')
2
> locate(['foo', 'bar', 'baz'], 'qux')
undefined

see also

kudos

  • leo - for donating the package name

license

MIT © Brandon Semilla

About

📜 Lightweight and context-free array operations

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published