@limitlesspc/std 0.20.2

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.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/dist/array.d.ts +209 -0
  4. package/dist/array.js +41 -0
  5. package/dist/async/index.d.ts +48 -0
  6. package/dist/async/index.js +155 -0
  7. package/dist/bytes.d.ts +38 -0
  8. package/dist/bytes.js +35 -0
  9. package/dist/chunk-6F4PWJZI.js +0 -0
  10. package/dist/chunk-C2DS6YRJ.js +1664 -0
  11. package/dist/chunk-DO4NH5XG.js +523 -0
  12. package/dist/chunk-EWSJTMH2.js +68 -0
  13. package/dist/chunk-ILLWUQPY.js +139 -0
  14. package/dist/chunk-LJSF3QBT.js +122 -0
  15. package/dist/chunk-MG5VQSTV.js +89 -0
  16. package/dist/chunk-TMLWLR46.js +12 -0
  17. package/dist/chunk-WBSY6KRH.js +358 -0
  18. package/dist/cmath/index.d.ts +57 -0
  19. package/dist/cmath/index.js +187 -0
  20. package/dist/cmp.d.ts +11 -0
  21. package/dist/cmp.js +12 -0
  22. package/dist/csv.d.ts +3 -0
  23. package/dist/csv.js +16 -0
  24. package/dist/easing.d.ts +37 -0
  25. package/dist/easing.js +157 -0
  26. package/dist/events.d.ts +20 -0
  27. package/dist/events.js +67 -0
  28. package/dist/fn/index.d.ts +66 -0
  29. package/dist/fn/index.js +25 -0
  30. package/dist/gfx/index.d.ts +13 -0
  31. package/dist/gfx/index.js +68 -0
  32. package/dist/iter/index.d.ts +226 -0
  33. package/dist/iter/index.js +65 -0
  34. package/dist/math/index.d.ts +463 -0
  35. package/dist/math/index.js +129 -0
  36. package/dist/object.d.ts +72 -0
  37. package/dist/object.js +24 -0
  38. package/dist/random.d.ts +68 -0
  39. package/dist/random.js +22 -0
  40. package/dist/string/index.d.ts +62 -0
  41. package/dist/string/index.js +98 -0
  42. package/dist/structs/index.d.ts +184 -0
  43. package/dist/structs/index.js +24 -0
  44. package/dist/time/index.d.ts +35 -0
  45. package/dist/time/index.js +84 -0
  46. package/dist/types.d.ts +21 -0
  47. package/dist/types.js +1 -0
  48. package/dist/vec3-D7Wuy2AZ.d.ts +70 -0
  49. package/package.json +111 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 limitlesspc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ <h1 align="center">std.js</h1>
