triangle-utils 1.4.163 → 1.4.165

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.
@@ -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>(inputs: T[], f: (input: T, iterator_id: number, index: number) => Promise<U>, options?: {
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>(inputs: T[], f: (inputs: T[], iterator_id: number, index: number) => Promise<U>, options: {
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 batch_import_csv<T>(stream: Readable, import_new_items: (new_items: any[]) => Promise<void>, options: {
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;
@@ -22,21 +22,21 @@ export class UtilsMisc {
22
22
  }
23
23
  return undefined;
24
24
  }
25
- async iterate(inputs, f, options = {}) {
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(inputs.length).fill(undefined);
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 < inputs.length) {
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 + "/" + inputs.length);
37
+ console.log(iterator_id + ":" + local_index + "/" + items.length);
38
38
  }
39
- const output = await this.safe_run(() => f(inputs[local_index], iterator_id, local_index));
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(inputs, f, options) {
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(inputs.length).fill(undefined);
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 < inputs.length) {
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 + "/" + inputs.length);
60
+ console.log(iterator_id + ":" + local_index + "/" + items.length);
61
61
  }
62
- const output = await this.safe_run(() => f(inputs.slice(local_index, local_index + batch_num_items), iterator_id, local_index));
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 batch_import_csv(stream, import_new_items, options) {
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 new_items = [];
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
- new_items.push(item);
133
+ items.push(item);
134
134
  if (i % batch_num_items === 0) {
135
135
  csv_stream.pause();
136
136
  console.log(i);
137
- await import_new_items(await Promise.all(new_items).then(new_items => new_items.flat()));
138
- new_items = [];
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 import_new_items(await Promise.all(new_items).then(new_items => new_items.flat()));
144
- new_items = [];
143
+ await f(await Promise.all(items).then(items => items.flat()));
144
+ items = [];
145
145
  resolve();
146
146
  });
147
147
  });
@@ -22,6 +22,7 @@ export declare class GlobalConfig {
22
22
  readonly youtube_api_key: string;
23
23
  readonly xai_api_key: string;
24
24
  readonly ballotpedia_api_key: string;
25
+ readonly brave_api_key: string;
25
26
  readonly [x: string]: any;
26
27
  constructor(global_config: GlobalConfig);
27
28
  static is(global_config: any): global_config is GlobalConfig;
@@ -22,6 +22,7 @@ export class GlobalConfig {
22
22
  youtube_api_key;
23
23
  xai_api_key;
24
24
  ballotpedia_api_key;
25
+ brave_api_key;
25
26
  constructor(global_config) {
26
27
  if (!GlobalConfig.is(global_config)) {
27
28
  throw Error("Invalid input.");
@@ -49,6 +50,7 @@ export class GlobalConfig {
49
50
  this.youtube_api_key = global_config.youtube_api_key;
50
51
  this.xai_api_key = global_config.xai_api_key;
51
52
  this.ballotpedia_api_key = global_config.ballotpedia_api_key;
53
+ this.brave_api_key = global_config.brave_api_key;
52
54
  }
53
55
  static is(global_config) {
54
56
  return (global_config !== undefined &&
@@ -74,6 +76,7 @@ export class GlobalConfig {
74
76
  typeof global_config.twitter_api_key === "string" &&
75
77
  typeof global_config.youtube_api_key === "string" &&
76
78
  typeof global_config.xai_api_key === "string" &&
77
- typeof global_config.ballotpedia_api_key === "string");
79
+ typeof global_config.ballotpedia_api_key === "string" &&
80
+ typeof global_config.brave_api_key === "string");
78
81
  }
79
82
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.163",
3
+ "version": "1.4.165",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
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>(inputs : T[], f : (input : T, iterator_id : number, index : number) => Promise<U>, options : { num_iterators? : number, verbosity? : number | boolean } = {}) {
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(inputs.length).fill(undefined)
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 < inputs.length) {
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 + "/" + inputs.length)
48
+ console.log(iterator_id + ":" + local_index + "/" + items.length)
49
49
  }
50
- const output = await this.safe_run(() => f(inputs[local_index], iterator_id, local_index))
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>(inputs : T[], f : (inputs : T[], iterator_id : number, index : number) => Promise<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(inputs.length).fill(undefined)
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 < inputs.length) {
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 + "/" + inputs.length)
73
+ console.log(iterator_id + ":" + local_index + "/" + items.length)
74
74
  }
75
- const output = await this.safe_run(() => f(inputs.slice(local_index, local_index + batch_num_items), iterator_id, local_index))
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 batch_import_csv<T>(stream : Readable, import_new_items : (new_items : any[]) => Promise<void>, options : { delimiter? : string, batch_num_items : number, max_num_items? : number }) {
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 new_items : Promise<T[]>[] = []
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
- new_items.push(item)
160
+ items.push(item)
161
161
  if (i % batch_num_items === 0) {
162
162
  csv_stream.pause()
163
163
  console.log(i)
164
- await import_new_items(await Promise.all(new_items).then(new_items => new_items.flat()))
165
- new_items = []
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 import_new_items(await Promise.all(new_items).then(new_items => new_items.flat()))
171
- new_items = []
170
+ await f(await Promise.all(items).then(items => items.flat()))
171
+ items = []
172
172
  resolve()
173
173
  })
174
174
  })
@@ -22,6 +22,7 @@ export class GlobalConfig {
22
22
  readonly youtube_api_key : string
23
23
  readonly xai_api_key : string
24
24
  readonly ballotpedia_api_key : string
25
+ readonly brave_api_key : string
25
26
  readonly [x : string] : any
26
27
 
27
28
  constructor(global_config : GlobalConfig) {
@@ -51,6 +52,7 @@ export class GlobalConfig {
51
52
  this.youtube_api_key = global_config.youtube_api_key
52
53
  this.xai_api_key = global_config.xai_api_key
53
54
  this.ballotpedia_api_key = global_config.ballotpedia_api_key
55
+ this.brave_api_key = global_config.brave_api_key
54
56
  }
55
57
 
56
58
  static is(global_config : any) : global_config is GlobalConfig {
@@ -78,7 +80,8 @@ export class GlobalConfig {
78
80
  typeof global_config.twitter_api_key === "string" &&
79
81
  typeof global_config.youtube_api_key === "string" &&
80
82
  typeof global_config.xai_api_key === "string" &&
81
- typeof global_config.ballotpedia_api_key === "string"
83
+ typeof global_config.ballotpedia_api_key === "string" &&
84
+ typeof global_config.brave_api_key === "string"
82
85
  )
83
86
  }
84
87
  }