utilium 1.9.0 → 1.10.1

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/misc.d.ts CHANGED
@@ -9,3 +9,13 @@ export declare function canary(error?: Error): () => void;
9
9
  * @see https://github.com/tc39/proposal-throw-expressions
10
10
  */
11
11
  export declare function _throw(e: unknown): never;
12
+ interface MemoizeMetadata extends DecoratorMetadata {
13
+ memoized?: Record<PropertyKey, any>;
14
+ }
15
+ /**
16
+ * Decorator for memoizing the result of a getter.
17
+ */
18
+ export declare function memoize<T, This>(get: () => T, context: ClassGetterDecoratorContext<This, T> & {
19
+ metadata?: MemoizeMetadata;
20
+ }): (this: This) => T;
21
+ export {};
package/dist/misc.js CHANGED
@@ -46,3 +46,20 @@ export function canary(error = new Error()) {
46
46
  export function _throw(e) {
47
47
  throw e;
48
48
  }
49
+ /**
50
+ * Decorator for memoizing the result of a getter.
51
+ */
52
+ export function memoize(get, context) {
53
+ if (context.kind != 'getter')
54
+ throw new Error('@memoize can only be used on getters');
55
+ return function () {
56
+ context.metadata ??= {};
57
+ const { memoized = {} } = context.metadata;
58
+ if (context.name in memoized) {
59
+ console.log('Using cached value for', context.name, JSON.stringify(memoized[context.name]));
60
+ return memoized[context.name];
61
+ }
62
+ memoized[context.name] = get.call(this);
63
+ return memoized[context.name];
64
+ };
65
+ }
package/dist/string.js CHANGED
@@ -16,7 +16,13 @@ const decoder = new TextDecoder();
16
16
  * Decodes a UTF-8 string from a buffer
17
17
  */
18
18
  export function decodeUTF8(input) {
19
- return decoder.decode(input);
19
+ if (!input)
20
+ return '';
21
+ if (input.buffer instanceof ArrayBuffer && !input.buffer.resizable)
22
+ return decoder.decode(input);
23
+ const buffer = new Uint8Array(input.byteLength);
24
+ buffer.set(input);
25
+ return decoder.decode(buffer);
20
26
  }
21
27
  export function encodeASCII(input) {
22
28
  const data = new Uint8Array(input.length);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.9.0",
3
+ "version": "1.10.1",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/misc.ts CHANGED
@@ -52,3 +52,29 @@ export function canary(error: Error = new Error()) {
52
52
  export function _throw(e: unknown): never {
53
53
  throw e;
54
54
  }
55
+
56
+ interface MemoizeMetadata extends DecoratorMetadata {
57
+ memoized?: Record<PropertyKey, any>;
58
+ }
59
+
60
+ /**
61
+ * Decorator for memoizing the result of a getter.
62
+ */
63
+ export function memoize<T, This>(
64
+ get: () => T,
65
+ context: ClassGetterDecoratorContext<This, T> & { metadata?: MemoizeMetadata }
66
+ ) {
67
+ if (context.kind != 'getter') throw new Error('@memoize can only be used on getters');
68
+
69
+ return function (this: This): T {
70
+ context.metadata ??= {};
71
+ const { memoized = {} } = context.metadata;
72
+
73
+ if (context.name in memoized) {
74
+ console.log('Using cached value for', context.name, JSON.stringify(memoized[context.name]));
75
+ return memoized[context.name];
76
+ }
77
+ memoized[context.name] = get.call(this);
78
+ return memoized[context.name];
79
+ };
80
+ }
package/src/string.ts CHANGED
@@ -36,7 +36,13 @@ const decoder = new TextDecoder();
36
36
  * Decodes a UTF-8 string from a buffer
37
37
  */
38
38
  export function decodeUTF8(input?: Uint8Array): string {
39
- return decoder.decode(input);
39
+ if (!input) return '';
40
+
41
+ if (input.buffer instanceof ArrayBuffer && !input.buffer.resizable) return decoder.decode(input);
42
+
43
+ const buffer = new Uint8Array(input.byteLength);
44
+ buffer.set(input);
45
+ return decoder.decode(buffer);
40
46
  }
41
47
 
42
48
  export function encodeASCII(input: string): Uint8Array {