triangle-utils 1.4.163 → 1.4.164
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.
- package/dist/src/UtilsMisc.d.ts +3 -3
- package/dist/src/UtilsMisc.js +17 -17
- package/package.json +1 -1
- package/src/UtilsMisc.ts +17 -17
package/dist/src/UtilsMisc.d.ts
CHANGED
|
@@ -8,11 +8,11 @@ export declare class UtilsMisc {
|
|
|
8
8
|
readonly config: GlobalConfig;
|
|
9
9
|
constructor(config: GlobalConfig);
|
|
10
10
|
safe_run<T>(f: () => Promise<T>): Promise<T | undefined>;
|
|
11
|
-
iterate<T, U>(
|
|
11
|
+
iterate<T, U>(items: T[], f: (item: T, iterator_id: number, index: number) => Promise<U>, options?: {
|
|
12
12
|
num_iterators?: number;
|
|
13
13
|
verbosity?: number | boolean;
|
|
14
14
|
}): Promise<(U | undefined)[]>;
|
|
15
|
-
batch_iterate<T, U>(
|
|
15
|
+
batch_iterate<T, U>(items: T[], f: (items: T[], iterator_id: number, index: number) => Promise<U>, options: {
|
|
16
16
|
batch_num_items?: number;
|
|
17
17
|
num_iterators?: number;
|
|
18
18
|
verbosity?: number | boolean;
|
|
@@ -26,7 +26,7 @@ export declare class UtilsMisc {
|
|
|
26
26
|
static encrypt(secret_key: string, text: string): Encryption;
|
|
27
27
|
static decrypt(secret_key: string, encryption: Encryption): string;
|
|
28
28
|
static get_secret_hash(username: string, cognito_client_id: string, cognito_client_secret: string): string;
|
|
29
|
-
static
|
|
29
|
+
static batch_iterate_csv<T>(stream: Readable, f: (items: any[]) => Promise<void>, options: {
|
|
30
30
|
delimiter?: string;
|
|
31
31
|
batch_num_items: number;
|
|
32
32
|
max_num_items?: number;
|
package/dist/src/UtilsMisc.js
CHANGED
|
@@ -22,21 +22,21 @@ export class UtilsMisc {
|
|
|
22
22
|
}
|
|
23
23
|
return undefined;
|
|
24
24
|
}
|
|
25
|
-
async iterate(
|
|
25
|
+
async iterate(items, f, options = {}) {
|
|
26
26
|
const num_iterators = options.num_iterators || 1;
|
|
27
27
|
const verbosity = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0);
|
|
28
28
|
let index = 0;
|
|
29
29
|
let iterators = [];
|
|
30
|
-
const outputs = new Array(
|
|
30
|
+
const outputs = new Array(items.length).fill(undefined);
|
|
31
31
|
for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
|
|
32
32
|
iterators.push((async () => {
|
|
33
|
-
while (index <
|
|
33
|
+
while (index < items.length) {
|
|
34
34
|
const local_index = index;
|
|
35
35
|
index += 1;
|
|
36
36
|
if (verbosity !== undefined && (local_index % verbosity === 0)) {
|
|
37
|
-
console.log(iterator_id + ":" + local_index + "/" +
|
|
37
|
+
console.log(iterator_id + ":" + local_index + "/" + items.length);
|
|
38
38
|
}
|
|
39
|
-
const output = await this.safe_run(() => f(
|
|
39
|
+
const output = await this.safe_run(() => f(items[local_index], iterator_id, local_index));
|
|
40
40
|
outputs[local_index] = output;
|
|
41
41
|
}
|
|
42
42
|
})());
|
|
@@ -44,22 +44,22 @@ export class UtilsMisc {
|
|
|
44
44
|
await Promise.all(iterators);
|
|
45
45
|
return outputs;
|
|
46
46
|
}
|
|
47
|
-
async batch_iterate(
|
|
47
|
+
async batch_iterate(items, f, options) {
|
|
48
48
|
const batch_num_items = options.batch_num_items || 1;
|
|
49
49
|
const num_iterators = options.num_iterators || 1;
|
|
50
50
|
const verbosity = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0);
|
|
51
51
|
let index = 0;
|
|
52
52
|
let iterators = [];
|
|
53
|
-
const outputs = new Array(
|
|
53
|
+
const outputs = new Array(items.length).fill(undefined);
|
|
54
54
|
for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
|
|
55
55
|
iterators.push((async () => {
|
|
56
|
-
while (index <
|
|
56
|
+
while (index < items.length) {
|
|
57
57
|
const local_index = index;
|
|
58
58
|
index += batch_num_items;
|
|
59
59
|
if (verbosity !== undefined && (local_index % verbosity === 0)) {
|
|
60
|
-
console.log(iterator_id + ":" + local_index + "/" +
|
|
60
|
+
console.log(iterator_id + ":" + local_index + "/" + items.length);
|
|
61
61
|
}
|
|
62
|
-
const output = await this.safe_run(() => f(
|
|
62
|
+
const output = await this.safe_run(() => f(items.slice(local_index, local_index + batch_num_items), iterator_id, local_index));
|
|
63
63
|
outputs[local_index] = output;
|
|
64
64
|
}
|
|
65
65
|
})());
|
|
@@ -115,11 +115,11 @@ export class UtilsMisc {
|
|
|
115
115
|
.digest("base64");
|
|
116
116
|
return secret_hash;
|
|
117
117
|
}
|
|
118
|
-
static async
|
|
118
|
+
static async batch_iterate_csv(stream, f, options) {
|
|
119
119
|
const delimiter = options.delimiter || ",";
|
|
120
120
|
const max_num_items = options.max_num_items;
|
|
121
121
|
const batch_num_items = options.batch_num_items;
|
|
122
|
-
let
|
|
122
|
+
let items = [];
|
|
123
123
|
let i = 0;
|
|
124
124
|
const csv_stream = stream.pipe(csv_parser({ separator: delimiter }));
|
|
125
125
|
await new Promise(resolve => {
|
|
@@ -130,18 +130,18 @@ export class UtilsMisc {
|
|
|
130
130
|
csv_stream.end();
|
|
131
131
|
return;
|
|
132
132
|
}
|
|
133
|
-
|
|
133
|
+
items.push(item);
|
|
134
134
|
if (i % batch_num_items === 0) {
|
|
135
135
|
csv_stream.pause();
|
|
136
136
|
console.log(i);
|
|
137
|
-
await
|
|
138
|
-
|
|
137
|
+
await f(await Promise.all(items).then(items => items.flat()));
|
|
138
|
+
items = [];
|
|
139
139
|
csv_stream.resume();
|
|
140
140
|
}
|
|
141
141
|
})
|
|
142
142
|
.on("end", async () => {
|
|
143
|
-
await
|
|
144
|
-
|
|
143
|
+
await f(await Promise.all(items).then(items => items.flat()));
|
|
144
|
+
items = [];
|
|
145
145
|
resolve();
|
|
146
146
|
});
|
|
147
147
|
});
|
package/package.json
CHANGED
package/src/UtilsMisc.ts
CHANGED
|
@@ -33,21 +33,21 @@ export class UtilsMisc {
|
|
|
33
33
|
return undefined
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
async iterate<T, U>(
|
|
36
|
+
async iterate<T, U>(items : T[], f : (item : T, iterator_id : number, index : number) => Promise<U>, options : { num_iterators? : number, verbosity? : number | boolean } = {}) {
|
|
37
37
|
const num_iterators = options.num_iterators || 1
|
|
38
38
|
const verbosity : number = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0)
|
|
39
39
|
let index = 0
|
|
40
40
|
let iterators = []
|
|
41
|
-
const outputs : (U | undefined)[] = new Array(
|
|
41
|
+
const outputs : (U | undefined)[] = new Array(items.length).fill(undefined)
|
|
42
42
|
for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
|
|
43
43
|
iterators.push((async () => {
|
|
44
|
-
while (index <
|
|
44
|
+
while (index < items.length) {
|
|
45
45
|
const local_index = index
|
|
46
46
|
index += 1
|
|
47
47
|
if (verbosity !== undefined && (local_index % verbosity === 0)) {
|
|
48
|
-
console.log(iterator_id + ":" + local_index + "/" +
|
|
48
|
+
console.log(iterator_id + ":" + local_index + "/" + items.length)
|
|
49
49
|
}
|
|
50
|
-
const output = await this.safe_run(() => f(
|
|
50
|
+
const output = await this.safe_run(() => f(items[local_index], iterator_id, local_index))
|
|
51
51
|
outputs[local_index] = output
|
|
52
52
|
}
|
|
53
53
|
})())
|
|
@@ -56,23 +56,23 @@ export class UtilsMisc {
|
|
|
56
56
|
return outputs
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
async batch_iterate<T, U>(
|
|
59
|
+
async batch_iterate<T, U>(items : T[], f : (items : T[], iterator_id : number, index : number) => Promise<U>,
|
|
60
60
|
options : { batch_num_items? : number, num_iterators? : number, verbosity? : number | boolean }) {
|
|
61
61
|
const batch_num_items = options.batch_num_items || 1
|
|
62
62
|
const num_iterators = options.num_iterators || 1
|
|
63
63
|
const verbosity : number = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0)
|
|
64
64
|
let index = 0
|
|
65
65
|
let iterators = []
|
|
66
|
-
const outputs : (U | undefined)[] = new Array(
|
|
66
|
+
const outputs : (U | undefined)[] = new Array(items.length).fill(undefined)
|
|
67
67
|
for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
|
|
68
68
|
iterators.push((async () => {
|
|
69
|
-
while (index <
|
|
69
|
+
while (index < items.length) {
|
|
70
70
|
const local_index = index
|
|
71
71
|
index += batch_num_items
|
|
72
72
|
if (verbosity !== undefined && (local_index % verbosity === 0)) {
|
|
73
|
-
console.log(iterator_id + ":" + local_index + "/" +
|
|
73
|
+
console.log(iterator_id + ":" + local_index + "/" + items.length)
|
|
74
74
|
}
|
|
75
|
-
const output = await this.safe_run(() => f(
|
|
75
|
+
const output = await this.safe_run(() => f(items.slice(local_index, local_index + batch_num_items), iterator_id, local_index))
|
|
76
76
|
outputs[local_index] = output
|
|
77
77
|
}
|
|
78
78
|
})())
|
|
@@ -142,11 +142,11 @@ export class UtilsMisc {
|
|
|
142
142
|
return secret_hash
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
static async
|
|
145
|
+
static async batch_iterate_csv<T>(stream : Readable, f : (items : any[]) => Promise<void>, options : { delimiter? : string, batch_num_items : number, max_num_items? : number }) {
|
|
146
146
|
const delimiter = options.delimiter || ","
|
|
147
147
|
const max_num_items = options.max_num_items
|
|
148
148
|
const batch_num_items = options.batch_num_items
|
|
149
|
-
let
|
|
149
|
+
let items : Promise<T[]>[] = []
|
|
150
150
|
let i = 0
|
|
151
151
|
const csv_stream = stream.pipe(csv_parser({ separator: delimiter }))
|
|
152
152
|
await new Promise<void>(resolve => {
|
|
@@ -157,18 +157,18 @@ export class UtilsMisc {
|
|
|
157
157
|
csv_stream.end()
|
|
158
158
|
return
|
|
159
159
|
}
|
|
160
|
-
|
|
160
|
+
items.push(item)
|
|
161
161
|
if (i % batch_num_items === 0) {
|
|
162
162
|
csv_stream.pause()
|
|
163
163
|
console.log(i)
|
|
164
|
-
await
|
|
165
|
-
|
|
164
|
+
await f(await Promise.all(items).then(items => items.flat()))
|
|
165
|
+
items = []
|
|
166
166
|
csv_stream.resume()
|
|
167
167
|
}
|
|
168
168
|
})
|
|
169
169
|
.on("end", async () => {
|
|
170
|
-
await
|
|
171
|
-
|
|
170
|
+
await f(await Promise.all(items).then(items => items.flat()))
|
|
171
|
+
items = []
|
|
172
172
|
resolve()
|
|
173
173
|
})
|
|
174
174
|
})
|