@noeldemartin/utils 0.5.1 → 0.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noeldemartin/utils",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "My JavaScript utilities",
5
5
  "main": "dist/noeldemartin-utils.cjs.js",
6
6
  "module": "dist/noeldemartin-utils.esm.js",
@@ -10,7 +10,7 @@
10
10
  "scripts": {
11
11
  "build": "rm -rf dist && npm run build:js && npm run build:types",
12
12
  "build:js": "noeldemartin-build-javascript",
13
- "build:types": "noeldemartin-build-types && ./scripts/fix-error-types.sh",
13
+ "build:types": "noeldemartin-build-types && ./scripts/fix-types.sh",
14
14
  "lint": "noeldemartin-lint src",
15
15
  "publish-next": "noeldemartin-publish-next",
16
16
  "test": "jest --verbose",
@@ -4,6 +4,7 @@ import {
4
4
  arrayFilter,
5
5
  arrayFirst,
6
6
  arrayFlatMap,
7
+ arrayGroupBy,
7
8
  arrayIsEmpty,
8
9
  arrayProject,
9
10
  arrayRandomItem,
@@ -31,6 +32,7 @@ const fluentArrayHelpers: FluentArrayHelpers<unknown> = {
31
32
  filter: arrayFilter,
32
33
  first: arrayFirst,
33
34
  flatMap: arrayFlatMap,
35
+ groupBy: arrayGroupBy,
34
36
  isEmpty: arrayIsEmpty,
35
37
  project: arrayProject,
36
38
  randomItem: arrayRandomItem,
@@ -50,7 +52,9 @@ export type FluentArrayHelpers<T> = {
50
52
  diff(items: T[], updated: T[]): { added: T[]; removed: T[] };
51
53
  first(items: T[], filter: (item: T) => boolean): T | null;
52
54
  filter(items: T[], filter?: (item: T) => boolean): T[] | Exclude<T, Falsy>[];
53
- flatMap<R>(items: T[], transformation?: (item: T, index: number) => R[]): R[];
55
+ flatMap<TMapped>(items: T[], map?: (item: T, index: number) => TMapped[]): TMapped[];
56
+ groupBy<TKey extends string>(items: T[], groupBy: (item: T) => TKey): Partial<Record<TKey, T[]>>;
57
+ groupBy<TKey extends keyof T>(items: T[], groupBy: TKey): Record<string, T[]>;
54
58
  isEmpty(items: T[]): boolean;
55
59
  project(items: T[], property: string): unknown[];
56
60
  randomItem(items: T[]): T | null;
@@ -1,53 +1,69 @@
1
- import { fail } from '@/helpers/error_helpers';
2
1
  import { getClassMethods } from '@/helpers/object_helpers';
3
2
  import { mock } from '@/testing/mocking';
4
- import type { ClassInstance } from '@/types/helpers';
5
3
  import type { Constructor } from '@/types/classes';
6
- import type { Obj } from '@/helpers/object_helpers';
7
-
8
- export type FacadeMethods<TInstance, TMockClass extends Constructor> = {
9
- instance: TInstance | null;
10
- mockClass: TMockClass | null;
11
- mockInstance: ClassInstance<TMockClass> | null;
12
- setInstance(instance: TInstance): void;
13
- setMockInstance: (mockClass: ClassInstance<TMockClass>) => void;
14
- setMockClass: (mockClass: TMockClass) => void;
4
+
5
+ export type FacadeMethods<TInstance, TMock extends TInstance> = {
6
+ setInstance(instance: TInstance): TInstance;
7
+ setMockClass(mockClass: Constructor<TMock>): void;
8
+ setMockFactory(mockFactory: () => TMock): void;
9
+ setMockFacade(mockFacade: Facade<TMock>): void;
15
10
  requireInstance(): TInstance;
16
- mock: <T extends ClassInstance<TMockClass>>(mockInstance?: T | Obj | object) => T;
11
+ mock(partialInstance?: Partial<TInstance>): TMock;
12
+ reset(): TInstance;
17
13
  };
18
14
 
19
- export type Facade<TInstance, TMockClass extends Constructor> = TInstance & FacadeMethods<TInstance, TMockClass>;
20
-
21
- export function facade<TInstance extends object>(
22
- instance?: TInstance | null
23
- ): Facade<TInstance, Constructor<TInstance>>;
24
- export function facade<TInstance extends object, TMockClass extends Constructor>(
25
- instance: TInstance | null,
26
- mockClass: TMockClass | null,
27
- ): Facade<TInstance, TMockClass>;
28
- export function facade<TInstance extends object, TMockClass extends Constructor>(
29
- instance: TInstance | null = null,
30
- mockClass: TMockClass | null = null,
31
- ): Facade<TInstance, TMockClass> {
32
- const facade: FacadeMethods<TInstance, TMockClass> = {
33
- instance,
34
- mockClass,
35
- mockInstance: null,
36
- setInstance: instance => facade.instance = instance,
37
- setMockInstance: mockInstance => facade.mockInstance = mockInstance,
38
- setMockClass: mockClass => facade.mockClass = mockClass,
39
- requireInstance: () => facade.instance ?? fail('Facade not initialized'),
40
- mock<T extends ClassInstance<TMockClass>>(mockInstance?: T | Obj | object) {
41
- const instance = mock(
42
- mockInstance ??
43
- getFacadeInstance(facade.mockInstance) ??
44
- (facade.mockClass ? new facade.mockClass() : getClassMethods(facade.instance as object)),
45
- );
46
-
47
- facade.instance = instance as TInstance;
48
-
49
- return instance as T;
50
- },
15
+ export type Facade<TInstance, TMock extends TInstance = TInstance> = TInstance & FacadeMethods<TInstance, TMock>;
16
+
17
+ export function facade<TInstance extends object, TMock extends TInstance>(
18
+ defaultClassOrFactory: (() => TInstance) | Constructor<TInstance>,
19
+ ): Facade<TInstance, TMock> {
20
+ let instance: TInstance | null = null;
21
+ let mockClass: Constructor<TMock> | null = null;
22
+ let mockFactory: (() => TMock) | null = null;
23
+ let mockFacade: Facade<TMock> | null = null;
24
+
25
+ function newDefaultInstance(): TInstance {
26
+ try {
27
+ return (defaultClassOrFactory as (() => TInstance))();
28
+ } catch (error) {
29
+ return new (defaultClassOrFactory as Constructor<TInstance>)();
30
+ }
31
+ }
32
+
33
+ function newMockInstance(partialInstance?: Partial<TInstance>): TMock {
34
+ if (partialInstance) {
35
+ return mock<TMock>(partialInstance);
36
+ }
37
+
38
+ if (mockFacade) {
39
+ return mock(mockFacade.reset());
40
+ }
41
+
42
+ if (mockClass) {
43
+ return mock(new mockClass());
44
+ }
45
+
46
+ if (mockFactory) {
47
+ return mockFactory();
48
+ }
49
+
50
+ return mock<TMock>(getClassMethods(facade.requireInstance()));
51
+ }
52
+
53
+ function setInstance(newInstance: TInstance): TInstance {
54
+ instance = newInstance;
55
+
56
+ return newInstance;
57
+ }
58
+
59
+ const facade: FacadeMethods<TInstance, TMock> = {
60
+ setInstance,
61
+ setMockClass: newMockClass => mockClass = newMockClass,
62
+ setMockFactory: newMockFactory => mockFactory = newMockFactory,
63
+ setMockFacade: newMockFacade => mockFacade = newMockFacade,
64
+ requireInstance: () => instance ?? setInstance(newDefaultInstance()),
65
+ mock: (partialInstance) => setInstance(newMockInstance(partialInstance)) as TMock,
66
+ reset: () => setInstance(newDefaultInstance()),
51
67
  };
52
68
 
53
69
  return new Proxy(facade, {
@@ -57,7 +73,7 @@ export function facade<TInstance extends object, TMockClass extends Constructor>
57
73
  return Reflect.get(t, property, receiver);
58
74
  },
59
75
  set: (_, property, receiver) => Reflect.set(facade.requireInstance(), property, receiver),
60
- }) as unknown as Facade<TInstance, TMockClass>;
76
+ }) as unknown as Facade<TInstance, TMock>;
61
77
  }
62
78
 
63
79
  export function isFacade(facade: unknown): facade is Facade<unknown, Constructor<object>> {
@@ -66,7 +82,7 @@ export function isFacade(facade: unknown): facade is Facade<unknown, Constructor
66
82
  && 'requireInstance' in facade;
67
83
  }
68
84
 
69
- export function getFacadeInstance<T>(facade: Facade<T, Constructor<object>>): T;
85
+ export function getFacadeInstance<T>(facade: Facade<T>): T;
70
86
  export function getFacadeInstance<T>(facade: T): T;
71
87
  export function getFacadeInstance<T>(facade: T): T {
72
88
  return isFacade(facade) ? facade.requireInstance() as T : facade;
@@ -61,6 +61,7 @@ export { tap } from './tap';
61
61
  export type { Tapped } from './tap';
62
62
 
63
63
  export { noop } from './noop';
64
+ export { required } from './required';
64
65
  export { value } from './value';
65
66
  export { when } from './when';
66
67
 
@@ -0,0 +1,46 @@
1
+ import { fail } from '@/helpers/error_helpers';
2
+
3
+ class RequiredHandler<Target extends object> implements ProxyHandler<Target> {
4
+
5
+ constructor(private errorMessage: string, private getValue: () => Target) {}
6
+
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ public get(_: Target, property: string | symbol, receiver: any): any {
9
+ const obj = this.getValue() ?? fail<Target>(this.errorMessage);
10
+ const value = Reflect.get(obj, property, receiver);
11
+
12
+ return typeof value === 'function' ? value.bind(obj) : value;
13
+ }
14
+
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ public set?(_: Target, property: string | symbol, value: any, receiver: any): boolean {
17
+ const obj = this.getValue() ?? fail<Target>(this.errorMessage);
18
+
19
+ return Reflect.set(obj, property, value, receiver);
20
+ }
21
+
22
+ public getPrototypeOf(): object | null {
23
+ const obj = this.getValue() ?? fail<Target>(this.errorMessage);
24
+
25
+ return Object.getPrototypeOf(obj);
26
+ }
27
+
28
+ }
29
+
30
+ export function required<T extends object | null | undefined>(
31
+ getValue: () => T,
32
+ errorMessage?: string
33
+ ): NonNullable<T>;
34
+ export function required<T>(value: T, errorMessage?: string): NonNullable<T>;
35
+ export function required<T>(value: (() => T) | T, errorMessage?: string): NonNullable<T> {
36
+ errorMessage ??= 'Required value is missing';
37
+
38
+ if (typeof value === 'function') {
39
+ return new Proxy(
40
+ {},
41
+ new RequiredHandler(errorMessage, value as unknown as () => object),
42
+ ) as NonNullable<T>;
43
+ }
44
+
45
+ return (value ?? fail(errorMessage)) as NonNullable<T>;
46
+ }
@@ -1,3 +1,5 @@
1
+ import { fail } from '@/helpers/error_helpers';
2
+
1
3
  import { toString } from './object_helpers';
2
4
 
3
5
  export type ObjectKeyExtractor<T> = (item: T) => string;
@@ -64,7 +66,7 @@ export default class ObjectsMap<Item extends object> {
64
66
  }
65
67
 
66
68
  public require(key: string): Item {
67
- return this.keysToItems.get(key) || fail(`Couldn't find item with '${key}' key`);
69
+ return this.keysToItems.get(key) ?? fail(`Couldn't find item with '${key}' key`);
68
70
  }
69
71
 
70
72
  public get(key: string): Item | undefined {
@@ -116,4 +118,18 @@ export default class ObjectsMap<Item extends object> {
116
118
  this.keysToItems.clear();
117
119
  }
118
120
 
121
+ public clone<T extends ObjectsMap<Item>>(this: T): T {
122
+ const ThisConstructor = this.constructor as { new (keyExtractor: (item: Item) => string): T };
123
+ const copy = new ThisConstructor(this.getKey);
124
+
125
+ copy.keysToItems = new Map(this.keysToItems);
126
+ copy.itemsToKeys = new WeakMap();
127
+
128
+ for (const [key, item] of copy.keysToItems.entries()) {
129
+ copy.itemsToKeys.set(item, key);
130
+ }
131
+
132
+ return copy;
133
+ }
134
+
119
135
  }
@@ -10,10 +10,14 @@ export default class Semaphore {
10
10
  this.listeners = new Set();
11
11
  }
12
12
 
13
- public async acquire(): Promise<void> {
14
- await this.waitAvailable();
13
+ public acquire(): Promise<void> {
14
+ if (this.count === 0) {
15
+ return this.waitAvailable().then(() => this.acquire());
16
+ }
15
17
 
16
18
  this.count = Math.max(this.count - 1, 0);
19
+
20
+ return Promise.resolve();
17
21
  }
18
22
 
19
23
  public release(): void {
@@ -22,17 +26,24 @@ export default class Semaphore {
22
26
  this.notifyAvailable();
23
27
  }
24
28
 
29
+ public isAvailable(): boolean {
30
+ return this.count !== 0;
31
+ }
32
+
25
33
  public async run<T>(operation: () => Promise<T>): Promise<T> {
26
34
  await this.acquire();
27
35
 
28
- const result = await operation();
29
- this.release();
36
+ try {
37
+ const result = await operation();
30
38
 
31
- return result;
39
+ return result;
40
+ } finally {
41
+ this.release();
42
+ }
32
43
  }
33
44
 
34
45
  private async waitAvailable(): Promise<void> {
35
- while (this.count === 0) {
46
+ while (!this.isAvailable()) {
36
47
  await new Promise(resolve => {
37
48
  const listener = () => {
38
49
  this.listeners.delete(listener);
@@ -1,7 +1,9 @@
1
1
  import type { Closure, Falsy } from '@/types/helpers';
2
2
 
3
3
  import { compare } from './logical_helpers';
4
- import { isIterable, isString } from './object_helpers';
4
+ import { isIterable, isString, toString } from './object_helpers';
5
+
6
+ export type ArrayFrom<T> = T extends Iterable<infer TItem> ? TItem[] : T[];
5
7
 
6
8
  export function arrayClear(items: unknown[]): void {
7
9
  items.splice(0, items.length);
@@ -66,8 +68,8 @@ export function arrayFirst<T>(items: T[], filter: (item: T) => boolean): T | nul
66
68
  return null;
67
69
  }
68
70
 
69
- export function arrayFlatMap<T, R>(items: T[], transformation: (item: T, index: number) => R[]): R[] {
70
- return [...items.entries()].flatMap(([index, item]) => transformation(item, index));
71
+ export function arrayFlatMap<T, R>(items: T[], map: (item: T, index: number) => R[]): R[] {
72
+ return [...items.entries()].flatMap(([index, item]) => map(item, index));
71
73
  }
72
74
 
73
75
  export function arrayWithItemAt<T>(items: T[], item: T, index: number): T[] {
@@ -78,6 +80,29 @@ export function arrayWithItemAt<T>(items: T[], item: T, index: number): T[] {
78
80
  ];
79
81
  }
80
82
 
83
+ export function arrayGroupBy<TItem, TKey extends string>(
84
+ items: TItem[],
85
+ groupBy: (item: TItem) => TKey,
86
+ ): Partial<Record<TKey, TItem[]>>;
87
+ export function arrayGroupBy<TItem, TKey extends keyof TItem>(
88
+ items: TItem[],
89
+ groupBy: TKey,
90
+ ): Record<string, TItem[]>;
91
+ export function arrayGroupBy<TItem>(
92
+ items: TItem[],
93
+ groupBy: string | ((item: TItem) => string),
94
+ ): Partial<Record<string, TItem[]>> {
95
+ const group = typeof groupBy === 'string'
96
+ ? (item: TItem) => toString(item[groupBy as unknown as keyof TItem])
97
+ : groupBy;
98
+
99
+ return items.reduce((groups, item) => {
100
+ (groups[group(item)] ??= []).push(item);
101
+
102
+ return groups;
103
+ }, {} as Record<string, TItem[]>);
104
+ }
105
+
81
106
  export function arrayIsEmpty(items: unknown[]): boolean {
82
107
  return items.length === 0;
83
108
  }
@@ -230,6 +255,15 @@ export function arrayUnique<T>(items: T[], extractKey?: (item: T) => string): T[
230
255
  : [...new Set(items)];
231
256
  }
232
257
 
258
+ export function arrayFind<T, K extends keyof T>(items: T[], filter: string, value?: T[K]): T | undefined {
259
+ return items.find(item => {
260
+ const property = item[filter as keyof T];
261
+ const result = typeof property === 'function' ? property.call(item) : property;
262
+
263
+ return value ? result === value : !!result;
264
+ });
265
+ }
266
+
233
267
  export function arrayWhere<T, K extends keyof T>(items: T[], filter: string, value?: T[K]): T[] {
234
268
  return items.filter(item => {
235
269
  const property = item[filter as keyof T];
@@ -266,12 +300,12 @@ export function arrayZip<T>(...arrays: T[][]): T[][] {
266
300
  return zippedArrays;
267
301
  }
268
302
 
269
- export function arrayFrom<T>(value: Iterable<T>): T[];
270
- export function arrayFrom<T>(value: T, ignoreEmptyValues?: boolean): T[];
271
- export function arrayFrom(value: unknown, ignoreEmptyValues: boolean = false): unknown[] {
272
- return Array.isArray(value) || (isIterable(value) && !isString(value))
303
+ export function arrayFrom<T>(value: T, ignoreEmptyValues: boolean = false): ArrayFrom<T> {
304
+ const items = Array.isArray(value) || (isIterable(value) && !isString(value))
273
305
  ? Array.from(value)
274
306
  : ignoreEmptyValues && (value === null || value === undefined) ? [] : [value];
307
+
308
+ return items as ArrayFrom<T>;
275
309
  }
276
310
 
277
311
  export function range(length: number): number[] {
@@ -1,6 +1,8 @@
1
1
  import PromisedValue from '@/helpers/PromisedValue';
2
+ import { arrayFrom } from '@/helpers/array_helpers';
2
3
  import type { Closure, ClosureArgs } from '@/types/index';
3
4
 
5
+ const cache: Record<string, unknown> = {};
4
6
  const asyncCache: Record<string, PromisedValue> = {};
5
7
 
6
8
  export function asyncMemo<T>(key: string, operation: () => Promise<T>): Promise<T> {
@@ -9,6 +11,12 @@ export function asyncMemo<T>(key: string, operation: () => Promise<T>): Promise<
9
11
  return asyncCache[key] as Promise<T>;
10
12
  }
11
13
 
14
+ export function memo<T>(key: string, operation: () => T): T {
15
+ cache[key] = cache[key] ?? operation();
16
+
17
+ return cache[key] as T;
18
+ }
19
+
12
20
  export function once<P extends ClosureArgs, R>(operation: Closure<P, R>): Closure<P, R> {
13
21
  const memo: { result?: R } = {};
14
22
 
@@ -16,8 +24,24 @@ export function once<P extends ClosureArgs, R>(operation: Closure<P, R>): Closur
16
24
  return (...params) => 'result' in memo ? memo.result : (memo.result = operation(...params)) as any;
17
25
  }
18
26
 
27
+ export function resetAsyncMemo(key?: string): void {
28
+ const keys = arrayFrom(key ?? Object.keys(asyncCache));
29
+
30
+ keys.forEach(key => delete asyncCache[key]);
31
+ }
32
+
33
+ export function resetMemo(key?: string): void {
34
+ const keys = arrayFrom(key ?? Object.keys(cache));
35
+
36
+ keys.forEach(key => delete cache[key]);
37
+ }
38
+
19
39
  export function setAsyncMemo(key: string, value: unknown): void {
20
40
  asyncCache[key] = asyncCache[key] ?? new PromisedValue();
21
41
 
22
42
  asyncCache[key]?.resolve(value);
23
43
  }
44
+
45
+ export function setMemo(key: string, value: unknown): void {
46
+ cache[key] = value;
47
+ }
@@ -0,0 +1,46 @@
1
+ import type { Equals } from '@/testing/index';
2
+
3
+ /**
4
+ * @deprecated use NonNullable<T> instead.
5
+ */
6
+ export type ValueWithoutEmpty<T> = T extends null | undefined ? never : T;
7
+
8
+ /**
9
+ * @deprecated use ReplaceValues<T, null | undefined> instead.
10
+ */
11
+ export type ReplaceEmpty<T> = { [K in keyof T]: ValueWithoutEmpty<T[K]> };
12
+
13
+ /**
14
+ * @deprecated use GetRequiredKeysWithout<T, null | undefined> instead.
15
+ */
16
+ export type GetRequiredKeysWithoutEmpty<T, U extends Record<keyof T, unknown> = ReplaceEmpty<T>> = {
17
+ [K in keyof T]:
18
+ Record<string, never> extends Pick<T, K>
19
+ ? never
20
+ : (
21
+ U[K] extends never
22
+ ? never
23
+ : (Equals<T[K], U[K]> extends true ? K : never)
24
+ )
25
+ }[keyof T];
26
+
27
+ /**
28
+ * @deprecated use GetOptionalKeysWithout<T, null | undefined> instead.
29
+ */
30
+ export type GetOptionalKeysWithoutEmpty<T, U extends Record<keyof T, unknown> = ReplaceEmpty<T>> = {
31
+ [K in keyof T]:
32
+ Record<string, never> extends Pick<T, K>
33
+ ? K
34
+ : (
35
+ U[K] extends never
36
+ ? never
37
+ : (Equals<T[K], U[K]> extends true ? never : K)
38
+ )
39
+ }[keyof T];
40
+
41
+ /**
42
+ * @deprecated use ObjectWithout<T, null | undefined> instead.
43
+ */
44
+ export type ObjectWithoutEmpty<T> =
45
+ { [K in GetRequiredKeysWithoutEmpty<T>]: ValueWithoutEmpty<T[K]> } &
46
+ { [K in GetOptionalKeysWithoutEmpty<T>]?: ValueWithoutEmpty<T[K]> };
@@ -3,5 +3,5 @@ export function escapeHtmlEntities(text: string): string {
3
3
  }
4
4
 
5
5
  export function escapeRegexText(text: string): string {
6
- return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
6
+ return text.replace(/[[\]{}()*+?.\\^$|]/g, '\\$&');
7
7
  }
@@ -14,6 +14,7 @@ export * from './boolean_helpers';
14
14
  export * from './caching_helpers';
15
15
  export * from './chaos_helpers';
16
16
  export * from './date_helpers';
17
+ export * from './deprecated';
17
18
  export * from './error_helpers';
18
19
  export * from './escape_helpers';
19
20
  export * from './file_helpers';
@@ -22,6 +23,7 @@ export * from './hash_helpers';
22
23
  export * from './identity_helpers';
23
24
  export * from './location_helpers';
24
25
  export * from './logical_helpers';
26
+ export * from './math_helpers';
25
27
  export * from './mixin_helpers';
26
28
  export * from './network_helpers';
27
29
  export * from './object_helpers';
@@ -1,3 +1,11 @@
1
+ import { after } from '@/helpers/time_helpers';
2
+
3
+ async function waitUrlUpdated(url: string): Promise<void> {
4
+ while (location.href !== url) {
5
+ await after({ ms: 1 });
6
+ }
7
+ }
8
+
1
9
  export function getLocationQueryParameter(parameter: string): string | undefined {
2
10
  return getLocationQueryParameters()[parameter];
3
11
  }
@@ -17,7 +25,7 @@ export function hasLocationQueryParameter(parameter: string): boolean {
17
25
  return url.searchParams.has(parameter);
18
26
  }
19
27
 
20
- export function updateLocationQueryParameters(parameters: Record<string, string | undefined>): void {
28
+ export async function updateLocationQueryParameters(parameters: Record<string, string | undefined>): Promise<void> {
21
29
  const url = Object.entries(parameters).reduce(
22
30
  (url, [parameter, value]) => {
23
31
  value
@@ -30,4 +38,21 @@ export function updateLocationQueryParameters(parameters: Record<string, string
30
38
  );
31
39
 
32
40
  history.pushState(null, document.title, url.href);
41
+
42
+ await waitUrlUpdated(url.href);
43
+ }
44
+
45
+ export async function deleteLocationQueryParameters(parameters: string[]): Promise<void> {
46
+ const url = parameters.reduce(
47
+ (url, parameter) => {
48
+ url.searchParams.delete(parameter);
49
+
50
+ return url;
51
+ },
52
+ new URL(location.href),
53
+ );
54
+
55
+ history.pushState(null, document.title, url.href);
56
+
57
+ await waitUrlUpdated(url.href);
33
58
  }
@@ -1,3 +1,17 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ function normalizeCompareValues<T>(a: T, b: T): [any, any] {
3
+ if ((a === undefined || a === null) && (b === null || b === undefined)) {
4
+ return [
5
+ a === null ? 1 : 0,
6
+ b === null ? 1 : 0,
7
+ ];
8
+ }
9
+
10
+ return [a ?? null, b ?? null];
11
+ }
12
+
1
13
  export function compare<T>(a: T, b: T): 1 | -1 | 0 {
2
- return a > b ? 1 : (b > a ? -1 : 0);
14
+ const [aValue, bValue] = normalizeCompareValues(a, b);
15
+
16
+ return aValue > bValue ? 1 : (bValue > aValue ? -1 : 0);
3
17
  }
@@ -0,0 +1,3 @@
1
+ export function clamp(value: number, min: number, max: number): number {
2
+ return Math.max(Math.min(value, max), min);
3
+ }