smartarray/readme.md

126 lines
5.7 KiB
Markdown
Raw Normal View History

2023-08-02 12:39:58 +00:00
# @push.rocks/smartarray
2024-04-27 06:12:29 +00:00
A library providing asynchronous operations like filter, map, and deduplication for arrays in TypeScript.
2021-06-29 18:41:15 +00:00
2024-04-14 15:20:59 +00:00
## Install
2024-04-27 06:12:29 +00:00
To install `@push.rocks/smartarray` in your project, run the following command:
2024-04-14 15:20:59 +00:00
```bash
npm install @push.rocks/smartarray --save
```
2024-04-27 06:12:29 +00:00
Make sure you have Node.js and npm installed beforehand.
2021-06-29 18:41:15 +00:00
## Usage
2024-04-27 06:12:29 +00:00
The `@push.rocks/smartarray` library is designed to facilitate asynchronous array operations in TypeScript projects. It simplifies tasks like mapping, filtering, and deduplication by embracing async/await patterns, making it an invaluable tool for modern JavaScript development. Below, we delve into the capabilities of this library, providing comprehensive examples to illustrate its use in a variety of scenarios.
2024-04-14 15:20:59 +00:00
2024-04-27 06:12:29 +00:00
### Importing the Library
2024-04-14 15:20:59 +00:00
2024-04-27 06:12:29 +00:00
Before you can utilize the library's functions, you need to import them into your TypeScript files. Depending on your use case, you can import specific functions or the entire library:
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-27 06:12:29 +00:00
### Async Map: Transforming Arrays
2024-04-14 15:20:59 +00:00
2024-04-27 06:12:29 +00:00
The `map` function lets you apply an asynchronous operation to each item in an array, constructing a new array with the transformed items.
2024-04-14 15:20:59 +00:00
2024-04-27 06:12:29 +00:00
#### Example: Doubling Numbers
2024-04-14 15:20:59 +00:00
```typescript
2024-04-27 06:12:29 +00:00
const numbers = [1, 2, 3, 4];
const doubleNumbers = await map(numbers, async (number) => number * 2);
console.log(doubleNumbers); // Output: [2, 4, 6, 8]
2024-04-26 13:36:25 +00:00
```
2024-04-27 06:12:29 +00:00
#### Async Filter: Conditional Array Traversal
2024-04-26 13:36:25 +00:00
2024-04-27 06:12:29 +00:00
With the `filter` function, you can asynchronously judge whether to keep or remove items from the array.
2024-04-26 13:36:25 +00:00
2024-04-27 06:12:29 +00:00
#### Example: Filtering Even Numbers
2024-04-26 13:36:25 +00:00
```typescript
2024-04-27 06:12:29 +00:00
const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = await filter(numbers, async (number) => number % 2 === 0);
console.log(evenNumbers); // Output: [2, 4, 6]
2024-04-14 15:20:59 +00:00
```
2024-04-27 06:12:29 +00:00
### Async Deduplicate: Removing Duplication
2024-04-14 15:20:59 +00:00
2024-04-27 06:12:29 +00:00
The `deduplicate` function excels in removing duplicates from an array based on asynchronously derived unique keys for each element.
2024-04-14 15:20:59 +00:00
2024-04-27 06:12:29 +00:00
#### Example: Deduplicating User Array
2024-04-14 15:20:59 +00:00
```typescript
2024-04-26 13:36:25 +00:00
const users = [
2024-04-27 06:12:29 +00:00
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 1, name: 'John' }
2024-04-14 15:20:59 +00:00
];
2024-04-27 06:12:29 +00:00
const deduplicatedUsers = await deduplicate(users, async (user) => user.id);
console.log(deduplicatedUsers);
// Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
2024-04-14 15:20:59 +00:00
```
2024-04-27 06:12:29 +00:00
### Deep-Dive Use Cases
#### Complex Data Transformation
Imagine you're working with a dataset of user objects fetched from an API, and you need to perform several transformations: filter out inactive users, double the user IDs for a new report, and ensure the list is deduplicated based on usernames.
2024-04-14 15:20:59 +00:00
2024-04-27 06:12:29 +00:00
```typescript
import { map, filter, deduplicate } from '@push.rocks/smartarray';
// Example users array
const users = [
{ id: 1, active: true, username: 'user1' },
{ id: 2, active: false, username: 'user2' },
{ id: 3, active: true, username: 'user3' },
{ id: 1, active: true, username: 'user1' } // Duplicate for demonstration
];
// First, filter out inactive users
const activeUsers = await filter(users, async (user) => user.active);
// Next, transform the user IDs
const transformedUsers = await map(activeUsers, async (user) => ({
...user,
id: user.id * 2
}));
// Finally, deduplicate based on usernames
const uniqueUsers = await deduplicate(transformedUsers, async (user) => user.username);
console.log(uniqueUsers);
2024-04-26 13:36:25 +00:00
```
2024-04-14 15:20:59 +00:00
2024-04-27 06:12:29 +00:00
This example demonstrates `@push.rocks/smartarray`'s power in handling complex, asynchronous data operations in an efficient, readable manner. By chaining these methods, you can achieve sophisticated data manipulation objectives with minimal code.
### Conclusion
`@push.rocks/smartarray` significantly simplifies the development experience when working with arrays in asynchronous environments. It not only enhances readability and maintainability but also ensures that your codebase remains scalable and efficient. By integrating this library into your projects, you unlock a higher level of programming paradigm where array manipulations are no longer a chore but a streamlined process.
For developers aiming to harness the full potential of asynchronous operations in TypeScript, `@push.rocks/smartarray` offers a comprehensive, easy-to-use solution that stands out for its performance and versatility. Whether youre mapping, filtering, or deduplicating arrays, this library empowers you to write cleaner, more efficient code, elevating your development workflow to new heights.
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.