From 795c7231eb64cf1d0dbf2784b2b8a51acad166a9 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 26 Apr 2024 15:36:25 +0200 Subject: [PATCH] fix(core): update --- npmextra.json | 19 +++++------- package.json | 21 ++++++------- readme.md | 65 +++++++++++++++++++++++----------------- ts/00_commitinfo_data.ts | 4 +-- 4 files changed, 57 insertions(+), 52 deletions(-) diff --git a/npmextra.json b/npmextra.json index 096bc62..8790833 100644 --- a/npmextra.json +++ b/npmextra.json @@ -5,22 +5,19 @@ "githost": "code.foss.global", "gitscope": "push.rocks", "gitrepo": "smartarray", - "description": "a package exposing async manipulation for arrays", + "description": "A library providing asynchronous operations like filter, map, and deduplication for arrays in TypeScript.", "npmPackagename": "@push.rocks/smartarray", "license": "MIT", "projectDomain": "push.rocks", "keywords": [ - "async", - "arrays", - "manipulation", - "filter", - "deduplicate", - "typescript", - "nodejs", - "async programming", - "development", + "asynchronous array operations", + "typescript library", + "array filtering", + "array mapping", + "array deduplication", + "async/await", "npm package", - "code quality", + "modern JavaScript development", "open source" ] } diff --git a/package.json b/package.json index 70ba77f..74556e7 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@push.rocks/smartarray", "version": "1.0.6", "private": false, - "description": "a package exposing async manipulation for arrays", + "description": "A library providing asynchronous operations like filter, map, and deduplication for arrays in TypeScript.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "author": "Lossless GmbH", @@ -37,17 +37,14 @@ ], "type": "module", "keywords": [ - "async", - "arrays", - "manipulation", - "filter", - "deduplicate", - "typescript", - "nodejs", - "async programming", - "development", + "asynchronous array operations", + "typescript library", + "array filtering", + "array mapping", + "array deduplication", + "async/await", "npm package", - "code quality", + "modern JavaScript development", "open source" ] -} +} \ No newline at end of file diff --git a/readme.md b/readme.md index 4419c86..1d6ff3b 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,4 @@ +```markdown # @push.rocks/smartarray a package exposing async manipulation for arrays @@ -12,57 +13,67 @@ This will install the package and add it to your project's dependencies. Ensure ## 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. +`@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. -### Importing the module +### Getting Started -First, import the functions you need from the module: +To use `@push.rocks/smartarray`, first import the functionalities you require: ```typescript -import { filter, deduplicate } from '@push.rocks/smartarray'; +import { map, filter, deduplicate } from '@push.rocks/smartarray'; ``` -### Filtering an Array Asynchronously +### Mapping Arrays 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. +The `map` function allows you to apply an asynchronous function to each item in an array and collect the results. -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` that checks user's status. Here's how you can filter out inactive user IDs: +Example: ```typescript -const userIds = [1, 2, 3, 4, 5]; +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]; const activeUserIds = await filter(userIds, async (userId) => { + // Let's assume isActiveUser is an async function returning a boolean return isActiveUser(userId); }); -console.log(activeUserIds); // Logs: [1, 3, 5] assuming these IDs are active +console.log(activeUserIds); // Assuming users 1, 3, and 4 are active: [1, 3, 4] ``` -### Deduplicating an Array Asynchronously +### Deduplicating Arrays 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. +Deduplicate an array based on a unique key derived asynchronously from each element using the `deduplicate` function. -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: +Example: ```typescript -const applications = [ - { userId: 1, applicationId: 'a1' }, - { userId: 2, applicationId: 'a2' }, - { userId: 1, applicationId: 'a3' }, // Duplicate user - { userId: 3, applicationId: 'a4' } +const users = [ + { id: 1, email: 'user1@example.com' }, + { id: 2, email: 'user2@example.com' }, + { id: 1, email: 'user1@example.com' } // Duplicate based on 'id' ]; - -const uniqueApplications = await deduplicate(applications, async (app) => { - // The key is the userId, ensuring uniqueness by user - return app.userId; +const uniqueUsers = await deduplicate(users, async (user) => { + return user.id; }); - -console.log(uniqueApplications); -// Logs: [{ userId: 1, applicationId: 'a1' }, { userId: 2, applicationId: 'a2' }, { userId: 3, applicationId: 'a4' }] +console.log(uniqueUsers); +// Output: [{ id: 1, email: 'user1@example.com' }, { id: 2, email: 'user2@example.com' }] ``` -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. +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. -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 diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 88facfb..21707a4 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.6', - description: 'a package exposing async manipulation for arrays' + version: '1.0.7', + description: 'A library providing asynchronous operations like filter, map, and deduplication for arrays in TypeScript.' }