triangle-utils 1.4.11 → 1.4.12

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.
@@ -28,5 +28,7 @@ export declare class UtilsMisc {
28
28
  chunk_index: [number, number];
29
29
  chunk_text: string;
30
30
  }[];
31
+ static instantiate<T>(constructor: new (x: any) => T, x: any): T | undefined;
32
+ static instantiator<T>(constructor: new (x: any) => T): (x: any) => T | undefined;
31
33
  }
32
34
  export {};
@@ -153,4 +153,16 @@ export class UtilsMisc {
153
153
  chunk_text: text.substring(...chunk_index)
154
154
  }));
155
155
  }
156
+ static instantiate(constructor, x) {
157
+ try {
158
+ const instance = new constructor(x);
159
+ return instance;
160
+ }
161
+ catch (error) {
162
+ return undefined;
163
+ }
164
+ }
165
+ static instantiator(constructor) {
166
+ return (x) => UtilsMisc.instantiate(constructor, x);
167
+ }
156
168
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.11",
3
+ "version": "1.4.12",
4
4
  "main": "dist/src/index.js",
5
5
  "directories": {
6
6
  "test": "vitest"
package/src/UtilsMisc.ts CHANGED
@@ -185,4 +185,17 @@ export class UtilsMisc {
185
185
  }))
186
186
  }
187
187
 
188
+ static instantiate<T>(constructor : new (x : any) => T, x : any) : T | undefined {
189
+ try {
190
+ const instance = new constructor(x)
191
+ return instance
192
+ } catch (error) {
193
+ return undefined
194
+ }
195
+ }
196
+
197
+ static instantiator<T>(constructor : new (x : any) => T) : (x : any) => T | undefined {
198
+ return (x : any) => UtilsMisc.instantiate(constructor, x)
199
+ }
200
+
188
201
  }
package/tsconfig.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "target" : "es2022",
11
11
  "noImplicitAny" : true,
12
12
  "noImplicitReturns" : true,
13
- "moduleResolution": "node",
13
+ "moduleResolution": "bundler",
14
14
  "declaration": true,
15
15
  // Allow JS files to be imported from TS and vice versa
16
16
  "allowJs": true,