utilium 1.8.0 → 1.10.0

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.d.ts CHANGED
@@ -15,3 +15,5 @@ export declare function encodeUTF8(input: string): Uint8Array;
15
15
  * Decodes a UTF-8 string from a buffer
16
16
  */
17
17
  export declare function decodeUTF8(input?: Uint8Array): string;
18
+ export declare function encodeASCII(input: string): Uint8Array;
19
+ export declare function decodeASCII(input: Uint8Array): string;
package/dist/string.js CHANGED
@@ -18,3 +18,17 @@ const decoder = new TextDecoder();
18
18
  export function decodeUTF8(input) {
19
19
  return decoder.decode(input);
20
20
  }
21
+ export function encodeASCII(input) {
22
+ const data = new Uint8Array(input.length);
23
+ for (let i = 0; i < input.length; i++) {
24
+ data[i] = input.charCodeAt(i);
25
+ }
26
+ return data;
27
+ }
28
+ export function decodeASCII(input) {
29
+ let output = '';
30
+ for (let i = 0; i < input.length; i++) {
31
+ output += String.fromCharCode(input[i]);
32
+ }
33
+ return output;
34
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
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
@@ -38,3 +38,19 @@ const decoder = new TextDecoder();
38
38
  export function decodeUTF8(input?: Uint8Array): string {
39
39
  return decoder.decode(input);
40
40
  }
41
+
42
+ export function encodeASCII(input: string): Uint8Array {
43
+ const data = new Uint8Array(input.length);
44
+ for (let i = 0; i < input.length; i++) {
45
+ data[i] = input.charCodeAt(i);
46
+ }
47
+ return data;
48
+ }
49
+
50
+ export function decodeASCII(input: Uint8Array): string {
51
+ let output = '';
52
+ for (let i = 0; i < input.length; i++) {
53
+ output += String.fromCharCode(input[i]);
54
+ }
55
+ return output;
56
+ }