magic-utils-yonava 1.0.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/magic-utils-1.0.0.tgz +0 -0
- package/package.json +17 -0
- package/src/clone.test.ts +47 -0
- package/src/clone.ts +15 -0
- package/src/colors.ts +567 -0
- package/src/ctx/index.ts +20 -0
- package/src/debounce.ts +15 -0
- package/src/debugging.ts +23 -0
- package/src/deepDelta/delta.test.ts +129 -0
- package/src/deepDelta/index.ts +48 -0
- package/src/deepMerge.test.ts +89 -0
- package/src/deepMerge.ts +37 -0
- package/src/fps.ts +64 -0
- package/src/fracDecConverter/index.ts +36 -0
- package/src/hashing.ts +9 -0
- package/src/id.ts +5 -0
- package/src/localStorage.ts +49 -0
- package/src/math.test.ts +57 -0
- package/src/math.ts +93 -0
- package/src/maybeGetter/index.ts +41 -0
- package/src/mouse.ts +8 -0
- package/src/random.ts +27 -0
- package/src/sets.ts +10 -0
- package/src/string.test.ts +17 -0
- package/src/string.ts +31 -0
- package/src/types.ts +79 -0
- package/tsconfig.json +19 -0
package/src/sets.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* combine an array of sets into a single set
|
|
3
|
+
*
|
|
4
|
+
* @param sets array of sets
|
|
5
|
+
* @returns a single set containing all elements from the input sets
|
|
6
|
+
* @example mergeSetArrayIntoSet([new Set([1, 2]), new Set([2, 3])]) // Set(1, 2, 3)
|
|
7
|
+
*/
|
|
8
|
+
export const mergeSetArrayIntoSet = <T>(sets: Set<T>[]) => {
|
|
9
|
+
return sets.reduce((acc, set) => new Set([...acc, ...set]), new Set<T>());
|
|
10
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { camelCaseToTitleCase, capitalize } from './string';
|
|
4
|
+
|
|
5
|
+
describe('capitalize', () => {
|
|
6
|
+
test('capitalizes the first letter of a string', () => {
|
|
7
|
+
expect(capitalize('hello')).toBe('Hello');
|
|
8
|
+
expect(capitalize('this is one string')).toBe('This is one string');
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('camelCaseToTitleCase', () => {
|
|
13
|
+
test('converts camelCase to title case', () => {
|
|
14
|
+
expect(camelCaseToTitleCase('camelCase')).toBe('Camel Case');
|
|
15
|
+
expect(camelCaseToTitleCase('thisIsOneString')).toBe('This Is One String');
|
|
16
|
+
});
|
|
17
|
+
});
|
package/src/string.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* makes the first letter of a string uppercase
|
|
3
|
+
*
|
|
4
|
+
* @param str - the string to capitalize
|
|
5
|
+
* @returns the capitalized string
|
|
6
|
+
* @example capitalize('hello') // 'Hello'
|
|
7
|
+
*/
|
|
8
|
+
export const capitalize = (str: string) =>
|
|
9
|
+
str.charAt(0).toUpperCase() + str.slice(1);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* turns a camelCase string into a title case string
|
|
13
|
+
*
|
|
14
|
+
* @param str - the string to convert
|
|
15
|
+
* @returns the title case string
|
|
16
|
+
* @example camelCaseToTitleCase('camelCase') // 'Camel Case'
|
|
17
|
+
*/
|
|
18
|
+
export const camelCaseToTitleCase = (str: string) => {
|
|
19
|
+
const result = str.replace(/([A-Z])/g, ' $1');
|
|
20
|
+
return capitalize(result);
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* turns an array of strings into a comma separated list with 'and' before the last item
|
|
24
|
+
* @example toCommaList(['a', 'b', 'c']) // 'a, b and c'
|
|
25
|
+
*/
|
|
26
|
+
export const getCommaList = (arr: string[]) => {
|
|
27
|
+
if (arr.length === 0) return '';
|
|
28
|
+
if (arr.length === 1) return arr[0];
|
|
29
|
+
const last = arr.pop();
|
|
30
|
+
return arr.join(', ') + ' and ' + last;
|
|
31
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Prettify, UnionToIntersection } from 'ts-essentials';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* makes only certain keys K in an object T optional
|
|
5
|
+
* @example
|
|
6
|
+
* type T = PartiallyPartial<{ a: number, b: string }, 'a'> // { a?: number, b: string }
|
|
7
|
+
*/
|
|
8
|
+
export type PartiallyPartial<T, K extends keyof T> = Prettify<
|
|
9
|
+
Omit<T, K> & Partial<Pick<T, K>>
|
|
10
|
+
>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* makes only certain keys K in an object T required
|
|
14
|
+
* @example
|
|
15
|
+
* type T = PartiallyRequired<{ a?: number, b?: string }, 'a'> // { a: number, b?: string }
|
|
16
|
+
*/
|
|
17
|
+
export type PartiallyRequired<T, K extends keyof T> = Prettify<
|
|
18
|
+
Omit<T, K> & Required<Pick<T, K>>
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* takes `any[]` out of a union of arrays
|
|
23
|
+
* @example RemoveAnyArray<number[] | any[]> // number[]
|
|
24
|
+
*/
|
|
25
|
+
export type RemoveAnyArray<T extends unknown[]> = Exclude<
|
|
26
|
+
T,
|
|
27
|
+
['!!!-@-NOT-A-TYPE-@-!!!'][]
|
|
28
|
+
>;
|
|
29
|
+
|
|
30
|
+
// HTML mouse and keyboard event types
|
|
31
|
+
|
|
32
|
+
type EventNames = keyof HTMLElementEventMap;
|
|
33
|
+
|
|
34
|
+
type FilterEventNames<T> = {
|
|
35
|
+
[K in EventNames]: HTMLElementEventMap[K] extends T ? K : never;
|
|
36
|
+
}[EventNames];
|
|
37
|
+
|
|
38
|
+
type MouseEventNames = FilterEventNames<MouseEvent>;
|
|
39
|
+
type KeyboardEventNames = FilterEventNames<KeyboardEvent>;
|
|
40
|
+
|
|
41
|
+
type EventMap<T extends EventNames, E> = Record<T, (ev: E) => void>;
|
|
42
|
+
|
|
43
|
+
export type MouseEventMap = EventMap<MouseEventNames, MouseEvent>;
|
|
44
|
+
export type KeyboardEventMap = EventMap<KeyboardEventNames, KeyboardEvent>;
|
|
45
|
+
|
|
46
|
+
export type MouseEventEntries = [
|
|
47
|
+
keyof MouseEventMap,
|
|
48
|
+
(ev: MouseEvent) => void,
|
|
49
|
+
][];
|
|
50
|
+
export type KeyboardEventEntries = [
|
|
51
|
+
keyof KeyboardEventMap,
|
|
52
|
+
(ev: KeyboardEvent) => void,
|
|
53
|
+
][];
|
|
54
|
+
|
|
55
|
+
export type TimeoutHandler = ReturnType<typeof setTimeout>;
|
|
56
|
+
export type IntervalHandler = ReturnType<typeof setInterval>;
|
|
57
|
+
|
|
58
|
+
type LastOf<U> =
|
|
59
|
+
UnionToIntersection<U extends any ? () => U : never> extends () => infer R
|
|
60
|
+
? R
|
|
61
|
+
: never;
|
|
62
|
+
|
|
63
|
+
type Push<T extends any[], V> = [...T, V];
|
|
64
|
+
|
|
65
|
+
type UnionToTuple<T, L = LastOf<T>> = [T] extends [never]
|
|
66
|
+
? []
|
|
67
|
+
: Push<UnionToTuple<Exclude<T, L>>, L>;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* turns the keys of an object into a tuple
|
|
71
|
+
*
|
|
72
|
+
* WARNING: order is not guaranteed! for deterministic ordering use
|
|
73
|
+
* readonly runtime array instead
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ObjectKeysToTuple<{ name: string, age: number, id: string }>
|
|
77
|
+
* // ["name", "age", "id"]
|
|
78
|
+
*/
|
|
79
|
+
export type ObjectKeysToTuple<T extends object> = UnionToTuple<keyof T>;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "./dist/types",
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
|
|
6
|
+
"target": "ES2022",
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
"lib": ["ES2023", "DOM"],
|
|
9
|
+
"types": ["node"],
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"allowJs": false,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
},
|
|
18
|
+
"include": ["src/**/*"],
|
|
19
|
+
}
|