@develia/commons 0.4.15 → 0.4.18
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/cjs/array.js +38 -2
- package/dist/cjs/array.js.map +1 -1
- package/dist/cjs/async-pool-executor.js +26 -11
- package/dist/cjs/async-pool-executor.js.map +1 -1
- package/dist/cjs/browser/form-utilities.js +35 -34
- package/dist/cjs/browser/form-utilities.js.map +1 -1
- package/dist/cjs/browser/geo-location.js +1 -1
- package/dist/cjs/browser/geo-location.js.map +1 -1
- package/dist/cjs/browser/native-bridge.js +3 -1
- package/dist/cjs/browser/native-bridge.js.map +1 -1
- package/dist/cjs/cancellable-promise.js +34 -13
- package/dist/cjs/cancellable-promise.js.map +1 -1
- package/dist/cjs/clean-url-search-params.js +26 -3
- package/dist/cjs/clean-url-search-params.js.map +1 -1
- package/dist/cjs/collections/cache-dictionary.js +3 -3
- package/dist/cjs/collections/cache-dictionary.js.map +1 -1
- package/dist/cjs/collections/default-map.js.map +1 -1
- package/dist/cjs/collections/default-weak-map.js.map +1 -1
- package/dist/cjs/collections/from.js +3 -3
- package/dist/cjs/collections/from.js.map +1 -1
- package/dist/cjs/collections/index.js.map +1 -1
- package/dist/cjs/draft.js +46 -11
- package/dist/cjs/draft.js.map +1 -1
- package/dist/cjs/event-bus.js +1 -1
- package/dist/cjs/event-bus.js.map +1 -1
- package/dist/cjs/functions.js +348 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/key-value-pair.js.map +1 -1
- package/dist/cjs/lazy.js.map +1 -1
- package/dist/cjs/math.js +3 -0
- package/dist/cjs/math.js.map +1 -1
- package/dist/cjs/order.js.map +1 -1
- package/dist/cjs/parse.js +10 -1
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/range.js +154 -0
- package/dist/cjs/range.js.map +1 -0
- package/dist/cjs/stopwatch.js.map +1 -1
- package/dist/cjs/string.js +8 -5
- package/dist/cjs/string.js.map +1 -1
- package/dist/cjs/timer.js +1 -1
- package/dist/cjs/timer.js.map +1 -1
- package/dist/cjs/timespan.js +21 -8
- package/dist/cjs/timespan.js.map +1 -1
- package/dist/cjs/type-code.js.map +1 -1
- package/dist/cjs/type.js +20 -19
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utilities.js +175 -39
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/array.js +36 -2
- package/dist/esm/array.js.map +1 -1
- package/dist/esm/async-pool-executor.js +26 -11
- package/dist/esm/async-pool-executor.js.map +1 -1
- package/dist/esm/browser/form-utilities.js +35 -34
- package/dist/esm/browser/form-utilities.js.map +1 -1
- package/dist/esm/browser/geo-location.js +1 -1
- package/dist/esm/browser/geo-location.js.map +1 -1
- package/dist/esm/browser/native-bridge.js +3 -1
- package/dist/esm/browser/native-bridge.js.map +1 -1
- package/dist/esm/cancellable-promise.js +34 -13
- package/dist/esm/cancellable-promise.js.map +1 -1
- package/dist/esm/clean-url-search-params.js +26 -3
- package/dist/esm/clean-url-search-params.js.map +1 -1
- package/dist/esm/collections/cache-dictionary.js +3 -3
- package/dist/esm/collections/cache-dictionary.js.map +1 -1
- package/dist/esm/collections/default-map.js.map +1 -1
- package/dist/esm/collections/default-weak-map.js.map +1 -1
- package/dist/esm/collections/from.js +3 -3
- package/dist/esm/collections/from.js.map +1 -1
- package/dist/esm/collections/index.js.map +1 -1
- package/dist/esm/draft.js +46 -11
- package/dist/esm/draft.js.map +1 -1
- package/dist/esm/event-bus.js +1 -1
- package/dist/esm/event-bus.js.map +1 -1
- package/dist/esm/functions.js +336 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/key-value-pair.js.map +1 -1
- package/dist/esm/lazy.js.map +1 -1
- package/dist/esm/math.js +3 -0
- package/dist/esm/math.js.map +1 -1
- package/dist/esm/order.js.map +1 -1
- package/dist/esm/parse.js +10 -1
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/range.js +151 -0
- package/dist/esm/range.js.map +1 -0
- package/dist/esm/stopwatch.js.map +1 -1
- package/dist/esm/string.js +8 -5
- package/dist/esm/string.js.map +1 -1
- package/dist/esm/timer.js +1 -1
- package/dist/esm/timer.js.map +1 -1
- package/dist/esm/timespan.js +21 -8
- package/dist/esm/timespan.js.map +1 -1
- package/dist/esm/type-code.js.map +1 -1
- package/dist/esm/type.js +20 -19
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utilities.js +174 -39
- package/dist/esm/utilities.js.map +1 -1
- package/dist/types/array.d.ts +29 -0
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/async-pool-executor.d.ts +2 -2
- package/dist/types/async-pool-executor.d.ts.map +1 -1
- package/dist/types/browser/form-utilities.d.ts.map +1 -1
- package/dist/types/browser/native-bridge.d.ts.map +1 -1
- package/dist/types/cancellable-promise.d.ts.map +1 -1
- package/dist/types/clean-url-search-params.d.ts +3 -1
- package/dist/types/clean-url-search-params.d.ts.map +1 -1
- package/dist/types/collections/from.d.ts +1 -1
- package/dist/types/draft.d.ts.map +1 -1
- package/dist/types/functions.d.ts +231 -0
- package/dist/types/functions.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math.d.ts.map +1 -1
- package/dist/types/parse.d.ts.map +1 -1
- package/dist/types/range.d.ts +72 -0
- package/dist/types/range.d.ts.map +1 -0
- package/dist/types/string.d.ts.map +1 -1
- package/dist/types/timer.d.ts +1 -1
- package/dist/types/timespan.d.ts.map +1 -1
- package/dist/types/type.d.ts +2 -0
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/types.d.ts +1 -1
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utilities.d.ts +21 -3
- package/dist/types/utilities.d.ts.map +1 -1
- package/dist/web/array.js +36 -2
- package/dist/web/array.js.map +1 -1
- package/dist/web/async-pool-executor.js +26 -11
- package/dist/web/async-pool-executor.js.map +1 -1
- package/dist/web/browser/form-utilities.js +35 -34
- package/dist/web/browser/form-utilities.js.map +1 -1
- package/dist/web/browser/geo-location.js +1 -1
- package/dist/web/browser/geo-location.js.map +1 -1
- package/dist/web/browser/native-bridge.js +3 -1
- package/dist/web/browser/native-bridge.js.map +1 -1
- package/dist/web/cancellable-promise.js +34 -13
- package/dist/web/cancellable-promise.js.map +1 -1
- package/dist/web/clean-url-search-params.js +26 -3
- package/dist/web/clean-url-search-params.js.map +1 -1
- package/dist/web/collections/cache-dictionary.js +3 -3
- package/dist/web/collections/cache-dictionary.js.map +1 -1
- package/dist/web/collections/default-map.js.map +1 -1
- package/dist/web/collections/default-weak-map.js.map +1 -1
- package/dist/web/collections/from.js +3 -3
- package/dist/web/collections/from.js.map +1 -1
- package/dist/web/collections/index.js.map +1 -1
- package/dist/web/draft.js +46 -11
- package/dist/web/draft.js.map +1 -1
- package/dist/web/event-bus.js +1 -1
- package/dist/web/event-bus.js.map +1 -1
- package/dist/web/functions.js +336 -0
- package/dist/web/functions.js.map +1 -0
- package/dist/web/index.js +2 -0
- package/dist/web/index.js.map +1 -1
- package/dist/web/key-value-pair.js.map +1 -1
- package/dist/web/lazy.js.map +1 -1
- package/dist/web/math.js +3 -0
- package/dist/web/math.js.map +1 -1
- package/dist/web/order.js.map +1 -1
- package/dist/web/parse.js +10 -1
- package/dist/web/parse.js.map +1 -1
- package/dist/web/patch.js.map +1 -1
- package/dist/web/range.js +151 -0
- package/dist/web/range.js.map +1 -0
- package/dist/web/stopwatch.js.map +1 -1
- package/dist/web/string.js +8 -5
- package/dist/web/string.js.map +1 -1
- package/dist/web/timer.js +1 -1
- package/dist/web/timer.js.map +1 -1
- package/dist/web/timespan.js +21 -8
- package/dist/web/timespan.js.map +1 -1
- package/dist/web/type-code.js.map +1 -1
- package/dist/web/type.js +20 -19
- package/dist/web/type.js.map +1 -1
- package/dist/web/types.js.map +1 -1
- package/dist/web/utilities.js +174 -39
- package/dist/web/utilities.js.map +1 -1
- package/package.json +66 -60
- package/src/array.ts +116 -68
- package/src/async-pool-executor.ts +107 -85
- package/src/browser/form-utilities.ts +32 -31
- package/src/browser/geo-location.ts +113 -113
- package/src/browser/native-bridge.ts +118 -116
- package/src/cancellable-promise.ts +102 -76
- package/src/clean-url-search-params.ts +36 -11
- package/src/collections/cache-dictionary.ts +115 -115
- package/src/collections/default-map.ts +48 -48
- package/src/collections/default-weak-map.ts +51 -51
- package/src/collections/from.ts +859 -859
- package/src/collections/index.ts +3 -3
- package/src/draft.ts +55 -16
- package/src/event-bus.ts +83 -83
- package/src/functions.ts +496 -0
- package/src/index.ts +44 -42
- package/src/key-value-pair.ts +24 -24
- package/src/lazy.ts +32 -32
- package/src/math.ts +33 -30
- package/src/order.ts +5 -5
- package/src/parse.ts +251 -240
- package/src/patch.ts +79 -79
- package/src/range.ts +165 -0
- package/src/stopwatch.ts +43 -43
- package/src/string.ts +8 -5
- package/src/timer.ts +67 -67
- package/src/timespan.ts +412 -398
- package/src/type-code.ts +15 -15
- package/src/type.ts +372 -371
- package/src/types.ts +30 -30
- package/src/utilities.ts +653 -480
package/src/patch.ts
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
import type {Constructor} from "./types.js";
|
|
2
|
-
|
|
3
|
-
export type PureMethod<T extends object, A extends any[] = any[], R = any> =
|
|
4
|
-
(obj: T, ...args: A) => R;
|
|
5
|
-
|
|
6
|
-
export type PatchSpec<T extends object> =
|
|
7
|
-
| Record<string, PureMethod<T, any[], any>>
|
|
8
|
-
| Map<PropertyKey, PureMethod<T, any[], any>>;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export function patch<C extends Constructor<any>>(
|
|
12
|
-
targetOrCtor: C,
|
|
13
|
-
fns: PatchSpec<InstanceType<C>>
|
|
14
|
-
): Map<PropertyKey, PropertyDescriptor | null>;
|
|
15
|
-
|
|
16
|
-
export function patch<T extends object>(
|
|
17
|
-
targetOrCtor: T,
|
|
18
|
-
fns: PatchSpec<T>
|
|
19
|
-
): Map<PropertyKey, PropertyDescriptor | null>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Devuelve un Map donde:
|
|
23
|
-
* - clave -> null (método añadido nuevo)
|
|
24
|
-
* - clave -> descriptorOriginal (método sustituido)
|
|
25
|
-
*/
|
|
26
|
-
export function patch(
|
|
27
|
-
targetOrCtor: any,
|
|
28
|
-
fns: PatchSpec<any>
|
|
29
|
-
): Map<PropertyKey, PropertyDescriptor | null> {
|
|
30
|
-
const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
|
|
31
|
-
|
|
32
|
-
const result = new Map<PropertyKey, PropertyDescriptor | null>();
|
|
33
|
-
const entries: Array<[PropertyKey, PureMethod<any>]> =
|
|
34
|
-
fns instanceof Map ? [...fns.entries()] : Object.entries(fns) as any;
|
|
35
|
-
|
|
36
|
-
for (const [key, fn] of entries) {
|
|
37
|
-
const prev = Object.getOwnPropertyDescriptor(target, key);
|
|
38
|
-
const method = function (this: unknown, ...args: unknown[]) {
|
|
39
|
-
return (fn as any)(this, ...args);
|
|
40
|
-
};
|
|
41
|
-
try {
|
|
42
|
-
Object.defineProperty(target, key, {
|
|
43
|
-
value: method,
|
|
44
|
-
writable: true,
|
|
45
|
-
configurable: true,
|
|
46
|
-
enumerable: false,
|
|
47
|
-
});
|
|
48
|
-
result.set(key, prev ?? null);
|
|
49
|
-
} catch { /* ignorar */
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Overloads unpatch
|
|
56
|
-
export function unpatch<C extends Constructor<any>>(
|
|
57
|
-
targetOrCtor: C,
|
|
58
|
-
changes: Map<PropertyKey, PropertyDescriptor | null>
|
|
59
|
-
): void;
|
|
60
|
-
export function unpatch<T extends object>(
|
|
61
|
-
targetOrCtor: T,
|
|
62
|
-
changes: Map<PropertyKey, PropertyDescriptor | null>
|
|
63
|
-
): void;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Revierte un patch a partir del mapa devuelto por `patch`.
|
|
67
|
-
*/
|
|
68
|
-
export function unpatch(targetOrCtor: any, changes: Map<PropertyKey, PropertyDescriptor | null>): void {
|
|
69
|
-
const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
|
|
70
|
-
for (const [key, prev] of changes) {
|
|
71
|
-
try {
|
|
72
|
-
if (prev === null) {
|
|
73
|
-
if (Object.prototype.hasOwnProperty.call(target, key)) delete target[key];
|
|
74
|
-
} else {
|
|
75
|
-
Object.defineProperty(target, key, prev);
|
|
76
|
-
}
|
|
77
|
-
} catch { /* ignorar */
|
|
78
|
-
}
|
|
79
|
-
}
|
|
1
|
+
import type {Constructor} from "./types.js";
|
|
2
|
+
|
|
3
|
+
export type PureMethod<T extends object, A extends any[] = any[], R = any> =
|
|
4
|
+
(obj: T, ...args: A) => R;
|
|
5
|
+
|
|
6
|
+
export type PatchSpec<T extends object> =
|
|
7
|
+
| Record<string, PureMethod<T, any[], any>>
|
|
8
|
+
| Map<PropertyKey, PureMethod<T, any[], any>>;
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export function patch<C extends Constructor<any>>(
|
|
12
|
+
targetOrCtor: C,
|
|
13
|
+
fns: PatchSpec<InstanceType<C>>
|
|
14
|
+
): Map<PropertyKey, PropertyDescriptor | null>;
|
|
15
|
+
|
|
16
|
+
export function patch<T extends object>(
|
|
17
|
+
targetOrCtor: T,
|
|
18
|
+
fns: PatchSpec<T>
|
|
19
|
+
): Map<PropertyKey, PropertyDescriptor | null>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Devuelve un Map donde:
|
|
23
|
+
* - clave -> null (método añadido nuevo)
|
|
24
|
+
* - clave -> descriptorOriginal (método sustituido)
|
|
25
|
+
*/
|
|
26
|
+
export function patch(
|
|
27
|
+
targetOrCtor: any,
|
|
28
|
+
fns: PatchSpec<any>
|
|
29
|
+
): Map<PropertyKey, PropertyDescriptor | null> {
|
|
30
|
+
const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
|
|
31
|
+
|
|
32
|
+
const result = new Map<PropertyKey, PropertyDescriptor | null>();
|
|
33
|
+
const entries: Array<[PropertyKey, PureMethod<any>]> =
|
|
34
|
+
fns instanceof Map ? [...fns.entries()] : Object.entries(fns) as any;
|
|
35
|
+
|
|
36
|
+
for (const [key, fn] of entries) {
|
|
37
|
+
const prev = Object.getOwnPropertyDescriptor(target, key);
|
|
38
|
+
const method = function (this: unknown, ...args: unknown[]) {
|
|
39
|
+
return (fn as any)(this, ...args);
|
|
40
|
+
};
|
|
41
|
+
try {
|
|
42
|
+
Object.defineProperty(target, key, {
|
|
43
|
+
value: method,
|
|
44
|
+
writable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
enumerable: false,
|
|
47
|
+
});
|
|
48
|
+
result.set(key, prev ?? null);
|
|
49
|
+
} catch { /* ignorar */
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Overloads unpatch
|
|
56
|
+
export function unpatch<C extends Constructor<any>>(
|
|
57
|
+
targetOrCtor: C,
|
|
58
|
+
changes: Map<PropertyKey, PropertyDescriptor | null>
|
|
59
|
+
): void;
|
|
60
|
+
export function unpatch<T extends object>(
|
|
61
|
+
targetOrCtor: T,
|
|
62
|
+
changes: Map<PropertyKey, PropertyDescriptor | null>
|
|
63
|
+
): void;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Revierte un patch a partir del mapa devuelto por `patch`.
|
|
67
|
+
*/
|
|
68
|
+
export function unpatch(targetOrCtor: any, changes: Map<PropertyKey, PropertyDescriptor | null>): void {
|
|
69
|
+
const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
|
|
70
|
+
for (const [key, prev] of changes) {
|
|
71
|
+
try {
|
|
72
|
+
if (prev === null) {
|
|
73
|
+
if (Object.prototype.hasOwnProperty.call(target, key)) delete target[key];
|
|
74
|
+
} else {
|
|
75
|
+
Object.defineProperty(target, key, prev);
|
|
76
|
+
}
|
|
77
|
+
} catch { /* ignorar */
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
80
|
}
|
package/src/range.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
export default class Range<T> {
|
|
2
|
+
|
|
3
|
+
private readonly start: T;
|
|
4
|
+
private readonly end: T;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Range constructor.
|
|
8
|
+
* @param start
|
|
9
|
+
* @param end
|
|
10
|
+
*/
|
|
11
|
+
constructor(start: T, end: T) {
|
|
12
|
+
this.start = start;
|
|
13
|
+
this.end = end;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Finds the intersection between two ranges.
|
|
18
|
+
*
|
|
19
|
+
* @param other The Range object to intersect with.
|
|
20
|
+
* @returns The intersected Range object, or null if there is no intersection.
|
|
21
|
+
*/
|
|
22
|
+
public intersect(other: Range<T>): Range<T> | null {
|
|
23
|
+
const start = (this.start > other.start) ? this.start : other.start;
|
|
24
|
+
const end = (this.end < other.end) ? this.end : other.end;
|
|
25
|
+
|
|
26
|
+
if (start < end) {
|
|
27
|
+
return new Range(start, end);
|
|
28
|
+
} else {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Subtract the given range from this range.
|
|
35
|
+
*
|
|
36
|
+
* @param other The range to subtract from this range.
|
|
37
|
+
* @returns An array of Range objects representing the result of the subtraction.
|
|
38
|
+
*/
|
|
39
|
+
public subtract(other: Range<T>): Range<T>[] {
|
|
40
|
+
const result: Range<T>[] = [];
|
|
41
|
+
|
|
42
|
+
if (this.start >= other.end || this.end <= other.start) {
|
|
43
|
+
result.push(new Range(this.start, this.end));
|
|
44
|
+
} else if (other.start <= this.start && other.end >= this.end) {
|
|
45
|
+
return result;
|
|
46
|
+
} else {
|
|
47
|
+
if (this.start < other.start) {
|
|
48
|
+
result.push(new Range(this.start, other.start));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (this.end > other.end) {
|
|
52
|
+
result.push(new Range(other.end, this.end));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Determines if the given range overlaps with the current range.
|
|
61
|
+
*
|
|
62
|
+
* @param other The range to be checked for overlap.
|
|
63
|
+
* @returns Returns true if there is an overlap between the ranges, false otherwise.
|
|
64
|
+
*/
|
|
65
|
+
public overlaps(other: Range<T>): boolean {
|
|
66
|
+
const s1 = this.start;
|
|
67
|
+
const e1 = this.end;
|
|
68
|
+
const s2 = other.start;
|
|
69
|
+
const e2 = other.end;
|
|
70
|
+
|
|
71
|
+
return (s2 > s1 && s2 < e1 || e2 > s1 && e2 < e1) ||
|
|
72
|
+
(s1 > s2 && s1 < e2 || e1 > s2 && e1 < e2) ||
|
|
73
|
+
(s1 === s2 && e1 === e2);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Determines whether the specified value is within the range defined by the start and end values.
|
|
78
|
+
*
|
|
79
|
+
* @param obj The value to check if it is within the range.
|
|
80
|
+
* @param inclusive Optional. If set to true, the range boundaries are considered inclusive.
|
|
81
|
+
* @returns Returns true if the specified value is within the range, false otherwise.
|
|
82
|
+
*/
|
|
83
|
+
public contains(obj: T, inclusive: boolean = true): boolean {
|
|
84
|
+
return inclusive ?
|
|
85
|
+
(this.start <= obj && this.end >= obj) :
|
|
86
|
+
(this.start < obj && this.end > obj);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @returns T
|
|
91
|
+
*/
|
|
92
|
+
public getStart(): T {
|
|
93
|
+
return this.start;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @returns T
|
|
98
|
+
*/
|
|
99
|
+
public getEnd(): T {
|
|
100
|
+
return this.end;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Finds the intersection between two sets of ranges.
|
|
105
|
+
*
|
|
106
|
+
* @param ranges1
|
|
107
|
+
* @param ranges2
|
|
108
|
+
* @returns
|
|
109
|
+
*/
|
|
110
|
+
public static intersectRanges<T>(ranges1: Range<T>[], ranges2: Range<T>[]): Range<T>[] {
|
|
111
|
+
const result: Range<T>[] = [];
|
|
112
|
+
for (const r1 of ranges1) {
|
|
113
|
+
for (const r2 of ranges2) {
|
|
114
|
+
const intersection = r1.intersect(r2);
|
|
115
|
+
if (intersection !== null) {
|
|
116
|
+
result.push(intersection);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Determines if any range in the first set overlaps with any range in the second set.
|
|
125
|
+
*
|
|
126
|
+
* @param ranges1
|
|
127
|
+
* @param ranges2
|
|
128
|
+
* @returns
|
|
129
|
+
*/
|
|
130
|
+
public static overlapsAny<T>(ranges1: Range<T>[], ranges2: Range<T>[]): boolean {
|
|
131
|
+
for (const r1 of ranges1) {
|
|
132
|
+
for (const r2 of ranges2) {
|
|
133
|
+
if (r1.overlaps(r2)) {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Subtracts the second set of ranges from the first set of ranges.
|
|
143
|
+
*
|
|
144
|
+
* @param ranges1
|
|
145
|
+
* @param ranges2
|
|
146
|
+
* @returns
|
|
147
|
+
*/
|
|
148
|
+
public static subtractRanges<T>(ranges1: Range<T>[], ranges2: Range<T>[]): Range<T>[] {
|
|
149
|
+
let currentRanges = ranges1;
|
|
150
|
+
for (const r2 of ranges2) {
|
|
151
|
+
const nextRanges: Range<T>[] = [];
|
|
152
|
+
for (const r1 of currentRanges) {
|
|
153
|
+
const subtracted = r1.subtract(r2);
|
|
154
|
+
for (const s of subtracted) {
|
|
155
|
+
nextRanges.push(s);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
currentRanges = nextRanges;
|
|
159
|
+
if (currentRanges.length === 0) {
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return currentRanges;
|
|
164
|
+
}
|
|
165
|
+
}
|
package/src/stopwatch.ts
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import TimeSpan from "./timespan.js";
|
|
2
|
-
|
|
3
|
-
export default class Stopwatch {
|
|
4
|
-
private startTime: number | null = null;
|
|
5
|
-
private elapsedTime: number | null = null;
|
|
6
|
-
|
|
7
|
-
/*static measure<T>(callback: () => T, measure: { value: TimeSpan | null }): T {
|
|
8
|
-
const stopwatch = new Stopwatch();
|
|
9
|
-
stopwatch.start();
|
|
10
|
-
try {
|
|
11
|
-
return callback();
|
|
12
|
-
} finally {
|
|
13
|
-
stopwatch.stop();
|
|
14
|
-
measure.value = stopwatch.getElapsedTime();
|
|
15
|
-
}
|
|
16
|
-
}*/
|
|
17
|
-
|
|
18
|
-
start(): void {
|
|
19
|
-
this.startTime = performance.now();
|
|
20
|
-
this.elapsedTime = null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
stop(): void {
|
|
24
|
-
if (this.startTime !== null) {
|
|
25
|
-
this.elapsedTime = performance.now() - this.startTime;
|
|
26
|
-
this.startTime = null;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
resume(): void {
|
|
31
|
-
if (this.elapsedTime !== null) {
|
|
32
|
-
this.startTime = performance.now() - this.elapsedTime;
|
|
33
|
-
this.elapsedTime = null;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
getElapsedTime(): TimeSpan {
|
|
38
|
-
if (this.startTime !== null) {
|
|
39
|
-
return TimeSpan.fromMilliseconds(performance.now() - this.startTime);
|
|
40
|
-
}
|
|
41
|
-
return TimeSpan.fromMilliseconds(this.elapsedTime ?? 0.0);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
1
|
+
import TimeSpan from "./timespan.js";
|
|
2
|
+
|
|
3
|
+
export default class Stopwatch {
|
|
4
|
+
private startTime: number | null = null;
|
|
5
|
+
private elapsedTime: number | null = null;
|
|
6
|
+
|
|
7
|
+
/*static measure<T>(callback: () => T, measure: { value: TimeSpan | null }): T {
|
|
8
|
+
const stopwatch = new Stopwatch();
|
|
9
|
+
stopwatch.start();
|
|
10
|
+
try {
|
|
11
|
+
return callback();
|
|
12
|
+
} finally {
|
|
13
|
+
stopwatch.stop();
|
|
14
|
+
measure.value = stopwatch.getElapsedTime();
|
|
15
|
+
}
|
|
16
|
+
}*/
|
|
17
|
+
|
|
18
|
+
start(): void {
|
|
19
|
+
this.startTime = performance.now();
|
|
20
|
+
this.elapsedTime = null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
stop(): void {
|
|
24
|
+
if (this.startTime !== null) {
|
|
25
|
+
this.elapsedTime = performance.now() - this.startTime;
|
|
26
|
+
this.startTime = null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
resume(): void {
|
|
31
|
+
if (this.elapsedTime !== null) {
|
|
32
|
+
this.startTime = performance.now() - this.elapsedTime;
|
|
33
|
+
this.elapsedTime = null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getElapsedTime(): TimeSpan {
|
|
38
|
+
if (this.startTime !== null) {
|
|
39
|
+
return TimeSpan.fromMilliseconds(performance.now() - this.startTime);
|
|
40
|
+
}
|
|
41
|
+
return TimeSpan.fromMilliseconds(this.elapsedTime ?? 0.0);
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/string.ts
CHANGED
|
@@ -36,7 +36,8 @@ export function ensureSuffix(str: string, suffix: string): string {
|
|
|
36
36
|
export function format(template: string, ...args: any[]): string {
|
|
37
37
|
|
|
38
38
|
let regex: RegExp;
|
|
39
|
-
|
|
39
|
+
const hasNamedPlaceholders = /{(?!\d+})[^{}]+}/.test(template);
|
|
40
|
+
if (args.length === 1 && args[0] !== null && typeof args[0] === 'object' && hasNamedPlaceholders) {
|
|
40
41
|
args = args[0];
|
|
41
42
|
regex = /{(.+?)}/g;
|
|
42
43
|
} else {
|
|
@@ -44,7 +45,9 @@ export function format(template: string, ...args: any[]): string {
|
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
return template.replace(regex, (match, key) => {
|
|
47
|
-
return typeof args[key] !== 'undefined'
|
|
48
|
+
return Object.prototype.hasOwnProperty.call(args, key) && typeof args[key] !== 'undefined'
|
|
49
|
+
? args[key]
|
|
50
|
+
: "";
|
|
48
51
|
});
|
|
49
52
|
}
|
|
50
53
|
|
|
@@ -61,11 +64,11 @@ function toWords(str: string): string[] {
|
|
|
61
64
|
|
|
62
65
|
return str
|
|
63
66
|
// Insert a space between a lowercase/digit and an uppercase letter (camelCase -> camel Case)
|
|
64
|
-
.replace(/([
|
|
67
|
+
.replace(/([\p{Ll}\p{N}])(\p{Lu})/gu, '$1 $2')
|
|
65
68
|
// Insert a space between consecutive uppercase letters followed by a lowercase letter (XMLParser -> XML Parser)
|
|
66
|
-
.replace(/(
|
|
69
|
+
.replace(/(\p{Lu}+)(\p{Lu}\p{Ll})/gu, '$1 $2')
|
|
67
70
|
// Replace any non-alphanumeric character with a space
|
|
68
|
-
.replace(/[
|
|
71
|
+
.replace(/[^\p{L}\p{N}]+/gu, ' ')
|
|
69
72
|
.trim()
|
|
70
73
|
.toLowerCase()
|
|
71
74
|
.split(/\s+/)
|
package/src/timer.ts
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A class representing a timer that executes a callback function at a specified interval.
|
|
3
|
-
*/
|
|
4
|
-
export default class Timer {
|
|
5
|
-
|
|
6
|
-
private _callback: () => void;
|
|
7
|
-
private _interval: number;
|
|
8
|
-
private _intervalId: any | null;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @param callback Callback
|
|
12
|
-
* @param interval
|
|
13
|
-
*/
|
|
14
|
-
constructor(callback: () => void, interval: number) {
|
|
15
|
-
this._callback = callback;
|
|
16
|
-
this._interval = interval;
|
|
17
|
-
this._intervalId = null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
get running(): boolean {
|
|
22
|
-
return this._intervalId !== null && this._intervalId !== undefined;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
get interval(): number {
|
|
26
|
-
return this._interval;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
set interval(value: number) {
|
|
30
|
-
if (value != this._interval) {
|
|
31
|
-
this._interval = value;
|
|
32
|
-
if (this.running)
|
|
33
|
-
this.restart();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get callback(): () => void {
|
|
38
|
-
return this._callback;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
set callback(value: () => void) {
|
|
42
|
-
if (value != this._callback) {
|
|
43
|
-
this._callback = value;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
start(): void {
|
|
49
|
-
if (this._intervalId === null) {
|
|
50
|
-
this._intervalId = setInterval(() => {
|
|
51
|
-
if (this._callback != null)
|
|
52
|
-
this._callback();
|
|
53
|
-
}, this._interval);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
stop(): void {
|
|
58
|
-
if (this._intervalId !== null) {
|
|
59
|
-
clearInterval(this._intervalId);
|
|
60
|
-
this._intervalId = null;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
restart(): void {
|
|
65
|
-
this.stop();
|
|
66
|
-
this.start();
|
|
67
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* A class representing a timer that executes a callback function at a specified interval.
|
|
3
|
+
*/
|
|
4
|
+
export default class Timer {
|
|
5
|
+
|
|
6
|
+
private _callback: () => void;
|
|
7
|
+
private _interval: number;
|
|
8
|
+
private _intervalId: any | null;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param callback Callback
|
|
12
|
+
* @param interval Milliseconds between calls
|
|
13
|
+
*/
|
|
14
|
+
constructor(callback: () => void, interval: number) {
|
|
15
|
+
this._callback = callback;
|
|
16
|
+
this._interval = interval;
|
|
17
|
+
this._intervalId = null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
get running(): boolean {
|
|
22
|
+
return this._intervalId !== null && this._intervalId !== undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get interval(): number {
|
|
26
|
+
return this._interval;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
set interval(value: number) {
|
|
30
|
+
if (value != this._interval) {
|
|
31
|
+
this._interval = value;
|
|
32
|
+
if (this.running)
|
|
33
|
+
this.restart();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get callback(): () => void {
|
|
38
|
+
return this._callback;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
set callback(value: () => void) {
|
|
42
|
+
if (value != this._callback) {
|
|
43
|
+
this._callback = value;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
start(): void {
|
|
49
|
+
if (this._intervalId === null) {
|
|
50
|
+
this._intervalId = setInterval(() => {
|
|
51
|
+
if (this._callback != null)
|
|
52
|
+
this._callback();
|
|
53
|
+
}, this._interval);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
stop(): void {
|
|
58
|
+
if (this._intervalId !== null) {
|
|
59
|
+
clearInterval(this._intervalId);
|
|
60
|
+
this._intervalId = null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
restart(): void {
|
|
65
|
+
this.stop();
|
|
66
|
+
this.start();
|
|
67
|
+
}
|
|
68
68
|
}
|