@gesslar/toolkit 0.5.0 → 0.7.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.
Files changed (66) hide show
  1. package/package.json +8 -8
  2. package/src/lib/Collection.js +132 -17
  3. package/src/lib/Contract.js +1 -1
  4. package/src/lib/Data.js +27 -18
  5. package/src/lib/DirectoryObject.js +1 -1
  6. package/src/lib/FS.js +10 -0
  7. package/src/lib/Glog.js +27 -10
  8. package/src/lib/Logger.js +3 -0
  9. package/src/lib/Sass.js +6 -1
  10. package/src/lib/Tantrum.js +43 -0
  11. package/src/lib/TypeSpec.js +11 -7
  12. package/src/lib/Util.js +82 -0
  13. package/src/lib/Valid.js +24 -6
  14. package/src/types/Collection.d.ts +6 -1
  15. package/src/types/Contract.d.ts +27 -27
  16. package/src/types/Data.d.ts +23 -23
  17. package/src/types/FS.d.ts +3 -3
  18. package/src/types/Glog.d.ts +302 -49
  19. package/src/types/Sass.d.ts +1 -1
  20. package/src/types/Schemer.d.ts +29 -29
  21. package/src/types/Tantrum.d.ts +10 -10
  22. package/src/types/Term.d.ts +1 -1
  23. package/src/types/Terms.d.ts +21 -21
  24. package/src/types/Type.d.ts +1 -1
  25. package/src/types/Util.d.ts +20 -2
  26. package/src/types/index.d.ts +17 -23
  27. package/src/types/index.d.ts.map +1 -0
  28. package/src/types/lib/Cache.d.ts +28 -0
  29. package/src/types/lib/Cache.d.ts.map +1 -0
  30. package/src/types/lib/Collection.d.ts +246 -0
  31. package/src/types/lib/Collection.d.ts.map +1 -0
  32. package/src/types/lib/Contract.d.ts +72 -0
  33. package/src/types/lib/Contract.d.ts.map +1 -0
  34. package/src/types/lib/Data.d.ts +189 -0
  35. package/src/types/lib/Data.d.ts.map +1 -0
  36. package/src/types/lib/DirectoryObject.d.ts +148 -0
  37. package/src/types/lib/DirectoryObject.d.ts.map +1 -0
  38. package/src/types/lib/FS.d.ts +70 -0
  39. package/src/types/lib/FS.d.ts.map +1 -0
  40. package/src/types/lib/FileObject.d.ts +189 -0
  41. package/src/types/lib/FileObject.d.ts.map +1 -0
  42. package/src/types/lib/Glog.d.ts +113 -0
  43. package/src/types/lib/Glog.d.ts.map +1 -0
  44. package/src/types/lib/Logger.d.ts +46 -0
  45. package/src/types/lib/Logger.d.ts.map +1 -0
  46. package/src/types/lib/Sass.d.ts +62 -0
  47. package/src/types/lib/Sass.d.ts.map +1 -0
  48. package/src/types/lib/Schemer.d.ts +23 -0
  49. package/src/types/lib/Schemer.d.ts.map +1 -0
  50. package/src/types/lib/Tantrum.d.ts +50 -0
  51. package/src/types/lib/Tantrum.d.ts.map +1 -0
  52. package/src/types/lib/Term.d.ts +103 -0
  53. package/src/types/lib/Term.d.ts.map +1 -0
  54. package/src/types/lib/Terms.d.ts +24 -0
  55. package/src/types/lib/Terms.d.ts.map +1 -0
  56. package/src/types/lib/TypeSpec.d.ts +92 -0
  57. package/src/types/lib/TypeSpec.d.ts.map +1 -0
  58. package/src/types/lib/Util.d.ts +197 -0
  59. package/src/types/lib/Util.d.ts.map +1 -0
  60. package/src/types/lib/Valid.d.ts +33 -0
  61. package/src/types/lib/Valid.d.ts.map +1 -0
  62. package/src/lib/Action.js +0 -283
  63. package/src/lib/ActionBuilder.js +0 -144
  64. package/src/lib/ActionRunner.js +0 -79
  65. package/src/lib/Hooks.js +0 -194
  66. package/src/lib/Piper.js +0 -155
package/src/lib/Util.js CHANGED
@@ -62,6 +62,27 @@ export default class Util {
62
62
  return `${" ".repeat(diff)}${work}`
63
63
  }
64
64
 
