utilium 1.0.4 → 1.1.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/list.d.ts +4 -2
- package/dist/list.js +11 -4
- package/dist/struct.d.ts +1 -1
- package/dist/struct.js +1 -1
- package/package.json +5 -1
- package/src/list.ts +16 -6
- package/src/struct.ts +2 -2
package/dist/list.d.ts
CHANGED
@@ -7,8 +7,10 @@ export declare class List<T> extends EventEmitter<'update'> implements RelativeI
|
|
7
7
|
toArray(): T[];
|
8
8
|
toJSON(): string;
|
9
9
|
toString(): string;
|
10
|
+
protected _set(index: number, value: T, _delete?: boolean): void;
|
10
11
|
set(index: number, value: T): void;
|
11
12
|
deleteAt(index: number): void;
|
13
|
+
insert(value: T, index?: number): void;
|
12
14
|
at(index: number): T;
|
13
15
|
pop(): T | undefined;
|
14
16
|
push(...items: T[]): number;
|
@@ -19,8 +21,8 @@ export declare class List<T> extends EventEmitter<'update'> implements RelativeI
|
|
19
21
|
delete(value: T): boolean;
|
20
22
|
has(value: T): boolean;
|
21
23
|
get size(): number;
|
22
|
-
entries(): IterableIterator<[
|
23
|
-
keys(): IterableIterator<
|
24
|
+
entries(): IterableIterator<[number, T]>;
|
25
|
+
keys(): IterableIterator<number>;
|
24
26
|
values(): IterableIterator<T>;
|
25
27
|
[Symbol.iterator](): IterableIterator<T>;
|
26
28
|
}
|
package/dist/list.js
CHANGED
@@ -20,21 +20,27 @@ export class List extends EventEmitter {
|
|
20
20
|
toString() {
|
21
21
|
return this.join(',');
|
22
22
|
}
|
23
|
-
|
23
|
+
_set(index, value, _delete = false) {
|
24
24
|
if (Math.abs(index) > this.data.size) {
|
25
25
|
throw new ReferenceError('Can not set an element outside the bounds of the list');
|
26
26
|
}
|
27
27
|
const data = Array.from(this.data);
|
28
|
-
data.splice(index,
|
28
|
+
data.splice(index, +_delete, value);
|
29
29
|
this.data = new Set(data);
|
30
30
|
this.emit('update');
|
31
31
|
}
|
32
|
+
set(index, value) {
|
33
|
+
this._set(index, value, true);
|
34
|
+
}
|
32
35
|
deleteAt(index) {
|
33
36
|
if (Math.abs(index) > this.data.size) {
|
34
37
|
throw new ReferenceError('Can not delete an element outside the bounds of the list');
|
35
38
|
}
|
36
39
|
this.delete(Array.from(this.data).at(index));
|
37
40
|
}
|
41
|
+
insert(value, index = this.data.size) {
|
42
|
+
this._set(index, value, false);
|
43
|
+
}
|
38
44
|
// Array methods
|
39
45
|
at(index) {
|
40
46
|
if (Math.abs(index) > this.data.size) {
|
@@ -89,11 +95,12 @@ export class List extends EventEmitter {
|
|
89
95
|
get size() {
|
90
96
|
return this.data.size;
|
91
97
|
}
|
98
|
+
// Iteration
|
92
99
|
entries() {
|
93
|
-
return this.
|
100
|
+
return this.toArray().entries();
|
94
101
|
}
|
95
102
|
keys() {
|
96
|
-
return this.
|
103
|
+
return this.toArray().keys();
|
97
104
|
}
|
98
105
|
values() {
|
99
106
|
return this.data.values();
|
package/dist/struct.d.ts
CHANGED
@@ -26,7 +26,7 @@ export declare function serialize(instance: unknown): Uint8Array;
|
|
26
26
|
/**
|
27
27
|
* Deserializes a struct from a Uint8Array
|
28
28
|
*/
|
29
|
-
export declare function deserialize(instance: unknown, _buffer:
|
29
|
+
export declare function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayBufferView): void;
|
30
30
|
declare function _member<T extends primitive.Valid>(type: T): {
|
31
31
|
<const V>(length: number): (value: V, context: MemberContext) => V;
|
32
32
|
<const V>(value: V, context: MemberContext): V;
|
package/dist/struct.js
CHANGED
@@ -108,7 +108,7 @@ export function deserialize(instance, _buffer) {
|
|
108
108
|
checkInstance(instance);
|
109
109
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];
|
110
110
|
const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);
|
111
|
-
const view = new DataView(buffer.buffer
|
111
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
112
112
|
for (const [name, { type, offset, length }] of members) {
|
113
113
|
for (let i = 0; i < (length || 1); i++) {
|
114
114
|
// @ts-expect-error 7053
|
package/package.json
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "utilium",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.1.1",
|
4
4
|
"description": "Typescript utilities",
|
5
|
+
"funding": {
|
6
|
+
"type": "individual",
|
7
|
+
"url": "https://github.com/sponsors/james-pre"
|
8
|
+
},
|
5
9
|
"main": "dist/index.js",
|
6
10
|
"types": "dist/index.d.ts",
|
7
11
|
"type": "module",
|
package/src/list.ts
CHANGED
@@ -28,17 +28,21 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
28
28
|
return this.join(',');
|
29
29
|
}
|
30
30
|
|
31
|
-
|
31
|
+
protected _set(index: number, value: T, _delete: boolean = false) {
|
32
32
|
if (Math.abs(index) > this.data.size) {
|
33
33
|
throw new ReferenceError('Can not set an element outside the bounds of the list');
|
34
34
|
}
|
35
35
|
|
36
36
|
const data = Array.from(this.data);
|
37
|
-
data.splice(index,
|
37
|
+
data.splice(index, +_delete, value);
|
38
38
|
this.data = new Set<T>(data);
|
39
39
|
this.emit('update');
|
40
40
|
}
|
41
41
|
|
42
|
+
public set(index: number, value: T): void {
|
43
|
+
this._set(index, value, true);
|
44
|
+
}
|
45
|
+
|
42
46
|
public deleteAt(index: number): void {
|
43
47
|
if (Math.abs(index) > this.data.size) {
|
44
48
|
throw new ReferenceError('Can not delete an element outside the bounds of the list');
|
@@ -47,6 +51,10 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
47
51
|
this.delete(Array.from(this.data).at(index)!);
|
48
52
|
}
|
49
53
|
|
54
|
+
public insert(value: T, index: number = this.data.size) {
|
55
|
+
this._set(index, value, false);
|
56
|
+
}
|
57
|
+
|
50
58
|
// Array methods
|
51
59
|
|
52
60
|
public at(index: number): T {
|
@@ -115,12 +123,14 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
115
123
|
return this.data.size;
|
116
124
|
}
|
117
125
|
|
118
|
-
|
119
|
-
|
126
|
+
// Iteration
|
127
|
+
|
128
|
+
public entries(): IterableIterator<[number, T]> {
|
129
|
+
return this.toArray().entries();
|
120
130
|
}
|
121
131
|
|
122
|
-
public keys(): IterableIterator<
|
123
|
-
return this.
|
132
|
+
public keys(): IterableIterator<number> {
|
133
|
+
return this.toArray().keys();
|
124
134
|
}
|
125
135
|
|
126
136
|
public values(): IterableIterator<T> {
|
package/src/struct.ts
CHANGED
@@ -127,13 +127,13 @@ export function serialize(instance: unknown): Uint8Array {
|
|
127
127
|
/**
|
128
128
|
* Deserializes a struct from a Uint8Array
|
129
129
|
*/
|
130
|
-
export function deserialize(instance: unknown, _buffer:
|
130
|
+
export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayBufferView) {
|
131
131
|
checkInstance(instance);
|
132
132
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];
|
133
133
|
|
134
134
|
const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);
|
135
135
|
|
136
|
-
const view = new DataView(buffer.buffer
|
136
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
137
137
|
|
138
138
|
for (const [name, { type, offset, length }] of members) {
|
139
139
|
for (let i = 0; i < (length || 1); i++) {
|