@gesslar/toolkit 1.5.0 → 1.6.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 (46) hide show
  1. package/package.json +2 -2
  2. package/src/browser/lib/Data.js +0 -7
  3. package/src/browser/lib/Disposer.js +1 -24
  4. package/src/browser/lib/Util.js +1 -11
  5. package/src/types/browser/lib/Data.d.ts.map +1 -1
  6. package/src/types/browser/lib/Disposer.d.ts +0 -6
  7. package/src/types/browser/lib/Disposer.d.ts.map +1 -1
  8. package/src/types/browser/lib/Util.d.ts +0 -1
  9. package/src/types/browser/lib/Util.d.ts.map +1 -1
  10. package/src/browser/lib/Hook.js +0 -82
  11. package/src/browser/lib/test.js +0 -25
  12. package/src/browser/lib/test2.js +0 -46
  13. package/src/types/Cache.d.ts +0 -30
  14. package/src/types/Collection.d.ts +0 -321
  15. package/src/types/Contract.d.ts +0 -162
  16. package/src/types/Data.d.ts +0 -175
  17. package/src/types/DirectoryObject.d.ts +0 -135
  18. package/src/types/FS.d.ts +0 -40
  19. package/src/types/FileObject.d.ts +0 -388
  20. package/src/types/Glog.d.ts +0 -345
  21. package/src/types/Sass.d.ts +0 -24
  22. package/src/types/Schemer.d.ts +0 -179
  23. package/src/types/Tantrum.d.ts +0 -81
  24. package/src/types/Term.d.ts +0 -16
  25. package/src/types/Terms.d.ts +0 -145
  26. package/src/types/Type.d.ts +0 -26
  27. package/src/types/Util.d.ts +0 -275
  28. package/src/types/Valid.d.ts +0 -13
  29. package/src/types/browser/lib/Disposable.d.ts +0 -35
  30. package/src/types/browser/lib/Disposable.d.ts.map +0 -10
  31. package/src/types/browser/lib/Hook.d.ts +0 -11
  32. package/src/types/browser/lib/Hook.d.ts.map +0 -1
  33. package/src/types/browser/lib/test.d.ts +0 -2
  34. package/src/types/browser/lib/test.d.ts.map +0 -1
  35. package/src/types/browser/lib/test2.d.ts +0 -2
  36. package/src/types/browser/lib/test2.d.ts.map +0 -1
  37. package/src/types/lib/Chide.d.ts +0 -37
  38. package/src/types/lib/Chide.d.ts.map +0 -1
  39. package/src/types/lib/Collection.d.ts +0 -246
  40. package/src/types/lib/Collection.d.ts.map +0 -1
  41. package/src/types/lib/Data.d.ts +0 -206
  42. package/src/types/lib/Data.d.ts.map +0 -1
  43. package/src/types/lib/Disposable.d.ts +0 -33
  44. package/src/types/lib/Disposable.d.ts.map +0 -10
  45. package/src/types/lib/TypeSpec.d.ts +0 -92
  46. package/src/types/lib/TypeSpec.d.ts.map +0 -1
