@tempots/std 0.19.0 → 0.21.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/array.cjs +1 -1
- package/array.d.ts +124 -284
- package/array.js +108 -128
- package/date.cjs +1 -0
- package/date.d.ts +393 -0
- package/date.js +49 -0
- package/function.cjs +1 -1
- package/function.d.ts +145 -0
- package/function.js +35 -5
- package/index.cjs +1 -1
- package/index.d.ts +6 -0
- package/index.js +272 -199
- package/iterator.cjs +1 -0
- package/iterator.d.ts +219 -0
- package/iterator.js +69 -0
- package/json.cjs +1 -1
- package/json.d.ts +40 -2
- package/json.js +7 -6
- package/map.cjs +1 -0
- package/map.d.ts +202 -0
- package/map.js +41 -0
- package/number.cjs +1 -1
- package/number.d.ts +16 -25
- package/number.js +26 -27
- package/object.cjs +1 -1
- package/object.d.ts +146 -8
- package/object.js +42 -15
- package/package.json +43 -1
- package/random.cjs +1 -0
- package/random.d.ts +200 -0
- package/random.js +72 -0
- package/set.cjs +1 -0
- package/set.d.ts +227 -0
- package/set.js +52 -0
- package/string.cjs +4 -4
- package/string.d.ts +2 -60
- package/string.js +122 -129
- package/timer.cjs +1 -1
- package/timer.d.ts +14 -0
- package/timer.js +49 -27
- package/url.cjs +1 -0
- package/url.d.ts +232 -0
- package/url.js +79 -0
package/iterator.d.ts
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for working with iterators and iterables.
|
|
3
|
+
*
|
|
4
|
+
* This module provides functional utilities for iterator manipulation,
|
|
5
|
+
* allowing for lazy evaluation and memory-efficient processing of sequences.
|
|
6
|
+
* All functions work with any iterable, including arrays, sets, maps, and custom iterators.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Takes the first n elements from an iterable.
|
|
12
|
+
*
|
|
13
|
+
* This function creates a new array containing the first n elements
|
|
14
|
+
* from the iterable. It's useful for limiting results or sampling data.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
19
|
+
* const first3 = take(numbers, 3) // [1, 2, 3]
|
|
20
|
+
*
|
|
21
|
+
* const firstUsers = take(userGenerator(), 5) // First 5 users from generator
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @param iterable - The iterable to take elements from
|
|
25
|
+
* @param count - The number of elements to take
|
|
26
|
+
* @returns An array containing the first count elements
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export declare const take: <T>(iterable: Iterable<T>, count: number) => T[];
|
|
30
|
+
/**
|
|
31
|
+
* Skips the first n elements and returns an iterable of the remaining elements.
|
|
32
|
+
*
|
|
33
|
+
* This function creates a new iterable that yields all elements after
|
|
34
|
+
* skipping the specified number of initial elements.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
39
|
+
* const afterFirst2 = [...skip(numbers, 2)] // [3, 4, 5]
|
|
40
|
+
*
|
|
41
|
+
* // Use with other iterator functions
|
|
42
|
+
* const result = take(skip(largeDataset, 100), 10) // Skip 100, take next 10
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @param iterable - The iterable to skip elements from
|
|
46
|
+
* @param count - The number of elements to skip
|
|
47
|
+
* @returns An iterable that yields remaining elements
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
export declare const skip: <T>(iterable: Iterable<T>, count: number) => Iterable<T>;
|
|
51
|
+
/**
|
|
52
|
+
* Filters an iterable, yielding only elements that satisfy the predicate.
|
|
53
|
+
*
|
|
54
|
+
* This function creates a new iterable that yields only elements
|
|
55
|
+
* for which the predicate returns true. It's lazy and memory-efficient.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const numbers = [1, 2, 3, 4, 5, 6]
|
|
60
|
+
* const evens = [...filter(numbers, n => n % 2 === 0)] // [2, 4, 6]
|
|
61
|
+
*
|
|
62
|
+
* // Chain with other operations
|
|
63
|
+
* const result = take(filter(largeDataset, isValid), 100)
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @param iterable - The iterable to filter
|
|
67
|
+
* @param predicate - Function that tests each element
|
|
68
|
+
* @returns An iterable that yields filtered elements
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export declare const filter: <T>(iterable: Iterable<T>, predicate: (item: T) => boolean) => Iterable<T>;
|
|
72
|
+
/**
|
|
73
|
+
* Maps an iterable, transforming each element with the mapper function.
|
|
74
|
+
*
|
|
75
|
+
* This function creates a new iterable that yields transformed elements.
|
|
76
|
+
* It's lazy and memory-efficient, processing elements on-demand.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
81
|
+
* const doubled = [...map(numbers, n => n * 2)] // [2, 4, 6, 8, 10]
|
|
82
|
+
*
|
|
83
|
+
* const users = [{ name: 'Alice' }, { name: 'Bob' }]
|
|
84
|
+
* const names = [...map(users, user => user.name)] // ['Alice', 'Bob']
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param iterable - The iterable to map
|
|
88
|
+
* @param mapper - Function that transforms each element
|
|
89
|
+
* @returns An iterable that yields transformed elements
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export declare const map: <T, U>(iterable: Iterable<T>, mapper: (item: T) => U) => Iterable<U>;
|
|
93
|
+
/**
|
|
94
|
+
* Reduces an iterable to a single value using the reducer function.
|
|
95
|
+
*
|
|
96
|
+
* This function processes all elements in the iterable, accumulating
|
|
97
|
+
* a result using the reducer function and initial value.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
102
|
+
* const sum = reduce(numbers, (acc, n) => acc + n, 0) // 15
|
|
103
|
+
*
|
|
104
|
+
* const words = ['hello', 'world', 'foo', 'bar']
|
|
105
|
+
* const longest = reduce(words, (acc, word) => word.length > acc.length ? word : acc, '')
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @param iterable - The iterable to reduce
|
|
109
|
+
* @param reducer - Function that combines accumulator with each element
|
|
110
|
+
* @param initial - Initial value for the accumulator
|
|
111
|
+
* @returns The final accumulated value
|
|
112
|
+
* @public
|
|
113
|
+
*/
|
|
114
|
+
export declare const reduce: <T, U>(iterable: Iterable<T>, reducer: (acc: U, item: T) => U, initial: U) => U;
|
|
115
|
+
/**
|
|
116
|
+
* Finds the first element in an iterable that satisfies the predicate.
|
|
117
|
+
*
|
|
118
|
+
* This function returns the first element for which the predicate returns true,
|
|
119
|
+
* or undefined if no such element is found.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const numbers = [1, 3, 5, 8, 9, 12]
|
|
124
|
+
* const firstEven = find(numbers, n => n % 2 === 0) // 8
|
|
125
|
+
*
|
|
126
|
+
* const users = [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }]
|
|
127
|
+
* const adult = find(users, user => user.age >= 18) // { name: 'Alice', age: 25 }
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @param iterable - The iterable to search
|
|
131
|
+
* @param predicate - Function that tests each element
|
|
132
|
+
* @returns The first matching element, or undefined if none found
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
135
|
+
export declare const find: <T>(iterable: Iterable<T>, predicate: (item: T) => boolean) => T | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* Tests whether all elements in an iterable satisfy the predicate.
|
|
138
|
+
*
|
|
139
|
+
* This function returns true if the predicate returns true for all elements,
|
|
140
|
+
* or false otherwise. It short-circuits on the first false result.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const numbers = [2, 4, 6, 8]
|
|
145
|
+
* const allEven = every(numbers, n => n % 2 === 0) // true
|
|
146
|
+
*
|
|
147
|
+
* const users = [{ age: 25 }, { age: 30 }, { age: 17 }]
|
|
148
|
+
* const allAdults = every(users, user => user.age >= 18) // false
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @param iterable - The iterable to test
|
|
152
|
+
* @param predicate - Function that tests each element
|
|
153
|
+
* @returns true if all elements satisfy the predicate, false otherwise
|
|
154
|
+
* @public
|
|
155
|
+
*/
|
|
156
|
+
export declare const every: <T>(iterable: Iterable<T>, predicate: (item: T) => boolean) => boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Tests whether at least one element in an iterable satisfies the predicate.
|
|
159
|
+
*
|
|
160
|
+
* This function returns true if the predicate returns true for any element,
|
|
161
|
+
* or false if no elements satisfy the predicate. It short-circuits on the first true result.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const numbers = [1, 3, 5, 8, 9]
|
|
166
|
+
* const hasEven = some(numbers, n => n % 2 === 0) // true
|
|
167
|
+
*
|
|
168
|
+
* const users = [{ age: 15 }, { age: 16 }, { age: 17 }]
|
|
169
|
+
* const hasAdult = some(users, user => user.age >= 18) // false
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* @param iterable - The iterable to test
|
|
173
|
+
* @param predicate - Function that tests each element
|
|
174
|
+
* @returns true if any element satisfies the predicate, false otherwise
|
|
175
|
+
* @public
|
|
176
|
+
*/
|
|
177
|
+
export declare const some: <T>(iterable: Iterable<T>, predicate: (item: T) => boolean) => boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Converts an iterable to an array.
|
|
180
|
+
*
|
|
181
|
+
* This function creates a new array containing all elements from the iterable.
|
|
182
|
+
* It's equivalent to [...iterable] but more explicit in intent.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const set = new Set([1, 2, 3, 2, 1])
|
|
187
|
+
* const array = toArray(set) // [1, 2, 3]
|
|
188
|
+
*
|
|
189
|
+
* const mapped = map([1, 2, 3], n => n * 2)
|
|
190
|
+
* const result = toArray(mapped) // [2, 4, 6]
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @param iterable - The iterable to convert
|
|
194
|
+
* @returns A new array containing all elements from the iterable
|
|
195
|
+
* @public
|
|
196
|
+
*/
|
|
197
|
+
export declare const toArray: <T>(iterable: Iterable<T>) => T[];
|
|
198
|
+
/**
|
|
199
|
+
* Chains multiple iterables into a single iterable.
|
|
200
|
+
*
|
|
201
|
+
* This function creates a new iterable that yields all elements
|
|
202
|
+
* from the input iterables in sequence.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const first = [1, 2, 3]
|
|
207
|
+
* const second = [4, 5, 6]
|
|
208
|
+
* const third = [7, 8, 9]
|
|
209
|
+
* const chained = [...chain(first, second, third)] // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
210
|
+
*
|
|
211
|
+
* // Works with different iterable types
|
|
212
|
+
* const mixed = [...chain([1, 2], new Set([3, 4]), 'ab')] // [1, 2, 3, 4, 'a', 'b']
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @param iterables - The iterables to chain
|
|
216
|
+
* @returns An iterable that yields all elements in sequence
|
|
217
|
+
* @public
|
|
218
|
+
*/
|
|
219
|
+
export declare const chain: <T>(...iterables: Iterable<T>[]) => Iterable<T>;
|
package/iterator.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const i = (t, e) => {
|
|
2
|
+
if (e <= 0)
|
|
3
|
+
return [];
|
|
4
|
+
const r = [];
|
|
5
|
+
let o = 0;
|
|
6
|
+
for (const n of t) {
|
|
7
|
+
if (o >= e)
|
|
8
|
+
break;
|
|
9
|
+
r.push(n), o++;
|
|
10
|
+
}
|
|
11
|
+
return r;
|
|
12
|
+
}, f = (t, e) => ({
|
|
13
|
+
*[Symbol.iterator]() {
|
|
14
|
+
let r = 0;
|
|
15
|
+
for (const o of t) {
|
|
16
|
+
if (r < e) {
|
|
17
|
+
r++;
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
yield o;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}), s = (t, e) => ({
|
|
24
|
+
*[Symbol.iterator]() {
|
|
25
|
+
for (const r of t)
|
|
26
|
+
e(r) && (yield r);
|
|
27
|
+
}
|
|
28
|
+
}), c = (t, e) => ({
|
|
29
|
+
*[Symbol.iterator]() {
|
|
30
|
+
for (const r of t)
|
|
31
|
+
yield e(r);
|
|
32
|
+
}
|
|
33
|
+
}), u = (t, e, r) => {
|
|
34
|
+
let o = r;
|
|
35
|
+
for (const n of t)
|
|
36
|
+
o = e(o, n);
|
|
37
|
+
return o;
|
|
38
|
+
}, l = (t, e) => {
|
|
39
|
+
for (const r of t)
|
|
40
|
+
if (e(r))
|
|
41
|
+
return r;
|
|
42
|
+
}, a = (t, e) => {
|
|
43
|
+
for (const r of t)
|
|
44
|
+
if (!e(r))
|
|
45
|
+
return !1;
|
|
46
|
+
return !0;
|
|
47
|
+
}, m = (t, e) => {
|
|
48
|
+
for (const r of t)
|
|
49
|
+
if (e(r))
|
|
50
|
+
return !0;
|
|
51
|
+
return !1;
|
|
52
|
+
}, y = (t) => Array.from(t), d = (...t) => ({
|
|
53
|
+
*[Symbol.iterator]() {
|
|
54
|
+
for (const e of t)
|
|
55
|
+
yield* e;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
export {
|
|
59
|
+
d as chain,
|
|
60
|
+
a as every,
|
|
61
|
+
s as filter,
|
|
62
|
+
l as find,
|
|
63
|
+
c as map,
|
|
64
|
+
u as reduce,
|
|
65
|
+
f as skip,
|
|
66
|
+
m as some,
|
|
67
|
+
i as take,
|
|
68
|
+
y as toArray
|
|
69
|
+
};
|
package/json.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./result-CdwVhaAc.cjs"),e=r=>typeof r=="object"&&!Array.isArray(r)&&r!=null&&Object.values(r).every(t),i=r=>Array.isArray(r)&&r.every(t),t=r=>o(r)||e(r)||i(r),o=r=>typeof r=="string"||typeof r=="boolean"||typeof r=="number"||r==null,n=r=>{try{return s.Result.success(JSON.parse(r))}catch(c){return s.Result.failure(c)}};exports.isJSON=t;exports.isJSONArray=i;exports.isJSONObject=e;exports.isJSONPrimitive=o;exports.parseJSON=n;
|
package/json.d.ts
CHANGED
|
@@ -32,13 +32,51 @@ export type JSONValue = JSONPrimitive | JSONObject | JSONArray;
|
|
|
32
32
|
*/
|
|
33
33
|
export declare const isJSONObject: (value: JSONValue) => value is JSONObject;
|
|
34
34
|
/**
|
|
35
|
-
* Checks if the value is a JSON array.
|
|
35
|
+
* Checks if the value is a JSON array with recursive validation.
|
|
36
|
+
*
|
|
37
|
+
* This function provides more thorough validation than `Array.isArray()` by
|
|
38
|
+
* recursively checking that all elements in the array are valid JSON values.
|
|
39
|
+
* This is essential for ensuring data can be safely serialized to JSON.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* isJSONArray([1, 2, 3]) // true
|
|
44
|
+
* isJSONArray(['hello', 'world']) // true
|
|
45
|
+
* isJSONArray([1, 'hello', true, null]) // true
|
|
46
|
+
* isJSONArray([1, 2, undefined]) // false (undefined is not JSON-serializable)
|
|
47
|
+
* isJSONArray([1, 2, () => {}]) // false (functions are not JSON-serializable)
|
|
48
|
+
* ```
|
|
36
49
|
*
|
|
37
50
|
* @param value - The value to check.
|
|
38
|
-
* @returns `true` if the value is a JSON array; otherwise, `false`.
|
|
51
|
+
* @returns `true` if the value is a JSON array with all valid JSON elements; otherwise, `false`.
|
|
39
52
|
* @public
|
|
40
53
|
*/
|
|
41
54
|
export declare const isJSONArray: (value: JSONValue) => value is JSONArray;
|
|
55
|
+
/**
|
|
56
|
+
* Checks if the value is a valid JSON value with comprehensive validation.
|
|
57
|
+
*
|
|
58
|
+
* This function provides thorough validation that goes beyond basic type checking
|
|
59
|
+
* by recursively validating nested objects and arrays to ensure the entire
|
|
60
|
+
* structure can be safely serialized to JSON.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* isJSON('hello') // true
|
|
65
|
+
* isJSON(42) // true
|
|
66
|
+
* isJSON(true) // true
|
|
67
|
+
* isJSON(null) // true
|
|
68
|
+
* isJSON({ name: 'Alice', age: 30 }) // true
|
|
69
|
+
* isJSON([1, 2, { nested: true }]) // true
|
|
70
|
+
* isJSON(undefined) // false
|
|
71
|
+
* isJSON(() => {}) // false
|
|
72
|
+
* isJSON({ func: () => {} }) // false (contains non-JSON value)
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @param value - The value to check.
|
|
76
|
+
* @returns `true` if the value is a completely valid JSON value; otherwise, `false`.
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export declare const isJSON: (value: JSONValue) => value is JSONValue;
|
|
42
80
|
/**
|
|
43
81
|
* Checks if the value is a JSON primitive.
|
|
44
82
|
*
|
package/json.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { R as t } from "./result-CGd0jCdl.js";
|
|
2
|
-
const e = (r) => typeof r == "object" && !Array.isArray(r) && r != null,
|
|
2
|
+
const e = (r) => typeof r == "object" && !Array.isArray(r) && r != null && Object.values(r).every(s), c = (r) => Array.isArray(r) && r.every(s), s = (r) => i(r) || e(r) || c(r), i = (r) => typeof r == "string" || typeof r == "boolean" || typeof r == "number" || r == null, y = (r) => {
|
|
3
3
|
try {
|
|
4
4
|
return t.success(JSON.parse(r));
|
|
5
|
-
} catch (
|
|
6
|
-
return t.failure(
|
|
5
|
+
} catch (o) {
|
|
6
|
+
return t.failure(o);
|
|
7
7
|
}
|
|
8
8
|
};
|
|
9
9
|
export {
|
|
10
|
-
|
|
10
|
+
s as isJSON,
|
|
11
|
+
c as isJSONArray,
|
|
11
12
|
e as isJSONObject,
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
i as isJSONPrimitive,
|
|
14
|
+
y as parseJSON
|
|
14
15
|
};
|
package/map.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=e=>new Map(e),p=e=>{const o={};for(const[t,s]of e)o[t]=s;return o},m=(e,o)=>{const t=new Map;for(const[s,r]of e)o(r,s)&&t.set(s,r);return t},c=(e,o)=>{const t=new Map;for(const[s,r]of e)t.set(s,o(r,s));return t},u=(...e)=>{const o=new Map;for(const t of e)for(const[s,r]of t)o.set(s,r);return o},f=(e,o)=>{const t=new Map;for(const s of e){const r=o(s),n=t.get(r);n?n.push(s):t.set(r,[s])}return t},l=e=>e.size===0,y=e=>Array.from(e.keys()),i=e=>Array.from(e.values()),M=e=>Array.from(e.entries());exports.mapEntries=M;exports.mapFilter=m;exports.mapFromEntries=a;exports.mapGroupBy=f;exports.mapIsEmpty=l;exports.mapKeys=y;exports.mapMap=c;exports.mapMerge=u;exports.mapToObject=p;exports.mapValues=i;
|
package/map.d.ts
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for working with Map objects.
|
|
3
|
+
*
|
|
4
|
+
* This module provides functional utilities for Map manipulation that go beyond
|
|
5
|
+
* the native Map methods, offering type-safe operations for filtering, mapping,
|
|
6
|
+
* merging, and converting Maps.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Creates a Map from an array of key-value pairs.
|
|
12
|
+
*
|
|
13
|
+
* This function provides better type inference than the Map constructor
|
|
14
|
+
* when working with typed key-value pairs.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
|
|
19
|
+
* const map = mapFromEntries(entries)
|
|
20
|
+
* // Result: Map { 'a' => 1, 'b' => 2, 'c' => 3 }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param entries - Array of key-value pairs
|
|
24
|
+
* @returns A new Map containing the entries
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export declare const mapFromEntries: <K, V>(entries: readonly [K, V][]) => Map<K, V>;
|
|
28
|
+
/**
|
|
29
|
+
* Converts a Map with string keys to a plain object.
|
|
30
|
+
*
|
|
31
|
+
* This is useful for serialization or when you need to work with APIs
|
|
32
|
+
* that expect plain objects instead of Maps.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const map = new Map([['name', 'Alice'], ['age', '30']])
|
|
37
|
+
* const obj = mapToObject(map)
|
|
38
|
+
* // Result: { name: 'Alice', age: '30' }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @param map - The Map to convert
|
|
42
|
+
* @returns A plain object with the same key-value pairs
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare const mapToObject: <V>(map: Map<string, V>) => Record<string, V>;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new Map containing only entries that satisfy the predicate.
|
|
48
|
+
*
|
|
49
|
+
* This provides a functional approach to filtering Maps, similar to
|
|
50
|
+
* Array.prototype.filter but for Map objects.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const map = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4]])
|
|
55
|
+
* const evenValues = mapFilter(map, (value) => value % 2 === 0)
|
|
56
|
+
* // Result: Map { 'b' => 2, 'd' => 4 }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @param map - The Map to filter
|
|
60
|
+
* @param predicate - Function that tests each entry
|
|
61
|
+
* @returns A new Map with entries that satisfy the predicate
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export declare const mapFilter: <K, V>(map: Map<K, V>, predicate: (value: V, key: K) => boolean) => Map<K, V>;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new Map with values transformed by the mapper function.
|
|
67
|
+
*
|
|
68
|
+
* This provides a functional approach to transforming Map values,
|
|
69
|
+
* similar to Array.prototype.map but for Map objects.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const map = new Map([['a', 1], ['b', 2], ['c', 3]])
|
|
74
|
+
* const doubled = mapMap(map, (value) => value * 2)
|
|
75
|
+
* // Result: Map { 'a' => 2, 'b' => 4, 'c' => 6 }
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @param map - The Map to transform
|
|
79
|
+
* @param mapper - Function that transforms each value
|
|
80
|
+
* @returns A new Map with transformed values
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
export declare const mapMap: <K, V, U>(map: Map<K, V>, mapper: (value: V, key: K) => U) => Map<K, U>;
|
|
84
|
+
/**
|
|
85
|
+
* Merges multiple Maps into a single Map.
|
|
86
|
+
*
|
|
87
|
+
* Later Maps take precedence over earlier ones for duplicate keys.
|
|
88
|
+
* This is useful for combining configuration objects or merging data.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const map1 = new Map([['a', 1], ['b', 2]])
|
|
93
|
+
* const map2 = new Map([['b', 3], ['c', 4]])
|
|
94
|
+
* const merged = mapMerge(map1, map2)
|
|
95
|
+
* // Result: Map { 'a' => 1, 'b' => 3, 'c' => 4 }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @param maps - Maps to merge
|
|
99
|
+
* @returns A new Map containing all entries from input Maps
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
export declare const mapMerge: <K, V>(...maps: Map<K, V>[]) => Map<K, V>;
|
|
103
|
+
/**
|
|
104
|
+
* Groups an array of items by a key function into a Map.
|
|
105
|
+
*
|
|
106
|
+
* This is more flexible than object-based grouping as it supports
|
|
107
|
+
* any key type, not just strings and symbols.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const users = [
|
|
112
|
+
* { name: 'Alice', department: 'Engineering' },
|
|
113
|
+
* { name: 'Bob', department: 'Engineering' },
|
|
114
|
+
* { name: 'Carol', department: 'Marketing' }
|
|
115
|
+
* ]
|
|
116
|
+
* const byDepartment = mapGroupBy(users, user => user.department)
|
|
117
|
+
* // Result: Map {
|
|
118
|
+
* // 'Engineering' => [{ name: 'Alice', ... }, { name: 'Bob', ... }],
|
|
119
|
+
* // 'Marketing' => [{ name: 'Carol', ... }]
|
|
120
|
+
* // }
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @param array - Array of items to group
|
|
124
|
+
* @param keyFn - Function that extracts the grouping key from each item
|
|
125
|
+
* @returns A Map where keys are group identifiers and values are arrays of items
|
|
126
|
+
* @public
|
|
127
|
+
*/
|
|
128
|
+
export declare const mapGroupBy: <T, K>(array: readonly T[], keyFn: (item: T) => K) => Map<K, T[]>;
|
|
129
|
+
/**
|
|
130
|
+
* Checks if a Map is empty.
|
|
131
|
+
*
|
|
132
|
+
* This provides a semantic way to check for empty Maps,
|
|
133
|
+
* making code more readable than checking size === 0.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const emptyMap = new Map()
|
|
138
|
+
* const nonEmptyMap = new Map([['a', 1]])
|
|
139
|
+
*
|
|
140
|
+
* mapIsEmpty(emptyMap) // true
|
|
141
|
+
* mapIsEmpty(nonEmptyMap) // false
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* @param map - The Map to check
|
|
145
|
+
* @returns true if the Map has no entries, false otherwise
|
|
146
|
+
* @public
|
|
147
|
+
*/
|
|
148
|
+
export declare const mapIsEmpty: <K, V>(map: Map<K, V>) => boolean;
|
|
149
|
+
/**
|
|
150
|
+
* Gets all keys from a Map as an array.
|
|
151
|
+
*
|
|
152
|
+
* This provides a convenient way to get Map keys as an array
|
|
153
|
+
* for further processing with array methods.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const map = new Map([['a', 1], ['b', 2], ['c', 3]])
|
|
158
|
+
* const keys = mapKeys(map)
|
|
159
|
+
* // Result: ['a', 'b', 'c']
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @param map - The Map to extract keys from
|
|
163
|
+
* @returns An array of all keys in the Map
|
|
164
|
+
* @public
|
|
165
|
+
*/
|
|
166
|
+
export declare const mapKeys: <K, V>(map: Map<K, V>) => K[];
|
|
167
|
+
/**
|
|
168
|
+
* Gets all values from a Map as an array.
|
|
169
|
+
*
|
|
170
|
+
* This provides a convenient way to get Map values as an array
|
|
171
|
+
* for further processing with array methods.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const map = new Map([['a', 1], ['b', 2], ['c', 3]])
|
|
176
|
+
* const values = mapValues(map)
|
|
177
|
+
* // Result: [1, 2, 3]
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @param map - The Map to extract values from
|
|
181
|
+
* @returns An array of all values in the Map
|
|
182
|
+
* @public
|
|
183
|
+
*/
|
|
184
|
+
export declare const mapValues: <K, V>(map: Map<K, V>) => V[];
|
|
185
|
+
/**
|
|
186
|
+
* Gets all entries from a Map as an array of key-value pairs.
|
|
187
|
+
*
|
|
188
|
+
* This provides a convenient way to get Map entries as an array
|
|
189
|
+
* for further processing or serialization.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* const map = new Map([['a', 1], ['b', 2], ['c', 3]])
|
|
194
|
+
* const entries = mapEntries(map)
|
|
195
|
+
* // Result: [['a', 1], ['b', 2], ['c', 3]]
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* @param map - The Map to extract entries from
|
|
199
|
+
* @returns An array of all key-value pairs in the Map
|
|
200
|
+
* @public
|
|
201
|
+
*/
|
|
202
|
+
export declare const mapEntries: <K, V>(map: Map<K, V>) => [K, V][];
|
package/map.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const a = (e) => new Map(e), c = (e) => {
|
|
2
|
+
const o = {};
|
|
3
|
+
for (const [t, s] of e)
|
|
4
|
+
o[t] = s;
|
|
5
|
+
return o;
|
|
6
|
+
}, p = (e, o) => {
|
|
7
|
+
const t = /* @__PURE__ */ new Map();
|
|
8
|
+
for (const [s, r] of e)
|
|
9
|
+
o(r, s) && t.set(s, r);
|
|
10
|
+
return t;
|
|
11
|
+
}, u = (e, o) => {
|
|
12
|
+
const t = /* @__PURE__ */ new Map();
|
|
13
|
+
for (const [s, r] of e)
|
|
14
|
+
t.set(s, o(r, s));
|
|
15
|
+
return t;
|
|
16
|
+
}, f = (...e) => {
|
|
17
|
+
const o = /* @__PURE__ */ new Map();
|
|
18
|
+
for (const t of e)
|
|
19
|
+
for (const [s, r] of t)
|
|
20
|
+
o.set(s, r);
|
|
21
|
+
return o;
|
|
22
|
+
}, m = (e, o) => {
|
|
23
|
+
const t = /* @__PURE__ */ new Map();
|
|
24
|
+
for (const s of e) {
|
|
25
|
+
const r = o(s), n = t.get(r);
|
|
26
|
+
n ? n.push(s) : t.set(r, [s]);
|
|
27
|
+
}
|
|
28
|
+
return t;
|
|
29
|
+
}, l = (e) => e.size === 0, y = (e) => Array.from(e.keys()), i = (e) => Array.from(e.values()), M = (e) => Array.from(e.entries());
|
|
30
|
+
export {
|
|
31
|
+
M as mapEntries,
|
|
32
|
+
p as mapFilter,
|
|
33
|
+
a as mapFromEntries,
|
|
34
|
+
m as mapGroupBy,
|
|
35
|
+
l as mapIsEmpty,
|
|
36
|
+
y as mapKeys,
|
|
37
|
+
u as mapMap,
|
|
38
|
+
f as mapMerge,
|
|
39
|
+
c as mapToObject,
|
|
40
|
+
i as mapValues
|
|
41
|
+
};
|
package/number.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("./string.cjs"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("./string.cjs"),l=1e-9,s=(e,t,r=360)=>{let n=(t-e)%r;return n<0&&(n+=r),n>r/2&&(n-=r),n},f=(e,t)=>{const r=Math.pow(10,t);return Math.ceil(e*r)/r},a=(e,t,r)=>Math.min(Math.max(e,t),r),u=(e,t,r)=>Math.trunc(a(e,t,r)),g=(e,t)=>a(e,-t,t),M=(e,t)=>e<t?-1:e>t?1:0,h=(e,t)=>{const r=Math.pow(10,t);return Math.floor(e*r)/r},A=(e,t=0)=>p.lpad(e.toString(16),"0",t),i=(e,t,r)=>(t-e)*r+e,N=(e,t,r,n=360)=>o(i(e,e+s(e,t,n),r),n),c=(e,t,r=360)=>{let n=(t-e)%r;return n<0&&(n+=r),n>r/2&&(n-=r),n},d=(e,t,r,n=360)=>o(i(e,e+c(e,t,n),r),n),w=(e,t,r,n=360)=>(e=o(e,n),t=o(t,n),t<e&&(t+=n),o(i(e,t,r),n)),m=(e,t,r,n=360)=>(e=o(e,n),t=o(t,n),t>e&&(t-=n),o(i(e,t,r),n)),C=(e,t,r=l)=>isFinite(e)?isFinite(t)?Math.abs(e-t)<=r:!1:isNaN(e)&&isNaN(t)?!0:isNaN(e)||isNaN(t)||isFinite(t)?!1:e>0==t>0,S=(e,t,r=360,n=l)=>Math.abs(s(e,t,r))<=n,T=(e,t=l)=>Math.abs(e)<=t,E=(e,t)=>Math.pow(e,1/t),W=(e,t)=>{const r=Math.pow(10,t);return Math.round(e*r)/r},q=(e,t,r)=>{const n=r-t+1;return e<t&&(e+=n*((t-e)/n+1)),t+(e-t)%n},o=(e,t)=>(e=e%t,e<0&&(e+=t),e);exports.EPSILON=l;exports.angleDifference=s;exports.ceilTo=f;exports.clamp=a;exports.clampInt=u;exports.clampSym=g;exports.compareNumbers=M;exports.floorTo=h;exports.interpolate=i;exports.interpolateAngle=N;exports.interpolateAngleCCW=m;exports.interpolateAngleCW=w;exports.interpolateWidestAngle=d;exports.nearEqual=C;exports.nearEqualAngles=S;exports.nearZero=T;exports.root=E;exports.roundTo=W;exports.toHex=A;exports.widestAngleDifference=c;exports.wrap=q;exports.wrapCircular=o;
|