All Projects → rpgeeganage → Async Ray

rpgeeganage / Async Ray

Licence: mit
Provide async/await callbacks for every, find, findIndex, filter, forEach, map, reduce, reduceRight and some methods in Array.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Async Ray

Massiv
Efficient Haskell Arrays featuring Parallel computation
Stars: ✭ 328 (+221.57%)
Mutual labels:  array, arrays
Solidarity
Solidarity is an environment checker for project dependencies across multiple machines.
Stars: ✭ 540 (+429.41%)
Mutual labels:  node-module, node-js
P Iteration
Utilities that make array iteration easy when using async/await or Promises
Stars: ✭ 337 (+230.39%)
Mutual labels:  array, async-await
arr-flatten
Recursively flatten an array or arrays. This is the fastest implementation of array flatten.
Stars: ✭ 55 (-46.08%)
Mutual labels:  utility, array
Phaser Node Kit
Rapid Game Development with PhaserJS and Node for Modern Browsers
Stars: ✭ 39 (-61.76%)
Mutual labels:  modules, node-js
action-sync-node-meta
GitHub Action that syncs package.json with the repository metadata.
Stars: ✭ 25 (-75.49%)
Mutual labels:  utility, node-js
Basic Ftp
FTP client for Node.js, supports FTPS over TLS, passive mode over IPv6, async/await, and Typescript.
Stars: ✭ 441 (+332.35%)
Mutual labels:  async-await, node-js
node-advanced
Node Advanced Courseware
Stars: ✭ 80 (-21.57%)
Mutual labels:  node-js, node-module
Cracking The Coding Interview
Solutions for Cracking the Coding Interview - 6th Edition
Stars: ✭ 35 (-65.69%)
Mutual labels:  array, arrays
Rando.js
The world's easiest, most powerful random function.
Stars: ✭ 659 (+546.08%)
Mutual labels:  node-module, node-js
arraync
Async Array methods polyfills
Stars: ✭ 16 (-84.31%)
Mutual labels:  array, async-await
Componentarrays.jl
Arrays with arbitrarily nested named components.
Stars: ✭ 72 (-29.41%)
Mutual labels:  array, arrays
warframe-worldstate-parser
📗 An Open parser for Warframe's Worldstate in Javascript
Stars: ✭ 50 (-50.98%)
Mutual labels:  node-js, node-module
Modclean
Remove unwanted files and directories from your node_modules folder
Stars: ✭ 309 (+202.94%)
Mutual labels:  modules, node-module
HashMapC
A tiny library for using easily HashMap, arraylist in the C.
Stars: ✭ 21 (-79.41%)
Mutual labels:  array, arrays
Practicalnode
Practical Node.js, 1st and 2nd Editions [Apress] 📓
Stars: ✭ 3,694 (+3521.57%)
Mutual labels:  node-module, node-js
node-screenlogic
Pentair ScreenLogic Javascript library using Node.JS
Stars: ✭ 38 (-62.75%)
Mutual labels:  node-js, node-module
php-collections
A collection library for php
Stars: ✭ 34 (-66.67%)
Mutual labels:  array, arrays
Kretes
A Programming Environment for TypeScript & Node.js built on top of VS Code
Stars: ✭ 570 (+458.82%)
Mutual labels:  async-await, node-js
Pgo
Go library for PHP community with convenient functions
Stars: ✭ 51 (-50%)
Mutual labels:  array, arrays

Async-Ray

License Version Language grade: JavaScript Codacy Badge Codacy Badge Build Status Known Vulnerabilities Maintainability

Purpose of this package is to provide async/await callbacks for every, filter, find, findIndex, forEach, map, reduce, reduceRight and some methods in Array.

TypeScript Doc: https://rpgeeganage.github.io/async-ray/doc/

Content

Basic usage

const { AsyncRay } = require('async-ray');

Supported methods

.aEvery

.aEvery(async callback(element[, index[, array]]))
async function dummy(element, needle) {
  return Promise.resolve(element > needle);
}

