Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 5, 2020 01:37 pm GMT

js-coroutines gives your code: data indexing and lookup functionality, in idle time

Alt Text

Following user feedback I've added some key "lodash" like functions to the out of the box features of js-coroutines.

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.

FunctionUse
appendAsyncAppends an array to another one, modifying the destination
compressAsyncCompresses a string using lz-string. All of the other lz-string methods are also available.
concatAsyncConcatenates two arrays creating a new one
decompressAsyncDecompresses a string compressed with lz-string
everyAsyncValidates that every member of a collection passes a predicate
findAsyncFinds an entry which passes a predicate function in a collection or null
findIndexAsyncFinds the first index which passes a predicate
forEachAsyncCalls a function for every element in a collection.
groupByAsyncCreates an index object where each key contains an array of all matching values
includesAsyncReturns true if an array includes a value
indexOfAsyncReturns the first index of an item in a collection
keyByAsyncCreates an index object where each key is the last item in a collection to generate the key
lastIndexOfAsyncReturns the last index of an item in a collection
mapAsyncRuns a mapping function against each element of an array and returns a new array with the results
parseAsyncParses JSON into an object or value
reduceAsyncRuns a reduce operation on all elements of a collection and returns the result
someAsyncChecks if some entries in a collection match a predicate
stringifyAsyncConverts a JavaScript object/value into JSON
uniqueByAsyncCreates an array of unique values. The value determining uniqueness is produced by calling a function with the array entry.

You can now execute code like this:

     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.

     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).


Original Link: https://dev.to/miketalbot/js-coroutines-adds-indexing-and-lookup-functionality-348n

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To