utilium 1.3.0 → 1.3.2

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/buffer.js CHANGED
@@ -1,4 +1,4 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unused-expressions */
1
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
2
2
  /**
3
3
  * Grows a buffer if it isn't large enough
4
4
  * @returns The original buffer if resized successfully, or a newly created buffer
@@ -0,0 +1,5 @@
1
+ /** Utilities for computing checksums */
2
+ /**
3
+ * Computes the CRC32C checksum of a Uint8Array.
4
+ */
5
+ export declare function crc32c(data: Uint8Array): number;
@@ -0,0 +1,20 @@
1
+ /** Utilities for computing checksums */
2
+ // Precompute CRC32C table
3
+ const crc32cTable = new Uint32Array(256);
4
+ for (let i = 0; i < 256; i++) {
5
+ let value = i;
6
+ for (let j = 0; j < 8; j++) {
7
+ value = value & 1 ? 0x82f63b78 ^ (value >>> 1) : value >>> 1;
8
+ }
9
+ crc32cTable[i] = value;
10
+ }
11
+ /**
12
+ * Computes the CRC32C checksum of a Uint8Array.
13
+ */
14
+ export function crc32c(data) {
15
+ let crc = 0xffffffff;
16
+ for (let i = 0; i < data.length; i++) {
17
+ crc = (crc >>> 8) ^ crc32cTable[(crc ^ data[i]) & 0xff];
18
+ }
19
+ return (crc ^ 0xffffffff) >>> 0;
20
+ }
package/dist/objects.d.ts CHANGED
@@ -32,3 +32,5 @@ export interface ConstMap<T extends Partial<Record<keyof any, any>>, K extends k
32
32
  export declare function map<const T extends Partial<Record<any, any>>>(items: T): Map<keyof T, T[keyof T]>;
33
33
  export declare function getByString(object: Record<string, any>, path: string, separator?: RegExp): Record<string, any>;
34
34
  export declare function setByString(object: Record<string, any>, path: string, value: unknown, separator?: RegExp): Record<string, any>;