65
+ /**
66
+ * Centre-align a string inside a fixed width (pad with spaces on left).
67
+ * If the string exceeds width it is returned unchanged.
68
+ *
69
+ * @param {string|number} text - Text to align.
70
+ * @param {number} width - Target field width (default 80).
71
+ * @returns {string} Padded string with text centred.
72
+ */
73
+ static centreAlignText(text, width=80) {
74
+ const work = String(text)
75
+
76
+ if(work.length >= width)
77
+ return work
78
+
79
+ const totalPadding = width - work.length
80
+ const leftPadding = Math.floor(totalPadding / 2)
81
+ const rightPadding = totalPadding - leftPadding
82
+
83
+ return `${" ".repeat(leftPadding)}${work}${" ".repeat(rightPadding)}`
84
+ }
85
+
65
86
  /**
66
87
  * Compute sha256 hash (hex) of the provided string.
67
88
  *
@@ -131,6 +152,67 @@ export default class Util {
131
152
  return await Promise.allSettled(promises)
132
153
  }
133
154
 
155
+ /**
156
+ * Checks if any result in the settled promise array is rejected.
157
+ *
158
+ * @param {Array<object>} result - Array of settled promise results.
159
+ * @returns {boolean} True if any result is rejected, false otherwise.
160
+ */
161
+ static anyRejected(result) {
162
+ return result.some(r => r.status === "rejected")
163
+ }
164
+
165
+ /**
166
+ * Filters and returns all rejected results from a settled promise array.
167
+ *
168
+ * @param {Array<object>} result - Array of settled promise results.
169
+ * @returns {Array<object>} Array of rejected results.
170
+ */
171
+ static settledAndRejected(result) {
172
+ return result.filter(r => r.status === "rejected")
173
+ }
174
+
175
+ /**
176
+ * Extracts the rejection reasons from an array of rejected promise results.
177
+ *
178
+ * @param {Array<object>} rejected - Array of rejected results.
179
+ * @returns {Array<unknown>} Array of rejection reasons.
180
+ */
181
+ static rejectedReasons(rejected) {
182
+ return rejected.map(r => r.reason)
183
+ }
184
+
185
+ /**
186
+ * Throws a Sass error containing all rejection reasons from settled promises.
187
+ *
188
+ * @param {string} [_message] - Optional error message. Defaults to "GIGO"
189
+ * @param {Array<object>} rejected - Array of rejected results.
190
+ * @throws {Error} Throws a Sass error with rejection reasons.
191
+ */
192
+ static throwRejected(_message="GIGO", rejected) {
193
+ throw Sass.new(Util.rejectedReasons(rejected))
194
+ }
195
+
196
+ /**
197
+ * Filters and returns all fulfilled results from a settled promise array.
198
+ *
199
+ * @param {Array<object>} result - Array of settled promise results.
200
+ * @returns {Array<object>} Array of fulfilled results.
201
+ */
202
+ static settledAndFulfilled(result) {
203
+ return result.filter(r => r.status === "fulfilled")
204
+ }
205
+
206
+ /**
207
+ * Extracts the values from all fulfilled results in a settled promise array.
208
+ *
209
+ * @param {Array<object>} result - Array of settled promise results.
210
+ * @returns {Array<unknown>} Array of fulfilled values.
211
+ */
212
+ static fulfilledValues(result) {
213
+ return this.settledAndFulfilled(result).map(r => r.value)
214
+ }
215
+
134
216
  /**
135
217
  * Returns the first promise to resolve or reject from an array of promises.
136
218
  * Wrapper around Promise.race for consistency with other utility methods.
package/src/lib/Valid.js CHANGED
@@ -1,17 +1,27 @@
1
+ /**
2
+ * @file Valid.js
3
+ *
4
+ * Provides validation utilities for type checking and assertion.
5
+ * Includes prototype pollution protection for secure object manipulation.
6
+ */
7
+
1
8
  import _assert from "node:assert/strict"
2
9
 
3
10
  import Sass from "./Sass.js"
4
11
  import Data from "./Data.js"
5
12
  import Collection from "./Collection.js"
6
13
 
