@tspro/ts-utils-lib 1.0.1 → 1.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.2.0] - 2025-07-17
4
+ ### Added
5
+ - Assert.assertEnum(...)
6
+
7
+ ## [1.1.0] - 2025-07-12
8
+ ### Added
9
+ - Utils.Obj.isObject
10
+ ### Changed
11
+ - Switched bundler from webpack to tsup.
12
+ - Switched license from zlib to MIT.
13
+
3
14
  ## [1.0.1] - 2025-06-30
4
15
  ### Changed
5
16
  - Configured babel for ES6.
package/LICENSE CHANGED
@@ -1,22 +1,25 @@
1
1
 
2
- This project is licensed under the zlib License.
2
+ This project is licensed under the MIT License.
3
3
 
4
4
  ---
5
5
 
6
6
  Copyright (c) 2023 PahkaSoft
7
-
8
- This software is provided 'as-is', without any express or implied
9
- warranty. In no event will the authors be held liable for any damages
10
- arising from the use of this software.
11
-
12
- Permission is granted to anyone to use this software for any purpose,
13
- including commercial applications, and to alter it and redistribute it
14
- freely, subject to the following restrictions:
15
-
16
- 1. The origin of this software must not be misrepresented; you must not
17
- claim that you wrote the original software. If you use this software
18
- in a product, an acknowledgment in the product documentation would be
19
- appreciated but is not required.
20
- 2. Altered source versions must be plainly marked as such, and must not be
21
- misrepresented as being the original software.
22
- 3. This notice may not be removed or altered from any source distribution.
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining
9
+ a copy of this software and associated documentation files (the
10
+ "Software"), to deal in the Software without restriction, including
11
+ without limitation the rights to use, copy, modify, merge, publish,
12
+ distribute, sublicense, and/or sell copies of the Software, and to
13
+ permit persons to whom the Software is furnished to do so, subject to
14
+ the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included
17
+ in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,348 @@
1
+ declare function isArray<T>(a: T[] | unknown): a is T[];
2
+ declare function toArray<T>(a: T | T[]): Array<T>;
3
+ declare function duplicate<T>(a: T[] | undefined): T[] | undefined;
4
+ declare function removeDuplicates<T>(a: T[]): T[];
5
+ declare function removeDuplicatesCmp<T>(arr: ReadonlyArray<T>, cmp: (t1: T, t2: T) => boolean): T[];
6
+ declare function fillArray<T>(fillValue: T, fillCount: number): T[];
7
+ declare function mapSequenceArray<T>(len: number, fn: (i: number) => T): T[];
8
+ declare function getSequenceArray(len: number): number[];
9
+ declare function mapRangeArray<T>(start: number, end: number, fn: (i: number) => T): T[];
10
+ declare function getRangeArray(start: number, end: number): number[];
11
+ declare function arrayContains<T extends unknown>(arg: T[], item: T): boolean;
12
+ declare function chunckArray<A>(arr: ReadonlyArray<A>, chunckSize: number): A[][];
13
+
14
+ declare const index$7_arrayContains: typeof arrayContains;
15
+ declare const index$7_chunckArray: typeof chunckArray;
16
+ declare const index$7_duplicate: typeof duplicate;
17
+ declare const index$7_fillArray: typeof fillArray;
18
+ declare const index$7_getRangeArray: typeof getRangeArray;
19
+ declare const index$7_getSequenceArray: typeof getSequenceArray;
20
+ declare const index$7_isArray: typeof isArray;
21
+ declare const index$7_mapRangeArray: typeof mapRangeArray;
22
+ declare const index$7_mapSequenceArray: typeof mapSequenceArray;
23
+ declare const index$7_removeDuplicates: typeof removeDuplicates;
24
+ declare const index$7_removeDuplicatesCmp: typeof removeDuplicatesCmp;
25
+ declare const index$7_toArray: typeof toArray;
26
+ declare namespace index$7 {
27
+ export { index$7_arrayContains as arrayContains, index$7_chunckArray as chunckArray, index$7_duplicate as duplicate, index$7_fillArray as fillArray, index$7_getRangeArray as getRangeArray, index$7_getSequenceArray as getSequenceArray, index$7_isArray as isArray, index$7_mapRangeArray as mapRangeArray, index$7_mapSequenceArray as mapSequenceArray, index$7_removeDuplicates as removeDuplicates, index$7_removeDuplicatesCmp as removeDuplicatesCmp, index$7_toArray as toArray };
28
+ }
29
+
30
+ interface CSSProperties {
31
+ padding?: string | number;
32
+ paddingLeft?: string | number;
33
+ paddingRight?: string | number;
34
+ paddingTop?: string | number;
35
+ paddingBottom?: string | number;
36
+ left?: string | number;
37
+ right?: string | number;
38
+ top?: string | number;
39
+ bottom?: string | number;
40
+ width?: string | number;
41
+ height?: string | number;
42
+ }
43
+ declare function hasClass(el: HTMLElement, className: string): boolean;
44
+ declare function addClass(el: HTMLElement, className: string): void;
45
+ declare function removeClass(el: HTMLElement, className: string): void;
46
+ declare function setOffset(el: HTMLElement, left: number, top: number, unit?: string): void;
47
+ declare function getOffset(el: HTMLElement): {
48
+ left: number;
49
+ top: number;
50
+ };
51
+ declare function getWidth(el: HTMLElement | Window): number;
52
+ declare function setWidth(el: HTMLElement, val: number): void;
53
+ declare function getHeight(el: HTMLElement | Window): number;
54
+ declare function setHeight(el: HTMLElement, val: number): void;
55
+ declare function appendTo(el: HTMLElement | SVGElement, to: HTMLElement | SVGElement): void;
56
+ declare function removeFromParent(el: HTMLElement | SVGElement): void;
57
+ declare function setVisibility(el: HTMLElement | SVGElement, visible: boolean): void;
58
+ declare function setRect(el: HTMLElement, left: number, top: number, width: number, height: number, unit?: string): void;
59
+ declare function getButton(btn: HTMLElement | string): HTMLButtonElement | undefined;
60
+ declare function getCanvas(canvas: HTMLElement | string): HTMLCanvasElement | undefined;
61
+ declare function getPadding(style?: CSSProperties): {
62
+ top: number;
63
+ right: number;
64
+ bottom: number;
65
+ left: number;
66
+ };
67
+ declare function getDimension(style?: CSSProperties): {
68
+ left: number | undefined;
69
+ top: number | undefined;
70
+ width: number | undefined;
71
+ height: number | undefined;
72
+ };
73
+ declare function styleLayoutChanged(style1?: CSSProperties, style2?: CSSProperties): boolean;
74
+ declare function getCanvasTextWidth(text: string, font: string): number;
75
+
76
+ type index$6_CSSProperties = CSSProperties;
77
+ declare const index$6_addClass: typeof addClass;
78
+ declare const index$6_appendTo: typeof appendTo;
79
+ declare const index$6_getButton: typeof getButton;
80
+ declare const index$6_getCanvas: typeof getCanvas;
81
+ declare const index$6_getCanvasTextWidth: typeof getCanvasTextWidth;
82
+ declare const index$6_getDimension: typeof getDimension;
83
+ declare const index$6_getHeight: typeof getHeight;
84
+ declare const index$6_getOffset: typeof getOffset;
85
+ declare const index$6_getPadding: typeof getPadding;
86
+ declare const index$6_getWidth: typeof getWidth;
87
+ declare const index$6_hasClass: typeof hasClass;
88
+ declare const index$6_removeClass: typeof removeClass;
89
+ declare const index$6_removeFromParent: typeof removeFromParent;
90
+ declare const index$6_setHeight: typeof setHeight;
91
+ declare const index$6_setOffset: typeof setOffset;
92
+ declare const index$6_setRect: typeof setRect;
93
+ declare const index$6_setVisibility: typeof setVisibility;
94
+ declare const index$6_setWidth: typeof setWidth;
95
+ declare const index$6_styleLayoutChanged: typeof styleLayoutChanged;
96
+ declare namespace index$6 {
97
+ export { type index$6_CSSProperties as CSSProperties, index$6_addClass as addClass, index$6_appendTo as appendTo, index$6_getButton as getButton, index$6_getCanvas as getCanvas, index$6_getCanvasTextWidth as getCanvasTextWidth, index$6_getDimension as getDimension, index$6_getHeight as getHeight, index$6_getOffset as getOffset, index$6_getPadding as getPadding, index$6_getWidth as getWidth, index$6_hasClass as hasClass, index$6_removeClass as removeClass, index$6_removeFromParent as removeFromParent, index$6_setHeight as setHeight, index$6_setOffset as setOffset, index$6_setRect as setRect, index$6_setVisibility as setVisibility, index$6_setWidth as setWidth, index$6_styleLayoutChanged as styleLayoutChanged };
98
+ }
99
+
100
+ type EnumObject = {
101
+ [key: string]: string | number;
102
+ };
103
+ type EnumValue<E extends EnumObject> = E extends {
104
+ [key: string]: string | infer V;
105
+ } ? V : never;
106
+ declare function getEnumValues<E extends EnumObject>(e: E): Array<EnumValue<E>>;
107
+
108
+ type index$5_EnumObject = EnumObject;
109
+ type index$5_EnumValue<E extends EnumObject> = EnumValue<E>;
110
+ declare const index$5_getEnumValues: typeof getEnumValues;
111
+ declare namespace index$5 {
112
+ export { type index$5_EnumObject as EnumObject, type index$5_EnumValue as EnumValue, index$5_getEnumValues as getEnumValues };
113
+ }
114
+
115
+ declare function getMapKeys<K, V>(map: Map<K, V>): K[];
116
+
117
+ declare const index$4_getMapKeys: typeof getMapKeys;
118
+ declare namespace index$4 {
119
+ export { index$4_getMapKeys as getMapKeys };
120
+ }
121
+
122
+ declare function isInteger(n: unknown): n is number;
123
+ declare function linearToDecibels(linearVolume: number): number;
124
+ declare function mod(m: number, n: number): number;
125
+ /**
126
+ * Convert number to roman number.
127
+ * https://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter
128
+ */
129
+ declare function romanize(n: number): string;
130
+ declare function toOrdinalNumber(n: number): string;
131
+ declare function interpolateCoord(startX: number, startY: number, endX: number, endY: number, t: number): {
132
+ x: number;
133
+ y: number;
134
+ };
135
+ declare function interpolateY(startX: number, startY: number, endX: number, endY: number, x: number): number;
136
+ declare function clamp(num: number, min: number, max: number): number;
137
+ declare function calcNormal(x1: number, y1: number, x2: number, y2: number): {
138
+ nx: number;
139
+ ny: number;
140
+ };
141
+ declare function sum(arr: number[]): number;
142
+
143
+ declare const index$3_calcNormal: typeof calcNormal;
144
+ declare const index$3_clamp: typeof clamp;
145
+ declare const index$3_interpolateCoord: typeof interpolateCoord;
146
+ declare const index$3_interpolateY: typeof interpolateY;
147
+ declare const index$3_isInteger: typeof isInteger;
148
+ declare const index$3_linearToDecibels: typeof linearToDecibels;
149
+ declare const index$3_mod: typeof mod;
150
+ declare const index$3_romanize: typeof romanize;
151
+ declare const index$3_sum: typeof sum;
152
+ declare const index$3_toOrdinalNumber: typeof toOrdinalNumber;
153
+ declare namespace index$3 {
154
+ export { index$3_calcNormal as calcNormal, index$3_clamp as clamp, index$3_interpolateCoord as interpolateCoord, index$3_interpolateY as interpolateY, index$3_isInteger as isInteger, index$3_linearToDecibels as linearToDecibels, index$3_mod as mod, index$3_romanize as romanize, index$3_sum as sum, index$3_toOrdinalNumber as toOrdinalNumber };
155
+ }
156
+
157
+ declare function isObject(value: unknown): value is Record<string, unknown>;
158
+
159
+ declare const index$2_isObject: typeof isObject;
160
+ declare namespace index$2 {
161
+ export { index$2_isObject as isObject };
162
+ }
163
+
164
+ /**
165
+ * toCharArray("abc") => ["a", "b", "c"].
166
+ * @param str
167
+ */
168
+ declare function toCharArray(str: string): string[];
169
+ /**
170
+ * repeat("abc", 3) => "abcabcabc".
171
+ * repeat("abc", 0) => "".
172
+ * @param repeatString
173
+ * @param repeatCount
174
+ */
175
+ declare function repeatString(repeatString: string, repeatCount: number): string;
176
+ /**
177
+ * "abcdefgh":chunkSize = 3 => ["abc", "def", "gh"]
178
+ * @param str
179
+ * @param chunkSize
180
+ */
181
+ declare function chunkString(str: string, chunkSize: number): string[];
182
+ /**
183
+ * Replaces part of string.
184
+ * @param str
185
+ * @param pos
186
+ * @param removeCount
187
+ * @param insert
188
+ */
189
+ declare function replaceAt(str: string, pos: number, removeCount: number, insert: string): string;
190
+ /**
191
+ * Insert string to another.
192
+ * @param str
193
+ * @param pos
194
+ * @param insertStr
195
+ */
196
+ declare function insertAt(str: string, pos: number, insertStr: string): string;
197
+ /**
198
+ * Remove part of string.
199
+ * @param str
200
+ * @param pos
201
+ * @param removeCount
202
+ */
203
+ declare function removeAt(str: string, pos: number, removeCount: number): string;
204
+ /**
205
+ * "UndeclaredVariable" => "Undeclared variable"
206
+ * @param PascalString
207
+ */
208
+ declare function makeSentenceFromPascal(PascalString: string): string;
209
+
210
+ declare const index$1_chunkString: typeof chunkString;
211
+ declare const index$1_insertAt: typeof insertAt;
212
+ declare const index$1_makeSentenceFromPascal: typeof makeSentenceFromPascal;
213
+ declare const index$1_removeAt: typeof removeAt;
214
+ declare const index$1_repeatString: typeof repeatString;
215
+ declare const index$1_replaceAt: typeof replaceAt;
216
+ declare const index$1_toCharArray: typeof toCharArray;
217
+ declare namespace index$1 {
218
+ export { index$1_chunkString as chunkString, index$1_insertAt as insertAt, index$1_makeSentenceFromPascal as makeSentenceFromPascal, index$1_removeAt as removeAt, index$1_repeatString as repeatString, index$1_replaceAt as replaceAt, index$1_toCharArray as toCharArray };
219
+ }
220
+
221
+ declare namespace index {
222
+ export { index$7 as Arr, index$6 as Dom, index$5 as Enum, index$4 as Map, index$3 as Math, index$2 as Obj, index$1 as Str };
223
+ }
224
+
225
+ declare namespace Assert {
226
+ type ErrorConstructor = new (msg: string) => Error;
227
+ function setErrorClass(ec?: ErrorConstructor): void;
228
+ function assert<T>(a: T, userMsg?: string): void;
229
+ function assertEnum<E extends Record<string, string | number>>(value: unknown, enumObj: E, name?: string): asserts value is E[keyof E];
230
+ function interrupt(userMsg?: string): never;
231
+ function int(a: unknown, userMsg?: string): number;
232
+ function eq<T>(a: T, b: T, userMsg?: string): T;
233
+ function int_eq(a: unknown, b: unknown, userMsg?: string): number;
234
+ function int_lt(a: unknown, b: unknown, userMsg?: string): number;
235
+ function int_lte(a: unknown, b: unknown, userMsg?: string): number;
236
+ function int_gt(a: unknown, b: unknown, userMsg?: string): number;
237
+ function int_gte(a: unknown, b: unknown, userMsg?: string): number;
238
+ function int_between(a: unknown, min: unknown, max: unknown, userMsg?: string): number;
239
+ function odd(a: unknown, userMsg?: string): number;
240
+ function even(a: unknown, userMsg?: string): number;
241
+ function in_group<T>(a: T, group: ReadonlyArray<T>, userMsg?: string): T;
242
+ function finite(a: unknown, userMsg?: string): number;
243
+ function array_id<T>(arr: Readonly<T[]>, id: unknown, userMsg?: string): number;
244
+ function array_elem<T>(arr: Readonly<T[]>, id: number, userMsg?: string): T;
245
+ function require<T>(arg: T, userMsg?: string): NonNullable<T>;
246
+ }
247
+
248
+ declare namespace Cookies {
249
+ function setExpireDays(days: number): void;
250
+ function isConsentPending(): boolean;
251
+ function accept(): void;
252
+ function decline(): void;
253
+ function save<T extends string | number | boolean>(name: string, value: T): T;
254
+ function read(name: string, defaultValue: string): string;
255
+ function readInt(name: string, defaultValue: number): number;
256
+ function readBool(name: string, defaultValue: boolean): boolean;
257
+ function erase(name: string): void;
258
+ function eraseAll(): void;
259
+ }
260
+
261
+ declare namespace Device {
262
+ const DPI: number;
263
+ const PxPerMm: number;
264
+ const ScrollbarWidth: number;
265
+ const FontSize: number;
266
+ const IsTouchDevice: boolean;
267
+ const IsMobileDevice: boolean;
268
+ const HostAddress: string;
269
+ function pxToMm(px: number): number;
270
+ function mmToPx(mm: number): number;
271
+ function toPx(input: string | number): number;
272
+ }
273
+
274
+ declare class Stack<T> {
275
+ private readonly list;
276
+ push(e: T): T;
277
+ pop(): T;
278
+ top(): T;
279
+ toArray(): T[];
280
+ get length(): number;
281
+ clear(): void;
282
+ }
283
+
284
+ declare class Vec2 {
285
+ readonly x: number;
286
+ readonly y: number;
287
+ constructor(x?: number, y?: number);
288
+ length(): number;
289
+ add(a: Vec2): Vec2;
290
+ sub(a: Vec2): Vec2;
291
+ mul(a: number): Vec2;
292
+ div(a: number): Vec2;
293
+ }
294
+
295
+ /**
296
+ * LRUCache class: Least Recently Used cache with a fixed capacity
297
+ *
298
+ * Provided by ChatGPT.
299
+ */
300
+ declare class LRUCache<K extends string, V> {
301
+ private cache;
302
+ private next;
303
+ private prev;
304
+ private head;
305
+ private tail;
306
+ private capacity;
307
+ private size;
308
+ private maxKeyLength;
309
+ constructor(maxSize: number, maxKeyLength?: number);
310
+ get(key: K): V | undefined;
311
+ set(key: K, value: V): void;
312
+ private touch;
313
+ private evict;
314
+ private removeKey;
315
+ private addToTail;
316
+ }
317
+
318
+ /**
319
+ * A cache-like structure optimized for small-range integer keys, including negatives.
320
+ *
321
+ * Internally implemented using two sparse arrays: one for non-negative keys,
322
+ * and one for negative keys (stored by index `-key - 1`).
323
+ *
324
+ * @remarks
325
+ * - Fast access for small integer keys.
326
+ * - Not suitable for large or sparse key ranges — memory usage may grow significantly.
327
+ * - Supports `get`, `set`, `delete`, `has` and `clear`
328
+ *
329
+ * @example
330
+ * ```ts
331
+ * const cache = new SmallIntCache<string>();
332
+ * cache.set(-2, 'A');
333
+ * cache.set(3, 'B');
334
+ * console.log(cache.get(-2)); // 'A'
335
+ * ```
336
+ */
337
+ declare class SmallIntCache<V> {
338
+ private pos;
339
+ private neg;
340
+ constructor();
341
+ set(key: number, value: V): void;
342
+ get(key: number): V | undefined;
343
+ has(key: number): boolean;
344
+ delete(key: number): void;
345
+ clear(): void;
346
+ }
347
+
348
+ export { Assert, Cookies, Device, LRUCache, SmallIntCache, Stack, index as Utils, Vec2 };