35
+ /** Binds a class member to the instance */
36
+ export declare function bound(value: unknown, { name, addInitializer }: ClassMethodDecoratorContext): void;
package/dist/objects.js CHANGED
@@ -18,7 +18,7 @@ export function assignWithDefaults(to, from, defaults = to) {
18
18
  try {
19
19
  to[key] = from[key] ?? defaults[key] ?? to[key];
20
20
  }
21
- catch (e) {
21
+ catch {
22
22
  // Do nothing
23
23
  }
24
24
  }
@@ -28,7 +28,7 @@ export function isJSON(str) {
28
28
  JSON.parse(str);
29
29
  return true;
30
30
  }
31
- catch (e) {
31
+ catch {
32
32
  return false;
33
33
  }
34
34
  }
@@ -56,3 +56,11 @@ export function setByString(object, path, value, separator = /[.[\]'"]/) {
56
56
  .filter(p => p)
57
57
  .reduce((o, p, i) => (o[p] = path.split(separator).filter(p => p).length === ++i ? value : o[p] || {}), object);
58
58
  }
59
+ /** Binds a class member to the instance */
60
+ // eslint-disable-next-line @typescript-eslint/unbound-method
61
+ export function bound(value, { name, addInitializer }) {
62
+ addInitializer(function () {
63
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
64
+ this[name] = this[name].bind(this);
65
+ });
66
+ }
package/dist/struct.d.ts CHANGED
@@ -7,6 +7,10 @@ export * as Struct from './internal/struct.js';
7
7
  * Gets the size in bytes of a type
8
8
  */
9
9
  export declare function sizeof<T extends primitive.Valid | StaticLike | InstanceLike>(type: T): Size<T>;
10
+ /**
11
+ * Returns the offset (in bytes) of a member in a struct.
12
+ */
13
+ export declare function offsetof(type: StaticLike | InstanceLike, memberName: string): number;
10
14
  /**
11
15
  * Aligns a number
12
16
  */
package/dist/struct.js CHANGED
@@ -15,6 +15,18 @@ export function sizeof(type) {
15
15
  const struct = isStatic(type) ? type : type.constructor;
16
16
  return struct[symbol_metadata(struct)][Symbol.struct_metadata].size;
17
17
  }
18
+ /**
19
+ * Returns the offset (in bytes) of a member in a struct.
20
+ */
21
+ export function offsetof(type, memberName) {
22
+ checkStruct(type);
23
+ const struct = isStatic(type) ? type : type.constructor;
24
+ const metadata = struct[symbol_metadata(struct)][Symbol.struct_metadata];
25
+ const member = metadata.members.get(memberName);
26
+ if (!member)
27
+ throw new Error('Struct does not have member: ' + memberName);
28
+ return member.offset;
29
+ }
18
30
  /**
19
31
  * Aligns a number
20
32
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/buffer.ts CHANGED
@@ -1,4 +1,4 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unused-expressions */
1
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
2
2
 
3
3
  /**
4
4
  * A generic ArrayBufferView (typed array) constructor
@@ -0,0 +1,22 @@
1
+ /** Utilities for computing checksums */
2
+
3
+ // Precompute CRC32C table
4
+ const crc32cTable = new Uint32Array(256);
5
+ for (let i = 0; i < 256; i++) {
6
+ let value = i;
7
+ for (let j = 0; j < 8; j++) {
8
+ value = value & 1 ? 0x82f63b78 ^ (value >>> 1) : value >>> 1;
9
+ }
10
+ crc32cTable[i] = value;
11
+ }
12
+
13
+ /**
14
+ * Computes the CRC32C checksum of a Uint8Array.
15
+ */
16
+ export function crc32c(data: Uint8Array): number {
17
+ let crc = 0xffffffff;
18
+ for (let i = 0; i < data.length; i++) {
19
+ crc = (crc >>> 8) ^ crc32cTable[(crc ^ data[i]) & 0xff];
20
+ }
21
+ return (crc ^ 0xffffffff) >>> 0;
22
+ }
package/src/objects.ts CHANGED
@@ -26,7 +26,7 @@ export function assignWithDefaults<To extends Record<keyof any, any>, From exten
26
26
  for (const key of keys) {
27
27
  try {
28
28
  to[key] = from[key] ?? defaults[key] ?? to[key];
29
- } catch (e) {
29
+ } catch {
30
30
  // Do nothing
31
31
  }
32
32
  }
@@ -46,7 +46,7 @@ export function isJSON(str: string) {
46
46
  try {
47
47
  JSON.parse(str);
48
48
  return true;
49
- } catch (e) {
49
+ } catch {
50
50
  return false;
51
51
  }
52
52
  }
@@ -89,3 +89,12 @@ export function setByString(object: Record<string, any>, path: string, value: un
89
89
  .filter(p => p)
90
90
  .reduce((o, p, i) => (o[p] = path.split(separator).filter(p => p).length === ++i ? value : o[p] || {}), object);
91
91
  }
92
+
93
+ /** Binds a class member to the instance */
94
+ // eslint-disable-next-line @typescript-eslint/unbound-method
95
+ export function bound(value: unknown, { name, addInitializer }: ClassMethodDecoratorContext) {
96
+ addInitializer(function (this: any) {
97
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
98
+ this[name] = this[name].bind(this);
99
+ });
100
+ }
package/src/struct.ts CHANGED
@@ -23,6 +23,20 @@ export function sizeof<T extends primitive.Valid | StaticLike | InstanceLike>(ty
23
23
  return struct[symbol_metadata(struct)][Symbol.struct_metadata].size as Size<T>;
24
24
  }
25
25
 
26
+ /**
27
+ * Returns the offset (in bytes) of a member in a struct.
28
+ */
29
+ export function offsetof(type: StaticLike | InstanceLike, memberName: string): number {
30
+ checkStruct(type);
31
+
32
+ const struct = isStatic(type) ? type : type.constructor;
33
+ const metadata = struct[symbol_metadata(struct)][Symbol.struct_metadata];
34
+
35
+ const member = metadata.members.get(memberName);
36
+ if (!member) throw new Error('Struct does not have member: ' + memberName);
37
+ return member.offset;
38
+ }
39
+
26
40
  /**
27
41
  * Aligns a number
28
42
  */