smartarray/readme.md

96 lines
3.8 KiB
Markdown
Raw Normal View History

2024-04-26 13:36:25 +00:00
```markdown
2023-08-02 12:39:58 +00:00
# @push.rocks/smartarray
2021-06-29 18:41:15 +00:00
a package exposing async manipulation for arrays
2024-04-14 15:20:59 +00:00
## Install
To add `@push.rocks/smartarray` to your project, run the following command:
```bash
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.
2021-06-29 18:41:15 +00:00
## Usage
2024-04-26 13:36:25 +00:00
`@push.rocks/smartarray` is designed to enhance array manipulation with asynchronous operations in TypeScript. Below, we explore various functionalities provided by the package, including mapping, filtering, and deduplication, with a focus on their asynchronous nature.
2024-04-14 15:20:59 +00:00
2024-04-26 13:36:25 +00:00
### Getting Started
2024-04-14 15:20:59 +00:00
2024-04-26 13:36:25 +00:00
To use `@push.rocks/smartarray`, first import the functionalities you require:
2024-04-14 15:20:59 +00:00
```typescript
2024-04-26 13:36:25 +00:00
import { map, filter, deduplicate } from '@push.rocks/smartarray';
2024-04-14 15:20:59 +00:00
```
2024-04-26 13:36:25 +00:00
### Mapping Arrays Asynchronously
2024-04-14 15:20:59 +00:00
2024-04-26 13:36:25 +00:00
The `map` function allows you to apply an asynchronous function to each item in an array and collect the results.
2024-04-14 15:20:59 +00:00
2024-04-26 13:36:25 +00:00
Example:
2024-04-14 15:20:59 +00:00
```typescript
2024-04-26 13:36:25 +00:00
const numbers = [1, 2, 3];
const doubledNumbers = await map(numbers, async (number) => {
return number * 2;
});
console.log(doubledNumbers); // [2, 4, 6]
```
### Filtering Arrays Asynchronously
You can asynchronously decide which items to keep in an array using the `filter` function.
Example:
```typescript
const userIds = [1, 2, 3, 4];
2024-04-14 15:20:59 +00:00
const activeUserIds = await filter(userIds, async (userId) => {
2024-04-26 13:36:25 +00:00
// Let's assume isActiveUser is an async function returning a boolean
2024-04-14 15:20:59 +00:00
return isActiveUser(userId);
});
2024-04-26 13:36:25 +00:00
console.log(activeUserIds); // Assuming users 1, 3, and 4 are active: [1, 3, 4]
2024-04-14 15:20:59 +00:00
```
2024-04-26 13:36:25 +00:00
### Deduplicating Arrays Asynchronously
2024-04-14 15:20:59 +00:00
2024-04-26 13:36:25 +00:00
Deduplicate an array based on a unique key derived asynchronously from each element using the `deduplicate` function.
2024-04-14 15:20:59 +00:00
2024-04-26 13:36:25 +00:00
Example:
2024-04-14 15:20:59 +00:00
```typescript
2024-04-26 13:36:25 +00:00
const users = [
{ id: 1, email: 'user1@example.com' },
{ id: 2, email: 'user2@example.com' },
{ id: 1, email: 'user1@example.com' } // Duplicate based on 'id'
2024-04-14 15:20:59 +00:00
];
2024-04-26 13:36:25 +00:00
const uniqueUsers = await deduplicate(users, async (user) => {
return user.id;
2024-04-14 15:20:59 +00:00
});
2024-04-26 13:36:25 +00:00
console.log(uniqueUsers);
// Output: [{ id: 1, email: 'user1@example.com' }, { id: 2, email: 'user2@example.com' }]
2024-04-14 15:20:59 +00:00
```
2024-04-26 13:36:25 +00:00
These examples underline the versatility and power of `@push.rocks/smartarray` in handling arrays with asynchronous operations, making it easier to integrate asynchronous logic into array manipulation tasks. Remember to handle asynchronous operations with `async/await` syntax to maintain clear and comprehensible code.
2024-04-14 15:20:59 +00:00
2024-04-26 13:36:25 +00:00
```
2024-04-14 15:20:59 +00:00
## 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](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.
2021-06-29 18:41:15 +00:00
2024-04-14 15:20:59 +00:00
### Company Information
2021-06-29 18:41:15 +00:00
2024-04-14 15:20:59 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2021-06-29 18:41:15 +00:00
2024-04-14 15:20:59 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2021-06-29 18:41:15 +00:00
2024-04-14 15:20:59 +00:00
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.