Compare commits

..

No commits in common. "master" and "v1.0.6" have entirely different histories.

8 changed files with 2811 additions and 7223 deletions

View File

@ -1,29 +0,0 @@
# Changelog
## 2025-01-04 - 1.1.0 - feat(core)
Add concurrent processing support
- Introduced ConcurrentProcessor class for handling asynchronous operations with a concurrency limit.
## 2024-05-29 - 1.0.8 - Documentation
Updated project description for clarity and accuracy.
- Enhanced documentation details in the project description.
## 2024-04-01 to 2024-04-27 - 1.0.5 to 1.0.7 - Maintenance and Configuration Updates
Multiple updates focusing on configuration and core functionality.
- Updated `tsconfig` for improved TypeScript setup.
- Refined `npmextra.json` to configure `githost`.
- Applied numerous core functionality enhancements and fixes.
## 2023-07-10 to 2023-08-02 - 1.0.3 to 1.0.4 - Organizational Changes and Fixes
Transition to new organizational structure and related updates.
- Implemented a new organizational scheme for the project.
- Applied essential fixes to core components ensuring stability.
## 2021-06-29 to 2021-06-30 - 1.0.1 to 1.0.2 - Core Fixes
Addressed core system updates and enhancements.
- Implemented bug fixes to enhance core functionality.

View File

@ -5,20 +5,22 @@
"githost": "code.foss.global", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartarray", "gitrepo": "smartarray",
"description": "A TypeScript library for enhancing array manipulation with asynchronous operations such as mapping, filtering, and deduplication.", "description": "a package exposing async manipulation for arrays",
"npmPackagename": "@push.rocks/smartarray", "npmPackagename": "@push.rocks/smartarray",
"license": "MIT", "license": "MIT",
"projectDomain": "push.rocks", "projectDomain": "push.rocks",
"keywords": [ "keywords": [
"TypeScript", "async",
"asynchronous programming", "arrays",
"array manipulation", "manipulation",
"array mapping", "filter",
"array filtering", "deduplicate",
"deduplication", "typescript",
"async/await", "nodejs",
"software development", "async programming",
"development",
"npm package", "npm package",
"code quality",
"open source" "open source"
] ]
} }

View File

@ -1,8 +1,8 @@
{ {
"name": "@push.rocks/smartarray", "name": "@push.rocks/smartarray",
"version": "1.1.0", "version": "1.0.6",
"private": false, "private": false,
"description": "A TypeScript library for enhancing array manipulation with asynchronous operations such as mapping, filtering, and deduplication.", "description": "a package exposing async manipulation for arrays",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH", "author": "Lossless GmbH",
@ -13,12 +13,12 @@
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.2.0", "@git.zone/tsbuild": "^2.1.66",
"@git.zone/tsbundle": "^2.1.0", "@git.zone/tsbundle": "^2.0.8",
"@git.zone/tsrun": "^1.3.3", "@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.77", "@git.zone/tstest": "^1.0.77",
"@push.rocks/tapbundle": "^5.5.4", "@push.rocks/tapbundle": "^5.0.12",
"@types/node": "^22.10.5" "@types/node": "^20.4.6"
}, },
"browserslist": [ "browserslist": [
"last 1 chrome versions" "last 1 chrome versions"
@ -37,20 +37,17 @@
], ],
"type": "module", "type": "module",
"keywords": [ "keywords": [
"TypeScript", "async",
"asynchronous programming", "arrays",
"array manipulation", "manipulation",
"array mapping", "filter",
"array filtering", "deduplicate",
"deduplication", "typescript",
"async/await", "nodejs",
"software development", "async programming",
"development",
"npm package", "npm package",
"code quality",
"open source" "open source"
], ]
"homepage": "https://code.foss.global/push.rocks/smartarray",
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smartarray.git"
}
} }

9786
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

105
readme.md
View File

