fix(core): update

This commit is contained in:
Philipp Kunz 2024-04-26 15:36:25 +02:00
parent 2a9f1622a5
commit 795c7231eb
4 changed files with 57 additions and 52 deletions

View File

@ -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"
]
}

View File

@ -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"
]
}
}

View File

@ -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<boolean>` 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

View File

@ -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.'
}