@gesslar/toolkit 1.5.0 → 1.8.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/package.json +5 -5
- package/src/browser/lib/Data.js +0 -7
- package/src/browser/lib/Disposer.js +1 -24
- package/src/browser/lib/Util.js +1 -11
- package/src/index.js +1 -0
- package/src/lib/DirectoryObject.js +25 -0
- package/src/lib/Notify.js +87 -0
- package/src/types/browser/lib/Data.d.ts.map +1 -1
- package/src/types/browser/lib/Disposer.d.ts +0 -6
- package/src/types/browser/lib/Disposer.d.ts.map +1 -1
- package/src/types/browser/lib/Util.d.ts +0 -1
- package/src/types/browser/lib/Util.d.ts.map +1 -1
- package/src/types/index.d.ts +1 -0
- package/src/types/lib/DirectoryObject.d.ts +14 -0
- package/src/types/lib/DirectoryObject.d.ts.map +1 -1
- package/src/types/lib/Notify.d.ts +55 -0
- package/src/types/lib/Notify.d.ts.map +1 -0
- package/src/browser/lib/Hook.js +0 -82
- package/src/browser/lib/test.js +0 -25
- package/src/browser/lib/test2.js +0 -46
- package/src/types/Cache.d.ts +0 -30
- package/src/types/Collection.d.ts +0 -321
- package/src/types/Contract.d.ts +0 -162
- package/src/types/Data.d.ts +0 -175
- package/src/types/DirectoryObject.d.ts +0 -135
- package/src/types/FS.d.ts +0 -40
- package/src/types/FileObject.d.ts +0 -388
- package/src/types/Glog.d.ts +0 -345
- package/src/types/Sass.d.ts +0 -24
- package/src/types/Schemer.d.ts +0 -179
- package/src/types/Tantrum.d.ts +0 -81
- package/src/types/Term.d.ts +0 -16
- package/src/types/Terms.d.ts +0 -145
- package/src/types/Type.d.ts +0 -26
- package/src/types/Util.d.ts +0 -275
- package/src/types/Valid.d.ts +0 -13
- package/src/types/browser/lib/Disposable.d.ts +0 -35
- package/src/types/browser/lib/Disposable.d.ts.map +0 -10
- package/src/types/browser/lib/Hook.d.ts +0 -11
- package/src/types/browser/lib/Hook.d.ts.map +0 -1
- package/src/types/browser/lib/test.d.ts +0 -2
- package/src/types/browser/lib/test.d.ts.map +0 -1
- package/src/types/browser/lib/test2.d.ts +0 -2
- package/src/types/browser/lib/test2.d.ts.map +0 -1
- package/src/types/lib/Chide.d.ts +0 -37
- package/src/types/lib/Chide.d.ts.map +0 -1
- package/src/types/lib/Collection.d.ts +0 -246
- package/src/types/lib/Collection.d.ts.map +0 -1
- package/src/types/lib/Data.d.ts +0 -206
- package/src/types/lib/Data.d.ts.map +0 -1
- package/src/types/lib/Disposable.d.ts +0 -33
- package/src/types/lib/Disposable.d.ts.map +0 -10
- package/src/types/lib/TypeSpec.d.ts +0 -92
- package/src/types/lib/TypeSpec.d.ts.map +0 -1
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
// Implementation: ../lib/Collection.js
|
|
2
|
-
// Type definitions for Collection utilities
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Collection utility functions for evaluating and manipulating arrays, objects, sets, and maps.
|
|
6
|
-
* Provides functional programming patterns for collection processing with consistent error handling.
|
|
7
|
-
*/
|
|
8
|
-
export default class Collection {
|
|
9
|
-
/**
|
|
10
|
-
* Evaluates an array with a predicate function, returning the first truthy result.
|
|
11
|
-
*
|
|
12
|
-
* @param collection - The array to evaluate
|
|
13
|
-
* @param predicate - Function called for each element: (element, index, array) => result
|
|
14
|
-
* @param forward - Whether to iterate forward (true) or backward (false). Default: true
|
|
15
|
-
* @returns The first truthy result from the predicate, or undefined if none found
|
|
16
|
-
*
|
|
17
|
-
* @throws {Sass} If collection is not an Array or predicate is not a Function
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```typescript
|
|
21
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
22
|
-
*
|
|
23
|
-
* const numbers = [1, 2, 3, 4, 5]
|
|
24
|
-
* const result = Collection.evalArray(numbers, (n, i) => n > 3 ? n * 2 : null)
|
|
25
|
-
* console.log(result) // 8 (first element > 3, doubled)
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
static evalArray<T, R>(
|
|
29
|
-
collection: T[],
|
|
30
|
-
predicate: (element: T, index: number, array: T[]) => R | null | undefined,
|
|
31
|
-
forward?: boolean
|
|
32
|
-
): R | undefined
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Evaluates an object with a predicate function, returning the first truthy result.
|
|
36
|
-
*
|
|
37
|
-
* @param collection - The object to evaluate
|
|
38
|
-
* @param predicate - Function called for each property: (value, key, object) => result
|
|
39
|
-
* @returns The first truthy result from the predicate, or undefined if none found
|
|
40
|
-
*
|
|
41
|
-
* @throws {Sass} If collection is not an Object or predicate is not a Function
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```typescript
|
|
45
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
46
|
-
*
|
|
47
|
-
* const obj = {a: 1, b: 2, c: 3}
|
|
48
|
-
* const result = Collection.evalObject(obj, (value, key) => value > 2 ? `${key}:${value}` : null)
|
|
49
|
-
* console.log(result) // "c:3"
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
static evalObject<T, R>(
|
|
53
|
-
collection: Record<string, T>,
|
|
54
|
-
predicate: (value: T, key: string, object: Record<string, T>) => R | null | undefined
|
|
55
|
-
): R | undefined
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Evaluates a Set with a predicate function, returning the first truthy result.
|
|
59
|
-
*
|
|
60
|
-
* @param collection - The Set to evaluate
|
|
61
|
-
* @param predicate - Function called for each element: (element, set) => result
|
|
62
|
-
* @returns The first truthy result from the predicate, or undefined if none found
|
|
63
|
-
*
|
|
64
|
-
* @throws {Sass} If collection is not a Set or predicate is not a Function
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```typescript
|
|
68
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
69
|
-
*
|
|
70
|
-
* const set = new Set([1, 2, 3, 4, 5])
|
|
71
|
-
* const result = Collection.evalSet(set, (n, s) => n > 3 ? n * 2 : null)
|
|
72
|
-
* console.log(result) // 8
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
static evalSet<T, R>(
|
|
76
|
-
collection: Set<T>,
|
|
77
|
-
predicate: (element: T, set: Set<T>) => R | null | undefined
|
|
78
|
-
): R | undefined
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Evaluates a Map with a predicate function, returning the first truthy result.
|
|
82
|
-
*
|
|
83
|
-
* @param collection - The Map to evaluate
|
|
84
|
-
* @param predicate - Function called for each entry: (value, key, map) => result
|
|
85
|
-
* @param forward - Whether to iterate forward (true) or backward (false). Default: true
|
|
86
|
-
* @returns The first truthy result from the predicate, or undefined if none found
|
|
87
|
-
*
|
|
88
|
-
* @throws {Sass} If collection is not a Map or predicate is not a Function
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```typescript
|
|
92
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
93
|
-
*
|
|
94
|
-
* const map = new Map([['a', 1], ['b', 2], ['c', 3]])
|
|
95
|
-
* const result = Collection.evalMap(map, (value, key) => value > 2 ? `${key}:${value}` : null)
|
|
96
|
-
* console.log(result) // "c:3"
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
static evalMap<K, V, R>(
|
|
100
|
-
collection: Map<K, V>,
|
|
101
|
-
predicate: (value: V, key: K, map: Map<K, V>) => R | null | undefined,
|
|
102
|
-
forward?: boolean
|
|
103
|
-
): R | undefined
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Zips two arrays together into an array of pairs.
|
|
107
|
-
*
|
|
108
|
-
* @param array1 - The first array
|
|
109
|
-
* @param array2 - The second array
|
|
110
|
-
* @returns Array of [element1, element2] pairs, length of shorter input array
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```typescript
|
|
114
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
115
|
-
*
|
|
116
|
-
* const result = Collection.zip([1, 2, 3], ['a', 'b', 'c'])
|
|
117
|
-
* console.log(result) // [[1, 'a'], [2, 'b'], [3, 'c']]
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
static zip<T, U>(array1: T[], array2: U[]): [T, U][]
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Unzips an array of arrays into separate arrays.
|
|
124
|
-
*
|
|
125
|
-
* @param array - Array of arrays to unzip
|
|
126
|
-
* @returns Array of separate arrays, one for each position
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* ```typescript
|
|
130
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
131
|
-
*
|
|
132
|
-
* const zipped = [[1, 'a'], [2, 'b'], [3, 'c']]
|
|
133
|
-
* const result = Collection.unzip(zipped)
|
|
134
|
-
* console.log(result) // [[1, 2, 3], ['a', 'b', 'c']]
|
|
135
|
-
* ```
|
|
136
|
-
*/
|
|
137
|
-
static unzip<T>(array: T[][]): T[][]
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Maps an array through an async function, executing operations sequentially.
|
|
141
|
-
*
|
|
142
|
-
* Unlike Promise.all(array.map(fn)), this executes each async operation
|
|
143
|
-
* one at a time, maintaining order and preventing overwhelming external resources.
|
|
144
|
-
*
|
|
145
|
-
* @param array - The array to map over
|
|
146
|
-
* @param asyncFn - Async function called for each element: (element) => Promise<result>
|
|
147
|
-
* @returns Promise resolving to array of mapped results
|
|
148
|
-
*
|
|
149
|
-
* @throws {Sass} If array is not an Array or asyncFn is not a Function
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
152
|
-
* ```typescript
|
|
153
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
154
|
-
*
|
|
155
|
-
* // Sequential API calls (won't overwhelm server)
|
|
156
|
-
* const urls = ['url1', 'url2', 'url3']
|
|
157
|
-
* const responses = await Collection.asyncMap(urls, async (url) => {
|
|
158
|
-
* return await fetch(url).then(r => r.json())
|
|
159
|
-
* })
|
|
160
|
-
* console.log(responses) // [data1, data2, data3]
|
|
161
|
-
*
|
|
162
|
-
* // Works with sync functions too
|
|
163
|
-
* const numbers = [1, 2, 3]
|
|
164
|
-
* const doubled = await Collection.asyncMap(numbers, async (n) => n * 2)
|
|
165
|
-
* console.log(doubled) // [2, 4, 6]
|
|
166
|
-
* ```
|
|
167
|
-
*/
|
|
168
|
-
static asyncMap<T, R>(array: T[], asyncFn: (element: T) => Promise<R> | R): Promise<R[]>
|
|
169
|
-
|
|
170
|
-
/** Check if all elements in an array are of a specified type or all the same type */
|
|
171
|
-
static isArrayUniform(arr: Array<unknown>, type?: string): boolean
|
|
172
|
-
|
|
173
|
-
/** Remove duplicate elements from an array, returning a new array with unique values */
|
|
174
|
-
static isArrayUnique<T>(arr: Array<T>): Array<T>
|
|
175
|
-
|
|
176
|
-
/** Get the intersection of two arrays */
|
|
177
|
-
static intersection<T>(arr1: Array<T>, arr2: Array<T>): Array<T>
|
|
178
|
-
|
|
179
|
-
/** Check if two arrays have any elements in common */
|
|
180
|
-
static intersects<T>(arr1: Array<T>, arr2: Array<T>): boolean
|
|
181
|
-
|
|
182
|
-
/** Pad an array to a specified length */
|
|
183
|
-
static arrayPad<T>(arr: Array<T>, length: number, value: T, position?: number): Array<T>
|
|
184
|
-
|
|
185
|
-
/** Filter an array asynchronously */
|
|
186
|
-
static asyncFilter<T>(arr: Array<T>, predicate: (item: T) => Promise<boolean>): Promise<Array<T>>
|
|
187
|
-
|
|
188
|
-
/** Clone an object */
|
|
189
|
-
static cloneObject<T extends Record<string, any>>(obj: T, freeze?: boolean): T
|
|
190
|
-
|
|
191
|
-
/** Check if an object is empty */
|
|
192
|
-
static isObjectEmpty(obj: Record<string, any>): boolean
|
|
193
|
-
|
|
194
|
-
/** Ensure a nested path of objects exists */
|
|
195
|
-
static assureObjectPath(obj: Record<string, any>, keys: Array<string>): Record<string, any>
|
|
196
|
-
|
|
197
|
-
/** Set a value in a nested object structure */
|
|
198
|
-
static setNestedValue(obj: Record<string, any>, keys: Array<string>, value: unknown): void
|
|
199
|
-
|
|
200
|
-
/** Deeply merge objects */
|
|
201
|
-
static mergeObject<T extends Record<string, any>>(...sources: Array<T>): T
|
|
202
|
-
|
|
203
|
-
/** Recursively freeze an object */
|
|
204
|
-
static deepFreezeObject<T>(obj: T): T
|
|
205
|
-
|
|
206
|
-
/** Map an object using a transformer function */
|
|
207
|
-
static mapObject<T extends Record<string, any>, R>(
|
|
208
|
-
original: T,
|
|
209
|
-
transformer: (key: string, value: any) => R | Promise<R>,
|
|
210
|
-
mutate?: boolean
|
|
211
|
-
): Promise<Record<string, R>>
|
|
212
|
-
|
|
213
|
-
/** Allocate an object from a source array and spec */
|
|
214
|
-
static allocateObject(
|
|
215
|
-
source: Array<unknown>,
|
|
216
|
-
spec:
|
|
217
|
-
| Array<unknown>
|
|
218
|
-
| ((source: Array<unknown>) => Promise<Array<unknown>> | Array<unknown>)
|
|
219
|
-
): Promise<Record<string, unknown>>
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Flattens one level of an array of plain objects, transposing values so each
|
|
223
|
-
* key maps to the collected values from every object.
|
|
224
|
-
*
|
|
225
|
-
* Accepts either a simple array of objects or an array that mixes objects and
|
|
226
|
-
* nested object arrays (one level deep). Nested arrays are flattened before
|
|
227
|
-
* transposition.
|
|
228
|
-
*
|
|
229
|
-
* @param input - Array of plain objects (optionally containing nested arrays)
|
|
230
|
-
* @returns Object with keys mapped to arrays of values from all input objects
|
|
231
|
-
*
|
|
232
|
-
* @throws {Sass} If input is not an Array or if any element is not a plain object after flattening
|
|
233
|
-
*
|
|
234
|
-
* @example
|
|
235
|
-
* ```typescript
|
|
236
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
237
|
-
*
|
|
238
|
-
* const objects = [
|
|
239
|
-
* [{ name: 'Alice', age: 25 }],
|
|
240
|
-
* { name: 'Bob', age: 30 }
|
|
241
|
-
* ]
|
|
242
|
-
*
|
|
243
|
-
* const result = Collection.flattenObjectArray(objects)
|
|
244
|
-
* // result: { name: ['Alice', 'Bob'], age: [25, 30] }
|
|
245
|
-
* ```
|
|
246
|
-
*/
|
|
247
|
-
static flattenObjectArray(
|
|
248
|
-
input: Array<Record<string, unknown> | Array<Record<string, unknown>>>
|
|
249
|
-
): Record<string, Array<unknown>>
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Transposes an array of plain objects into an object of arrays, keyed by the
|
|
253
|
-
* original object keys.
|
|
254
|
-
*
|
|
255
|
-
* @param objects - Array of plain objects to transpose
|
|
256
|
-
* @returns Object with keys mapped to arrays of values from the input objects
|
|
257
|
-
*
|
|
258
|
-
* @throws {Sass} If objects is not an Array or if any element is not a plain object
|
|
259
|
-
*/
|
|
260
|
-
static transposeObjects(objects: Array<Record<string, unknown>>): Record<string, Array<unknown>>
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Trims falsy values from both ends of an array.
|
|
264
|
-
*
|
|
265
|
-
* @param arr - The array to trim
|
|
266
|
-
* @param except - Array of values to exclude from trimming (default: [])
|
|
267
|
-
* @returns The trimmed array (modified in place)
|
|
268
|
-
*
|
|
269
|
-
* @throws {Sass} If arr or except is not an Array
|
|
270
|
-
*
|
|
271
|
-
* @example
|
|
272
|
-
* ```typescript
|
|
273
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
274
|
-
*
|
|
275
|
-
* const arr = [null, 0, 1, 2, "", undefined]
|
|
276
|
-
* Collection.trimArray(arr)
|
|
277
|
-
* console.log(arr) // [1, 2]
|
|
278
|
-
* ```
|
|
279
|
-
*/
|
|
280
|
-
static trimArray<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Trims falsy values from the right end of an array.
|
|
284
|
-
*
|
|
285
|
-
* @param arr - The array to trim
|
|
286
|
-
* @param except - Array of values to exclude from trimming (default: [])
|
|
287
|
-
* @returns The trimmed array (modified in place)
|
|
288
|
-
*
|
|
289
|
-
* @throws {Sass} If arr or except is not an Array
|
|
290
|
-
*
|
|
291
|
-
* @example
|
|
292
|
-
* ```typescript
|
|
293
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
294
|
-
*
|
|
295
|
-
* const arr = [1, "", undefined]
|
|
296
|
-
* Collection.trimArrayRight(arr)
|
|
297
|
-
* console.log(arr) // [1]
|
|
298
|
-
* ```
|
|
299
|
-
*/
|
|
300
|
-
static trimArrayRight<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Trims falsy values from the left end of an array.
|
|
304
|
-
*
|
|
305
|
-
* @param arr - The array to trim
|
|
306
|
-
* @param except - Array of values to exclude from trimming (default: [])
|
|
307
|
-
* @returns The trimmed array (modified in place)
|
|
308
|
-
*
|
|
309
|
-
* @throws {Sass} If arr or except is not an Array
|
|
310
|
-
*
|
|
311
|
-
* @example
|
|
312
|
-
* ```typescript
|
|
313
|
-
* import { Collection } from '@gesslar/toolkit'
|
|
314
|
-
*
|
|
315
|
-
* const arr = [null, undefined, "value"]
|
|
316
|
-
* Collection.trimArrayLeft(arr)
|
|
317
|
-
* console.log(arr) // ["value"]
|
|
318
|
-
* ```
|
|
319
|
-
*/
|
|
320
|
-
static trimArrayLeft<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
321
|
-
}
|
package/src/types/Contract.d.ts
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
// Implementation: ../lib/Contract.js
|
|
2
|
-
|
|
3
|
-
import type { ValidateFunction } from 'ajv'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Debug function type for Contract operations
|
|
7
|
-
*/
|
|
8
|
-
export type DebugFunction = (message: string, level?: number, ...args: unknown[]) => void
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Contract represents a successful negotiation between Terms.
|
|
12
|
-
* It handles validation and compatibility checking between what
|
|
13
|
-
* one action provides and what another accepts.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* // Two-party contract between provider and consumer
|
|
18
|
-
* const provider = new Terms(providerDefinition)
|
|
19
|
-
* const consumer = new Terms(consumerDefinition)
|
|
20
|
-
* const contract = new Contract(provider, consumer, { debug: console.log })
|
|
21
|
-
*
|
|
22
|
-
* // Validate data against the contract
|
|
23
|
-
* const isValid = contract.validate(someData)
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* // Single-party contract from terms definition
|
|
29
|
-
* const contract = Contract.fromTerms("parser", {
|
|
30
|
-
* provides: {
|
|
31
|
-
* type: "object",
|
|
32
|
-
* properties: {
|
|
33
|
-
* name: { type: "string" },
|
|
34
|
-
* age: { type: "number" }
|
|
35
|
-
* }
|
|
36
|
-
* }
|
|
37
|
-
* })
|
|
38
|
-
*
|
|
39
|
-
* contract.validate({ name: "John", age: 30 }) // true
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
declare class Contract {
|
|
43
|
-
/**
|
|
44
|
-
* Creates a contract by negotiating between provider and consumer terms
|
|
45
|
-
*
|
|
46
|
-
* @param providerTerms - What the provider offers
|
|
47
|
-
* @param consumerTerms - What the consumer expects
|
|
48
|
-
* @param options - Configuration options
|
|
49
|
-
* @param options.debug - Debug function for logging negotiation details
|
|
50
|
-
*
|
|
51
|
-
* @throws {Sass} If contract negotiation fails due to incompatible terms
|
|
52
|
-
*/
|
|
53
|
-
constructor(
|
|
54
|
-
providerTerms: import('./Terms.js').default | null,
|
|
55
|
-
consumerTerms: import('./Terms.js').default | null,
|
|
56
|
-
options?: { debug?: DebugFunction }
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Creates a contract from terms with schema validation
|
|
61
|
-
*
|
|
62
|
-
* @param name - Contract identifier for error reporting
|
|
63
|
-
* @param termsDefinition - The terms definition object
|
|
64
|
-
* @param validator - Optional AJV schema validator function with .errors property
|
|
65
|
-
* @param debug - Debug function for logging validation details
|
|
66
|
-
* @returns New contract instance ready for data validation
|
|
67
|
-
*
|
|
68
|
-
* @throws {Sass} If terms definition is invalid according to the validator
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```typescript
|
|
72
|
-
* const contract = Contract.fromTerms("user-parser", {
|
|
73
|
-
* provides: {
|
|
74
|
-
* type: "object",
|
|
75
|
-
* properties: {
|
|
76
|
-
* id: { type: "string" },
|
|
77
|
-
* name: { type: "string" }
|
|
78
|
-
* },
|
|
79
|
-
* required: ["id", "name"]
|
|
80
|
-
* }
|
|
81
|
-
* })
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
static fromTerms(
|
|
85
|
-
name: string,
|
|
86
|
-
termsDefinition: object,
|
|
87
|
-
validator?: ValidateFunction | null,
|
|
88
|
-
debug?: DebugFunction
|
|
89
|
-
): Contract
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Validates data against this contract's schema
|
|
93
|
-
*
|
|
94
|
-
* @param data - Data object to validate against the contract
|
|
95
|
-
* @returns True if validation passes
|
|
96
|
-
*
|
|
97
|
-
* @throws {Sass} If validation fails with detailed error messages
|
|
98
|
-
* @throws {Sass} If contract has not been successfully negotiated
|
|
99
|
-
* @throws {Sass} If no validator is available for this contract
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* ```typescript
|
|
103
|
-
* try {
|
|
104
|
-
* contract.validate({ id: "123", name: "John" })
|
|
105
|
-
* console.log("Data is valid!")
|
|
106
|
-
* } catch (error) {
|
|
107
|
-
* console.error("Validation failed:", error.message)
|
|
108
|
-
* }
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
validate(data: object): boolean
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Check if contract negotiation was successful
|
|
115
|
-
*
|
|
116
|
-
* @returns True if the contract has been successfully negotiated
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* ```typescript
|
|
120
|
-
* if (contract.isNegotiated) {
|
|
121
|
-
* contract.validate(data)
|
|
122
|
-
* } else {
|
|
123
|
-
* console.error("Contract negotiation failed")
|
|
124
|
-
* }
|
|
125
|
-
* ```
|
|
126
|
-
*/
|
|
127
|
-
get isNegotiated(): boolean
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Get the provider terms (if any)
|
|
131
|
-
*
|
|
132
|
-
* @returns Provider terms or null for single-party contracts
|
|
133
|
-
*/
|
|
134
|
-
get providerTerms(): import('./Terms.js').default | null
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Get the consumer terms (if any)
|
|
138
|
-
*
|
|
139
|
-
* @returns Consumer terms or null for single-party contracts
|
|
140
|
-
*/
|
|
141
|
-
get consumerTerms(): import('./Terms.js').default | null
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Get the contract validator function
|
|
145
|
-
*
|
|
146
|
-
* @returns The AJV validator function used by this contract, or null if none available
|
|
147
|
-
*
|
|
148
|
-
* @example
|
|
149
|
-
* ```typescript
|
|
150
|
-
* const validator = contract.validator
|
|
151
|
-
* if (validator) {
|
|
152
|
-
* const isValid = validator(someData)
|
|
153
|
-
* if (!isValid) {
|
|
154
|
-
* console.log("Validation errors:", validator.errors)
|
|
155
|
-
* }
|
|
156
|
-
* }
|
|
157
|
-
* ```
|
|
158
|
-
*/
|
|
159
|
-
get validator(): ((data: object) => boolean) | null
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export default Contract
|
package/src/types/Data.d.ts
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
// Implementation: ../lib/Data.js
|
|
2
|
-
// Type definitions for Data utilities
|
|
3
|
-
|
|
4
|
-
import Type from './Type.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Data utility functions for type checking, object manipulation, and array operations.
|
|
8
|
-
*/
|
|
9
|
-
export default class Data {
|
|
10
|
-
/** Array of JavaScript primitive type names */
|
|
11
|
-
static readonly primitives: ReadonlyArray<string>
|
|
12
|
-
|
|
13
|
-
/** Array of JavaScript constructor names for built-in objects */
|
|
14
|
-
static readonly constructors: ReadonlyArray<string>
|
|
15
|
-
|
|
16
|
-
/** Combined array of all supported data types */
|
|
17
|
-
static readonly dataTypes: ReadonlyArray<string>
|
|
18
|
-
|
|
19
|
-
/** Array of type names that can be checked for emptiness */
|
|
20
|
-
static readonly emptyableTypes: ReadonlyArray<string>
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Append a suffix string to the end of a string if it doesn't already end with it.
|
|
24
|
-
*
|
|
25
|
-
* Useful for ensuring strings have consistent endings like file extensions,
|
|
26
|
-
* URL paths, or punctuation. Performs case-sensitive comparison and only appends
|
|
27
|
-
* if the string doesn't already end with the specified suffix.
|
|
28
|
-
*
|
|
29
|
-
* @param string - The base string to potentially append to. Can be empty string.
|
|
30
|
-
* @param append - The suffix to append if not already present. Cannot be empty.
|
|
31
|
-
* @returns The string with the suffix appended, or the original string if suffix already present
|
|
32
|
-
*
|
|
33
|
-
* @throws {Error} When append parameter is empty or undefined
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```typescript
|
|
37
|
-
* import { Data } from '@gesslar/toolkit'
|
|
38
|
-
*
|
|
39
|
-
* // Basic usage with file extensions
|
|
40
|
-
* const filename = Data.appendString('config', '.json')
|
|
41
|
-
* console.log(filename) // 'config.json'
|
|
42
|
-
*
|
|
43
|
-
* // No double-appending
|
|
44
|
-
* const alreadyHasExt = Data.appendString('package.json', '.json')
|
|
45
|
-
* console.log(alreadyHasExt) // 'package.json' (unchanged)
|
|
46
|
-
*
|
|
47
|
-
* // URL path handling
|
|
48
|
-
* const apiPath = Data.appendString('/api/users', '/')
|
|
49
|
-
* console.log(apiPath) // '/api/users/'
|
|
50
|
-
*
|
|
51
|
-
* // Works with empty strings
|
|
52
|
-
* const fromEmpty = Data.appendString('', '.txt')
|
|
53
|
-
* console.log(fromEmpty) // '.txt'
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
static appendString(string: string, append: string): string
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Prepend a prefix string to the beginning of a string if it doesn't already start with it.
|
|
60
|
-
*
|
|
61
|
-
* Useful for ensuring strings have consistent beginnings like protocol prefixes,
|
|
62
|
-
* path separators, or formatting markers. Performs case-sensitive comparison and
|
|
63
|
-
* only prepends if the string doesn't already start with the specified prefix.
|
|
64
|
-
*
|
|
65
|
-
* @param string - The base string to potentially prepend to. Can be empty string.
|
|
66
|
-
* @param prepend - The prefix to prepend if not already present. Cannot be empty.
|
|
67
|
-
* @returns The string with the prefix prepended, or the original string if prefix already present
|
|
68
|
-
*
|
|
69
|
-
* @throws {Error} When prepend parameter is empty or undefined
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```typescript
|
|
73
|
-
* import { Data } from '@gesslar/toolkit'
|
|
74
|
-
*
|
|
75
|
-
* // Basic usage with protocols
|
|
76
|
-
* const url = Data.prependString('example.com', 'https://')
|
|
77
|
-
* console.log(url) // 'https://example.com'
|
|
78
|
-
*
|
|
79
|
-
* // No double-prepending
|
|
80
|
-
* const alreadyHasProtocol = Data.prependString('https://api.example.com', 'https://')
|
|
81
|
-
* console.log(alreadyHasProtocol) // 'https://api.example.com' (unchanged)
|
|
82
|
-
*
|
|
83
|
-
* // File path handling
|
|
84
|
-
* const absolutePath = Data.prependString('home/user/docs', '/')
|
|
85
|
-
* console.log(absolutePath) // '/home/user/docs'
|
|
86
|
-
*
|
|
87
|
-
* // CSS class prefixing
|
|
88
|
-
* const className = Data.prependString('button-primary', 'css-')
|
|
89
|
-
* console.log(className) // 'css-button-primary'
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
static prependString(string: string, prepend: string): string
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
/** Create a type spec from a string */
|
|
96
|
-
static newTypeSpec(string: string, options?: any): Type
|
|
97
|
-
|
|
98
|
-
/** Check if a value is of a specified type */
|
|
99
|
-
static isType(value: unknown, type: string | Type, options?: { allowEmpty?: boolean }): boolean
|
|
100
|
-
|
|
101
|
-
/** Check if a type is valid */
|
|
102
|
-
static isValidType(type: string): boolean
|
|
103
|
-
|
|
104
|
-
/** Check if a value is of a base type (primitive or constructor) */
|
|
105
|
-
static isBaseType(value: unknown, type: string): boolean
|
|
106
|
-
|
|
107
|
-
/** Get the type of a value */
|
|
108
|
-
static typeOf(value: unknown): string
|
|
109
|
-
|
|
110
|
-
/** Check if a value is undefined or null */
|
|
111
|
-
static isNothing(value: unknown): value is null | undefined
|
|
112
|
-
|
|
113
|
-
/** Check if a value is empty */
|
|
114
|
-
static isEmpty(value: unknown, checkForNothing?: boolean): boolean
|
|
115
|
-
|
|
116
|
-
/** Recursively freeze an object */
|
|
117
|
-
static deepFreezeObject<T>(obj: T): T
|
|
118
|
-
|
|
119
|
-
/** Ensure a nested path of objects exists */
|
|
120
|
-
static assureObjectPath(obj: Record<string, any>, keys: Array<string>): Record<string, any>
|
|
121
|
-
|
|
122
|
-
/** Set a value in a nested object structure */
|
|
123
|
-
static setNestedValue(obj: Record<string, any>, keys: Array<string>, value: unknown): void
|
|
124
|
-
|
|
125
|
-
/** Deeply merge objects */
|
|
126
|
-
static mergeObject<T extends Record<string, any>>(...sources: Array<T>): T
|
|
127
|
-
|
|
128
|
-
/** Filter an array asynchronously */
|
|
129
|
-
static asyncFilter<T>(arr: Array<T>, predicate: (item: T) => Promise<boolean>): Promise<Array<T>>
|
|
130
|
-
|
|
131
|
-
/** Ensures a value is within a specified range */
|
|
132
|
-
static clamp(val: number, min: number, max: number): number
|
|
133
|
-
|
|
134
|
-
/** Checks if a value is within a specified range (inclusive) */
|
|
135
|
-
static clamped(val: number, min: number, max: number): boolean
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Checks if a value is a plain object - created with object literals {},
|
|
139
|
-
* new Object(), or Object.create(null).
|
|
140
|
-
*
|
|
141
|
-
* Distinguishes plain objects from objects created by custom constructors, built-ins,
|
|
142
|
-
* or primitives. Plain objects only have Object.prototype or null in their prototype chain.
|
|
143
|
-
* Useful for validating configuration objects or data structures that should be plain objects.
|
|
144
|
-
*
|
|
145
|
-
* @param value - The value to check for plain object status
|
|
146
|
-
* @returns True if the value is a plain object, false otherwise
|
|
147
|
-
*
|
|
148
|
-
* @example
|
|
149
|
-
* ```typescript
|
|
150
|
-
* import { Data } from '@gesslar/toolkit'
|
|
151
|
-
*
|
|
152
|
-
* // Plain objects return true
|
|
153
|
-
* console.log(Data.isPlainObject({})) // true
|
|
154
|
-
* console.log(Data.isPlainObject(new Object())) // true
|
|
155
|
-
* console.log(Data.isPlainObject(Object.create(null))) // true
|
|
156
|
-
*
|
|
157
|
-
* // Non-plain objects return false
|
|
158
|
-
* console.log(Data.isPlainObject([])) // false
|
|
159
|
-
* console.log(Data.isPlainObject(new Date())) // false
|
|
160
|
-
* console.log(Data.isPlainObject(/regex/)) // false
|
|
161
|
-
* console.log(Data.isPlainObject(null)) // false
|
|
162
|
-
* console.log(Data.isPlainObject('string')) // false
|
|
163
|
-
*
|
|
164
|
-
* // Useful for validating config objects
|
|
165
|
-
* function processConfig(config: unknown) {
|
|
166
|
-
* if (!Data.isPlainObject(config)) {
|
|
167
|
-
* throw new Error('Config must be a plain object')
|
|
168
|
-
* }
|
|
169
|
-
* // Safe to treat as object with string keys
|
|
170
|
-
* return Object.entries(config)
|
|
171
|
-
* }
|
|
172
|
-
* ```
|
|
173
|
-
*/
|
|
174
|
-
static isPlainObject(value: unknown): boolean
|
|
175
|
-
}
|