2
+ <p align="center">A standard library for JavaScript</p>
@@ -0,0 +1,209 @@
1
+ import { Compare } from './cmp.js';
2
+ import { Indexable, MaybeArray } from './types.js';
3
+
4
+ /**
5
+ * @module
6
+ * Functions for working with arrays
7
+ */
8
+
9
+ /**
10
+ * Switches the positions of two values in an array in-place
11
+ * @param array
12
+ * @param i the first index
13
+ * @param j the second index
14
+ * @example
15
+ * ```ts
16
+ * const array = [1, 2, 3, 4, 5];
17
+ * swap(array, 0, 4);
18
+ * console.log(array); // [5, 2, 3, 4, 1]
19
+ * ```
20
+ */
21
+ declare function swap<T>(array: Indexable<T>, i: number, j: number): void;
22
+ /**
23
+ * Removes a value from an array
24
+ * @param array
25
+ * @param item
26
+ * @returns if the item was removed
27
+ * @example
28
+ * ```ts
29
+ * const array = [1, 2, 3, 4, 5];
30
+ * console.log(remove(array, 3)); // true
31
+ * console.log(array); // [1, 2, 4, 5]
32
+ * console.log(remove(array, 3)); // false
33
+ * console.log(array); // [1, 2, 4, 5]
34
+ * ```
35
+ */
36
+ declare function remove<T>(array: T[], item: T): boolean;
37
+ /**
38
+ * Removes an item from an array without maintaining order
39
+ * @param array
40
+ * @param index
41
+ * @returns the removed item
42
+ * @example
43
+ * ```ts
44
+ * const array = [1, 2, 3, 4, 5];
45
+ * console.log(unorderedRemove(array, 1)); // 2
46
+ * console.log(array); // [1, 5, 3, 4]
47
+ * console.log(unorderedRemove(array, 1)); // 5
48
+ * console.log(array); // [1, 4, 3]
49
+ * ```
50
+ */
51
+ declare function unorderedRemove<T>(array: T[], index: number): T | undefined;
52
+ /**
53
+ * Returns an array containing items found in all arrays
54
+ * @param arrays
55
+ * @example
56
+ * ```ts
57
+ * console.log(intersection([1, 2, 3], [2, 3, 4], [3, 4, 5])); // [3]
58
+ * console.log(intersection([1, 2, 3], [4, 5, 6])); // []
59
+ * ```
60
+ */
61
+ declare function intersection<T>(...arrays: ReadonlyArray<readonly T[]>): T[];
62
+ /**
63
+ * Returns an array containing items found in the second array that are not in the first
64
+ * @param oldArray
65
+ * @param newArray
66
+ * @example
67
+ * ```ts
68
+ * console.log(added([1, 2, 3], [2, 3, 4])); // [4]
69
+ * console.log(added([1, 2, 3], [4, 5, 6])); // [4, 5, 6]
70
+ * ```
71
+ */
72
+ declare function added<T, U>(oldArray: readonly T[], newArray: readonly U[]): U[];
73
+ /**
74
+ * Returns an array containing items found in the first array that are not in the second
75
+ * @param oldArray
76
+ * @param newArray
77
+ * @example
78
+ * ```ts
79
+ * console.log(removed([1, 2, 3], [2, 3, 4])); // [1]
80
+ * console.log(removed([1, 2, 3], [4, 5, 6])); // [1, 2, 3]
81
+ * ```
82
+ */
83
+ declare function removed<T, U>(oldArray: readonly T[], newArray: readonly U[]): T[];
84
+ /**
85
+ * Get items that were added and removed from an array
86
+ * @param oldArray
87
+ * @param newArray
88
+ * @returns a tuple containing 1. the added items and 2. the removed items
89
+ * @example
90
+ * ```ts
91
+ * console.log(changes([1, 2, 3], [2, 3, 4])); // [[4], [1]]
92
+ * console.log(changes([1, 2, 3], [4, 5, 6])); // [[4, 5, 6], [1, 2, 3]]
93
+ * ```
94
+ */
95
+ declare function changes<T, U>(oldArray: readonly T[], newArray: readonly U[]): [added: U[], removed: T[]];
96
+ /**
97
+ * Determines if there is an item in one array that is not in the other
98
+ * @example
99
+ * ```ts
100
+ * console.log(hasChange([1, 2, 3], [1, 2, 3])); // false
101
+ * console.log(hasChange([1, 2, 3], [2, 3, 1])); // false
102
+ * console.log(hasChange([1, 2, 3], [1, 2, 6])); // true
103
+ * ```
104
+ */
105
+ declare function hasChange(a: readonly any[], b: readonly any[]): boolean;
106
+ /**
107
+ * Returns an array containing items found in either array, but not both
108
+ * @param array1
109
+ * @param array2
110
+ * @example
111
+ * ```ts
112
+ * console.log(difference([1, 2, 3], [2, 3, 4])); // [1, 4]
113
+ * console.log(difference([1, 2, 3], [4, 5, 6])); // [1, 2, 3, 4, 5, 6]
114
+ * ```
115
+ */
116
+ declare function difference<T>(array1: readonly T[], array2: readonly T[]): T[];
117
+ /**
118
+ * Returns an array containing items from all arrays deduplicated
119
+ * @param arrays
120
+ * @example
121
+ * ```ts
122
+ * console.log(union([1, 2, 3], [2, 3, 4], [3, 4, 5])); // [1, 2, 3, 4, 5]
123
+ * console.log(union([1, 2, 3], [4, 5, 6])); // [1, 2, 3, 4, 5, 6]
124
+ * ```
125
+ */
126
+ declare function union<T>(...arrays: Array<Iterable<T>>): T[];
127
+ /**
128
+ * Test if two arrays contain the same items in the same order
129
+ * @param a
130
+ * @param b
131
+ * @returns if the arrays are equal
132
+ * @example
133
+ * ```ts
134
+ * console.log(equal([1, 2, 3], [1, 2, 3])); // true
135
+ * console.log(equal([1, 2, 3], [3, 2, 1])); // false
136
+ * ```
137
+ */
138
+ declare function equal<T>(a: readonly T[], b: readonly T[]): boolean;
139
+ /**
140
+ * Check if an array contains at least one of the items in another
141
+ * @param a the array to check
142
+ * @param b items the array should include
143
+ * @returns if the `a` has at least one of the items in `b`
144
+ * @example
145
+ * ```ts
146
+ * console.log(includesAny([1, 2, 3], [2, 3, 4])); // true
147
+ * console.log(includesAny([1, 2, 3], [4, 5, 6])); // false
148
+ * ```
149
+ */
150
+ declare function includesAny<T, U>(a: readonly T[], b: readonly U[]): boolean;
151
+ /**
152
+ * Check if an array contains all of the items in another
153
+ * @param a the array to check
154
+ * @param b items the array should include
155
+ * @returns if the `a` has all of the items in `b`
156
+ * @example
157
+ * ```ts
158
+ * console.log(includesAll([1, 2, 3], [2, 3, 1])); // true
159
+ * console.log(includesAll([1, 2, 3], [4, 5, 6])); // false
160
+ * ```
161
+ */
162
+ declare function includesAll<T, U>(a: readonly T[], b: readonly U[]): boolean;
163
+ /**
164
+ * Returns a new array with items with a unique key
165
+ * @param array
166
+ * @param key
167
+ * @example
168
+ * ```ts
169
+ * const array = [
170
+ * { id: 1, name: "Alice" },
171
+ * { id: 2, name: "Bob" },
172
+ * { id: 1, name: "Charlie" },
173
+ * { id: 3, name: "David" },
174
+ * { id: 2, name: "Eve" },
175
+ * { id: 4, name: "Frank" },
176
+ * ];
177
+ * console.log(dedupe(array, "id")); // [
178
+ * // { id: 1, name: "Alice" },
179
+ * // { id: 2, name: "Bob" },
180
+ * // { id: 3, name: "David" },
181
+ * // { id: 4, name: "Frank" }
182
+ * // ]
183
+ */
184
+ declare function dedupe<T extends {
185
+ [K in keyof T]: T[K];
186
+ }>(array: Iterable<T>, key: keyof T): T[];
187
+ declare function filterByKey<T>(array: readonly T[], key: keyof T): T[];
188
+ /**
189
+ * Sorts an array of objects by the value of a key
190
+ * @param array
191
+ * @param key
192
+ * @param compare
193
+ * @example
194
+ * ```ts
195
+ * const array = [
196
+ * { id: 3, name: "Charlie" },
197
+ * { id: 1, name: "Alice" },
198
+ * { id: 2, name: "Bob" },
199
+ * ];
200
+ * console.log(sortByKeys(array, "id")); // [
201
+ * // { id: 1, name: "Alice" },
202
+ * // { id: 2, name: "Bob" },
203
+ * // { id: 3, name: "Charlie" }
204
+ * // ]
205
+ * ```
206
+ */
207
+ declare function sortByKeys<T, K extends keyof T>(array: Iterable<T>, key: MaybeArray<K>, compare?: Compare<T[K]>): T[];
208
+
209
+ export { added, changes, dedupe, difference, equal, filterByKey, hasChange, includesAll, includesAny, intersection, remove, removed, sortByKeys, swap, union, unorderedRemove };
package/dist/array.js ADDED
@@ -0,0 +1,41 @@
1
+ import {
2
+ added,
3
+ changes,
4
+ dedupe,
5
+ difference,
6
+ equal,
7
+ filterByKey,
8
+ hasChange,
9
+ includesAll,
10
+ includesAny,
11
+ intersection,
12
+ remove,
13
+ removed,
14
+ sortByKeys,
15
+ swap,
16
+ union,
17
+ unorderedRemove
18
+ } from "./chunk-ILLWUQPY.js";
19
+ import "./chunk-WBSY6KRH.js";
20
+ import "./chunk-MG5VQSTV.js";
21
+ import "./chunk-6F4PWJZI.js";
22
+ import "./chunk-TMLWLR46.js";
23
+ import "./chunk-LJSF3QBT.js";
24
+ export {
25
+ added,
26
+ changes,
27
+ dedupe,
28
+ difference,
29
+ equal,
30
+ filterByKey,
31
+ hasChange,
32
+ includesAll,
33
+ includesAny,
34
+ intersection,
35
+ remove,
36
+ removed,
37
+ sortByKeys,
38
+ swap,
39
+ union,
40
+ unorderedRemove
41
+ };
@@ -0,0 +1,48 @@
1
+ import { uint, Awaitable, AnyFunction, Result } from '../types.js';
2
+
3
+ type Task<T> = () => Awaitable<T>;
4
+ interface TaskQueueItem<T> {
5
+ task: () => Awaitable<T>;
6
+ resolve: (result: T) => void;
7
+ reject: (reason?: any) => void;
8
+ }
9
+ declare class TaskQueue<T> {
10
+ protected tasks: Array<TaskQueueItem<T>>;
11
+ private stopped;
12
+ private paused;
13
+ private pendingTask;
14
+ constructor(tasks?: Array<TaskQueueItem<T>>);
15
+ get size(): uint;
16
+ enqueue(task: Task<T>, autoDequeue?: boolean): Promise<T>;
17
+ dequeue(): Promise<boolean>;
18
+ stop(): void;
19
+ pause(): void;
20
+ start(): Promise<boolean>;
21
+ }
22
+
23
+ /**
24
+ * @module
25
+ * Functions for working with promises
26
+ */
27
+
28
+ declare const sleep: (ms?: number) => Promise<void>;
29
+ declare function throttle<T extends AnyFunction>(func: T, ms: number): (...args: Parameters<T>) => void;
30
+ declare function debounce<T extends AnyFunction>(func: T, ms: number): (...args: Parameters<T>) => void;
31
+ /**
32
+ * Runs promises concurrently with a limit to the number of promises running at once
33
+ * @param funcs an iterable of functions that return promises
34
+ * @param max the most promises that can run at once
35
+ * @returns an array of results of all the promises in the order they were passed in
36
+ */
37
+ declare function concurrently<T>(funcs: Iterable<() => Promise<T>>, max: number): Promise<T[]>;
38
+ declare function retry<T>(fn: () => Promise<T>, { maxAttempts, delay }?: {
39
+ maxAttempts?: uint;
40
+ delay?: number;
41
+ }): Promise<Result<T, unknown>>;
42
+ declare function retryWithExponentialBackoff<T>(fn: () => Promise<T>, { maxAttempts, startDelay, multiplier, }?: {
43
+ maxAttempts?: uint;
44
+ startDelay?: number;
45
+ multiplier?: number;
46
+ }): Promise<Result<T, unknown>>;
47
+
48
+ export { TaskQueue, concurrently, debounce, retry, retryWithExponentialBackoff, sleep, throttle };
@@ -0,0 +1,155 @@
1
+ import {
2
+ Queue
3
+ } from "../chunk-DO4NH5XG.js";
4
+ import "../chunk-ILLWUQPY.js";
5
+ import "../chunk-WBSY6KRH.js";
6
+ import "../chunk-MG5VQSTV.js";
7
+ import "../chunk-6F4PWJZI.js";
8
+ import "../chunk-TMLWLR46.js";
9
+ import "../chunk-LJSF3QBT.js";
10
+
11
+ // src/async/queue.ts
12
+ var TaskQueue = class {
13
+ constructor(tasks = []) {
14
+ this.tasks = tasks;
15
+ }
16
+ stopped = false;
17
+ paused = false;
18
+ pendingTask = false;
19
+ get size() {
20
+ return this.tasks.length;
21
+ }
22
+ async enqueue(task, autoDequeue = true) {
23
+ return new Promise((resolve, reject) => {
24
+ this.tasks.push({ task, resolve, reject });
25
+ if (autoDequeue) {
26
+ void this.dequeue();
27
+ }
28
+ });
29
+ }
30
+ async dequeue() {
31
+ if (this.pendingTask || this.paused) {
32
+ return false;
33
+ }
34
+ if (this.stopped) {
35
+ this.tasks.length = 0;
36
+ this.stopped = false;
37
+ return false;
38
+ }
39
+ const item = this.tasks.pop();
40
+ if (!item) {
41
+ return false;
42
+ }
43
+ try {
44
+ this.pendingTask = true;
45
+ const result = await item.task();
46
+ this.pendingTask = false;
47
+ item.resolve(result);
48
+ } catch (error) {
49
+ item.reject(error);
50
+ this.pendingTask = false;
51
+ }
52
+ void this.dequeue();
53
+ return true;
54
+ }
55
+ stop() {
56
+ this.stopped = true;
57
+ }
58
+ pause() {
59
+ this.paused = true;
60
+ }
61
+ async start() {
62
+ this.stopped = false;
63
+ this.paused = false;
64
+ return this.dequeue();
65
+ }
66
+ };
67
+
68
+ // src/async/index.ts
69
+ var sleep = async (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));
70
+ function throttle(func, ms) {
71
+ let timeout;
72
+ let lastCalled = 0;
73
+ return (...args) => {
74
+ clearTimeout(timeout);
75
+ timeout = setTimeout(
76
+ () => {
77
+ func(...args);
78
+ timeout = void 0;
79
+ lastCalled = Date.now();
80
+ },
81
+ ms - (Date.now() - lastCalled)
82
+ );
83
+ };
84
+ }
85
+ function debounce(func, ms) {
86
+ let timeout;
87
+ return (...args) => {
88
+ clearTimeout(timeout);
89
+ timeout = setTimeout(() => {
90
+ func(...args);
91
+ timeout = void 0;
92
+ }, ms);
93
+ };
94
+ }
95
+ async function concurrently(funcs, max) {
96
+ const tasks = [...funcs];
97
+ const results = Array.from({ length: tasks.length });
98
+ const queue = new Queue(tasks);
99
+ const length = queue.size;
100
+ let i = 0;
101
+ return new Promise((resolve) => {
102
+ const next = async () => {
103
+ const func = queue.dequeue();
104
+ if (func) {
105
+ results[i++] = await func();
106
+ void next();
107
+ } else if (results.length === length) {
108
+ resolve(results);
109
+ }
110
+ };
111
+ for (let i2 = 0; i2 < max; i2++) {
112
+ void next();
113
+ }
114
+ });
115
+ }
116
+ async function retry(fn, { maxAttempts = 5, delay = 0 } = {}) {
117
+ let attempts = 0;
118
+ while (true) {
119
+ try {
120
+ return [await fn(), void 0];
121
+ } catch (error) {
122
+ if (++attempts >= maxAttempts) {
123
+ return [void 0, error];
124
+ }
125
+ await sleep(delay);
126
+ }
127
+ }
128
+ }
129
+ async function retryWithExponentialBackoff(fn, {
130
+ maxAttempts = 5,
131
+ startDelay = 1e3,
132
+ multiplier = 2
133
+ } = {}) {
134
+ let attempts = 0;
135
+ while (true) {
136
+ try {
137
+ return [await fn(), void 0];
138
+ } catch (error) {
139
+ const delay = startDelay * multiplier ** attempts;
140
+ if (++attempts >= maxAttempts) {
141
+ return [void 0, error];
142
+ }
143
+ await sleep(delay);
144
+ }
145
+ }
146
+ }
147
+ export {
148
+ TaskQueue,
149
+ concurrently,
150
+ debounce,
151
+ retry,
152
+ retryWithExponentialBackoff,
153
+ sleep,
154
+ throttle
155
+ };
@@ -0,0 +1,38 @@
1
+ import { uint } from './types.js';
2
+
3
+ /**
4
+ * @module
5
+ * Utilities for working with binary data
6
+ */
7
+
8
+ declare const KILOBYTE = 1000;
9
+ declare const MEGABYTE: number;
10
+ declare const GIGABYTE: number;
11
+ declare const TERABYTE: number;
12
+ declare const KIBIBYTE = 1024;
13
+ declare const MEBIBYTE: number;
14
+ declare const GIBIBYTE: number;
15
+ declare const TEBIBYTE: number;
16
+ /**
17
+ * Convert binary into its hex representation
18
+ * @param buffer
19
+ * @returns hex string
20
+ * @example
21
+ * ```ts
22
+ * const buffer = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
23
+ * console.log(hex(buffer)); // "48656c6c6f"
24
+ * ```
25
+ */
26
+ declare function hex(buffer: Iterable<uint>): string;
27
+ /**
28
+ * Convert a hex string into binary
29
+ * @param hex
30
+ * @returns uint8 buffer
31
+ * @example
32
+ * ```ts
33
+ * const buffer = unhex("48656c6c6f");
34
+ * console.log(buffer); // Uint8Array [ 72, 101, 108, 108, 111 ]
35
+ */
36
+ declare function unhex(hex: string): Uint8Array;
37
+
38
+ export { GIBIBYTE, GIGABYTE, KIBIBYTE, KILOBYTE, MEBIBYTE, MEGABYTE, TEBIBYTE, TERABYTE, hex, unhex };
package/dist/bytes.js ADDED
@@ -0,0 +1,35 @@
1
+ // src/bytes.ts
2
+ var KILOBYTE = 1e3;
3
+ var MEGABYTE = 1e3 * KILOBYTE;
4
+ var GIGABYTE = 1e3 * MEGABYTE;
5
+ var TERABYTE = 1e3 * GIGABYTE;
6
+ var KIBIBYTE = 1024;
7
+ var MEBIBYTE = 1024 * KIBIBYTE;
8
+ var GIBIBYTE = 1024 * MEBIBYTE;
9
+ var TEBIBYTE = 1024 * GIBIBYTE;
10
+ function hex(buffer) {
11
+ const hexCodes = [];
12
+ for (const element of buffer) {
13
+ hexCodes.push(element.toString(16).padStart(2, "0"));
14
+ }
15
+ return hexCodes.join("");
16
+ }
17
+ function unhex(hex2) {
18
+ const bytes = new Uint8Array(hex2.length / 2);
19
+ for (let i = 0; i < bytes.length; i++) {
20
+ bytes[i] = Number.parseInt(hex2.slice(i * 2, i * 2 + 2), 16);
21
+ }
22
+ return bytes;
23
+ }
24
+ export {
25
+ GIBIBYTE,
26
+ GIGABYTE,
27
+ KIBIBYTE,
28
+ KILOBYTE,
29
+ MEBIBYTE,
30
+ MEGABYTE,
31
+ TEBIBYTE,
32
+ TERABYTE,
33
+ hex,
34
+ unhex
35
+ };
File without changes