7
- export default class Valid {
8
14
  /**
9
- * Validates a value against a type. Uses Data.isType.
10
- *
11
- * @param {unknown} value - The value to validate
12
- * @param {string} type - The expected type in the form of "object", "object[]", "object|object[]"
13
- * @param {object} [options] - Additional options for validation.
15
+ * Validation utility class providing type checking and assertion methods.
14
16
  */
17
+ export default class Valid {
18
+ /**
19
+ * Validates a value against a type. Uses Data.isType.
20
+ *
21
+ * @param {unknown} value - The value to validate
22
+ * @param {string} type - The expected type in the form of "object", "object[]", "object|object[]"
23
+ * @param {object} [options] - Additional options for validation.
24
+ */
15
25
  static type(value, type, options) {
16
26
  Valid.assert(
17
27
  Data.isType(value, type, options),
@@ -48,6 +58,14 @@ export default class Valid {
48
58
  }
49
59
 
50
60
  static #restrictedProto = ["__proto__", "constructor", "prototype"]
61
+
62
+ /**
63
+ * Protects against prototype pollution by checking keys for dangerous property names.
64
+ * Throws if any restricted prototype properties are found in the keys array.
65
+ *
66
+ * @param {Array<string>} keys - Array of property keys to validate
67
+ * @throws {Sass} If any key matches restricted prototype properties (__proto__, constructor, prototype)
68
+ */
51
69
  static prototypePollutionProtection(keys) {
52
70
  Valid.type(keys, "String[]")
53
71
 
@@ -211,7 +211,12 @@ export default class Collection {
211
211
  ): Promise<Record<string, R>>
212
212
 
213
213
  /** Allocate an object from a source array and spec */
214
- static allocateObject(source: Array<unknown>, spec: Array<unknown> | ((source: Array<unknown>) => Promise<Array<unknown>> | Array<unknown>)): Promise<Record<string, unknown>>
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>>
215
220
 
216
221
  /**
217
222
  * Flattens one level of an array of plain objects, transposing values so each
@@ -11,18 +11,18 @@ export type DebugFunction = (message: string, level?: number, ...args: unknown[]
11
11
  * Contract represents a successful negotiation between Terms.
12
12
  * It handles validation and compatibility checking between what
13
13
  * one action provides and what another accepts.
14
- *
14
+ *
15
15
  * @example
16
16
  * ```typescript
17
17
  * // Two-party contract between provider and consumer
18
18
  * const provider = new Terms(providerDefinition)
19
19
  * const consumer = new Terms(consumerDefinition)
20
20
  * const contract = new Contract(provider, consumer, { debug: console.log })
21
- *
21
+ *
22
22
  * // Validate data against the contract
23
23
  * const isValid = contract.validate(someData)
24
24
  * ```
25
- *
25
+ *
26
26
  * @example
27
27
  * ```typescript
28
28
  * // Single-party contract from terms definition
@@ -35,43 +35,43 @@ export type DebugFunction = (message: string, level?: number, ...args: unknown[]
35
35
  * }
36
36
  * }
37
37
  * })
38
- *
38
+ *
39
39
  * contract.validate({ name: "John", age: 30 }) // true
40
40
  * ```
41
41
  */
42
42
  declare class Contract {
43
43
  /**
44
44
  * Creates a contract by negotiating between provider and consumer terms
45
- *
45
+ *
46
46
  * @param providerTerms - What the provider offers
47
- * @param consumerTerms - What the consumer expects
47
+ * @param consumerTerms - What the consumer expects
48
48
  * @param options - Configuration options
49
49
  * @param options.debug - Debug function for logging negotiation details
50
- *
50
+ *
51
51
  * @throws {Sass} If contract negotiation fails due to incompatible terms
52
52
  */
53
53
  constructor(
54
- providerTerms: import('./Terms.js').default | null,
55
- consumerTerms: import('./Terms.js').default | null,
54
+ providerTerms: import('./Terms.js').default | null,
55
+ consumerTerms: import('./Terms.js').default | null,
56
56
  options?: { debug?: DebugFunction }
57
57
  )
58
58
 
59
59
  /**
60
60
  * Creates a contract from terms with schema validation
61
- *
61
+ *
62
62
  * @param name - Contract identifier for error reporting
63
63
  * @param termsDefinition - The terms definition object
64
64
  * @param validator - Optional AJV schema validator function with .errors property
65
65
  * @param debug - Debug function for logging validation details
66
66
  * @returns New contract instance ready for data validation
67
- *
67
+ *
68
68
  * @throws {Sass} If terms definition is invalid according to the validator
69
- *
69
+ *
70
70
  * @example
71
71
  * ```typescript
72
72
  * const contract = Contract.fromTerms("user-parser", {
73
73
  * provides: {
74
- * type: "object",
74
+ * type: "object",
75
75
  * properties: {
76
76
  * id: { type: "string" },
77
77
  * name: { type: "string" }
@@ -82,22 +82,22 @@ declare class Contract {
82
82
  * ```
83
83
  */
84
84
  static fromTerms(
85
- name: string,
86
- termsDefinition: object,
87
- validator?: ValidateFunction | null,
85
+ name: string,
86
+ termsDefinition: object,
87
+ validator?: ValidateFunction | null,
88
88
  debug?: DebugFunction
89
89
  ): Contract
90
90
 
91
91
  /**
92
92
  * Validates data against this contract's schema
93
- *
93
+ *
94
94
  * @param data - Data object to validate against the contract
95
95
  * @returns True if validation passes
96
- *
96
+ *
97
97
  * @throws {Sass} If validation fails with detailed error messages
98
98
  * @throws {Sass} If contract has not been successfully negotiated
99
99
  * @throws {Sass} If no validator is available for this contract
100
- *
100
+ *
101
101
  * @example
102
102
  * ```typescript
103
103
  * try {
@@ -112,9 +112,9 @@ declare class Contract {
112
112
 
113
113
  /**
114
114
  * Check if contract negotiation was successful
115
- *
115
+ *
116
116
  * @returns True if the contract has been successfully negotiated
117
- *
117
+ *
118
118
  * @example
119
119
  * ```typescript
120
120
  * if (contract.isNegotiated) {
@@ -128,23 +128,23 @@ declare class Contract {
128
128
 
129
129
  /**
130
130
  * Get the provider terms (if any)
131
- *
131
+ *
132
132
  * @returns Provider terms or null for single-party contracts
133
133
  */
134
134
  get providerTerms(): import('./Terms.js').default | null
135
135
 
136
136
  /**
137
137
  * Get the consumer terms (if any)
138
- *
139
- * @returns Consumer terms or null for single-party contracts
138
+ *
139
+ * @returns Consumer terms or null for single-party contracts
140
140
  */
141
141
  get consumerTerms(): import('./Terms.js').default | null
142
142
 
143
143
  /**
144
144
  * Get the contract validator function
145
- *
145
+ *
146
146
  * @returns The AJV validator function used by this contract, or null if none available
147
- *
147
+ *
148
148
  * @example
149
149
  * ```typescript
150
150
  * const validator = contract.validator
@@ -159,4 +159,4 @@ declare class Contract {
159
159
  get validator(): ((data: object) => boolean) | null
160
160
  }
161
161
 
162
- export default Contract
162
+ export default Contract
@@ -21,33 +21,33 @@ export default class Data {
21
21
 
22
22
  /**
23
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,
24
+ *
25
+ * Useful for ensuring strings have consistent endings like file extensions,
26
26
  * URL paths, or punctuation. Performs case-sensitive comparison and only appends
27
27
  * if the string doesn't already end with the specified suffix.
28
28
  *
29
29
  * @param string - The base string to potentially append to. Can be empty string.
30
30
  * @param append - The suffix to append if not already present. Cannot be empty.
31
31
  * @returns The string with the suffix appended, or the original string if suffix already present
32
- *
32
+ *
33
33
  * @throws {Error} When append parameter is empty or undefined
34
34
  *
35
35
  * @example
36
36
  * ```typescript
37
37
  * import { Data } from '@gesslar/toolkit'
38
- *
38
+ *
39
39
  * // Basic usage with file extensions
40
40
  * const filename = Data.appendString('config', '.json')
41
41
  * console.log(filename) // 'config.json'
42
- *
42
+ *
43
43
  * // No double-appending
44
- * const alreadyHasExt = Data.appendString('package.json', '.json')
44
+ * const alreadyHasExt = Data.appendString('package.json', '.json')
45
45
  * console.log(alreadyHasExt) // 'package.json' (unchanged)
46
- *
46
+ *
47
47
  * // URL path handling
48
48
  * const apiPath = Data.appendString('/api/users', '/')
49
49
  * console.log(apiPath) // '/api/users/'
50
- *
50
+ *
51
51
  * // Works with empty strings
52
52
  * const fromEmpty = Data.appendString('', '.txt')
53
53
  * console.log(fromEmpty) // '.txt'
@@ -57,33 +57,33 @@ export default class Data {
57
57
 
58
58
  /**
59
59
  * Prepend a prefix string to the beginning of a string if it doesn't already start with it.
60
- *
60
+ *
61
61
  * Useful for ensuring strings have consistent beginnings like protocol prefixes,
62
- * path separators, or formatting markers. Performs case-sensitive comparison and
62
+ * path separators, or formatting markers. Performs case-sensitive comparison and
63
63
  * only prepends if the string doesn't already start with the specified prefix.
64
64
  *
65
65
  * @param string - The base string to potentially prepend to. Can be empty string.
66
66
  * @param prepend - The prefix to prepend if not already present. Cannot be empty.
67
67
  * @returns The string with the prefix prepended, or the original string if prefix already present
68
- *
68
+ *
69
69
  * @throws {Error} When prepend parameter is empty or undefined
70
70
  *
71
71
  * @example
72
72
  * ```typescript
73
73
  * import { Data } from '@gesslar/toolkit'
74
- *
74
+ *
75
75
  * // Basic usage with protocols
76
76
  * const url = Data.prependString('example.com', 'https://')
77
77
  * console.log(url) // 'https://example.com'
78
- *
78
+ *
79
79
  * // No double-prepending
80
80
  * const alreadyHasProtocol = Data.prependString('https://api.example.com', 'https://')
81
81
  * console.log(alreadyHasProtocol) // 'https://api.example.com' (unchanged)
82
- *
82
+ *
83
83
  * // File path handling
84
84
  * const absolutePath = Data.prependString('home/user/docs', '/')
85
85
  * console.log(absolutePath) // '/home/user/docs'
86
- *
86
+ *
87
87
  * // CSS class prefixing
88
88
  * const className = Data.prependString('button-primary', 'css-')
89
89
  * console.log(className) // 'css-button-primary'
@@ -135,32 +135,32 @@ export default class Data {
135
135
  static clamped(val: number, min: number, max: number): boolean
136
136
 
137
137
  /**
138
- * Checks if a value is a plain object - created with object literals {},
138
+ * Checks if a value is a plain object - created with object literals {},
139
139
  * new Object(), or Object.create(null).
140
- *
141
- * Distinguishes plain objects from objects created by custom constructors, built-ins,
140
+ *
141
+ * Distinguishes plain objects from objects created by custom constructors, built-ins,
142
142
  * or primitives. Plain objects only have Object.prototype or null in their prototype chain.
143
143
  * Useful for validating configuration objects or data structures that should be plain objects.
144
144
  *
145
145
  * @param value - The value to check for plain object status
146
146
  * @returns True if the value is a plain object, false otherwise
147
- *
147
+ *
148
148
  * @example
149
149
  * ```typescript
150
150
  * import { Data } from '@gesslar/toolkit'
151
- *
151
+ *
152
152
  * // Plain objects return true
153
153
  * console.log(Data.isPlainObject({})) // true
154
- * console.log(Data.isPlainObject(new Object())) // true
154
+ * console.log(Data.isPlainObject(new Object())) // true
155
155
  * console.log(Data.isPlainObject(Object.create(null))) // true
156
- *
156
+ *
157
157
  * // Non-plain objects return false
158
158
  * console.log(Data.isPlainObject([])) // false
159
159
  * console.log(Data.isPlainObject(new Date())) // false
160
160
  * console.log(Data.isPlainObject(/regex/)) // false
161
161
  * console.log(Data.isPlainObject(null)) // false
162
162
  * console.log(Data.isPlainObject('string')) // false
163
- *
163
+ *
164
164
  * // Useful for validating config objects
165
165
  * function processConfig(config: unknown) {
166
166
  * if (!Data.isPlainObject(config)) {
package/src/types/FS.d.ts CHANGED
@@ -9,13 +9,13 @@ import DirectoryObject from './DirectoryObject.js'
9
9
  */
10
10
  export default class FS {
11
11
  /** Array of lowercase file descriptor types */
12
- static readonly fdTypes: readonly ["file", "directory"]
12
+ static readonly fdTypes: readonly ['file', 'directory']
13
13
 
14
14
  /** Array of uppercase file descriptor types */
15
- static readonly upperFdTypes: readonly ["FILE", "DIRECTORY"]
15
+ static readonly upperFdTypes: readonly ['FILE', 'DIRECTORY']
16
16
 
17
17
  /** Mapping from uppercase to lowercase file descriptor types */
18
- static readonly fdType: Readonly<Record<"FILE" | "DIRECTORY", "file" | "directory">>
18
+ static readonly fdType: Readonly<Record<'FILE' | 'DIRECTORY', 'file' | 'directory'>>
19
19
 
20
20
  /** Fix slashes in a path */
21
21
  static fixSlashes(pathName: string): string