magic-utils-yonava 1.0.4 → 1.0.6
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 +2 -1
- package/dist/clone.d.ts +0 -9
- package/dist/clone.js +0 -15
- package/dist/clone.test.d.ts +0 -1
- package/dist/clone.test.js +0 -42
- package/dist/colors.d.ts +0 -522
- package/dist/colors.js +0 -517
- package/dist/ctx/index.d.ts +0 -10
- package/dist/ctx/index.js +0 -19
- package/dist/debounce.d.ts +0 -9
- package/dist/debounce.js +0 -15
- package/dist/debugging.d.ts +0 -2
- package/dist/debugging.js +0 -21
- package/dist/deepDelta/delta.test.d.ts +0 -1
- package/dist/deepDelta/delta.test.js +0 -114
- package/dist/deepDelta/index.d.ts +0 -8
- package/dist/deepDelta/index.js +0 -40
- package/dist/deepMerge.d.ts +0 -16
- package/dist/deepMerge.js +0 -32
- package/dist/deepMerge.test.d.ts +0 -1
- package/dist/deepMerge.test.js +0 -68
- package/dist/fps.d.ts +0 -6
- package/dist/fps.js +0 -51
- package/dist/fracDecConverter/index.d.ts +0 -9
- package/dist/fracDecConverter/index.js +0 -34
- package/dist/hashing.d.ts +0 -2
- package/dist/hashing.js +0 -7
- package/dist/id.d.ts +0 -5
- package/dist/id.js +0 -5
- package/dist/localStorage.d.ts +0 -37
- package/dist/localStorage.js +0 -22
- package/dist/math.d.ts +0 -60
- package/dist/math.js +0 -89
- package/dist/math.test.d.ts +0 -1
- package/dist/math.test.js +0 -42
- package/dist/maybeGetter/index.d.ts +0 -26
- package/dist/maybeGetter/index.js +0 -15
- package/dist/mouse.d.ts +0 -8
- package/dist/mouse.js +0 -8
- package/dist/random.d.ts +0 -19
- package/dist/random.js +0 -21
- package/dist/sets.d.ts +0 -8
- package/dist/sets.js +0 -10
- package/dist/string.d.ts +0 -21
- package/dist/string.js +0 -31
- package/dist/string.test.d.ts +0 -1
- package/dist/string.test.js +0 -14
- package/dist/types.d.ts +0 -54
- package/dist/types.js +0 -1
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* gets the delta between two objects
|
|
3
|
-
*
|
|
4
|
-
* @param oldObject
|
|
5
|
-
* @param newObject
|
|
6
|
-
* @returns an object with only the changes, the values are the new values
|
|
7
|
-
*/
|
|
8
|
-
export declare const delta: (oldObject: Record<any, any>, newObject: Record<any, any>) => Record<any, any> | null;
|
package/dist/deepDelta/index.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// recursively compare two objects and return the delta
|
|
2
|
-
// -----------------
|
|
3
|
-
const isObj = (obj) => Object.prototype.toString.call(obj) === '[object Object]';
|
|
4
|
-
/**
|
|
5
|
-
* gets the delta between two objects
|
|
6
|
-
*
|
|
7
|
-
* @param oldObject
|
|
8
|
-
* @param newObject
|
|
9
|
-
* @returns an object with only the changes, the values are the new values
|
|
10
|
-
*/
|
|
11
|
-
export const delta = (oldObject, newObject) => {
|
|
12
|
-
const output = {};
|
|
13
|
-
if (!oldObject)
|
|
14
|
-
return newObject;
|
|
15
|
-
if (!newObject)
|
|
16
|
-
return null;
|
|
17
|
-
const oldObjectKeys = Object.keys(oldObject);
|
|
18
|
-
const newObjectKeys = Object.keys(newObject);
|
|
19
|
-
for (const key of newObjectKeys) {
|
|
20
|
-
if (!oldObjectKeys.includes(key)) {
|
|
21
|
-
output[key] = newObject[key];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
for (const key of oldObjectKeys) {
|
|
25
|
-
if (isObj(oldObject[key])) {
|
|
26
|
-
const diffObj = delta(oldObject[key], newObject[key]);
|
|
27
|
-
if (diffObj)
|
|
28
|
-
output[key] = diffObj;
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
if (Array.isArray(oldObject[key])) {
|
|
32
|
-
if (JSON.stringify(oldObject[key]) !== JSON.stringify(newObject[key]))
|
|
33
|
-
output[key] = newObject[key];
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
else if (oldObject[key] !== newObject[key])
|
|
37
|
-
output[key] = newObject[key];
|
|
38
|
-
}
|
|
39
|
-
return Object.keys(output).length ? output : null;
|
|
40
|
-
};
|
package/dist/deepMerge.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export declare const isPlainObject: (obj: any) => obj is Record<string | number | symbol, unknown>;
|
|
2
|
-
/**
|
|
3
|
-
* Deeply merges multiple objects. Properties from later objects overwrite those from earlier ones.
|
|
4
|
-
* Non-object values (including arrays) are replaced, not merged.
|
|
5
|
-
*
|
|
6
|
-
* @param {...any[]} objects - Objects to merge. The rightmost object's properties take precedence.
|
|
7
|
-
* @returns {any} - A new deeply merged object.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const result = deepMerge(
|
|
11
|
-
* { a: 1, b: { c: 2 } },
|
|
12
|
-
* { b: { d: 3 }, e: 4 }
|
|
13
|
-
* );
|
|
14
|
-
* // result: { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
15
|
-
*/
|
|
16
|
-
export declare const deepMerge: (...objects: any[]) => any;
|
package/dist/deepMerge.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export const isPlainObject = (obj) => !!obj &&
|
|
2
|
-
typeof obj === 'object' &&
|
|
3
|
-
Object.getPrototypeOf(obj) === Object.prototype;
|
|
4
|
-
/**
|
|
5
|
-
* Deeply merges multiple objects. Properties from later objects overwrite those from earlier ones.
|
|
6
|
-
* Non-object values (including arrays) are replaced, not merged.
|
|
7
|
-
*
|
|
8
|
-
* @param {...any[]} objects - Objects to merge. The rightmost object's properties take precedence.
|
|
9
|
-
* @returns {any} - A new deeply merged object.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* const result = deepMerge(
|
|
13
|
-
* { a: 1, b: { c: 2 } },
|
|
14
|
-
* { b: { d: 3 }, e: 4 }
|
|
15
|
-
* );
|
|
16
|
-
* // result: { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
17
|
-
*/
|
|
18
|
-
export const deepMerge = (...objects) => objects.reduce((acc, obj) => {
|
|
19
|
-
if (obj === undefined || obj === null)
|
|
20
|
-
return acc;
|
|
21
|
-
if (!isPlainObject(obj))
|
|
22
|
-
return obj;
|
|
23
|
-
Object.keys(obj).forEach((key) => {
|
|
24
|
-
if (isPlainObject(obj[key]) && isPlainObject(acc[key])) {
|
|
25
|
-
acc[key] = deepMerge(acc[key], obj[key]);
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
acc[key] = obj[key];
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
return acc;
|
|
32
|
-
}, {});
|
package/dist/deepMerge.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/deepMerge.test.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { deepMerge } from './deepMerge';
|
|
3
|
-
describe('deepMerge', () => {
|
|
4
|
-
it('merges flat objects with rightmost priority', () => {
|
|
5
|
-
const result = deepMerge({ a: 1 }, { a: 2, b: 3 });
|
|
6
|
-
expect(result).toEqual({ a: 2, b: 3 });
|
|
7
|
-
});
|
|
8
|
-
it('merges nested objects deeply', () => {
|
|
9
|
-
const result = deepMerge({ a: { b: 1, c: 2 } }, { a: { b: 3, d: 4 } });
|
|
10
|
-
expect(result).toEqual({ a: { b: 3, c: 2, d: 4 } });
|
|
11
|
-
});
|
|
12
|
-
it('overwrites arrays instead of merging them', () => {
|
|
13
|
-
const result = deepMerge({ a: [1, 2], b: { c: [3, 4] } }, { a: [5], b: { c: [6] } });
|
|
14
|
-
expect(result).toEqual({ a: [5], b: { c: [6] } });
|
|
15
|
-
});
|
|
16
|
-
it('handles primitive overwrites correctly', () => {
|
|
17
|
-
const result = deepMerge({ a: { b: 1 } }, { a: 5 });
|
|
18
|
-
expect(result).toEqual({ a: 5 });
|
|
19
|
-
});
|
|
20
|
-
it('returns non-object when passed a primitive last', () => {
|
|
21
|
-
const result = deepMerge({ a: 1 }, 5);
|
|
22
|
-
expect(result).toBe(5);
|
|
23
|
-
});
|
|
24
|
-
it('handles merging with undefined and null safely', () => {
|
|
25
|
-
const result = deepMerge({ a: { b: 1 } }, undefined, null, { a: { c: 2 } });
|
|
26
|
-
expect(result).toEqual({ a: { b: 1, c: 2 } });
|
|
27
|
-
});
|
|
28
|
-
it('handles empty inputs', () => {
|
|
29
|
-
const result = deepMerge();
|
|
30
|
-
expect(result).toEqual({});
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
describe('deepMerge - special cases', () => {
|
|
34
|
-
it('overwrites Date objects', () => {
|
|
35
|
-
const date1 = new Date('2020-01-01');
|
|
36
|
-
const date2 = new Date('2021-01-01');
|
|
37
|
-
const result = deepMerge({ date: date1 }, { date: date2 });
|
|
38
|
-
expect(result.date).toBe(date2);
|
|
39
|
-
});
|
|
40
|
-
it('overwrites Map objects', () => {
|
|
41
|
-
const map1 = new Map([['a', 1]]);
|
|
42
|
-
const map2 = new Map([['b', 2]]);
|
|
43
|
-
const result = deepMerge({ map: map1 }, { map: map2 });
|
|
44
|
-
expect(result.map).toBe(map2);
|
|
45
|
-
});
|
|
46
|
-
it('overwrites Set objects', () => {
|
|
47
|
-
const set1 = new Set([1, 2]);
|
|
48
|
-
const set2 = new Set([3, 4]);
|
|
49
|
-
const result = deepMerge({ set: set1 }, { set: set2 });
|
|
50
|
-
expect(result.set).toBe(set2);
|
|
51
|
-
});
|
|
52
|
-
it('overwrites functions', () => {
|
|
53
|
-
const fn1 = () => 1;
|
|
54
|
-
const fn2 = () => 2;
|
|
55
|
-
const result = deepMerge({ fn: fn1 }, { fn: fn2 });
|
|
56
|
-
expect(result.fn).toBe(fn2);
|
|
57
|
-
expect(result.fn()).toBe(2);
|
|
58
|
-
});
|
|
59
|
-
it('overwrites class instances', () => {
|
|
60
|
-
class Example {
|
|
61
|
-
x = 1;
|
|
62
|
-
}
|
|
63
|
-
const obj1 = { instance: new Example() };
|
|
64
|
-
const obj2 = { instance: { y: 2 } };
|
|
65
|
-
const result = deepMerge(obj1, obj2);
|
|
66
|
-
expect(result.instance).toEqual({ y: 2 });
|
|
67
|
-
});
|
|
68
|
-
});
|
package/dist/fps.d.ts
DELETED
package/dist/fps.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { onMounted, onUnmounted, ref } from "vue";
|
|
2
|
-
export function useFPS() {
|
|
3
|
-
const fps = ref(0);
|
|
4
|
-
const frameTime = ref(0);
|
|
5
|
-
const slowFrameCount = ref(0);
|
|
6
|
-
const slowFrameRatio = ref(0);
|
|
7
|
-
let frameId;
|
|
8
|
-
let intervalId;
|
|
9
|
-
let lastMeasure = performance.now();
|
|
10
|
-
let lastFrame = performance.now();
|
|
11
|
-
let frames = 0;
|
|
12
|
-
let slowFrames = 0;
|
|
13
|
-
const SLOW_FRAME_THRESHOLD = 33.3; // ms
|
|
14
|
-
const update = () => {
|
|
15
|
-
const now = performance.now();
|
|
16
|
-
const delta = now - lastFrame;
|
|
17
|
-
frameTime.value = delta;
|
|
18
|
-
lastFrame = now;
|
|
19
|
-
if (delta > SLOW_FRAME_THRESHOLD) {
|
|
20
|
-
slowFrames++;
|
|
21
|
-
}
|
|
22
|
-
frames++;
|
|
23
|
-
frameId = requestAnimationFrame(update);
|
|
24
|
-
};
|
|
25
|
-
const measure = () => {
|
|
26
|
-
const now = performance.now();
|
|
27
|
-
const delta = now - lastMeasure;
|
|
28
|
-
fps.value = Math.round((frames * 1000) / delta);
|
|
29
|
-
slowFrameCount.value = slowFrames;
|
|
30
|
-
slowFrameRatio.value = frames > 0 ? slowFrames / frames : 0;
|
|
31
|
-
frames = 0;
|
|
32
|
-
slowFrames = 0;
|
|
33
|
-
lastMeasure = now;
|
|
34
|
-
};
|
|
35
|
-
onMounted(() => {
|
|
36
|
-
lastFrame = performance.now();
|
|
37
|
-
lastMeasure = performance.now();
|
|
38
|
-
frameId = requestAnimationFrame(update);
|
|
39
|
-
intervalId = setInterval(measure, 500);
|
|
40
|
-
});
|
|
41
|
-
onUnmounted(() => {
|
|
42
|
-
cancelAnimationFrame(frameId);
|
|
43
|
-
clearInterval(intervalId);
|
|
44
|
-
});
|
|
45
|
-
return {
|
|
46
|
-
fps,
|
|
47
|
-
frameTime,
|
|
48
|
-
slowFrameCount,
|
|
49
|
-
slowFrameRatio,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export declare const gcd: (a: number, b: number) => number;
|
|
2
|
-
export declare const isNullOrUnd: (input: any) => boolean;
|
|
3
|
-
export declare const isFraction: (input: string) => boolean;
|
|
4
|
-
export declare const decimalToFraction: (decimalInput: number) => string;
|
|
5
|
-
/**
|
|
6
|
-
* @param fractionInput a string representing a fraction
|
|
7
|
-
* @returns the decimal representation of the fraction or undefined if the input is not a fraction
|
|
8
|
-
*/
|
|
9
|
-
export declare const fractionToDecimal: (fractionInput: string) => number | undefined;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export const gcd = (a, b) => (b ? gcd(b, a % b) : a);
|
|
2
|
-
export const isNullOrUnd = (input) => input === null || input === undefined;
|
|
3
|
-
export const isFraction = (input) => {
|
|
4
|
-
const fraction = input.trim().split('/').filter(Boolean);
|
|
5
|
-
if (fraction.length !== 2)
|
|
6
|
-
return false;
|
|
7
|
-
const [numerator, denominator] = fraction.map(Number);
|
|
8
|
-
if (isNullOrUnd(numerator) || isNullOrUnd(denominator))
|
|
9
|
-
return false;
|
|
10
|
-
return true;
|
|
11
|
-
};
|
|
12
|
-
export const decimalToFraction = (decimalInput) => {
|
|
13
|
-
const decimal = Math.round(decimalInput * 1e10) / 1e10;
|
|
14
|
-
const len = decimal.toString().length - 2;
|
|
15
|
-
let denominator = 10 ** len;
|
|
16
|
-
let numerator = decimal * denominator;
|
|
17
|
-
const divisor = gcd(numerator, denominator);
|
|
18
|
-
numerator /= divisor;
|
|
19
|
-
denominator /= divisor;
|
|
20
|
-
if (denominator === 1)
|
|
21
|
-
return numerator.toString();
|
|
22
|
-
return `${numerator}/${denominator}`;
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* @param fractionInput a string representing a fraction
|
|
26
|
-
* @returns the decimal representation of the fraction or undefined if the input is not a fraction
|
|
27
|
-
*/
|
|
28
|
-
export const fractionToDecimal = (fractionInput) => {
|
|
29
|
-
if (!isFraction(fractionInput))
|
|
30
|
-
return;
|
|
31
|
-
const fraction = fractionInput.split('/');
|
|
32
|
-
const [numerator, denominator] = fraction.map(Number);
|
|
33
|
-
return numerator / denominator;
|
|
34
|
-
};
|
package/dist/hashing.d.ts
DELETED
package/dist/hashing.js
DELETED
package/dist/id.d.ts
DELETED
package/dist/id.js
DELETED
package/dist/localStorage.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* a registry for all localStorage keys this application uses
|
|
3
|
-
*/
|
|
4
|
-
export declare const localKeys: {
|
|
5
|
-
/** nodes in graph product */
|
|
6
|
-
readonly nodes: (key: string) => `nodes-${string}`;
|
|
7
|
-
/** edges in graph product */
|
|
8
|
-
readonly edges: (key: string) => `edges-${string}`;
|
|
9
|
-
/** graph product simulation speed */
|
|
10
|
-
readonly simulationPlaybackSpeed: "simulation-playback-speed";
|
|
11
|
-
/** graph theme set by user - {@link Graph.preferredTheme} */
|
|
12
|
-
readonly preferredTheme: "preferred-theme";
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* all return values of localStorage are, by default, string.
|
|
16
|
-
* this type allows string to be narrowed to types such as 'true' | 'false'
|
|
17
|
-
*/
|
|
18
|
-
type TypeOverride = {};
|
|
19
|
-
type LocalObj = typeof localKeys;
|
|
20
|
-
/**
|
|
21
|
-
* @example
|
|
22
|
-
* type T = TypeOrReturnType<number> // number
|
|
23
|
-
* type TFunc = TypeOrReturnType<() => number> // number
|
|
24
|
-
*/
|
|
25
|
-
type TypeOrReturnType<T> = T extends (...args: any[]) => infer U ? U : T;
|
|
26
|
-
type LocalKeys = TypeOrReturnType<LocalObj[keyof LocalObj]>;
|
|
27
|
-
type LocalType<T extends LocalKeys> = T extends keyof TypeOverride ? TypeOverride[T] : string;
|
|
28
|
-
/**
|
|
29
|
-
* perform **type safe** localStorage actions
|
|
30
|
-
*/
|
|
31
|
-
export declare const local: {
|
|
32
|
-
get: <T extends LocalKeys>(key: T) => string | null;
|
|
33
|
-
set: <T extends LocalKeys, K extends LocalType<T>>(key: T, value: K) => void;
|
|
34
|
-
remove: <T extends LocalKeys>(key: T) => void;
|
|
35
|
-
clear: () => void;
|
|
36
|
-
};
|
|
37
|
-
export {};
|
package/dist/localStorage.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* a registry for all localStorage keys this application uses
|
|
3
|
-
*/
|
|
4
|
-
export const localKeys = {
|
|
5
|
-
/** nodes in graph product */
|
|
6
|
-
nodes: (key) => `nodes-${key}`,
|
|
7
|
-
/** edges in graph product */
|
|
8
|
-
edges: (key) => `edges-${key}`,
|
|
9
|
-
/** graph product simulation speed */
|
|
10
|
-
simulationPlaybackSpeed: 'simulation-playback-speed',
|
|
11
|
-
/** graph theme set by user - {@link Graph.preferredTheme} */
|
|
12
|
-
preferredTheme: 'preferred-theme',
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* perform **type safe** localStorage actions
|
|
16
|
-
*/
|
|
17
|
-
export const local = {
|
|
18
|
-
get: (key) => localStorage.getItem(key),
|
|
19
|
-
set: (key, value) => localStorage.setItem(key, value),
|
|
20
|
-
remove: (key) => localStorage.removeItem(key),
|
|
21
|
-
clear: localStorage.clear,
|
|
22
|
-
};
|
package/dist/math.d.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* the golden ratio constant.
|
|
3
|
-
* {@link} https://en.wikipedia.org/wiki/Golden_ratio
|
|
4
|
-
*/
|
|
5
|
-
export declare const GOLDEN_RATIO = 1.618;
|
|
6
|
-
/**
|
|
7
|
-
* rounds a number to the nearest multiple of another number
|
|
8
|
-
*
|
|
9
|
-
* @param n the number to round
|
|
10
|
-
* @param nearest the number to round to
|
|
11
|
-
* @returns the rounded number
|
|
12
|
-
* @example const roundToNearest5 = roundToNearestN(5);
|
|
13
|
-
* roundToNearest5(13) // 15
|
|
14
|
-
* roundToNearest5(12) // 10
|
|
15
|
-
*/
|
|
16
|
-
export declare const roundToNearestN: (nearest: number) => (n: number) => number;
|
|
17
|
-
/**
|
|
18
|
-
* get the prime factors of a number
|
|
19
|
-
*
|
|
20
|
-
* @param num the number to get the prime factors of
|
|
21
|
-
* @returns the prime factors of the number
|
|
22
|
-
* @example getPrimeFactors(12) // [2, 2, 3]
|
|
23
|
-
* getPrimeFactors(15) // [3, 5]
|
|
24
|
-
*/
|
|
25
|
-
export declare const getPrimeFactors: (num: number) => number[];
|
|
26
|
-
/**
|
|
27
|
-
* get the lowest prime factor of a number
|
|
28
|
-
*
|
|
29
|
-
* @param num the number to get the lowest prime factor of
|
|
30
|
-
* @returns the lowest prime factor of the number
|
|
31
|
-
* @example
|
|
32
|
-
* lowestPrimeFactor(12) // 12 = 2 * 2 * 3, min(2, 2, 3) = 2
|
|
33
|
-
* lowestPrimeFactor(15) // 15 = 3 * 5, min(3, 5) = 3
|
|
34
|
-
*/
|
|
35
|
-
export declare const lowestPrimeFactor: (num: number) => number;
|
|
36
|
-
/**
|
|
37
|
-
* get the greatest common divisor of two numbers.
|
|
38
|
-
* {@link} https://en.wikipedia.org/wiki/Euclidean_algorithm
|
|
39
|
-
* {@link} https://en.wikipedia.org/wiki/Greatest_common_divisor
|
|
40
|
-
*
|
|
41
|
-
* @param a the first number
|
|
42
|
-
* @param b the second number
|
|
43
|
-
* @returns the greatest common divisor of the two numbers
|
|
44
|
-
* @example gcd(12, 15) // 3
|
|
45
|
-
* gcd(12, 18) // 6
|
|
46
|
-
*/
|
|
47
|
-
export declare const gcd: (a: number, b: number) => number;
|
|
48
|
-
/**
|
|
49
|
-
* check if two numbers are within a certain tolerance of each other
|
|
50
|
-
*/
|
|
51
|
-
export declare const within: (tolerance: number) => (a: number, b: number) => boolean;
|
|
52
|
-
/**
|
|
53
|
-
* get the average of an array of numbers
|
|
54
|
-
*
|
|
55
|
-
* @param arr the array of numbers to average
|
|
56
|
-
* @returns the average of the array
|
|
57
|
-
* @example average([1, 2, 3]) // 2
|
|
58
|
-
* average([1, 2, 3, 4]) // 2.5
|
|
59
|
-
*/
|
|
60
|
-
export declare const average: (arr: number[]) => number;
|
package/dist/math.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* the golden ratio constant.
|
|
3
|
-
* {@link} https://en.wikipedia.org/wiki/Golden_ratio
|
|
4
|
-
*/
|
|
5
|
-
export const GOLDEN_RATIO = 1.618;
|
|
6
|
-
/**
|
|
7
|
-
* rounds a number to the nearest multiple of another number
|
|
8
|
-
*
|
|
9
|
-
* @param n the number to round
|
|
10
|
-
* @param nearest the number to round to
|
|
11
|
-
* @returns the rounded number
|
|
12
|
-
* @example const roundToNearest5 = roundToNearestN(5);
|
|
13
|
-
* roundToNearest5(13) // 15
|
|
14
|
-
* roundToNearest5(12) // 10
|
|
15
|
-
*/
|
|
16
|
-
export const roundToNearestN = (nearest) => (n) => {
|
|
17
|
-
return Math.round(n / nearest) * nearest;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* get the prime factors of a number
|
|
21
|
-
*
|
|
22
|
-
* @param num the number to get the prime factors of
|
|
23
|
-
* @returns the prime factors of the number
|
|
24
|
-
* @example getPrimeFactors(12) // [2, 2, 3]
|
|
25
|
-
* getPrimeFactors(15) // [3, 5]
|
|
26
|
-
*/
|
|
27
|
-
export const getPrimeFactors = (num) => {
|
|
28
|
-
const factors = [];
|
|
29
|
-
let divisor = 2;
|
|
30
|
-
while (num >= 2) {
|
|
31
|
-
if (num % divisor === 0) {
|
|
32
|
-
factors.push(divisor);
|
|
33
|
-
num = num / divisor;
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
divisor++;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return factors;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* get the lowest prime factor of a number
|
|
43
|
-
*
|
|
44
|
-
* @param num the number to get the lowest prime factor of
|
|
45
|
-
* @returns the lowest prime factor of the number
|
|
46
|
-
* @example
|
|
47
|
-
* lowestPrimeFactor(12) // 12 = 2 * 2 * 3, min(2, 2, 3) = 2
|
|
48
|
-
* lowestPrimeFactor(15) // 15 = 3 * 5, min(3, 5) = 3
|
|
49
|
-
*/
|
|
50
|
-
export const lowestPrimeFactor = (num) => {
|
|
51
|
-
if (num === 1)
|
|
52
|
-
return 1; // 1 has no prime factors
|
|
53
|
-
return Math.min(...getPrimeFactors(num));
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* get the greatest common divisor of two numbers.
|
|
57
|
-
* {@link} https://en.wikipedia.org/wiki/Euclidean_algorithm
|
|
58
|
-
* {@link} https://en.wikipedia.org/wiki/Greatest_common_divisor
|
|
59
|
-
*
|
|
60
|
-
* @param a the first number
|
|
61
|
-
* @param b the second number
|
|
62
|
-
* @returns the greatest common divisor of the two numbers
|
|
63
|
-
* @example gcd(12, 15) // 3
|
|
64
|
-
* gcd(12, 18) // 6
|
|
65
|
-
*/
|
|
66
|
-
export const gcd = (a, b) => {
|
|
67
|
-
if (b === 0)
|
|
68
|
-
return a;
|
|
69
|
-
return gcd(b, a % b);
|
|
70
|
-
};
|
|
71
|
-
/**
|
|
72
|
-
* check if two numbers are within a certain tolerance of each other
|
|
73
|
-
*/
|
|
74
|
-
export const within = (tolerance) => (a, b) => {
|
|
75
|
-
return Math.abs(a - b) <= tolerance;
|
|
76
|
-
};
|
|
77
|
-
/**
|
|
78
|
-
* get the average of an array of numbers
|
|
79
|
-
*
|
|
80
|
-
* @param arr the array of numbers to average
|
|
81
|
-
* @returns the average of the array
|
|
82
|
-
* @example average([1, 2, 3]) // 2
|
|
83
|
-
* average([1, 2, 3, 4]) // 2.5
|
|
84
|
-
*/
|
|
85
|
-
export const average = (arr) => {
|
|
86
|
-
if (arr.length === 0)
|
|
87
|
-
return 0;
|
|
88
|
-
return arr.reduce((acc, val) => acc + val, 0) / arr.length;
|
|
89
|
-
};
|
package/dist/math.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/math.test.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest';
|
|
2
|
-
import { average, gcd, getPrimeFactors, lowestPrimeFactor, roundToNearestN, } from './math';
|
|
3
|
-
describe('roundToNearestN', () => {
|
|
4
|
-
test('rounds a number to the nearest multiple of n', () => {
|
|
5
|
-
const roundToNearest5 = roundToNearestN(5);
|
|
6
|
-
expect(roundToNearest5(13)).toBe(15);
|
|
7
|
-
expect(roundToNearest5(12)).toBe(10);
|
|
8
|
-
});
|
|
9
|
-
});
|
|
10
|
-
describe('getPrimeFactors', () => {
|
|
11
|
-
test('returns the prime factors of a number', () => {
|
|
12
|
-
expect(getPrimeFactors(12)).toEqual([2, 2, 3]);
|
|
13
|
-
expect(getPrimeFactors(15)).toEqual([3, 5]);
|
|
14
|
-
});
|
|
15
|
-
test('edge case: 1', () => {
|
|
16
|
-
expect(getPrimeFactors(1)).toEqual([]);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
describe('lowestPrimeFactor', () => {
|
|
20
|
-
test('returns the lowest prime factor of a number', () => {
|
|
21
|
-
expect(lowestPrimeFactor(12)).toBe(2);
|
|
22
|
-
expect(lowestPrimeFactor(15)).toBe(3);
|
|
23
|
-
});
|
|
24
|
-
test('edge case: 1', () => {
|
|
25
|
-
expect(lowestPrimeFactor(1)).toBe(1);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
describe('gcd', () => {
|
|
29
|
-
test('returns the greatest common divisor of two numbers', () => {
|
|
30
|
-
expect(gcd(12, 15)).toBe(3);
|
|
31
|
-
expect(gcd(12, 18)).toBe(6);
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
describe('average', () => {
|
|
35
|
-
test('returns the average of a list of numbers', () => {
|
|
36
|
-
expect(average([1, 2, 3, 4, 5])).toBe(3);
|
|
37
|
-
expect(average([1, 2, 3, 4, 5, 6])).toBe(3.5);
|
|
38
|
-
});
|
|
39
|
-
test('edge case: empty list', () => {
|
|
40
|
-
expect(average([])).toBe(0);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { RemoveAnyArray } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* taking some data that may be a plain value or a function that returns that value
|
|
4
|
-
*
|
|
5
|
-
* @template T - the type of the value
|
|
6
|
-
* @template K - the type of the arguments necessary in order to resolve the value
|
|
7
|
-
*/
|
|
8
|
-
export type MaybeGetter<T, K extends any[] = []> = T | ((...arg: K) => T);
|
|
9
|
-
/**
|
|
10
|
-
* the value of a MaybeGetter
|
|
11
|
-
*/
|
|
12
|
-
export type UnwrapMaybeGetter<T> = T extends MaybeGetter<infer U, infer _> ? U : T;
|
|
13
|
-
/**
|
|
14
|
-
* the parameters of a MaybeGetter
|
|
15
|
-
*/
|
|
16
|
-
export type MaybeGetterParams<T> = RemoveAnyArray<T extends MaybeGetter<infer _, infer K> ? K : []>;
|
|
17
|
-
/**
|
|
18
|
-
* unwraps a MaybeGetter into a value of type T.
|
|
19
|
-
*
|
|
20
|
-
* @param value - the value to unwrap
|
|
21
|
-
* @param args - the arguments to pass to the getter if the value is a getter
|
|
22
|
-
* @returns T, which is UnwrapMaybeGetter<MaybeGetter<T, K>>
|
|
23
|
-
* @example getValue(5) // 5
|
|
24
|
-
* getValue(() => 5) // 5
|
|
25
|
-
*/
|
|
26
|
-
export declare const getValue: <T, K extends any[]>(value: MaybeGetter<T, K>, ...args: K) => T;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* unwraps a MaybeGetter into a value of type T.
|
|
3
|
-
*
|
|
4
|
-
* @param value - the value to unwrap
|
|
5
|
-
* @param args - the arguments to pass to the getter if the value is a getter
|
|
6
|
-
* @returns T, which is UnwrapMaybeGetter<MaybeGetter<T, K>>
|
|
7
|
-
* @example getValue(5) // 5
|
|
8
|
-
* getValue(() => 5) // 5
|
|
9
|
-
*/
|
|
10
|
-
export const getValue = (value, ...args) => {
|
|
11
|
-
if (typeof value === 'function') {
|
|
12
|
-
return value(...args);
|
|
13
|
-
}
|
|
14
|
-
return value;
|
|
15
|
-
};
|
package/dist/mouse.d.ts
DELETED
package/dist/mouse.js
DELETED
package/dist/random.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* given two numbers, this function returns a random number between them (inclusive)
|
|
3
|
-
*
|
|
4
|
-
* @param min - the lowest number
|
|
5
|
-
* @param max - the highest number
|
|
6
|
-
* @returns a random number between min and max
|
|
7
|
-
*/
|
|
8
|
-
export declare const getRandomInRange: (min: number, max: number) => number;
|
|
9
|
-
export declare const getRandomPointOnCanvas: (canvas: HTMLCanvasElement, buffer?: number) => {
|
|
10
|
-
x: number;
|
|
11
|
-
y: number;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* get a random element from an array
|
|
15
|
-
*
|
|
16
|
-
* @param array
|
|
17
|
-
* @returns random element from given array
|
|
18
|
-
*/
|
|
19
|
-
export declare const getRandomElement: <T>(array: ArrayLike<T>) => T;
|
package/dist/random.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* given two numbers, this function returns a random number between them (inclusive)
|
|
3
|
-
*
|
|
4
|
-
* @param min - the lowest number
|
|
5
|
-
* @param max - the highest number
|
|
6
|
-
* @returns a random number between min and max
|
|
7
|
-
*/
|
|
8
|
-
export const getRandomInRange = (min, max) => Math.round(Math.random() * (max - min) + min);
|
|
9
|
-
export const getRandomPointOnCanvas = (canvas, buffer = 50) => ({
|
|
10
|
-
x: getRandomInRange(buffer, canvas.width - buffer),
|
|
11
|
-
y: getRandomInRange(buffer, canvas.height - buffer),
|
|
12
|
-
});
|
|
13
|
-
/**
|
|
14
|
-
* get a random element from an array
|
|
15
|
-
*
|
|
16
|
-
* @param array
|
|
17
|
-
* @returns random element from given array
|
|
18
|
-
*/
|
|
19
|
-
export const getRandomElement = (array) => {
|
|
20
|
-
return array[Math.floor(Math.random() * array.length)];
|
|
21
|
-
};
|