From 92e251b8d4d9e2cd1224c691cbdf1a448ccd7b43 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sat, 27 Apr 2024 08:12:29 +0200 Subject: [PATCH] fix(core): update --- npmextra.json | 13 ++--- package.json | 15 +++--- readme.md | 102 +++++++++++++++++++++++++-------------- ts/00_commitinfo_data.ts | 4 +- 4 files changed, 83 insertions(+), 51 deletions(-) diff --git a/npmextra.json b/npmextra.json index 8790833..adce3db 100644 --- a/npmextra.json +++ b/npmextra.json @@ -5,19 +5,20 @@ "githost": "code.foss.global", "gitscope": "push.rocks", "gitrepo": "smartarray", - "description": "A library providing asynchronous operations like filter, map, and deduplication for arrays in TypeScript.", + "description": "A TypeScript library for enhancing array manipulation with asynchronous operations such as mapping, filtering, and deduplication.", "npmPackagename": "@push.rocks/smartarray", "license": "MIT", "projectDomain": "push.rocks", "keywords": [ - "asynchronous array operations", - "typescript library", - "array filtering", + "TypeScript", + "asynchronous programming", + "array manipulation", "array mapping", - "array deduplication", + "array filtering", + "deduplication", "async/await", + "software development", "npm package", - "modern JavaScript development", "open source" ] } diff --git a/package.json b/package.json index d45ac81..fe6cb56 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@push.rocks/smartarray", "version": "1.0.7", "private": false, - "description": "A library providing asynchronous operations like filter, map, and deduplication for arrays in TypeScript.", + "description": "A TypeScript library for enhancing array manipulation with asynchronous operations such as mapping, filtering, and deduplication.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "author": "Lossless GmbH", @@ -37,14 +37,15 @@ ], "type": "module", "keywords": [ - "asynchronous array operations", - "typescript library", - "array filtering", + "TypeScript", + "asynchronous programming", + "array manipulation", "array mapping", - "array deduplication", + "array filtering", + "deduplication", "async/await", + "software development", "npm package", - "modern JavaScript development", "open source" ] -} +} \ No newline at end of file diff --git a/readme.md b/readme.md index 1d6ff3b..58928af 100644 --- a/readme.md +++ b/readme.md @@ -1,79 +1,109 @@ -```markdown # @push.rocks/smartarray -a package exposing async manipulation for arrays + +A library providing asynchronous operations like filter, map, and deduplication for arrays in TypeScript. ## Install -To add `@push.rocks/smartarray` to your project, run the following command: + +To install `@push.rocks/smartarray` in 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. +Make sure you have Node.js and npm installed beforehand. ## Usage -`@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. +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. -### Getting Started +### Importing the Library -To use `@push.rocks/smartarray`, first import the functionalities you require: +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: ```typescript import { map, filter, deduplicate } from '@push.rocks/smartarray'; ``` -### Mapping Arrays Asynchronously +### Async Map: Transforming Arrays -The `map` function allows you to apply an asynchronous function to each item in an array and collect the results. +The `map` function lets you apply an asynchronous operation to each item in an array, constructing a new array with the transformed items. -Example: +#### Example: Doubling Numbers ```typescript -const numbers = [1, 2, 3]; -const doubledNumbers = await map(numbers, async (number) => { - return number * 2; -}); -console.log(doubledNumbers); // [2, 4, 6] +const numbers = [1, 2, 3, 4]; +const doubleNumbers = await map(numbers, async (number) => number * 2); +console.log(doubleNumbers); // Output: [2, 4, 6, 8] ``` -### Filtering Arrays Asynchronously +#### Async Filter: Conditional Array Traversal -You can asynchronously decide which items to keep in an array using the `filter` function. +With the `filter` function, you can asynchronously judge whether to keep or remove items from the array. -Example: +#### Example: Filtering Even Numbers ```typescript -const userIds = [1, 2, 3, 4]; -const activeUserIds = await filter(userIds, async (userId) => { - // Let's assume isActiveUser is an async function returning a boolean - return isActiveUser(userId); -}); -console.log(activeUserIds); // Assuming users 1, 3, and 4 are active: [1, 3, 4] +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] ``` -### Deduplicating Arrays Asynchronously +### Async Deduplicate: Removing Duplication -Deduplicate an array based on a unique key derived asynchronously from each element using the `deduplicate` function. +The `deduplicate` function excels in removing duplicates from an array based on asynchronously derived unique keys for each element. -Example: +#### Example: Deduplicating User Array ```typescript const users = [ - { id: 1, email: 'user1@example.com' }, - { id: 2, email: 'user2@example.com' }, - { id: 1, email: 'user1@example.com' } // Duplicate based on 'id' + { id: 1, name: 'John' }, + { id: 2, name: 'Jane' }, + { id: 1, name: 'John' } ]; -const uniqueUsers = await deduplicate(users, async (user) => { - return user.id; -}); +const deduplicatedUsers = await deduplicate(users, async (user) => user.id); +console.log(deduplicatedUsers); +// Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }] +``` + +### 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. + +```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); -// Output: [{ id: 1, email: 'user1@example.com' }, { id: 2, email: 'user2@example.com' }] ``` -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. +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 you’re mapping, filtering, or deduplicating arrays, this library empowers you to write cleaner, more efficient code, elevating your development workflow to new heights. ## License and Legal Information diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 21707a4..c1e3184 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartarray', - version: '1.0.7', - description: 'A library providing asynchronous operations like filter, map, and deduplication for arrays in TypeScript.' + version: '1.0.8', + description: 'A TypeScript library for enhancing array manipulation with asynchronous operations such as mapping, filtering, and deduplication.' }