@ -1,109 +1,68 @@
# @push.rocks/smartarray # @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 ## 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 ```bash
npm install @push.rocks/smartarray --save npm install @push.rocks/smartarray --save
``` ```
Make sure you have Node.js and npm installed beforehand. 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.
## Usage ## Usage
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. `@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.
### Importing the Library ### Importing the module
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: First, import the functions you need from the module:
```typescript ```typescript
import { map, filter, deduplicate } from '@push.rocks/smartarray'; import { filter, deduplicate } from '@push.rocks/smartarray';
``` ```
### Async Map: Transforming Arrays ### Filtering an Array Asynchronously
The `map` function lets you apply an asynchronous operation to each item in an array, constructing a new array with the transformed items. 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.
#### Example: Doubling Numbers 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:
```typescript ```typescript
const numbers = [1, 2, 3, 4]; const userIds = [1, 2, 3, 4, 5];
const doubleNumbers = await map(numbers, async (number) => number * 2); const activeUserIds = await filter(userIds, async (userId) => {
console.log(doubleNumbers); // Output: [2, 4, 6, 8] return isActiveUser(userId);
});
console.log(activeUserIds); // Logs: [1, 3, 5] assuming these IDs are active
``` ```
#### Async Filter: Conditional Array Traversal ### Deduplicating an Array Asynchronously
With the `filter` function, you can asynchronously judge whether to keep or remove items from the array. 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.
#### Example: Filtering Even Numbers 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:
```typescript ```typescript
const numbers = [1, 2, 3, 4, 5, 6]; const applications = [
const evenNumbers = await filter(numbers, async (number) => number % 2 === 0); { userId: 1, applicationId: 'a1' },
console.log(evenNumbers); // Output: [2, 4, 6] { userId: 2, applicationId: 'a2' },
``` { userId: 1, applicationId: 'a3' }, // Duplicate user
{ userId: 3, applicationId: 'a4' }
### Async Deduplicate: Removing Duplication
The `deduplicate` function excels in removing duplicates from an array based on asynchronously derived unique keys for each element.
#### Example: Deduplicating User Array
```typescript
const users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 1, name: 'John' }
];
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 uniqueApplications = await deduplicate(applications, async (app) => {
const activeUsers = await filter(users, async (user) => user.active); // The key is the userId, ensuring uniqueness by user
return app.userId;
});
// Next, transform the user IDs console.log(uniqueApplications);
const transformedUsers = await map(activeUsers, async (user) => ({ // Logs: [{ userId: 1, applicationId: 'a1' }, { userId: 2, applicationId: 'a2' }, { userId: 3, applicationId: 'a4' }]
...user,
id: user.id * 2
}));
// Finally, deduplicate based on usernames
const uniqueUsers = await deduplicate(transformedUsers, async (user) => user.username);
console.log(uniqueUsers);
``` ```
In this example, the second application from the same user (`userId: 1`) was removed, leaving only unique user applications in the array.
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. 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.
### Conclusion 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.
`@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.
## License and Legal Information ## License and Legal Information

View File

@ -1,8 +1,8 @@
/** /**
* autocreated commitinfo by @push.rocks/commitinfo * autocreated commitinfo by @pushrocks/commitinfo
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartarray', name: '@push.rocks/smartarray',
version: '1.1.0', version: '1.0.6',
description: 'A TypeScript library for enhancing array manipulation with asynchronous operations such as mapping, filtering, and deduplication.' description: 'a package exposing async manipulation for arrays'
} }

View File

@ -1,35 +0,0 @@
export class ConcurrentProcessor<T> {
private asyncFunction: (item: T) => Promise<void>;
private concurrencyLimit: number;
constructor(asyncFunction: (item: T) => Promise<void>, concurrencyLimit: number) {
this.asyncFunction = asyncFunction;
this.concurrencyLimit = concurrencyLimit;
}
async process(items: T[]): Promise<void> {
const queue: Array<Promise<void>> = [];
let totalProcessed = 0;
for (const item of items) {
const promise = this.asyncFunction(item).then(() => {
totalProcessed++;
if (totalProcessed % 10000 === 0) {
console.log(`${totalProcessed} items processed.`);
}
// Remove the completed promise from the queue
queue.splice(queue.indexOf(promise), 1);
});
queue.push(promise);
// Wait if the number of promises exceeds the concurrency limit
if (queue.length >= this.concurrencyLimit) {
await Promise.race(queue);
}
}
// Wait for the remaining promises to resolve
await Promise.all(queue);
}
}

View File

@ -1,5 +1,3 @@
export * from './classes.concurrentprocessor.js';
export const map = async <T = any, R = any>( export const map = async <T = any, R = any>(
arrayArg: T[], arrayArg: T[],
mapFunction: (itemArg: T) => Promise<R> mapFunction: (itemArg: T) => Promise<R>