.gitea/workflows | ||
.vscode | ||
test | ||
ts | ||
.gitignore | ||
.gitlab-ci.yml | ||
license | ||
npmextra.json | ||
package.json | ||
pnpm-lock.yaml | ||
readme.hints.md | ||
readme.md | ||
tsconfig.json |
@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.
License and Legal Information
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.