const inputArray = [10, 20, 30, 40];

// Call Every method
const output = await AsyncRay(inputArray).aEvery(
  async (i, index, collection) => {
    // Dummy async function
    return await dummy(i, 5);
  }
);

console.log(output);
// Output is true

.aFilter

.aFilter(async callback(element[, index[, array]]))
async function dummy(element, needle) {
  return Promise.resolve(element > needle);
}

const inputArray = [1, 2, 3, 4];

// Call Filter method
const filterArray = await AsyncRay(inputArray).aFilter(
  async (i, index, collection) => {
    // Dummy async function
    return await dummy(i, 2);
  }
);

console.log(filterArray);
// Output is [3, 4]

.aFind

.aFind(async callback(element[, index[, array]]))

Find will return the found value or undefined

async function dummy(element, needle) {
  return Promise.resolve(element === needle);
}

const inputArray = [1, 2, 3, 4];

// Call Find method
const outputElement = await AsyncRay(inputArray).aFind(
  async (i, index, collection) => {
    return await dummy(i, 2);
  }
);

console.log('Output is ', outputElement);
// Output is 2

.aFindIndex

.aFindIndex(async callback(element[, index[, array]]))

FindIndex will return the index of found value or -1

async function dummy(element, needle) {
  return Promise.resolve(element === needle);
}

const inputArray = [1, 2, 3, 4];

// Call Find method
const outputIndex = await AsyncRay(inputArray).aFindIndex(
  async (i, index, collection) => {
    return await dummy(i, 2);
  }
);

console.log('Output is ', outputIndex);
// Output is 1

.aFlatMap

.aFlatMap(async callback(element[, index[, array]]))
async function dummy(element) {
  return Promise.resolve([element, element * 2]);
}

const inputArray = [1, 2, 3, 4];

// Call Map method
const flatMappedArray = await AsyncRay(inputArray).aFlatMap(
  async (i, index, collection) => {
    // Dummy async function
    return await dummy(i);
  }
);
console.log(flatMappedArray);
// Output is [1, 2, 2, 4, 3, 6, 4, 8]

.aForEach

.aForEach(async callback(element[, index[, array]]))
async function dummy(element) {
  return Promise.resolve(element);
}

const inputArray = [1, 2, 3, 4];
const outputArray = [];

// Call ForEach method
await AsyncRay(inputArray).aForEach(async (i, index, collection) => {
  outputArray.push(await dummy(i));
});

console.log('Output is ', outputArray);
// Output is [1, 2, 3, 4]

.aMap

.aMap(async callback(element[, index[, array]]))
async function dummy(element) {
  return Promise.resolve(element);
}

const inputArray = [1, 2, 3, 4];

// Call Map method
const mappedArray = await AsyncRay(inputArray).aMap(
  async (i, index, collection) => {
    // Dummy async function
    return await dummy(i);
  }
);
console.log(mappedArray);
// Output is [1, 2, 3, 4]

.aReduce

.aReduce(async callback(accumulator, element[, index[, array]]), [initialValue])
async function dummy(element) {
  return Promise.resolve(element);
}

const inputArray = [10, 20, 30, 40];

// Call Reduce method
const output = await AsyncRay(inputArray).aReduce(
  async (acc, i, index, collection) => {
    return acc + (await dummy(i));
  },
  1
);

console.log('Output is ', output);
// Output is 101

.aReduceRight

.aReduceRight(async callback(accumulator, element[, index[, array]]), [initialValue])
async function dummy(element) {
  return Promise.resolve(element);
}

const inputArray = [10, 20, 30, 40];

// Call Reduce method
const output = await AsyncRay(inputArray).aReduceRight(
  async (acc, i, index, collection) => {
    return acc + (await dummy(i));
  },
  1
);

console.log('Output is ', output);
// Output is 101

.aSome

.aSome(async callback(element[, index[, array]]))
async function dummy(element, needle) {
  return Promise.resolve(element > needle);
}

