@coast/service-common 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ export declare class Generators {
2
+ static chunked<T>(iterator: AsyncGenerator<T>, size: number): AsyncGenerator<T[]>;
3
+ static take<T>(iterator: AsyncGenerator<T>, size: number): AsyncGenerator<T>;
4
+ static map<T, Y>(iterator: AsyncGenerator<T>, mapper: (data: T) => Y): AsyncGenerator<Y>;
5
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Generators = void 0;
4
+ class Generators {
5
+ static async *chunked(iterator, size) {
6
+ let chunk = [];
7
+ for await (const item of iterator) {
8
+ chunk.push(item);
9
+ if (chunk.length >= size) {
10
+ yield chunk;
11
+ chunk = [];
12
+ }
13
+ }
14
+ yield chunk;
15
+ }
16
+ static async *take(iterator, size) {
17
+ let count = 0;
18
+ for await (const item of iterator) {
19
+ if (count < size) {
20
+ yield item;
21
+ count++;
22
+ }
23
+ else {
24
+ break;
25
+ }
26
+ }
27
+ }
28
+ static async *map(iterator, mapper) {
29
+ for await (const item of iterator) {
30
+ yield mapper(item);
31
+ }
32
+ }
33
+ }
34
+ exports.Generators = Generators;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const Generators_1 = require("../Generators");
4
+ const common_test_1 = require("@paradoxical-io/common-test");
5
+ test('take async', async () => {
6
+ const data = [];
7
+ for await (const i of Generators_1.Generators.take(infinityAsync(), 10)) {
8
+ data.push(i);
9
+ }
10
+ (0, common_test_1.safeExpect)(data).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
11
+ });
12
+ test('chunked async', async () => {
13
+ const data = [];
14
+ const chunked = Generators_1.Generators.chunked(infinityAsync(), 2);
15
+ for await (const i of Generators_1.Generators.take(chunked, 2)) {
16
+ data.push(i);
17
+ }
18
+ (0, common_test_1.safeExpect)(data).toEqual([
19
+ [0, 1],
20
+ [2, 3],
21
+ ]);
22
+ });
23
+ test('chunked async with end', async () => {
24
+ const data = [];
25
+ const chunked = Generators_1.Generators.chunked(Generators_1.Generators.take(infinityAsync(), 10), 4);
26
+ for await (const i of Generators_1.Generators.take(chunked, 3)) {
27
+ data.push(i);
28
+ }
29
+ (0, common_test_1.safeExpect)(data).toEqual([
30
+ [0, 1, 2, 3],
31
+ [4, 5, 6, 7],
32
+ [8, 9],
33
+ ]);
34
+ });
35
+ test('map async', async () => {
36
+ const data = [];
37
+ const mapped = Generators_1.Generators.map(infinityAsync(), (i) => i.toString());
38
+ for await (const i of Generators_1.Generators.take(mapped, 2)) {
39
+ data.push(i);
40
+ }
41
+ (0, common_test_1.safeExpect)(data).toEqual(['0', '1']);
42
+ });
43
+ async function* infinityAsync() {
44
+ let start = 0;
45
+ while (true) {
46
+ yield start;
47
+ start++;
48
+ }
49
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coast/service-common",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Common service package",
5
5
  "main": "./modules",
6
6
  "types": "./modules",
@@ -10,6 +10,13 @@
10
10
  "exports": {
11
11
  "./*": "./dist/modules/*"
12
12
  },
13
+ "typesVersions": {
14
+ "*": {
15
+ "*": [
16
+ "./dist/modules/*"
17
+ ]
18
+ }
19
+ },
13
20
  "files": [
14
21
  "dist",
15
22
  "README.md",
@@ -31,6 +38,7 @@
31
38
  "@nestjs/common": "^10.4.1",
32
39
  "@nestjs/config": "^3.2.3",
33
40
  "@nestjs/schematics": "^10.1.4",
41
+ "@paradoxical-io/common-test": "^1.0.7",
34
42
  "@types/jest": "^29.5.12",
35
43
  "@types/node": "^22.5.0",
36
44
  "@types/supertest": "^6.0.2",