{"id":16255265,"url":"https://github.com/rpgeeganage/async-ray","last_synced_at":"2025-03-17T14:14:13.762Z","repository":{"id":33046035,"uuid":"150082335","full_name":"rpgeeganage/async-ray","owner":"rpgeeganage","description":"Provide async/await callbacks for every, find, findIndex, filter, forEach, map, reduce, reduceRight and some methods in Array.","archived":false,"fork":false,"pushed_at":"2023-01-06T06:39:47.000Z","size":939,"stargazers_count":104,"open_issues_count":9,"forks_count":5,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-03T22:55:34.999Z","etag":null,"topics":["array","array-helper","array-manipulations","array-methods","array-processing","array-utils","arrays","async-await","async-callback","async-ray","es6-modules","javascript","loops","modules","node-js","node-module","typescript-library","utility"],"latest_commit_sha":null,"homepage":"https://rpgeeganage.github.io/async-ray","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rpgeeganage.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-24T09:48:47.000Z","updated_at":"2022-11-24T04:35:59.000Z","dependencies_parsed_at":"2023-01-14T23:10:50.254Z","dependency_job_id":null,"html_url":"https://github.com/rpgeeganage/async-ray","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpgeeganage%2Fasync-ray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpgeeganage%2Fasync-ray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpgeeganage%2Fasync-ray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpgeeganage%2Fasync-ray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rpgeeganage","download_url":"https://codeload.github.com/rpgeeganage/async-ray/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244047646,"owners_count":20389206,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","array-helper","array-manipulations","array-methods","array-processing","array-utils","arrays","async-await","async-callback","async-ray","es6-modules","javascript","loops","modules","node-js","node-module","typescript-library","utility"],"created_at":"2024-10-10T15:29:11.890Z","updated_at":"2025-03-17T14:14:13.724Z","avatar_url":"https://github.com/rpgeeganage.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Async-Ray\n[![License](https://img.shields.io/npm/l/async-ray.svg)](https://img.shields.io/npm/l/async-ray.svg)\n[![Version](https://img.shields.io/npm/v/async-ray.svg)](https://img.shields.io/npm/v/async-ray.svg)\n[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/rpgeeganage/async-ray.svg?logo=lgtm\u0026logoWidth=18)](https://lgtm.com/projects/g/rpgeeganage/async-ray/context:javascript)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/967e339f9fdb4424a48ba37a0292f221)](https://app.codacy.com/app/rpgeeganage/async-ray?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=rpgeeganage/async-ray\u0026utm_campaign=Badge_Grade_Settings)\n[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/3e1503ed17af4dc5aadb1fbbc41191d3)](https://www.codacy.com/app/rpgeeganage/async-ray?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=rpgeeganage/async-ray\u0026utm_campaign=Badge_Coverage)\n[![Build Status](https://travis-ci.org/rpgeeganage/async-ray.svg?branch=master)](https://travis-ci.org/rpgeeganage/async-ray)\n[![Known Vulnerabilities](https://snyk.io/test/github/rpgeeganage/async-ray/badge.svg?targetFile=package.json)](https://snyk.io/test/github/rpgeeganage/async-ray?targetFile=package.json)\n[![Maintainability](https://api.codeclimate.com/v1/badges/4c9603df3595612def57/maintainability)](https://codeclimate.com/github/rpgeeganage/async-ray/maintainability)\n\nPurpose of this package is to provide `async/await` callbacks for `every`, `filter`, `find`, `findIndex`, `forEach`, `map`, `reduce`, `reduceRight` and `some` methods in **_Array_**.\n\n### TypeScript Doc: [https://rpgeeganage.github.io/async-ray/doc/](https://rpgeeganage.github.io/async-ray/doc/)\n\n### Content\n* [ ***Basic Usage*** ](#basic-usage)\n* [ ***Supported methods*** ](#supported-methods)\n* [ ***Using methods individually*** ](#using-methods-individually)\n* [ ***Chaining*** ](#chaining)\n\n## Basic usage\n\n```js\nconst { AsyncRay } = require('async-ray');\n```\n\n## Supported methods\n* [ ***.aEvery*** ](#aevery)\n* [ ***.aFilter*** ](#afilter)\n* [ ***.aFind*** ](#afind)\n* [ ***.aFindIndex*** ](#afindindex)\n* [ ***.aFlatMap*** ](#aflatMap)\n* [ ***.aForEach*** ](#aforeach)\n* [ ***.aMap*** ](#amap)\n* [ ***.aReduce*** ](#areduce)\n* [ ***.aReduceRight*** ](#areduceright)\n* [ ***.aSome*** ](#asome)\n\n### .aEvery\n\n##### .aEvery(async callback(element[, index[, array]]))\n\n```js\nasync function dummy(element, needle) {\n  return Promise.resolve(element \u003e needle);\n}\n\nconst inputArray = [10, 20, 30, 40];\n\n// Call Every method\nconst output = await AsyncRay(inputArray).aEvery(\n  async (i, index, collection) =\u003e {\n    // Dummy async function\n    return await dummy(i, 5);\n  }\n);\n\nconsole.log(output);\n// Output is true\n```\n\n### .aFilter\n\n##### .aFilter(async callback(element[, index[, array]]))\n\n```js\nasync function dummy(element, needle) {\n  return Promise.resolve(element \u003e needle);\n}\n\nconst inputArray = [1, 2, 3, 4];\n\n// Call Filter method\nconst filterArray = await AsyncRay(inputArray).aFilter(\n  async (i, index, collection) =\u003e {\n    // Dummy async function\n    return await dummy(i, 2);\n  }\n);\n\nconsole.log(filterArray);\n// Output is [3, 4]\n```\n\n### .aFind\n\n##### .aFind(async callback(element[, index[, array]]))\n\nFind will return the found value or undefined\n\n```js\nasync function dummy(element, needle) {\n  return Promise.resolve(element === needle);\n}\n\nconst inputArray = [1, 2, 3, 4];\n\n// Call Find method\nconst outputElement = await AsyncRay(inputArray).aFind(\n  async (i, index, collection) =\u003e {\n    return await dummy(i, 2);\n  }\n);\n\nconsole.log('Output is ', outputElement);\n// Output is 2\n```\n### .aFindIndex\n\n##### .aFindIndex(async callback(element[, index[, array]]))\n\nFindIndex will return the index of found value or -1\n\n```js\nasync function dummy(element, needle) {\n  return Promise.resolve(element === needle);\n}\n\nconst inputArray = [1, 2, 3, 4];\n\n// Call Find method\nconst outputIndex = await AsyncRay(inputArray).aFindIndex(\n  async (i, index, collection) =\u003e {\n    return await dummy(i, 2);\n  }\n);\n\nconsole.log('Output is ', outputIndex);\n// Output is 1\n```\n\n### .aFlatMap\n\n##### .aFlatMap(async callback(element[, index[, array]]))\n\n```js\nasync function dummy(element) {\n  return Promise.resolve([element, element * 2]);\n}\n\nconst inputArray = [1, 2, 3, 4];\n\n// Call Map method\nconst flatMappedArray = await AsyncRay(inputArray).aFlatMap(\n  async (i, index, collection) =\u003e {\n    // Dummy async function\n    return await dummy(i);\n  }\n);\nconsole.log(flatMappedArray);\n// Output is [1, 2, 2, 4, 3, 6, 4, 8]\n```\n\n### .aForEach\n\n##### .aForEach(async callback(element[, index[, array]]))\n\n```js\nasync function dummy(element) {\n  return Promise.resolve(element);\n}\n\nconst inputArray = [1, 2, 3, 4];\nconst outputArray = [];\n\n// Call ForEach method\nawait AsyncRay(inputArray).aForEach(async (i, index, collection) =\u003e {\n  outputArray.push(await dummy(i));\n});\n\nconsole.log('Output is ', outputArray);\n// Output is [1, 2, 3, 4]\n```\n\n### .aMap\n\n##### .aMap(async callback(element[, index[, array]]))\n\n```js\nasync function dummy(element) {\n  return Promise.resolve(element);\n}\n\nconst inputArray = [1, 2, 3, 4];\n\n// Call Map method\nconst mappedArray = await AsyncRay(inputArray).aMap(\n  async (i, index, collection) =\u003e {\n    // Dummy async function\n    return await dummy(i);\n  }\n);\nconsole.log(mappedArray);\n// Output is [1, 2, 3, 4]\n```\n\n### .aReduce\n\n##### .aReduce(async callback(accumulator, element[, index[, array]]), [initialValue])\n\n```js\nasync function dummy(element) {\n  return Promise.resolve(element);\n}\n\nconst inputArray = [10, 20, 30, 40];\n\n// Call Reduce method\nconst output = await AsyncRay(inputArray).aReduce(\n  async (acc, i, index, collection) =\u003e {\n    return acc + (await dummy(i));\n  },\n  1\n);\n\nconsole.log('Output is ', output);\n// Output is 101\n```\n### .aReduceRight\n\n##### .aReduceRight(async callback(accumulator, element[, index[, array]]), [initialValue])\n\n```js\nasync function dummy(element) {\n  return Promise.resolve(element);\n}\n\nconst inputArray = [10, 20, 30, 40];\n\n// Call Reduce method\nconst output = await AsyncRay(inputArray).aReduceRight(\n  async (acc, i, index, collection) =\u003e {\n    return acc + (await dummy(i));\n  },\n  1\n);\n\nconsole.log('Output is ', output);\n// Output is 101\n```\n\n### .aSome\n\n##### .aSome(async callback(element[, index[, array]]))\n\n```js\nasync function dummy(element, needle) {\n  return Promise.resolve(element \u003e needle);\n}\n\nconst inputArray = [10, 20, 30, 40];\n\n// Call Some method\nconst output = await AsyncRay(inputArray).aSome(\n  async (i, index, collection) =\u003e {\n    // Dummy async function\n    return await dummy(i, 30);\n  }\n);\n\nconsole.log(output);\n// Output is true\n```\n## Using methods individually\nYou can use each method without creating ```AsyncRay ``` object.\n```js\nimport {\n  aEvery, aFilter, aFind, aFindIndex,\n  aForEach, aMap, aReduce, aReduceRight, aSome\n} from 'async-ray';\n\n// aEvery\nconst everyResult = await aEvery(\n  [1, 2, 3],\n  async (e) =\u003e Promise.resolve(e \u003e 0)\n);\n\n// aFilter\nconst filterResult = await aFilter(\n  [1, 2, 3],\n  async (e) =\u003e Promise.resolve(e \u003e 1)\n);\n\n// aFind\nconst findResult = await aFind(\n  [1, 2, 3],\n  async (e) =\u003e Promise.resolve(e === 3)\n);\n\n// aFindIndex\nconst findIndexResult = await aFindIndex(\n  [1, 2, 3],\n  async (e) =\u003e Promise.resolve(e === 2)\n);\n\n// aForEach\nconst forEachResult: number[] = [];\nawait aForEach(\n  [1, 2, 3],\n  async (e) =\u003e {\n    const op = await Promise.resolve(e * 10);\n\t  forEachResult.push(op);\n  }\n);\n\n// aMap\nconst mapResult = await aMap(\n  [1, 2, 3],\n  async (e) =\u003e Promise.resolve(e * 10)\n);\n\n// aReduce\nconst reduceResult = await aReduce(\n  [1, 2, 3],\n  async (acc, e) =\u003e Promise.resolve(e + acc),\n  0\n);\n\n// aReduceRight\nconst reduceRightResult = await aReduceRight(\n  [1, 2, 3],\n  async (acc, e) =\u003e Promise.resolve(e + acc),\n  0\n);\n\n// aSome\nconst someResult = await aSome(\n  [1, 2, 3],\n  async (e) =\u003e Promise.resolve(e \u003e 1)\n);\n```\n## Chaining\n\n### Async-Ray methods can be chained using ```Chain``` functionality\n### Basic usage\n```js\nconst { Chain } = require('async-ray');\n```\n---\n***Chaining will return an instance of Async-Ray if returned type is an array.***\n\n---\n\n#### sample 1 - `aMap()` and `aFilter()`\n##### ([sample code](https://github.com/rpgeeganage/async-ray/blob/master/examples/chain_sample_1.ts))\n---\nThe `process()` method  __***must be called explicitly***__ to process the chain because `aMap()` and `aFilter()` method returns an array.\n```js\nconst input = [1, 2, 3];\n\nconst op = await Chain(input)\n  .aMap(\n    async (e) =\u003e Promise.resolve(e * 10)\n  )\n  .aFilter(\n    async (e) =\u003e Promise.resolve(e \u003e 10)\n  )\n  .aMap(\n    async (e) =\u003e Promise.resolve(e * 10)\n  )\n  // Call the process() method to execute the chain\n  .process();\n\nconsole.log('Output is ', op);\n// Output is [ 200, 300 ]\n```\n#### sample 2 - `aMap()`, `aFilter()` and `aFind()`\n##### ([sample code](https://github.com/rpgeeganage/async-ray/blob/master/examples/chain_sample_2.ts))\n---\nThe `process()` method  __***should not be called***__ because `aFind()` does not return an array.\n```js\nconst input = [1, 2, 3];\n\nconst op = await Chain(input)\n  .aMap(\n    async (e) =\u003e Promise.resolve(e * 10)\n  )\n  .aFilter(\n    async (e) =\u003e Promise.resolve(e \u003e 10)\n  )\n  .aMap(\n    async (e) =\u003e Promise.resolve(e * 10)\n  )\n  .aFind(\n    async (e) =\u003e Promise.resolve(e === 300)\n  );\n  // No need to call process() method\n\nconsole.log('Output is ', op);\n// Output is 300\n```\n\n#### Between other [Array methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#) methods\n\n---\n\n#### Sample 1 - Async-Ray `Chain` with `filter()`\n```js\nconst input = [1, 2, 3];\n\nconst op = (\n  await Chain(input)\n    .aMap(\n      async (e) =\u003e Promise.resolve(e * 10)\n    )\n    .aFilter(\n      async (e) =\u003e Promise.resolve(e \u003e 10)\n    )\n    .aMap(\n      async (e) =\u003e Promise.resolve(e * 10)\n    )\n    .process()\n)\n.filter(e =\u003e e \u003e 200)\n\nconsole.log('Output is ', op);\n// Output is [ 300 ]\n```\n---\n\n#### Sample 2 - Async-Ray `Chain` with `find()`\n```js\nconst input = [1, 2, 3];\n\nconst op = (\n    await Chain(input)\n    .aMap(\n      async (e) =\u003e Promise.resolve(e * 10)\n    )\n    .aFilter(\n      async (e) =\u003e Promise.resolve(e \u003e 10)\n    )\n    .aMap(\n      async (e) =\u003e Promise.resolve(e * 10)\n    )\n    .process()\n)\n.find(e =\u003e e === 200)\n\nconsole.log('Output is ', op);\n// Output is 200\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpgeeganage%2Fasync-ray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frpgeeganage%2Fasync-ray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpgeeganage%2Fasync-ray/lists"}