@@ -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
@@ -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
- }
@@ -1,135 +0,0 @@
1
- // Implementation: ../lib/DirectoryObject.js
2
- // Type definitions for DirectoryObject
3
-
4
- import FS from './FS.js'
5
- import FileObject from './FileObject.js'
6
-
7
- export interface DirectoryListing {
8
- /** Array of FileObject instances */
9
- files: Array<FileObject>
10
- /** Array of DirectoryObject instances */
11
- directories: Array<DirectoryObject>
12
- }
13
-
14
- /**
15
- * DirectoryObject encapsulates metadata and operations for a directory,
16
- * including path resolution and existence checks.
17
- */
18
- export default class DirectoryObject extends FS {
19
- /**
20
- * Create a new DirectoryObject instance.
21
- * @param directory - The directory path
22
- */
23
- constructor(directory: string)
24
-
25
- /** User-supplied path */
26
- readonly supplied: string
27
-
28
- /** The absolute directory path */
29
- readonly path: string
30
-
31
- /** The directory URI */
32
- readonly uri: string
33
-
34
- /** The directory name */
35
- readonly name: string
36
-
37
- /** The directory name without extension */
38
- readonly module: string
39
-
40
- /** The directory extension (usually empty) */
41
- readonly extension: string
42
-
43
- /** The platform-specific path separator (e.g., '/' on Unix, '\\' on Windows) */
44
- readonly sep: string
45
-
46
- /** Array of directory path segments split by separator */
47
- readonly trail: string[]
48
-
49
- /** Always false for directories */
50
- readonly isFile: false
51
-
52
- /** Always true for directories */
53
- readonly isDirectory: true
54
-
55
- /** Whether the directory exists (async) */
56
- readonly exists: Promise<boolean>
57
-
58
- /**
59
- * Generator that walks up the directory tree, yielding parent directories.
60
- * Starts from the current directory and yields each parent until reaching the root.
61
- *
62
- * @example
63
- * ```typescript
64
- * const dir = new DirectoryObject('/path/to/deep/directory')
65
- * for (const parent of dir.walkUp) {
66
- * console.log(parent.path)
67
- * // /path/to/deep/directory
68
- * // /path/to/deep
69
- * // /path/to
70
- * // /path
71
- * // /
72
- * }
73
- * ```
74
- */
75
- readonly walkUp: Generator<DirectoryObject, void, unknown>
76
-
77
- /** Returns a string representation of the DirectoryObject */
78
- toString(): string
79
-
80
- /** Returns a JSON representation of the DirectoryObject */
81
- toJSON(): {
82
- supplied: string
83
- path: string
84
- uri: string
85
- name: string
86
- module: string
87
- extension: string
88
- isFile: boolean
89
- isDirectory: boolean
90
- }
91
-
92
- /**
93
- * Lists the contents of this directory.
94
- * Returns FileObject instances for files and DirectoryObject instances for subdirectories.
95
- *
96
- * @returns Promise resolving to object with files and directories arrays
97
- * @throws {Error} If directory cannot be read
98
- *
99
- * @example
100
- * ```typescript
101
- * const dir = new DirectoryObject('./src')
102
- * const {files, directories} = await dir.read()
103
- *
104
- * console.log(`Found ${files.length} files`)
105
- * files.forEach(file => console.log(file.name))
106
- *
107
- * console.log(`Found ${directories.length} subdirectories`)
108
- * directories.forEach(subdir => console.log(subdir.name))
109
- * ```
110
- */
111
- read(): Promise<DirectoryListing>
112
-
113
- /**
114
- * Ensures this directory exists, creating it if necessary.
115
- * Gracefully handles the case where the directory already exists (EEXIST error).
116
- * Pass options to control directory creation behavior (e.g., recursive, mode).
117
- *
118
- * @param options - Options to pass to fs.mkdir (e.g., {recursive: true, mode: 0o755})
119
- * @returns Promise that resolves when directory exists or has been created
120
- * @throws {Sass} If directory creation fails for reasons other than already existing
121
- *
122
- * @example
123
- * ```typescript
124
- * const dir = new DirectoryObject('./build/output')
125
- *
126
- * // Create directory recursively
127
- * await dir.assureExists({recursive: true})
128
- *
129
- * // Now safe to write files
130
- * const file = new FileObject('result.json', dir)
131
- * await file.write(JSON.stringify(data))
132
- * ```
133
- */
134
- assureExists(options?: any): Promise<void>
135
- }
package/src/types/FS.d.ts DELETED
@@ -1,40 +0,0 @@
1
- // Implementation: ../lib/FS.js
2
- // Type definitions for FS utilities
3
-
4
- import FileObject from './FileObject.js'
5
- import DirectoryObject from './DirectoryObject.js'
6
-
7
- /**
8
- * Base filesystem utilities class. FileObject and DirectoryObject extend this class.
9
- */
10
- export default class FS {
11
- /** Array of lowercase file descriptor types */
12
- static readonly fdTypes: readonly ['file', 'directory']
13
-
14
- /** Array of uppercase file descriptor types */
15
- static readonly upperFdTypes: readonly ['FILE', 'DIRECTORY']
16
-
17
- /** Mapping from uppercase to lowercase file descriptor types */
18
- static readonly fdType: Readonly<Record<'FILE' | 'DIRECTORY', 'file' | 'directory'>>
19
-
20
- /** Fix slashes in a path */
21
- static fixSlashes(pathName: string): string
22
-
23
- /** Convert a path to a URI */
24
- static pathToUri(pathName: string): string
25
-
26
- /** Convert a URI to a path */
27
- static uriToPath(pathName: string): string
28
-
29
- /** Retrieve files matching glob pattern(s) */
30
- static getFiles(glob: string | Array<string>): Promise<Array<FileObject>>
31
-
32
- /** Compute relative path between two file system objects */
33
- static relativeOrAbsolutePath(from: FileObject | DirectoryObject, to: FileObject | DirectoryObject): string
34
-
35
- /** Merge two paths by finding overlapping segments and combining them efficiently */
36
- static mergeOverlappingPaths(path1: string, path2: string, sep?: string): string
37
-
38
- /** Resolve a path relative to another path using various strategies. Handles absolute paths, relative navigation, and overlap-based merging */
39
- static resolvePath(fromPath: string, toPath: string): string
40
- }