utilium 0.7.2 → 0.7.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/internal/struct.d.ts +7 -2
- package/dist/internal/struct.js +26 -2
- package/dist/objects.d.ts +1 -1
- package/dist/struct.d.ts +3 -2
- package/dist/struct.js +1 -2
- package/package.json +15 -9
- package/src/internal/struct.ts +30 -4
- package/src/objects.ts +4 -4
- package/src/string.ts +2 -2
- package/src/struct.ts +6 -21
- package/src/types.ts +1 -1
@@ -1,5 +1,5 @@
|
|
1
|
-
import { ClassLike } from '../types.js';
|
2
|
-
import * as primitive from './primitives.js';
|
1
|
+
import type { ClassLike } from '../types.js';
|
2
|
+
import type * as primitive from './primitives.js';
|
3
3
|
export interface MemberInit {
|
4
4
|
name: string;
|
5
5
|
type: string | ClassLike;
|
@@ -45,6 +45,11 @@ export interface StaticLike<T extends Metadata = Metadata> extends ClassLike {
|
|
45
45
|
export declare function isValidMetadata<T extends Metadata = Metadata>(arg: unknown): arg is DecoratorMetadata & {
|
46
46
|
[metadata]: T;
|
47
47
|
};
|
48
|
+
/**
|
49
|
+
* Polyfill context.metadata
|
50
|
+
* @see https://github.com/microsoft/TypeScript/issues/53461
|
51
|
+
*/
|
52
|
+
export declare function _polyfill_contextMetadata(target: object): void;
|
48
53
|
/**
|
49
54
|
* Gets a reference to Symbol.metadata, even on platforms that do not expose it globally (like Node)
|
50
55
|
*/
|
package/dist/internal/struct.js
CHANGED
@@ -3,11 +3,35 @@ export const metadata = Symbol('struct');
|
|
3
3
|
export function isValidMetadata(arg) {
|
4
4
|
return arg != null && typeof arg == 'object' && metadata in arg;
|
5
5
|
}
|
6
|
+
/**
|
7
|
+
* Polyfill Symbol.metadata
|
8
|
+
* @see https://github.com/microsoft/TypeScript/issues/53461
|
9
|
+
*/
|
10
|
+
Symbol.metadata ??= Symbol.for('Symbol.metadata');
|
11
|
+
/**
|
12
|
+
* Polyfill context.metadata
|
13
|
+
* @see https://github.com/microsoft/TypeScript/issues/53461
|
14
|
+
*/
|
15
|
+
export function _polyfill_contextMetadata(target) {
|
16
|
+
if (!Symbol?.metadata) {
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
if (Symbol.metadata in target) {
|
20
|
+
return;
|
21
|
+
}
|
22
|
+
Object.defineProperty(target, Symbol.metadata, {
|
23
|
+
enumerable: true,
|
24
|
+
configurable: true,
|
25
|
+
writable: true,
|
26
|
+
value: Object.create(null),
|
27
|
+
});
|
28
|
+
}
|
6
29
|
/**
|
7
30
|
* Gets a reference to Symbol.metadata, even on platforms that do not expose it globally (like Node)
|
8
31
|
*/
|
9
32
|
export function symbol_metadata(arg) {
|
10
33
|
const symbol_metadata = Symbol.metadata || Object.getOwnPropertySymbols(arg).find(s => s.description == 'Symbol.metadata');
|
34
|
+
_polyfill_contextMetadata(arg);
|
11
35
|
if (!symbol_metadata) {
|
12
36
|
throw new ReferenceError('Could not get a reference to Symbol.metadata');
|
13
37
|
}
|
@@ -21,7 +45,7 @@ export function isInstance(arg) {
|
|
21
45
|
}
|
22
46
|
export function checkInstance(arg) {
|
23
47
|
if (!isInstance(arg)) {
|
24
|
-
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance');
|
48
|
+
throw new TypeError((typeof arg == 'function' ? arg.name : typeof arg == 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance');
|
25
49
|
}
|
26
50
|
}
|
27
51
|
export function isStruct(arg) {
|
@@ -29,6 +53,6 @@ export function isStruct(arg) {
|
|
29
53
|
}
|
30
54
|
export function checkStruct(arg) {
|
31
55
|
if (!isStruct(arg)) {
|
32
|
-
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct');
|
56
|
+
throw new TypeError((typeof arg == 'function' ? arg.name : typeof arg == 'object' && arg ? arg.constructor.name : arg) + ' is not a struct');
|
33
57
|
}
|
34
58
|
}
|
package/dist/objects.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { UnionToTuple } from './types.js';
|
1
|
+
import type { UnionToTuple } from './types.js';
|
2
2
|
export declare function filterObject<O extends object, R extends object>(object: O, predicate: (key: keyof O, value: O[keyof O]) => boolean): R;
|
3
3
|
export declare function pick<T extends object, K extends keyof T>(object: T, ...keys: readonly K[]): Pick<T, K>;
|
4
4
|
export declare function pick<T extends object, K extends keyof T>(object: T, ...keys: readonly (readonly K[])[]): Pick<T, K>;
|
package/dist/struct.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import * as primitive from './internal/primitives.js';
|
2
|
-
import { DecoratorContext, InstanceLike, Options, Size, StaticLike
|
3
|
-
import {
|
2
|
+
import type { DecoratorContext, InstanceLike, Options, Size, StaticLike } from './internal/struct.js';
|
3
|
+
import { type MemberContext } from './internal/struct.js';
|
4
|
+
import type { ClassLike } from './types.js';
|
4
5
|
export * as Struct from './internal/struct.js';
|
5
6
|
/**
|
6
7
|
* Gets the size in bytes of a type
|
package/dist/struct.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import * as primitive from './internal/primitives.js';
|
2
|
-
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata
|
2
|
+
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata } from './internal/struct.js';
|
3
3
|
import { capitalize } from './string.js';
|
4
4
|
export * as Struct from './internal/struct.js';
|
5
5
|
/**
|
@@ -25,7 +25,6 @@ export function align(value, alignment) {
|
|
25
25
|
* Decorates a class as a struct
|
26
26
|
*/
|
27
27
|
export function struct(options = {}) {
|
28
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
29
28
|
return function _decorateStruct(target, context) {
|
30
29
|
context.metadata ??= {};
|
31
30
|
context.metadata[init] ||= [];
|
package/package.json
CHANGED
@@ -1,25 +1,28 @@
|
|
1
1
|
{
|
2
2
|
"name": "utilium",
|
3
|
-
"version": "0.7.
|
3
|
+
"version": "0.7.4",
|
4
4
|
"description": "Typescript utilies",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
7
7
|
"type": "module",
|
8
8
|
"exports": {
|
9
9
|
".": "./dist/index.js",
|
10
|
-
"./*": "./dist/*"
|
10
|
+
"./*": "./dist/*",
|
11
|
+
"./eslint": "./eslint.shared.js",
|
12
|
+
"./source": "./src/*"
|
11
13
|
},
|
12
14
|
"files": [
|
13
15
|
"src",
|
14
16
|
"dist",
|
15
|
-
"tsconfig.json"
|
17
|
+
"tsconfig.json",
|
18
|
+
"scripts"
|
16
19
|
],
|
17
20
|
"scripts": {
|
18
21
|
"format:check": "prettier --check .",
|
19
22
|
"format": "prettier --write .",
|
20
|
-
"lint": "
|
23
|
+
"lint": "tsc --noEmit && eslint src",
|
21
24
|
"build": "tsc -p tsconfig.json",
|
22
|
-
"build:docs": "typedoc
|
25
|
+
"build:docs": "typedoc",
|
23
26
|
"prepublishOnly": "npm run build"
|
24
27
|
},
|
25
28
|
"repository": {
|
@@ -33,13 +36,16 @@
|
|
33
36
|
},
|
34
37
|
"homepage": "https://github.com/james-pre/utilium#readme",
|
35
38
|
"devDependencies": {
|
39
|
+
"@eslint/js": "^9.12.0",
|
40
|
+
"@stylistic/eslint-plugin": "^2.9.0",
|
41
|
+
"@types/eslint__js": "^8.42.3",
|
36
42
|
"@types/node": "^20.12.7",
|
37
|
-
"
|
38
|
-
"
|
39
|
-
"eslint": "^8.45.0",
|
43
|
+
"eslint": "^9.12.0",
|
44
|
+
"globals": "^15.10.0",
|
40
45
|
"prettier": "^3.2.5",
|
41
46
|
"typedoc": "^0.26.6",
|
42
|
-
"typescript": "^5.5.4"
|
47
|
+
"typescript": "^5.5.4",
|
48
|
+
"typescript-eslint": "^8.8.0"
|
43
49
|
},
|
44
50
|
"dependencies": {
|
45
51
|
"eventemitter3": "^5.0.1"
|
package/src/internal/struct.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { ClassLike } from '../types.js';
|
2
|
-
import * as primitive from './primitives.js';
|
1
|
+
import type { ClassLike } from '../types.js';
|
2
|
+
import type * as primitive from './primitives.js';
|
3
3
|
|
4
4
|
export interface MemberInit {
|
5
5
|
name: string;
|
@@ -62,11 +62,37 @@ export function isValidMetadata<T extends Metadata = Metadata>(
|
|
62
62
|
return arg != null && typeof arg == 'object' && metadata in arg;
|
63
63
|
}
|
64
64
|
|
65
|
+
/**
|
66
|
+
* Polyfill Symbol.metadata
|
67
|
+
* @see https://github.com/microsoft/TypeScript/issues/53461
|
68
|
+
*/
|
69
|
+
(Symbol as { metadata: symbol }).metadata ??= Symbol.for('Symbol.metadata');
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Polyfill context.metadata
|
73
|
+
* @see https://github.com/microsoft/TypeScript/issues/53461
|
74
|
+
*/
|
75
|
+
export function _polyfill_contextMetadata(target: object): void {
|
76
|
+
if (!Symbol?.metadata) {
|
77
|
+
return;
|
78
|
+
}
|
79
|
+
if (Symbol.metadata in target) {
|
80
|
+
return;
|
81
|
+
}
|
82
|
+
Object.defineProperty(target, Symbol.metadata, {
|
83
|
+
enumerable: true,
|
84
|
+
configurable: true,
|
85
|
+
writable: true,
|
86
|
+
value: Object.create(null),
|
87
|
+
});
|
88
|
+
}
|
89
|
+
|
65
90
|
/**
|
66
91
|
* Gets a reference to Symbol.metadata, even on platforms that do not expose it globally (like Node)
|
67
92
|
*/
|
68
93
|
export function symbol_metadata(arg: ClassLike): typeof Symbol.metadata {
|
69
94
|
const symbol_metadata = Symbol.metadata || Object.getOwnPropertySymbols(arg).find(s => s.description == 'Symbol.metadata');
|
95
|
+
_polyfill_contextMetadata(arg);
|
70
96
|
if (!symbol_metadata) {
|
71
97
|
throw new ReferenceError('Could not get a reference to Symbol.metadata');
|
72
98
|
}
|
@@ -92,7 +118,7 @@ export function isInstance<T extends Metadata = Metadata>(arg: unknown): arg is
|
|
92
118
|
|
93
119
|
export function checkInstance<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T> {
|
94
120
|
if (!isInstance(arg)) {
|
95
|
-
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance');
|
121
|
+
throw new TypeError((typeof arg == 'function' ? arg.name : typeof arg == 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance');
|
96
122
|
}
|
97
123
|
}
|
98
124
|
|
@@ -102,7 +128,7 @@ export function isStruct<T extends Metadata = Metadata>(arg: unknown): arg is In
|
|
102
128
|
|
103
129
|
export function checkStruct<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T> | Static<T> {
|
104
130
|
if (!isStruct(arg)) {
|
105
|
-
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct');
|
131
|
+
throw new TypeError((typeof arg == 'function' ? arg.name : typeof arg == 'object' && arg ? arg.constructor.name : arg) + ' is not a struct');
|
106
132
|
}
|
107
133
|
}
|
108
134
|
|
package/src/objects.ts
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
import { UnionToTuple } from './types.js';
|
1
|
+
import type { UnionToTuple } from './types.js';
|
2
2
|
|
3
3
|
export function filterObject<O extends object, R extends object>(object: O, predicate: (key: keyof O, value: O[keyof O]) => boolean): R {
|
4
|
-
const entries =
|
5
|
-
return
|
4
|
+
const entries = Object.entries(object) as [keyof O, O[keyof O]][];
|
5
|
+
return Object.fromEntries(entries.filter(([key, value]) => predicate(key, value))) as R;
|
6
6
|
}
|
7
7
|
|
8
8
|
export function pick<T extends object, K extends keyof T>(object: T, ...keys: readonly K[]): Pick<T, K>;
|
9
9
|
export function pick<T extends object, K extends keyof T>(object: T, ...keys: readonly (readonly K[])[]): Pick<T, K>;
|
10
10
|
export function pick<T extends object, K extends keyof T>(object: T, ...keys: readonly K[] | readonly (readonly K[])[]): Pick<T, K> {
|
11
|
-
const picked =
|
11
|
+
const picked = {} as Pick<T, K>;
|
12
12
|
for (const key of keys.flat() as K[]) {
|
13
13
|
picked[key] = object[key];
|
14
14
|
}
|
package/src/string.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
export function capitalize<T extends string>(value: T): Capitalize<T> {
|
2
|
-
return
|
2
|
+
return (value.at(0)!.toUpperCase() + value.slice(1)) as Capitalize<T>;
|
3
3
|
}
|
4
4
|
|
5
5
|
export function uncapitalize<T extends string>(value: T): Uncapitalize<T> {
|
6
|
-
return
|
6
|
+
return (value.at(0)!.toLowerCase() + value.slice(1)) as Uncapitalize<T>;
|
7
7
|
}
|
8
8
|
|
9
9
|
export type ConcatString<T extends string[]> = T extends [infer F extends string, ...infer R extends string[]] ? `${F}${ConcatString<R>}` : '';
|
package/src/struct.ts
CHANGED
@@ -1,22 +1,8 @@
|
|
1
1
|
import * as primitive from './internal/primitives.js';
|
2
|
-
import {
|
3
|
-
|
4
|
-
checkStruct,
|
5
|
-
DecoratorContext,
|
6
|
-
init,
|
7
|
-
InstanceLike,
|
8
|
-
isStatic,
|
9
|
-
MemberInit,
|
10
|
-
Metadata,
|
11
|
-
metadata,
|
12
|
-
Options,
|
13
|
-
Size,
|
14
|
-
StaticLike,
|
15
|
-
symbol_metadata,
|
16
|
-
type MemberContext,
|
17
|
-
} from './internal/struct.js';
|
2
|
+
import type { DecoratorContext, InstanceLike, Member, MemberInit, Metadata, Options, Size, StaticLike } from './internal/struct.js';
|
3
|
+
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata, type MemberContext } from './internal/struct.js';
|
18
4
|
import { capitalize } from './string.js';
|
19
|
-
import { ClassLike } from './types.js';
|
5
|
+
import type { ClassLike } from './types.js';
|
20
6
|
export * as Struct from './internal/struct.js';
|
21
7
|
|
22
8
|
/**
|
@@ -48,12 +34,11 @@ export function align(value: number, alignment: number): number {
|
|
48
34
|
* Decorates a class as a struct
|
49
35
|
*/
|
50
36
|
export function struct(options: Partial<Options> = {}) {
|
51
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
52
37
|
return function _decorateStruct<const T extends StaticLike>(target: T, context: ClassDecoratorContext & DecoratorContext): T {
|
53
38
|
context.metadata ??= {};
|
54
39
|
context.metadata[init] ||= [];
|
55
40
|
let size = 0;
|
56
|
-
const members = new Map();
|
41
|
+
const members = new Map<string, Member>();
|
57
42
|
for (const _ of context.metadata[init]) {
|
58
43
|
const { name, type, length } = _;
|
59
44
|
if (!primitive.isValid(type) && !isStatic(type)) {
|
@@ -121,7 +106,7 @@ export function serialize(instance: unknown): Uint8Array {
|
|
121
106
|
}
|
122
107
|
|
123
108
|
const Type = capitalize(type);
|
124
|
-
const fn =
|
109
|
+
const fn = ('set' + Type) as `set${typeof Type}`;
|
125
110
|
if (fn == 'setInt64') {
|
126
111
|
view.setBigInt64(iOff, BigInt(value), !options.bigEndian);
|
127
112
|
continue;
|
@@ -177,7 +162,7 @@ export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBuffe
|
|
177
162
|
}
|
178
163
|
|
179
164
|
const Type = capitalize(type);
|
180
|
-
const fn =
|
165
|
+
const fn = ('get' + Type) as `get${typeof Type}`;
|
181
166
|
if (fn == 'getInt64') {
|
182
167
|
object[key] = view.getBigInt64(iOff, !options.bigEndian);
|
183
168
|
continue;
|
package/src/types.ts
CHANGED
@@ -28,7 +28,7 @@ export type ExtractProperties<T, P> = {
|
|
28
28
|
* Extract the keys of T which are required
|
29
29
|
* @see https://stackoverflow.com/a/55247867/17637456
|
30
30
|
*/
|
31
|
-
// eslint-disable-next-line @typescript-eslint/
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
32
32
|
export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K }[keyof T];
|
33
33
|
|
34
34
|
/**
|