toosoon-utils 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/src/now.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Polyfill for "now()" functions
3
+ */
4
+ let now: () => number;
5
+
6
+ // In node.js, use process.hrtime
7
+ if (typeof process !== 'undefined' && process.hrtime) {
8
+ now = function () {
9
+ // Convert [seconds, nanoseconds] to milliseconds
10
+ const time = process.hrtime();
11
+ return time[0] * 1000 + time[1] / 1000000;
12
+ };
13
+ }
14
+ // In a browser use performance or Date
15
+ else if (typeof performance !== 'undefined') {
16
+ // This must be bound, because directly assigning this function leads to an invocation exception in Chrome
17
+ now = performance.now.bind(performance);
18
+ } else if (typeof Date.now !== 'undefined') {
19
+ now = Date.now;
20
+ } else {
21
+ now = function () {
22
+ return new Date().getTime();
23
+ };
24
+ }
25
+
26
+ export default now;
package/src/random.ts ADDED
@@ -0,0 +1,162 @@
1
+ import { radToSphere } from './geometry';
2
+ import { Vector2, Vector3 } from './types';
3
+
4
+ /**
5
+ * Generate random boolean (true or false)
6
+ *
7
+ * @param {number} [probability=0.5] Probability to get true
8
+ * @returns {boolean} Either true or false
9
+ */
10
+ export function randomBoolean(probability: number = 0.5): boolean {
11
+ return Math.random() < probability;
12
+ }
13
+
14
+ /**
15
+ * Generate random sign (1 or -1)
16
+ *
17
+ * @param {number} [probability=0.5] Probability to get 1
18
+ * @returns {number} Either 1 or -1
19
+ */
20
+ export function randomSign(probability: number = 0.5): number {
21
+ return randomBoolean(probability) ? 1 : -1;
22
+ }
23
+
24
+ /**
25
+ * Generate a random float
26
+ *
27
+ * @param {number} [min=0] Minimum boundary
28
+ * @param {number} [max=1] Maximum boundary
29
+ * @param {number} [precision=2] Number of digits after the decimal point
30
+ * @returns {number} Generated float
31
+ */
32
+ export function randomFloat(min: number = 0, max: number = 1, precision: number = 2): number {
33
+ return parseFloat(Math.min(min + Math.random() * (max - min), max).toFixed(precision));
34
+ }
35
+
36
+ /**
37
+ * Generate a random integer
38
+ *
39
+ * @param {number} min Minimum boundary
40
+ * @param {number} max Maximum boundary
41
+ * @returns {number} Generated integer
42
+ */
43
+ export function randomInt(min: number, max: number): number {
44
+ return Math.floor(Math.random() * (max - min + 1) + min);
45
+ }
46
+
47
+ /**
48
+ * Generate a random hexadecimal color
49
+ *
50
+ * @returns {string} Hexadecimal color
51
+ */
52
+ export function randomHexColor(): string {
53
+ return '#' + ('00000' + ((Math.random() * (1 << 24)) | 0).toString(16)).slice(-6);
54
+ }
55
+
56
+ /**
57
+ * Pick a random item from an array
58
+ *
59
+ * @param {I[]} [array=[]] Array to pick the item from
60
+ * @returns {I|undefined} Random item picked
61
+ */
62
+ export function randomItem<I = unknown>(array: I[] = []): I | undefined {
63
+ if (array.length === 0) return undefined;
64
+ return array[randomInt(0, array.length - 1)];
65
+ }
66
+
67
+ /**
68
+ * Pick a random property from an object
69
+ *
70
+ * @param {object} [object={}] Object to pick the property from
71
+ * @returns {unknown|undefined} Random item picked
72
+ */
73
+ export function randomObjectProperty(object: object = {}): unknown | undefined {
74
+ const keys = Object.keys(object);
75
+ const key = randomItem(keys);
76
+ if (key && object.hasOwnProperty(key)) {
77
+ return object[key as keyof object];
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Return a random index from an array of weights
83
+ *
84
+ * @param {number[]} weights Array of weights
85
+ * @returns {number} Random index based on weights
86
+ */
87
+ export function randomIndex(weights: number[] = []): number {
88
+ if (weights.length === 0) return -1;
89
+
90
+ let totalWeight = 0;
91
+ for (let weight of weights) {
92
+ totalWeight += weight;
93
+ }
94
+
95
+ if (totalWeight <= 0) {
96
+ console.warn('randomIndex()', 'Weights must sum to > 0', totalWeight);
97
+ }
98
+
99
+ let random = Math.random() * totalWeight;
100
+ for (let i = 0; i < weights.length; i++) {
101
+ if (random < weights[i]) return i;
102
+ random -= weights[i];
103
+ }
104
+
105
+ return 0;
106
+ }
107
+
108
+ // *********************
109
+ // Geometry
110
+ // *********************
111
+ /**
112
+ * Produce a random 2D point around the perimiter of a unit circle
113
+ *
114
+ * @param {number} [radius=1] Radius of the circle
115
+ * @param {Vector2} target Target vector
116
+ * @returns {Vector2} Random 2D point on circle
117
+ */
118
+ export function onCircle(radius: number = 1, target: Vector2 = { x: 0, y: 0 }): Vector2 {
119
+ const angle = Math.random() * 2.0 * Math.PI;
120
+ target.x = radius * Math.cos(angle);
121
+ target.y = radius * Math.sin(angle);
122
+ return target;
123
+ }
124
+
125
+ /**
126
+ * Produce a random 2D point inside a unit circle
127
+ *
128
+ * @param {number} [radius=1] Radius of the circle
129
+ * @param {Vector2} target Target vector
130
+ * @returns {Vector2} Random 2D point inside circle
131
+ */
132
+ export function insideCircle(radius: number = 1, target: Vector2 = { x: 0, y: 0 }): Vector2 {
133
+ radius *= Math.random();
134
+ return onCircle(radius, target);
135
+ }
136
+
137
+ /**
138
+ * Produce a random 3D point on the surface of a unit sphere
139
+ *
140
+ * @param {number} [radius=1] Radius of the sphere
141
+ * @param {Vector3} target Target vector
142
+ * @returns {Vector3} Random 3D point on sphere
143
+ */
144
+ export function onSphere(radius: number = 1, target: Vector3 = { x: 0, y: 0, z: 0 }): Vector3 {
145
+ const u = Math.random() * Math.PI * 2;
146
+ const v = Math.random() * 2 - 1;
147
+ const phi = u;
148
+ const theta = Math.acos(v);
149
+ return radToSphere(radius, phi, theta, target);
150
+ }
151
+
152
+ /**
153
+ * Produce a random 3D point inside a unit sphere
154
+ *
155
+ * @param {number} [radius=1] Radius of the sphere
156
+ * @param {Vector3} target Target vector
157
+ * @returns {Vector3} Random 3D point inside sphere
158
+ */
159
+ export function insideSphere(radius: number = 1, target: Vector3 = { x: 0, y: 0, z: 0 }): Vector3 {
160
+ radius *= Math.random();
161
+ return onSphere(radius, target);
162
+ }
package/src/strings.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Capitalize a string
3
+ *
4
+ * @param {string} string String to capitalize
5
+ * @returns {string} Capitalized string
6
+ */
7
+ export function capitalize(string: string): string {
8
+ return string.charAt(0).toUpperCase() + string.slice(1);
9
+ }
10
+
11
+ /**
12
+ * Clean a path by removing params
13
+ *
14
+ * @param {string} path Path to clean
15
+ * @returns {string} Cleaned path
16
+ */
17
+ export function cleanPath(path: string): string {
18
+ return path.split('#')[0].split('?')[0];
19
+ }
package/src/types.ts ADDED
@@ -0,0 +1,24 @@
1
+ // *********************
2
+ // Geometry
3
+ // *********************
4
+ export interface Vector2 {
5
+ x: number;
6
+ y: number;
7
+ }
8
+
9
+ export interface Vector3 extends Vector2 {
10
+ z: number;
11
+ }
12
+
13
+ export interface Vector4 extends Vector3 {
14
+ w: number;
15
+ }
16
+
17
+ // *********************
18
+ // Functions
19
+ // *********************
20
+ export interface Deferred<T> {
21
+ promise: Promise<T>;
22
+ resolve: (value: T | PromiseLike<T>) => void;
23
+ reject: (reason?: unknown) => void;
24
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "include": ["src"],
3
+ "exclude": ["node_modules"],
4
+ "compilerOptions": {
5
+ "baseUrl": ".",
6
+ "outDir": "lib",
7
+ "target": "es5",
8
+ "module": "commonjs",
9
+ "allowJs": true,
10
+ "allowSyntheticDefaultImports": true,
11
+ "declaration": true,
12
+ "esModuleInterop": true,
13
+ "experimentalDecorators": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "isolatedModules": true,
16
+ "incremental": true,
17
+ "noUnusedParameters": true,
18
+ "noUnusedLocals": true,
19
+ "resolveJsonModule": true,
20
+ "skipLibCheck": true,
21
+ "strict": true
22
+ }
23
+ }