list-promise
Maximally-concurrent Promise-aware array manipulation via ListPromise
. They're like the plural form of a Promise or a finite Observable.
Install
$ npm install --save list-promise
Usage
;; // Promisify an array. // Read each file (callback returns a Promise). // Chunk the characters of each file. // Merge the chunks into a single array. // Remove empty chunks. // Sort the characters. // Reverse the characters. // Join chunks into a string. // Write result to a new file. ;
API
listPromise(list) : ListPromise
list
Array|Promise<Array>
A list of items which may or may not be Promises.
Creates a promise with map
, filter
, and reduce
methods that can be used to iterate over list items as they are resolved.
ListPromise
.map(fn): ListPromise
fn
Function(item, i) : item
Map callback.
Creates a new ListPromise
for the results of mapping a list with a given map function.
; ; // -> ['A', 'B']
.mapProp(key, fn): ListPromise
key
String|Number
Key of property to modify.fn
Function(property, i, item) : item
Map callback.
Creates a new ListPromise
for the results of mapping a list of items' properties with a given map function.
; ; // -> [{ foo: 'A' }, { foo: 'B' }]
.concat(): ListPromise
Flattens a list of lists into a single list.
; ; // -> ['a', 'b', 'b', 'c']
.concatMap(fn): ListPromise
fn
Function(item, i) : item
Map callback.
Convenience shorthand for .map(fn).concat()
.
; ; // -> ['a', 'b', 'b', 'c']
.filter(fn): ListPromise
fn
Function(item, i) : context
Filter callback.
Creates a new ListPromise
for the results of filtering a list with a given filter function.
; ; // -> [10, 9, 8]
.reduce(fn, [initialValue]): ListPromise
fn
Function(context, item, i) : context
Reduce callback.initialValue
*
(default:undefined
) Initial value to pass to the reducer.
Creates a new ListPromise
for the result of reducing a list with a given reducer function. If the reduction results in an array, that array may then be iterated.
; ; // -> 31
.reverse(): ListPromise
Creates a new ListPromise
for the result of reversing the list.
; // -> [3, 2, 1, 'c', 'b', 'a']
.sortBy(fn): ListPromise
fn
Function(item, i) : any
Sort value getter.
Creates a new ListPromise
for the result of sorting a list with a property getter function.
; ; // -> [{ foo: '1' }, { foo: '2' }, { foo: '3' }]
Contribute
Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.
Test
$ npm test
© Shannon Moeller me@shannonmoeller.com (http://shannonmoeller.com)
Licensed under MIT