js-coroutines gives your code: data indexing and lookup functionality, in idle time
Tags: javascript, webdev
Published: 2020-07-05 13:37:38

[![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qc7xww7co3re0jr8weh1.png)](http://js-coroutines.com)

Following user feedback I've added some key "lodash" like functions to the out of the box features of [js-coroutines](http://js-coroutines.com).

I've added `keyByAsync` `groupByAsync`, `includesAsync`, `uniqueByAsync`. Here are all of the "out of the box" functions now available. They all work asynchronously, spreading the load over multiple frames to ensure that your app stays interactive.

<table class="MuiTable-root"><thead class="MuiTableHead-root"><tr class="MuiTableRow-root MuiTableRow-head"><th class="MuiTableCell-root MuiTableCell-head" scope="col">Function</th><th class="MuiTableCell-root MuiTableCell-head" scope="col">Use</th></tr></thead><tbody class="MuiTableBody-root"><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>appendAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Appends an array to another one, modifying the destination</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>compressAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Compresses a string using lz-string. All of the other lz-string methods are also available.</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>concatAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Concatenates two arrays creating a new one</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>decompressAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Decompresses a string compressed with lz-string</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>everyAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Validates that every member of a collection passes a predicate</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>findAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Finds an entry which passes a predicate function in a collection or null</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>findIndexAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Finds the first index which passes a predicate</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>forEachAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Calls a function for every element in a collection.</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>groupByAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Creates an index object where each key contains an array of all matching values</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>includesAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Returns true if an array includes a value</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>indexOfAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Returns the first index of an item in a collection</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>keyByAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Creates an index object where each key is the last item in a collection to generate the key</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>lastIndexOfAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Returns the last index of an item in a collection</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>mapAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Runs a mapping function against each element of an array and returns a new array with the results</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>parseAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Parses JSON into an object or value</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>reduceAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Runs a reduce operation on all elements of a collection and returns the result</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>someAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Checks if some entries in a collection match a predicate</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>stringifyAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Converts a JavaScript object/value into JSON</td></tr><tr class="MuiTableRow-root"><td class="MuiTableCell-root MuiTableCell-body"><code>uniqueByAsync</code></td><td class="MuiTableCell-root MuiTableCell-body">Creates an array of unique values. The value determining uniqueness is produced by calling a function with the array entry.</td></tr></tbody></table>

You can now execute code like this:

```js
     const response = await fetch("some/url")
     const data = await parseAsync(response.text())
     const index = await keyByAsync(data, v=>v.id)
     const groups = await groupByAsync(data, v=>v.category)

```

Of course, you can also write your own generator functions to split up any kind of processing you might need to do - and all of these functions work with the new `pipe()` to create functional pipelines that don't hog the main thread.

```js

     const process = pipe(
       decompressAsync,
       parseAsync,
       keyByAsync.with(v=>v.id)
     )
```
 
Another new feature is support for "collections" where we can use objects with key value pairs as well as arrays with all of the key functions that make sense (in the table above these are shown as handling 'collection' parameters).