@inox-tools/utils 0.1.2 → 0.1.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.
package/dist/lazy.js CHANGED
@@ -1,5 +1,5 @@
1
1
  class a{constructor(i){this.factory=i;}initialized=!1;value;static of(i){return new this(i)}get(){return this.initialized||(this.value=this.factory(),this.initialized=!0),this.value}}
2
2
 
3
3
  export { a as Lazy };
4
- //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=lazy.js.map
5
5
  //# sourceMappingURL=lazy.js.map
package/dist/lazy.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/lazy.ts"],"names":["Lazy","factory"],"mappings":"AAMO,MAAMA,CAAQ,CAGZ,YAAoBC,EAAkB,CAAlB,aAAAA,CAAmB,CAFvC,YAAc,GACd,MAGR,OAAc,GAAMA,EAA2B,CAC9C,OAAO,IAAI,KAAKA,CAAO,CACxB,CAEO,KAAS,CACf,OAAK,KAAK,cACT,KAAK,MAAQ,KAAK,QAAQ,EAC1B,KAAK,YAAc,IAGb,KAAK,KACb,CACD","sourcesContent":["/**\n * A lazily computed memoized value.\n *\n * The given factory is only constructed on first use of the value.\n * Any subsequent use retrieves the same instance of the value.\n */\nexport class Lazy<T> {\n\tprivate initialized = false;\n\tprivate value?: T;\n\tprivate constructor(private factory: () => T) {}\n\n\tpublic static of<T>(factory: () => T): Lazy<T> {\n\t\treturn new this(factory);\n\t}\n\n\tpublic get(): T {\n\t\tif (!this.initialized) {\n\t\t\tthis.value = this.factory();\n\t\t\tthis.initialized = true;\n\t\t}\n\n\t\treturn this.value!;\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../src/lazy.ts"],"names":["Lazy","factory"],"mappings":"AAMO,MAAMA,CAAQ,CAGZ,WAAA,CAAoBC,CAAkB,CAAA,CAAlB,aAAAA,EAAmB,CAFvC,WAAc,CAAA,CAAA,CAAA,CACd,MAGR,OAAc,EAAA,CAAMA,CAA2B,CAAA,CAC9C,OAAO,IAAI,IAAA,CAAKA,CAAO,CACxB,CAEO,GAAS,EAAA,CACf,OAAK,IAAA,CAAK,cACT,IAAK,CAAA,KAAA,CAAQ,IAAK,CAAA,OAAA,GAClB,IAAK,CAAA,WAAA,CAAc,CAGb,CAAA,CAAA,CAAA,IAAA,CAAK,KACb,CACD","file":"lazy.js","sourcesContent":["/**\n * A lazily computed memoized value.\n *\n * The given factory is only constructed on first use of the value.\n * Any subsequent use retrieves the same instance of the value.\n */\nexport class Lazy<T> {\n\tprivate initialized = false;\n\tprivate value?: T;\n\tprivate constructor(private factory: () => T) {}\n\n\tpublic static of<T>(factory: () => T): Lazy<T> {\n\t\treturn new this(factory);\n\t}\n\n\tpublic get(): T {\n\t\tif (!this.initialized) {\n\t\t\tthis.value = this.factory();\n\t\t\tthis.initialized = true;\n\t\t}\n\n\t\treturn this.value!;\n\t}\n}\n"]}
package/dist/once.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  declare class Once {
2
2
  #private;
3
3
  do(callback: () => void): void;
4
+ doAsync(callback: () => Promise<void>): Promise<void>;
4
5
  }
5
6
 
6
7
  export { Once };
package/dist/once.js CHANGED
@@ -1,5 +1,5 @@
1
- class e{#o=!1;do(o){this.#o||o();}}
1
+ class o{#i=!1;do(i){this.#i||(i(),this.#i=!0);}async doAsync(i){this.#i||(await i(),this.#i=!0);}}
2
2
 
3
- export { e as Once };
4
- //# sourceMappingURL=out.js.map
3
+ export { o as Once };
4
+ //# sourceMappingURL=once.js.map
5
5
  //# sourceMappingURL=once.js.map
package/dist/once.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/once.ts"],"names":["Once","#done","callback"],"mappings":"AAAO,MAAMA,CAAK,CACRC,GAAQ,GAEV,GAAGC,EAA4B,CAChC,KAAKD,IACTC,EAAS,CAEX,CACD","sourcesContent":["export class Once {\n\treadonly #done = false;\n\n\tpublic do(callback: () => void): void {\n\t\tif (!this.#done) {\n\t\t\tcallback();\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../src/once.ts"],"names":["Once","#done","callback"],"mappings":"AAAO,MAAMA,CAAK,CACjBC,EAAAA,CAAQ,CAED,CAAA,CAAA,EAAA,CAAGC,CAA4B,CAAA,CAChC,IAAKD,CAAAA,EAAAA,GACTC,CAAS,EAAA,CACT,IAAKD,CAAAA,EAAAA,CAAQ,CAEf,CAAA,EAAA,CAEA,MAAa,OAAA,CAAQC,CAA8C,CAAA,CAC7D,IAAKD,CAAAA,EAAAA,GACT,MAAMC,CAAAA,EACN,CAAA,IAAA,CAAKD,EAAQ,CAAA,CAAA,CAAA,EAEf,CACD","file":"once.js","sourcesContent":["export class Once {\n\t#done = false;\n\n\tpublic do(callback: () => void): void {\n\t\tif (!this.#done) {\n\t\t\tcallback();\n\t\t\tthis.#done = true;\n\t\t}\n\t}\n\n\tpublic async doAsync(callback: () => Promise<void>): Promise<void> {\n\t\tif (!this.#done) {\n\t\t\tawait callback();\n\t\t\tthis.#done = true;\n\t\t}\n\t}\n}\n"]}
package/dist/types.js CHANGED
@@ -1,3 +1,3 @@
1
1
 
2
- //# sourceMappingURL=out.js.map
2
+ //# sourceMappingURL=types.js.map
3
3
  //# sourceMappingURL=types.js.map
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"types.js"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * A value that can be might be pending to be resolved.
3
+ */
4
+ type MaybePromise<T> = T | Promise<T>;
5
+ /**
6
+ * A value or a thunk for a value.
7
+ *
8
+ * A "thunk" is a function that takes no arguments and return
9
+ * a value that is potentially expensive to compute.
10
+ * This can be used when the value might not be needed and as
11
+ * such can be computed on demand potentially saving the
12
+ * expensive computation.
13
+ *
14
+ * If the value is not expensive to compute it can be used directly
15
+ * for simplicity.
16
+ *
17
+ * A value type that is itself a function cannot be a "maybe" thunk.
18
+ *
19
+ * @see https://en.wikipedia.org/wiki/Thunk
20
+ */
21
+ type MaybeThunk<T> = T extends Function ? never : T | (() => T);
22
+ /**
23
+ * A value or a thunk for a synchronous or asynchronous value.
24
+ *
25
+ * @see MaybePromise
26
+ * @see MaybeThunk
27
+ */
28
+ type MaybeAsyncThunk<T> = MaybeThunk<MaybePromise<T>>;
29
+ /**
30
+ * Load a value from a possibly thunk argument.
31
+ *
32
+ * If the value is a thunk it is called and the result is returned.
33
+ * Otherwise the value itself is returned.
34
+ *
35
+ * @see MaybeThunk
36
+ */
37
+ declare function loadThunkValue<T>(value: MaybeThunk<T>): T;
38
+
39
+ export { type MaybeAsyncThunk, type MaybePromise, type MaybeThunk, loadThunkValue };
package/dist/values.js ADDED
@@ -0,0 +1,5 @@
1
+ function T(e){return typeof e=="function"?e():e}
2
+
3
+ export { T as loadThunkValue };
4
+ //# sourceMappingURL=values.js.map
5
+ //# sourceMappingURL=values.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/values.ts"],"names":["loadThunkValue","value"],"mappings":"AAuCO,SAASA,CAAAA,CAAkBC,EAAyB,CAC1D,OAAO,OAAOA,CAAU,EAAA,UAAA,CAAaA,CAAM,EAAA,CAAIA,CAChD","file":"values.js","sourcesContent":["/**\n * A value that can be might be pending to be resolved.\n */\nexport type MaybePromise<T> = T | Promise<T>;\n\n/**\n * A value or a thunk for a value.\n *\n * A \"thunk\" is a function that takes no arguments and return\n * a value that is potentially expensive to compute.\n * This can be used when the value might not be needed and as\n * such can be computed on demand potentially saving the\n * expensive computation.\n *\n * If the value is not expensive to compute it can be used directly\n * for simplicity.\n *\n * A value type that is itself a function cannot be a \"maybe\" thunk.\n *\n * @see https://en.wikipedia.org/wiki/Thunk\n */\nexport type MaybeThunk<T> = T extends Function ? never : T | (() => T);\n\n/**\n * A value or a thunk for a synchronous or asynchronous value.\n *\n * @see MaybePromise\n * @see MaybeThunk\n */\nexport type MaybeAsyncThunk<T> = MaybeThunk<MaybePromise<T>>;\n\n/**\n * Load a value from a possibly thunk argument.\n *\n * If the value is a thunk it is called and the result is returned.\n * Otherwise the value itself is returned.\n *\n * @see MaybeThunk\n */\nexport function loadThunkValue<T>(value: MaybeThunk<T>): T {\n\treturn typeof value === 'function' ? value() : value;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inox-tools/utils",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "A collection of utilities used throughout Inox Tools",
5
5
  "keywords": [
6
6
  "utilities"
@@ -20,15 +20,15 @@
20
20
  "src"
21
21
  ],
22
22
  "devDependencies": {
23
- "@types/node": "^20.14.10",
24
- "@vitest/coverage-v8": "^1.6.0",
25
- "@vitest/ui": "^1.6.0",
26
- "astro": "^4.11.5",
23
+ "@types/node": "^22.5.4",
24
+ "@vitest/coverage-v8": "^2.0.5",
25
+ "@vitest/ui": "^2.0.5",
26
+ "astro": "^4.15.3",
27
27
  "jest-extended": "^4.0.2",
28
- "tsup": "^8.1.0",
29
- "typescript": "^5.5.3",
30
- "vite": "^5.3.3",
31
- "vitest": "^1.6.0"
28
+ "tsup": "8.2.4",
29
+ "typescript": "^5.5.4",
30
+ "vite": "^5.4.3",
31
+ "vitest": "^2.0.5"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "tsup",
package/src/once.ts CHANGED
@@ -1,9 +1,17 @@
1
1
  export class Once {
2
- readonly #done = false;
2
+ #done = false;
3
3
 
4
4
  public do(callback: () => void): void {
5
5
  if (!this.#done) {
6
6
  callback();
7
+ this.#done = true;
8
+ }
9
+ }
10
+
11
+ public async doAsync(callback: () => Promise<void>): Promise<void> {
12
+ if (!this.#done) {
13
+ await callback();
14
+ this.#done = true;
7
15
  }
8
16
  }
9
17
  }
package/src/values.ts ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * A value that can be might be pending to be resolved.
3
+ */
4
+ export type MaybePromise<T> = T | Promise<T>;
5
+
6
+ /**
7
+ * A value or a thunk for a value.
8
+ *
9
+ * A "thunk" is a function that takes no arguments and return
10
+ * a value that is potentially expensive to compute.
11
+ * This can be used when the value might not be needed and as
12
+ * such can be computed on demand potentially saving the
13
+ * expensive computation.
14
+ *
15
+ * If the value is not expensive to compute it can be used directly
16
+ * for simplicity.
17
+ *
18
+ * A value type that is itself a function cannot be a "maybe" thunk.
19
+ *
20
+ * @see https://en.wikipedia.org/wiki/Thunk
21
+ */
22
+ export type MaybeThunk<T> = T extends Function ? never : T | (() => T);
23
+
24
+ /**
25
+ * A value or a thunk for a synchronous or asynchronous value.
26
+ *
27
+ * @see MaybePromise
28
+ * @see MaybeThunk
29
+ */
30
+ export type MaybeAsyncThunk<T> = MaybeThunk<MaybePromise<T>>;
31
+
32
+ /**
33
+ * Load a value from a possibly thunk argument.
34
+ *
35
+ * If the value is a thunk it is called and the result is returned.
36
+ * Otherwise the value itself is returned.
37
+ *
38
+ * @see MaybeThunk
39
+ */
40
+ export function loadThunkValue<T>(value: MaybeThunk<T>): T {
41
+ return typeof value === 'function' ? value() : value;
42
+ }