const inputArray = [10, 20, 30, 40];

// Call Some method
const output = await AsyncRay(inputArray).aSome(
  async (i, index, collection) => {
    // Dummy async function
    return await dummy(i, 30);
  }
);

console.log(output);
// Output is true

Using methods individually

You can use each method without creating AsyncRay object.

import {
  aEvery, aFilter, aFind, aFindIndex,
  aForEach, aMap, aReduce, aReduceRight, aSome
} from 'async-ray';

// aEvery
const everyResult = await aEvery(
  [1, 2, 3],
  async (e) => Promise.resolve(e > 0)
);

// aFilter
const filterResult = await aFilter(
  [1, 2, 3],
  async (e) => Promise.resolve(e > 1)
);

// aFind
const findResult = await aFind(
  [1, 2, 3],
  async (e) => Promise.resolve(e === 3)
);

// aFindIndex
const findIndexResult = await aFindIndex(
  [1, 2, 3],
  async (e) => Promise.resolve(e === 2)
);

// aForEach
const forEachResult: number[] = [];
await aForEach(
  [1, 2, 3],
  async (e) => {
    const op = await Promise.resolve(e * 10);
	  forEachResult.push(op);
  }
);

// aMap
const mapResult = await aMap(
  [1, 2, 3],
  async (e) => Promise.resolve(e * 10)
);

// aReduce
const reduceResult = await aReduce(
  [1, 2, 3],
  async (acc, e) => Promise.resolve(e + acc),
  0
);

// aReduceRight
const reduceRightResult = await aReduceRight(
  [1, 2, 3],
  async (acc, e) => Promise.resolve(e + acc),
  0
);

// aSome
const someResult = await aSome(
  [1, 2, 3],
  async (e) => Promise.resolve(e > 1)
);

Chaining

Async-Ray methods can be chained using Chain functionality

Basic usage

const { Chain } = require('async-ray');

Chaining will return an instance of Async-Ray if returned type is an array.


sample 1 - aMap() and aFilter()

(sample code)

The process() method must be called explicitly to process the chain because aMap() and aFilter() method returns an array.

const input = [1, 2, 3];

const op = await Chain(input)
  .aMap(
    async (e) => Promise.resolve(e * 10)
  )
  .aFilter(
    async (e) => Promise.resolve(e > 10)
  )
  .aMap(
    async (e) => Promise.resolve(e * 10)
  )
  // Call the process() method to execute the chain
  .process();

console.log('Output is ', op);
// Output is [ 200, 300 ]

sample 2 - aMap(), aFilter() and aFind()

(sample code)

The process() method should not be called because aFind() does not return an array.

const input = [1, 2, 3];

const op = await Chain(input)
  .aMap(
    async (e) => Promise.resolve(e * 10)
  )
  .aFilter(
    async (e) => Promise.resolve(e > 10)
  )
  .aMap(
    async (e) => Promise.resolve(e * 10)
  )
  .aFind(
    async (e) => Promise.resolve(e === 300)
  );
  // No need to call process() method

console.log('Output is ', op);
// Output is 300

Between other Array methods methods


Sample 1 - Async-Ray Chain with filter()

const input = [1, 2, 3];

const op = (
  await Chain(input)
    .aMap(
      async (e) => Promise.resolve(e * 10)
    )
    .aFilter(
      async (e) => Promise.resolve(e > 10)
    )
    .aMap(
      async (e) => Promise.resolve(e * 10)
    )
    .process()
)
.filter(e => e > 200)

console.log('Output is ', op);
// Output is [ 300 ]

Sample 2 - Async-Ray Chain with find()

const input = [1, 2, 3];

const op = (
    await Chain(input)
    .aMap(
      async (e) => Promise.resolve(e * 10)
    )
    .aFilter(
      async (e) => Promise.resolve(e > 10)
    )
    .aMap(
      async (e) => Promise.resolve(e * 10)
    )
    .process()
)
.find(e => e === 200)

console.log('Output is ', op);
// Output is 200
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].