A TypeScript library for enhancing array manipulation with asynchronous operations such as mapping, filtering, and deduplication.
Go to file
2024-04-26 14:05:49 +02:00
.gitea/workflows fix(core): update 2024-04-26 14:05:48 +02:00
.vscode fix(core): update 2023-08-02 14:39:58 +02:00
test fix(core): update 2023-08-02 14:39:58 +02:00
ts fix(core): update 2024-04-26 14:05:48 +02:00
.gitignore fix(core): update 2021-06-29 20:41:15 +02:00
.gitlab-ci.yml fix(core): update 2024-04-26 14:05:48 +02:00
license fix(core): update 2021-06-29 20:41:15 +02:00
npmextra.json update tsconfig 2024-04-14 17:20:59 +02:00
package.json 1.0.6 2024-04-26 14:05:49 +02:00
pnpm-lock.yaml fix(core): update 2024-04-26 14:05:48 +02:00
readme.hints.md update tsconfig 2024-04-14 17:20:59 +02:00
readme.md update tsconfig 2024-04-14 17:20:59 +02:00
tsconfig.json update npmextra.json: githost 2024-04-01 21:33:50 +02:00

@push.rocks/smartarray

a package exposing async manipulation for arrays

Install

To add @push.rocks/smartarray to your project, run the following command:

npm install @push.rocks/smartarray --save

This will install the package and add it to your project's dependencies. Ensure you have Node.js and npm installed on your machine before running this command.

Usage

@push.rocks/smartarray simplifies the manipulation of arrays with asynchronous operations in TypeScript. It provides utility functions such as filtering and deduplication of arrays with asynchronous predicates or key generation functions. Let's delve into code examples to understand how to utilize these features effectively.

Importing the module

First, import the functions you need from the module:

import { filter, deduplicate } from '@push.rocks/smartarray';

Filtering an Array Asynchronously

The filter function allows you to filter an array based on a predicate function that returns a Promise. This is particularly useful when your filtering condition involves asynchronous operations, such as fetching data from an API or accessing a database.

Suppose you have an array of user IDs, and you want to filter out IDs that do not correspond to active users. You might have an asynchronous function isActiveUser(userId): Promise<boolean> that checks user's status. Here's how you can filter out inactive user IDs:

const userIds = [1, 2, 3, 4, 5];
const activeUserIds = await filter(userIds, async (userId) => {
  return isActiveUser(userId);
});
console.log(activeUserIds); // Logs: [1, 3, 5] assuming these IDs are active

Deduplicating an Array Asynchronously

The deduplicate function removes duplicate elements from an array based on a key generated by an asynchronous function. This is handy when you need to remove duplicates based on complex or asynchronous criteria.

Imagine you have an array of objects representing job applications, where each object contains a userId and applicationId. If you want to ensure that there's only one application per user, you could use the deduplicate function as shown:

const applications = [
  { userId: 1, applicationId: 'a1' },
  { userId: 2, applicationId: 'a2' },
  { userId: 1, applicationId: 'a3' }, // Duplicate user
  { userId: 3, applicationId: 'a4' }
];

const uniqueApplications = await deduplicate(applications, async (app) => {
  // The key is the userId, ensuring uniqueness by user
  return app.userId;
});

console.log(uniqueApplications);
// Logs: [{ userId: 1, applicationId: 'a1' }, { userId: 2, applicationId: 'a2' }, { userId: 3, applicationId: 'a4' }]

In this example, the second application from the same user (userId: 1) was removed, leaving only unique user applications in the array.

These examples illustrate how @push.rocks/smartarray can be utilized for asynchronous array manipulation, offering flexibility and performance for handling complex data processing tasks in a modern JavaScript or TypeScript application.

Remember, all operations return promises, so ensure you handle them properly using async/await syntax or .then().catch() chains according to your application's structure or personal preference.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.