indusagi-coding-agent 0.1.22 → 0.1.24

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 (222) hide show
  1. package/CHANGELOG.md +72 -11
  2. package/README.md +2 -36
  3. package/dist/cli/args.d.ts +117 -1
  4. package/dist/cli/args.d.ts.map +1 -1
  5. package/dist/cli/args.js +231 -64
  6. package/dist/cli/args.js.map +1 -1
  7. package/dist/cli/config-selector.d.ts +58 -2
  8. package/dist/cli/config-selector.d.ts.map +1 -1
  9. package/dist/cli/config-selector.js +130 -12
  10. package/dist/cli/config-selector.js.map +1 -1
  11. package/dist/cli/file-processor.d.ts +70 -2
  12. package/dist/cli/file-processor.d.ts.map +1 -1
  13. package/dist/cli/file-processor.js +240 -15
  14. package/dist/cli/file-processor.js.map +1 -1
  15. package/dist/cli/list-models.d.ts +63 -3
  16. package/dist/cli/list-models.d.ts.map +1 -1
  17. package/dist/cli/list-models.js +202 -27
  18. package/dist/cli/list-models.js.map +1 -1
  19. package/dist/cli/login-handler.d.ts +82 -8
  20. package/dist/cli/login-handler.d.ts.map +1 -1
  21. package/dist/cli/login-handler.js +410 -77
  22. package/dist/cli/login-handler.js.map +1 -1
  23. package/dist/cli/session-picker.d.ts +74 -2
  24. package/dist/cli/session-picker.d.ts.map +1 -1
  25. package/dist/cli/session-picker.js +236 -12
  26. package/dist/cli/session-picker.js.map +1 -1
  27. package/dist/core/agent-session.d.ts +214 -9
  28. package/dist/core/agent-session.d.ts.map +1 -1
  29. package/dist/core/agent-session.js +214 -9
  30. package/dist/core/agent-session.js.map +1 -1
  31. package/dist/core/bash-executor.d.ts +302 -12
  32. package/dist/core/bash-executor.d.ts.map +1 -1
  33. package/dist/core/bash-executor.js +302 -12
  34. package/dist/core/bash-executor.js.map +1 -1
  35. package/dist/core/diagnostics.d.ts +191 -0
  36. package/dist/core/diagnostics.d.ts.map +1 -1
  37. package/dist/core/diagnostics.js +142 -0
  38. package/dist/core/diagnostics.js.map +1 -1
  39. package/dist/core/event-bus.d.ts +146 -0
  40. package/dist/core/event-bus.d.ts.map +1 -1
  41. package/dist/core/event-bus.js +93 -0
  42. package/dist/core/event-bus.js.map +1 -1
  43. package/dist/core/export-html/ansi-to-html.d.ts +4 -0
  44. package/dist/core/export-html/ansi-to-html.d.ts.map +1 -1
  45. package/dist/core/export-html/ansi-to-html.js +4 -0
  46. package/dist/core/export-html/ansi-to-html.js.map +1 -1
  47. package/dist/core/export-html/index.d.ts +128 -0
  48. package/dist/core/export-html/index.d.ts.map +1 -1
  49. package/dist/core/export-html/index.js +128 -0
  50. package/dist/core/export-html/index.js.map +1 -1
  51. package/dist/core/export-html/tool-renderer.d.ts +4 -0
  52. package/dist/core/export-html/tool-renderer.d.ts.map +1 -1
  53. package/dist/core/export-html/tool-renderer.js +4 -0
  54. package/dist/core/export-html/tool-renderer.js.map +1 -1
  55. package/dist/core/keybindings.d.ts +142 -0
  56. package/dist/core/keybindings.d.ts.map +1 -1
  57. package/dist/core/keybindings.js +142 -0
  58. package/dist/core/keybindings.js.map +1 -1
  59. package/dist/core/model-registry.d.ts +98 -1
  60. package/dist/core/model-registry.d.ts.map +1 -1
  61. package/dist/core/model-registry.js +98 -1
  62. package/dist/core/model-registry.js.map +1 -1
  63. package/dist/core/model-resolver.d.ts +99 -1
  64. package/dist/core/model-resolver.d.ts.map +1 -1
  65. package/dist/core/model-resolver.js +99 -1
  66. package/dist/core/model-resolver.js.map +1 -1
  67. package/dist/core/prompt-templates.js.map +1 -1
  68. package/dist/core/sdk.d.ts +1 -1
  69. package/dist/core/sdk.d.ts.map +1 -1
  70. package/dist/core/sdk.js +0 -2
  71. package/dist/core/sdk.js.map +1 -1
  72. package/dist/core/session-manager.d.ts +127 -0
  73. package/dist/core/session-manager.d.ts.map +1 -1
  74. package/dist/core/session-manager.js +125 -0
  75. package/dist/core/session-manager.js.map +1 -1
  76. package/dist/core/skills.js.map +1 -1
  77. package/dist/core/subagents.js.map +1 -1
  78. package/dist/core/tools/bash.d.ts +391 -11
  79. package/dist/core/tools/bash.d.ts.map +1 -1
  80. package/dist/core/tools/bash.js +269 -2
  81. package/dist/core/tools/bash.js.map +1 -1
  82. package/dist/core/tools/edit.d.ts +284 -6
  83. package/dist/core/tools/edit.d.ts.map +1 -1
  84. package/dist/core/tools/edit.js +238 -0
  85. package/dist/core/tools/edit.js.map +1 -1
  86. package/dist/core/tools/find.d.ts +169 -5
  87. package/dist/core/tools/find.d.ts.map +1 -1
  88. package/dist/core/tools/find.js +136 -0
  89. package/dist/core/tools/find.js.map +1 -1
  90. package/dist/core/tools/grep.d.ts +285 -5
  91. package/dist/core/tools/grep.d.ts.map +1 -1
  92. package/dist/core/tools/grep.js +247 -0
  93. package/dist/core/tools/grep.js.map +1 -1
  94. package/dist/core/tools/index.d.ts +0 -18
  95. package/dist/core/tools/index.d.ts.map +1 -1
  96. package/dist/core/tools/index.js +1 -23
  97. package/dist/core/tools/index.js.map +1 -1
  98. package/dist/core/tools/ls.d.ts +6 -0
  99. package/dist/core/tools/ls.d.ts.map +1 -1
  100. package/dist/core/tools/ls.js +6 -0
  101. package/dist/core/tools/ls.js.map +1 -1
  102. package/dist/core/tools/read.d.ts +308 -7
  103. package/dist/core/tools/read.d.ts.map +1 -1
  104. package/dist/core/tools/read.js +231 -0
  105. package/dist/core/tools/read.js.map +1 -1
  106. package/dist/core/tools/webfetch.d.ts +118 -3
  107. package/dist/core/tools/webfetch.d.ts.map +1 -1
  108. package/dist/core/tools/webfetch.js +118 -3
  109. package/dist/core/tools/webfetch.js.map +1 -1
  110. package/dist/core/tools/websearch.d.ts +130 -3
  111. package/dist/core/tools/websearch.d.ts.map +1 -1
  112. package/dist/core/tools/websearch.js +130 -3
  113. package/dist/core/tools/websearch.js.map +1 -1
  114. package/dist/core/tools/write.d.ts +251 -5
  115. package/dist/core/tools/write.d.ts.map +1 -1
  116. package/dist/core/tools/write.js +210 -0
  117. package/dist/core/tools/write.js.map +1 -1
  118. package/dist/modes/interactive/components/assistant-message.d.ts +164 -1
  119. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  120. package/dist/modes/interactive/components/assistant-message.js +164 -1
  121. package/dist/modes/interactive/components/assistant-message.js.map +1 -1
  122. package/dist/modes/interactive/components/bash-execution.d.ts +297 -1
  123. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
  124. package/dist/modes/interactive/components/bash-execution.js +297 -1
  125. package/dist/modes/interactive/components/bash-execution.js.map +1 -1
  126. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  127. package/dist/modes/interactive/components/tool-execution.js +251 -1
  128. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  129. package/dist/modes/interactive/components/user-message.d.ts +186 -1
  130. package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
  131. package/dist/modes/interactive/components/user-message.js +186 -1
  132. package/dist/modes/interactive/components/user-message.js.map +1 -1
  133. package/dist/modes/interactive/interactive-mode.d.ts +1567 -13
  134. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  135. package/dist/modes/interactive/interactive-mode.js +1567 -13
  136. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  137. package/dist/modes/interactive/theme/theme.d.ts +422 -0
  138. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  139. package/dist/modes/interactive/theme/theme.js +422 -0
  140. package/dist/modes/interactive/theme/theme.js.map +1 -1
  141. package/dist/modes/print-mode.d.ts +538 -5
  142. package/dist/modes/print-mode.d.ts.map +1 -1
  143. package/dist/modes/print-mode.js +538 -5
  144. package/dist/modes/print-mode.js.map +1 -1
  145. package/dist/modes/rpc/rpc-client.d.ts +921 -8
  146. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  147. package/dist/modes/rpc/rpc-client.js +921 -8
  148. package/dist/modes/rpc/rpc-client.js.map +1 -1
  149. package/dist/modes/rpc/rpc-mode.d.ts +802 -9
  150. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  151. package/dist/modes/rpc/rpc-mode.js +802 -9
  152. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  153. package/dist/modes/rpc/rpc-types.d.ts +356 -3
  154. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  155. package/dist/modes/rpc/rpc-types.js +356 -3
  156. package/dist/modes/rpc/rpc-types.js.map +1 -1
  157. package/dist/modes/shared.d.ts +386 -0
  158. package/dist/modes/shared.d.ts.map +1 -0
  159. package/dist/modes/shared.js +543 -0
  160. package/dist/modes/shared.js.map +1 -0
  161. package/dist/utils/array.d.ts +389 -0
  162. package/dist/utils/array.d.ts.map +1 -0
  163. package/dist/utils/array.js +585 -0
  164. package/dist/utils/array.js.map +1 -0
  165. package/dist/utils/color-formatter.d.ts +318 -0
  166. package/dist/utils/color-formatter.d.ts.map +1 -0
  167. package/dist/utils/color-formatter.js +442 -0
  168. package/dist/utils/color-formatter.js.map +1 -0
  169. package/dist/utils/data-transformer.d.ts +326 -0
  170. package/dist/utils/data-transformer.d.ts.map +1 -0
  171. package/dist/utils/data-transformer.js +512 -0
  172. package/dist/utils/data-transformer.js.map +1 -0
  173. package/dist/utils/date-formatter.d.ts +281 -0
  174. package/dist/utils/date-formatter.d.ts.map +1 -0
  175. package/dist/utils/date-formatter.js +503 -0
  176. package/dist/utils/date-formatter.js.map +1 -0
  177. package/dist/utils/error-handler.d.ts +541 -0
  178. package/dist/utils/error-handler.d.ts.map +1 -0
  179. package/dist/utils/error-handler.js +726 -0
  180. package/dist/utils/error-handler.js.map +1 -0
  181. package/dist/utils/file-operations.d.ts +297 -0
  182. package/dist/utils/file-operations.d.ts.map +1 -0
  183. package/dist/utils/file-operations.js +505 -0
  184. package/dist/utils/file-operations.js.map +1 -0
  185. package/dist/utils/frontmatter.d.ts +268 -6
  186. package/dist/utils/frontmatter.d.ts.map +1 -1
  187. package/dist/utils/frontmatter.js +500 -21
  188. package/dist/utils/frontmatter.js.map +1 -1
  189. package/dist/utils/json-formatter.d.ts +259 -0
  190. package/dist/utils/json-formatter.d.ts.map +1 -0
  191. package/dist/utils/json-formatter.js +517 -0
  192. package/dist/utils/json-formatter.js.map +1 -0
  193. package/dist/utils/logger.d.ts +176 -0
  194. package/dist/utils/logger.d.ts.map +1 -0
  195. package/dist/utils/logger.js +346 -0
  196. package/dist/utils/logger.js.map +1 -0
  197. package/dist/utils/markdown-formatter.d.ts +211 -0
  198. package/dist/utils/markdown-formatter.d.ts.map +1 -0
  199. package/dist/utils/markdown-formatter.js +482 -0
  200. package/dist/utils/markdown-formatter.js.map +1 -0
  201. package/dist/utils/path-validator.d.ts +603 -0
  202. package/dist/utils/path-validator.d.ts.map +1 -0
  203. package/dist/utils/path-validator.js +870 -0
  204. package/dist/utils/path-validator.js.map +1 -0
  205. package/dist/utils/string-formatter.d.ts +609 -0
  206. package/dist/utils/string-formatter.d.ts.map +1 -0
  207. package/dist/utils/string-formatter.js +806 -0
  208. package/dist/utils/string-formatter.js.map +1 -0
  209. package/dist/utils/type-guards.d.ts +629 -0
  210. package/dist/utils/type-guards.d.ts.map +1 -0
  211. package/dist/utils/type-guards.js +662 -0
  212. package/dist/utils/type-guards.js.map +1 -0
  213. package/docs/COMPLETE-GUIDE.md +300 -0
  214. package/docs/MODES-ARCHITECTURE.md +565 -0
  215. package/docs/PRINT-MODE-GUIDE.md +456 -0
  216. package/docs/README.md +1 -2
  217. package/docs/RPC-GUIDE.md +705 -0
  218. package/docs/UTILS-IMPLEMENTATION-SUMMARY.md +647 -0
  219. package/docs/UTILS-MODULE-OVERVIEW.md +1480 -0
  220. package/docs/UTILS-QA-CHECKLIST.md +1061 -0
  221. package/docs/UTILS-USAGE-GUIDE.md +1419 -0
  222. package/package.json +3 -3
@@ -0,0 +1,662 @@
1
+ /**
2
+ * @fileoverview Type Guards and Type Narrowing Utilities
3
+ *
4
+ * This module provides comprehensive runtime type checking functions that serve as
5
+ * TypeScript type predicates. Type guards enable type narrowing in TypeScript, allowing
6
+ * the compiler to automatically refine types based on runtime checks. This is essential
7
+ * for:
8
+ *
9
+ * 1. **Runtime Type Safety**: Validate data at runtime before it's processed
10
+ * 2. **Type Narrowing**: Automatically narrow union types based on guard results
11
+ * 3. **Discriminated Unions**: Pattern match on type characteristics
12
+ * 4. **API Response Validation**: Verify external data conforms to expected types
13
+ * 5. **Error Handling**: Safely handle different error types
14
+ *
15
+ * ## Type Narrowing in TypeScript
16
+ *
17
+ * Type guards allow TypeScript to narrow types within conditional blocks:
18
+ *
19
+ * ```typescript
20
+ * const value: string | number = getValue();
21
+ *
22
+ * if (isString(value)) {
23
+ * // TypeScript now knows value is string
24
+ * value.toUpperCase(); // ✓ Allowed
25
+ * } else if (isNumber(value)) {
26
+ * // TypeScript now knows value is number
27
+ * value.toFixed(2); // ✓ Allowed
28
+ * }
29
+ * ```
30
+ *
31
+ * ## When to Use Type Guards
32
+ *
33
+ * - Validating external data (API responses, user input, config files)
34
+ * - Handling union types safely
35
+ * - Implementing discriminated unions
36
+ * - Working with `any` or `unknown` types
37
+ * - Defensive programming at module boundaries
38
+ *
39
+ * @author Coding Agent
40
+ * @version 1.0.0
41
+ */
42
+ /**
43
+ * Checks if a value is a primitive type (string, number, boolean, null, undefined, symbol, bigint).
44
+ *
45
+ * Primitive types in JavaScript are immutable and passed by value. This is useful for
46
+ * quick type categorization and distinguishing primitives from objects.
47
+ *
48
+ * @param value - The value to check
49
+ * @returns true if value is a primitive type
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * isPrimitive(42) // true
54
+ * isPrimitive("hello") // true
55
+ * isPrimitive(true) // true
56
+ * isPrimitive(null) // true
57
+ * isPrimitive(undefined) // true
58
+ * isPrimitive({}) // false
59
+ * isPrimitive([]) // false
60
+ * isPrimitive(() => {}) // false
61
+ * ```
62
+ *
63
+ * @usage
64
+ * ```typescript
65
+ * function processValue(value: unknown) {
66
+ * if (isPrimitive(value)) {
67
+ * // Safely stringify or log primitive
68
+ * console.log(String(value));
69
+ * } else {
70
+ * // Handle complex object
71
+ * processObject(value as object);
72
+ * }
73
+ * }
74
+ * ```
75
+ */
76
+ export function isPrimitive(value) {
77
+ const type = typeof value;
78
+ return type === 'string' || type === 'number' || type === 'boolean' ||
79
+ type === 'symbol' || type === 'bigint' || value === null || value === undefined;
80
+ }
81
+ /**
82
+ * Checks if a value is a plain JavaScript object (not null, array, function, or built-in object).
83
+ *
84
+ * This guard specifically identifies plain objects created with `{}` or `Object.create(null)`,
85
+ * excluding arrays, dates, regex, errors, and other special object types. Useful for
86
+ * differentiating data objects from other object types.
87
+ *
88
+ * @param value - The value to check
89
+ * @returns true if value is a plain object
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * isObject({}) // true
94
+ * isObject({ name: 'John' }) // true
95
+ * isObject(Object.create(null)) // true
96
+ * isObject([]) // false (array)
97
+ * isObject(new Date()) // false (built-in object)
98
+ * isObject(new Map()) // false (built-in object)
99
+ * isObject(null) // false
100
+ * isObject("string") // false
101
+ * ```
102
+ *
103
+ * @usage
104
+ * ```typescript
105
+ * // Merge configuration objects safely
106
+ * function mergeConfig(target: Record<string, any>, source: unknown) {
107
+ * if (isObject(source)) {
108
+ * Object.assign(target, source);
109
+ * } else {
110
+ * throw new Error('Configuration must be a plain object');
111
+ * }
112
+ * }
113
+ * ```
114
+ */
115
+ export function isObject(value) {
116
+ return value !== null && typeof value === 'object' && !Array.isArray(value) &&
117
+ !(value instanceof Date) && !(value instanceof Map) &&
118
+ !(value instanceof Set) && !(value instanceof RegExp) &&
119
+ !(value instanceof Error);
120
+ }
121
+ /**
122
+ * Checks if a value is an array.
123
+ *
124
+ * Type narrows to Array<unknown>. Use type parameters for more specific array types
125
+ * in combination with element validation.
126
+ *
127
+ * @param value - The value to check
128
+ * @returns true if value is an array
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * isArray([]) // true
133
+ * isArray([1, 2, 3]) // true
134
+ * isArray(['a', 'b']) // true
135
+ * isArray(new Array()) // true
136
+ * isArray({}) // false
137
+ * isArray("string") // false
138
+ * isArray(null) // false
139
+ * ```
140
+ *
141
+ * @usage
142
+ * ```typescript
143
+ * // Type-safe array processing
144
+ * function processItems(items: unknown): number {
145
+ * if (isArray(items)) {
146
+ * return items.reduce((sum, item) => {
147
+ * return sum + (isNumber(item) ? item : 0);
148
+ * }, 0);
149
+ * }
150
+ * return 0;
151
+ * }
152
+ *
153
+ * // Iterate safely with type narrowing
154
+ * if (isArray(data)) {
155
+ * data.forEach(item => {
156
+ * console.log(item);
157
+ * });
158
+ * }
159
+ * ```
160
+ */
161
+ export function isArray(value) {
162
+ return Array.isArray(value);
163
+ }
164
+ /**
165
+ * Checks if a value is a string.
166
+ *
167
+ * Useful for string validation, form input verification, and discriminating
168
+ * between string and number types.
169
+ *
170
+ * @param value - The value to check
171
+ * @returns true if value is a string
172
+ *
173
+ * @example
174
+ * ```typescript
175
+ * isString("hello") // true
176
+ * isString("") // true
177
+ * isString(new String("text")) // false (object wrapper)
178
+ * isString(123) // false
179
+ * isString(null) // false
180
+ * ```
181
+ *
182
+ * @usage
183
+ * ```typescript
184
+ * // Validate user input
185
+ * function processUserInput(input: unknown): void {
186
+ * if (!isString(input)) {
187
+ * throw new TypeError('Input must be a string');
188
+ * }
189
+ *
190
+ * const trimmed = input.trim();
191
+ * if (trimmed.length === 0) {
192
+ * throw new Error('Input cannot be empty');
193
+ * }
194
+ * }
195
+ *
196
+ * // Parse environment variables safely
197
+ * function getEnvString(key: string, defaultValue: string = ''): string {
198
+ * const value = process.env[key];
199
+ * return isString(value) ? value : defaultValue;
200
+ * }
201
+ * ```
202
+ */
203
+ export function isString(value) {
204
+ return typeof value === 'string';
205
+ }
206
+ /**
207
+ * Checks if a value is a number (excluding NaN and Infinity).
208
+ *
209
+ * This strict guard rejects NaN and Infinity, which are valid JavaScript numbers
210
+ * but often represent error states. Use with caution when you specifically need
211
+ * to accept these special values.
212
+ *
213
+ * @param value - The value to check
214
+ * @returns true if value is a valid finite number
215
+ *
216
+ * @example
217
+ * ```typescript
218
+ * isNumber(42) // true
219
+ * isNumber(3.14) // true
220
+ * isNumber(-5) // true
221
+ * isNumber(0) // true
222
+ * isNumber(Infinity) // false (rejected for safety)
223
+ * isNumber(-Infinity) // false
224
+ * isNumber(NaN) // false
225
+ * isNumber("123") // false
226
+ * isNumber(new Number(42)) // false (wrapper object)
227
+ * ```
228
+ *
229
+ * @usage
230
+ * ```typescript
231
+ * // Calculate average safely
232
+ * function calculateAverage(values: unknown[]): number | null {
233
+ * const numbers = values.filter(isNumber);
234
+ * if (numbers.length === 0) return null;
235
+ * return numbers.reduce((a, b) => a + b, 0) / numbers.length;
236
+ * }
237
+ *
238
+ * // Validate port numbers
239
+ * function isValidPort(port: unknown): boolean {
240
+ * return isNumber(port) && port >= 0 && port <= 65535;
241
+ * }
242
+ * ```
243
+ */
244
+ export function isNumber(value) {
245
+ return typeof value === 'number' && isFinite(value);
246
+ }
247
+ /**
248
+ * Checks if a value is a boolean (true or false).
249
+ *
250
+ * @param value - The value to check
251
+ * @returns true if value is a boolean
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * isBoolean(true) // true
256
+ * isBoolean(false) // true
257
+ * isBoolean(1) // false
258
+ * isBoolean("true") // false
259
+ * isBoolean(new Boolean(true)) // false (wrapper object)
260
+ * ```
261
+ *
262
+ * @usage
263
+ * ```typescript
264
+ * // Type-safe configuration
265
+ * interface Config {
266
+ * debug?: boolean;
267
+ * }
268
+ *
269
+ * function parseConfig(input: unknown): Config {
270
+ * if (!isObject(input)) throw new Error('Config must be an object');
271
+ *
272
+ * const debug = (input as any).debug;
273
+ * return {
274
+ * debug: isBoolean(debug) ? debug : false
275
+ * };
276
+ * }
277
+ * ```
278
+ */
279
+ export function isBoolean(value) {
280
+ return typeof value === 'boolean';
281
+ }
282
+ /**
283
+ * Checks if a value is a function.
284
+ *
285
+ * Distinguishes callable functions from other object types. Useful for
286
+ * callback validation and dynamic function calling.
287
+ *
288
+ * @param value - The value to check
289
+ * @returns true if value is a function
290
+ *
291
+ * @example
292
+ * ```typescript
293
+ * isFunction(() => {}) // true
294
+ * isFunction(function() {}) // true
295
+ * isFunction(async () => {}) // true
296
+ * isFunction(class MyClass {}) // true
297
+ * isFunction(Promise.resolve) // true
298
+ * isFunction(Math.max) // true
299
+ * isFunction({}) // false
300
+ * isFunction("function") // false
301
+ * ```
302
+ *
303
+ * @usage
304
+ * ```typescript
305
+ * // Callback validation and execution
306
+ * function processWithCallback(
307
+ * data: unknown,
308
+ * callback: unknown
309
+ * ): unknown {
310
+ * if (!isFunction(callback)) {
311
+ * throw new TypeError('Callback must be a function');
312
+ * }
313
+ * return callback(data);
314
+ * }
315
+ *
316
+ * // Build event emitter safely
317
+ * const listeners: Map<string, Function[]> = new Map();
318
+ *
319
+ * function on(event: string, listener: unknown) {
320
+ * if (!isFunction(listener)) {
321
+ * throw new TypeError('Listener must be a function');
322
+ * }
323
+ * if (!listeners.has(event)) {
324
+ * listeners.set(event, []);
325
+ * }
326
+ * listeners.get(event)!.push(listener);
327
+ * }
328
+ * ```
329
+ */
330
+ export function isFunction(value) {
331
+ return typeof value === 'function';
332
+ }
333
+ /**
334
+ * Checks if a value is an Error instance.
335
+ *
336
+ * Identifies instances of Error and its subclasses (TypeError, ReferenceError, etc.).
337
+ * Essential for error handling and recovery strategies.
338
+ *
339
+ * @param value - The value to check
340
+ * @returns true if value is an Error instance
341
+ *
342
+ * @example
343
+ * ```typescript
344
+ * isError(new Error("message")) // true
345
+ * isError(new TypeError("message")) // true
346
+ * isError(new ReferenceError("message")) // true
347
+ * isError(new Error()) // true
348
+ * isError(new CustomError()) // true (if extends Error)
349
+ * isError({ message: "error" }) // false
350
+ * isError("error string") // false
351
+ * isError(null) // false
352
+ * ```
353
+ *
354
+ * @usage
355
+ * ```typescript
356
+ * // Safe error handling
357
+ * async function executeWithFallback(fn: () => Promise<void>) {
358
+ * try {
359
+ * await fn();
360
+ * } catch (error) {
361
+ * if (isError(error)) {
362
+ * console.error('Caught error:', error.message);
363
+ * console.error('Stack:', error.stack);
364
+ * } else {
365
+ * console.error('Unknown error type:', error);
366
+ * }
367
+ * }
368
+ * }
369
+ *
370
+ * // Error type discrimination
371
+ * function handleError(error: unknown): string {
372
+ * if (!isError(error)) {
373
+ * return 'Unknown error';
374
+ * }
375
+ *
376
+ * if (error instanceof TypeError) {
377
+ * return 'Type error: ' + error.message;
378
+ * } else if (error instanceof RangeError) {
379
+ * return 'Range error: ' + error.message;
380
+ * } else {
381
+ * return 'Error: ' + error.message;
382
+ * }
383
+ * }
384
+ * ```
385
+ */
386
+ export function isError(value) {
387
+ return value instanceof Error;
388
+ }
389
+ /**
390
+ * Checks if a value is a Date instance.
391
+ *
392
+ * Useful for temporal data validation and date handling workflows.
393
+ *
394
+ * @param value - The value to check
395
+ * @returns true if value is a Date instance
396
+ *
397
+ * @example
398
+ * ```typescript
399
+ * isDate(new Date()) // true
400
+ * isDate(new Date('2024-01-01')) // true
401
+ * isDate(Date.now()) // false (returns number)
402
+ * isDate("2024-01-01") // false
403
+ * isDate({ toISOString: () => {} }) // false (duck typing doesn't work)
404
+ * isDate(null) // false
405
+ * ```
406
+ *
407
+ * @usage
408
+ * ```typescript
409
+ * // Validate API response dates
410
+ * function parseResponse(data: unknown): { timestamp: Date } | null {
411
+ * if (!isObject(data)) return null;
412
+ *
413
+ * const timestamp = (data as any).timestamp;
414
+ * if (isDate(timestamp)) {
415
+ * return { timestamp };
416
+ * }
417
+ *
418
+ * // Try parsing string dates
419
+ * if (isString(timestamp)) {
420
+ * const parsed = new Date(timestamp);
421
+ * return isValidDate(parsed) ? { timestamp: parsed } : null;
422
+ * }
423
+ *
424
+ * return null;
425
+ * }
426
+ *
427
+ * // Helper to validate Date is not invalid (NaN)
428
+ * function isValidDate(date: Date): boolean {
429
+ * return isDate(date) && !isNaN(date.getTime());
430
+ * }
431
+ * ```
432
+ */
433
+ export function isDate(value) {
434
+ return value instanceof Date;
435
+ }
436
+ /**
437
+ * Checks if a value is a RegExp instance.
438
+ *
439
+ * Useful for pattern validation, regex-based routing, and configuration parsing.
440
+ *
441
+ * @param value - The value to check
442
+ * @returns true if value is a RegExp instance
443
+ *
444
+ * @example
445
+ * ```typescript
446
+ * isRegExp(/test/) // true
447
+ * isRegExp(new RegExp('test')) // true
448
+ * isRegExp(/test/gi) // true
449
+ * isRegExp("/test/") // false (string)
450
+ * isRegExp({ test: () => {} }) // false
451
+ * isRegExp(null) // false
452
+ * ```
453
+ *
454
+ * @usage
455
+ * ```typescript
456
+ * // Validate pattern matchers
457
+ * function buildMatcher(pattern: unknown): (str: string) => boolean {
458
+ * if (isRegExp(pattern)) {
459
+ * return (str: string) => pattern.test(str);
460
+ * } else if (isString(pattern)) {
461
+ * const regex = new RegExp(pattern);
462
+ * return (str: string) => regex.test(str);
463
+ * } else {
464
+ * throw new TypeError('Pattern must be RegExp or string');
465
+ * }
466
+ * }
467
+ *
468
+ * // Route matching with regex validation
469
+ * interface Route {
470
+ * pattern: unknown;
471
+ * handler: (match: string) => void;
472
+ * }
473
+ *
474
+ * function createRouter(routes: Route[]) {
475
+ * return {
476
+ * match(path: string) {
477
+ * for (const route of routes) {
478
+ * if (isRegExp(route.pattern)) {
479
+ * const matches = path.match(route.pattern);
480
+ * if (matches) {
481
+ * route.handler(path);
482
+ * return true;
483
+ * }
484
+ * }
485
+ * }
486
+ * return false;
487
+ * }
488
+ * };
489
+ * }
490
+ * ```
491
+ */
492
+ export function isRegExp(value) {
493
+ return value instanceof RegExp;
494
+ }
495
+ /**
496
+ * Checks if a value is null or undefined.
497
+ *
498
+ * Handy shortcut for checking nullish values. Use together with other guards
499
+ * for optional property validation.
500
+ *
501
+ * @param value - The value to check
502
+ * @returns true if value is null or undefined
503
+ *
504
+ * @example
505
+ * ```typescript
506
+ * isNullOrUndefined(null) // true
507
+ * isNullOrUndefined(undefined) // true
508
+ * isNullOrUndefined(0) // false
509
+ * isNullOrUndefined("") // false
510
+ * isNullOrUndefined(false) // false
511
+ * ```
512
+ *
513
+ * @usage
514
+ * ```typescript
515
+ * // Optional property validation
516
+ * interface User {
517
+ * id: number;
518
+ * email?: string;
519
+ * }
520
+ *
521
+ * function validateUser(data: unknown): User | null {
522
+ * if (!isObject(data)) return null;
523
+ *
524
+ * const { id, email } = data as any;
525
+ *
526
+ * if (!isNumber(id)) return null;
527
+ * if (!isNullOrUndefined(email) && !isString(email)) {
528
+ * return null;
529
+ * }
530
+ *
531
+ * return { id, email };
532
+ * }
533
+ *
534
+ * // Provide defaults for undefined
535
+ * function getWithDefault<T>(value: T | null | undefined, defaultValue: T): T {
536
+ * return isNullOrUndefined(value) ? defaultValue : value;
537
+ * }
538
+ * ```
539
+ */
540
+ export function isNullOrUndefined(value) {
541
+ return value === null || value === undefined;
542
+ }
543
+ /**
544
+ * Checks if a value is not null or undefined (type guard for non-nullish).
545
+ *
546
+ * Useful in filter operations and optional chaining scenarios.
547
+ *
548
+ * @param value - The value to check
549
+ * @returns true if value is not null or undefined
550
+ *
551
+ * @example
552
+ * ```typescript
553
+ * isDefined("text") // true
554
+ * isDefined(0) // true
555
+ * isDefined(false) // true
556
+ * isDefined([]) // true
557
+ * isDefined(null) // false
558
+ * isDefined(undefined) // false
559
+ * ```
560
+ *
561
+ * @usage
562
+ * ```typescript
563
+ * // Filter nullish values from arrays
564
+ * const values = [1, null, 2, undefined, 3];
565
+ * const filtered = values.filter(isDefined);
566
+ * // Result: [1, 2, 3]
567
+ *
568
+ * // Type narrowing in conditional
569
+ * function process(value: string | null) {
570
+ * if (isDefined(value)) {
571
+ * // TypeScript knows value is string (never null)
572
+ * console.log(value.toUpperCase());
573
+ * }
574
+ * }
575
+ * ```
576
+ */
577
+ export function isDefined(value) {
578
+ return value !== null && value !== undefined;
579
+ }
580
+ /**
581
+ * Checks if an object has a specific property (type guard for discriminated unions).
582
+ *
583
+ * Enables property-based type narrowing for discriminated unions and polymorphic types.
584
+ *
585
+ * @param obj - The object to check
586
+ * @param prop - The property name to look for
587
+ * @returns true if object has the property
588
+ *
589
+ * @example
590
+ * ```typescript
591
+ * interface Success { success: true; data: string }
592
+ * interface Error { error: true; message: string }
593
+ * type Result = Success | Error;
594
+ *
595
+ * const result: Result = { success: true, data: "ok" };
596
+ *
597
+ * if (hasProperty(result, 'success')) {
598
+ * // TypeScript narrows to Success
599
+ * console.log(result.data);
600
+ * }
601
+ * ```
602
+ *
603
+ * @usage
604
+ * ```typescript
605
+ * // Discriminate union types
606
+ * function handleResult(result: unknown) {
607
+ * if (isObject(result)) {
608
+ * if (hasProperty(result, 'error')) {
609
+ * console.error(result.error);
610
+ * } else if (hasProperty(result, 'data')) {
611
+ * console.log(result.data);
612
+ * }
613
+ * }
614
+ * }
615
+ * ```
616
+ */
617
+ export function hasProperty(obj, prop) {
618
+ return prop in obj;
619
+ }
620
+ /**
621
+ * Type guard for array elements of a specific type.
622
+ *
623
+ * Validates all array elements match a predicate function.
624
+ *
625
+ * @param arr - The array to check
626
+ * @param guard - The type guard function for elements
627
+ * @returns true if all elements pass the guard
628
+ *
629
+ * @example
630
+ * ```typescript
631
+ * const data = [1, 2, 3];
632
+ * if (isArrayOf(data, isNumber)) {
633
+ * // TypeScript knows data is number[]
634
+ * const sum = data.reduce((a, b) => a + b, 0);
635
+ * }
636
+ * ```
637
+ *
638
+ * @usage
639
+ * ```typescript
640
+ * // Validate API response array
641
+ * interface User { id: number; name: string }
642
+ *
643
+ * function isUser(obj: unknown): obj is User {
644
+ * return isObject(obj) &&
645
+ * isNumber((obj as any).id) &&
646
+ * isString((obj as any).name);
647
+ * }
648
+ *
649
+ * function processUsers(data: unknown) {
650
+ * if (isArray(data) && isArrayOf(data, isUser)) {
651
+ * // TypeScript knows data is User[]
652
+ * data.forEach(user => {
653
+ * console.log(user.name);
654
+ * });
655
+ * }
656
+ * }
657
+ * ```
658
+ */
659
+ export function isArrayOf(arr, guard) {
660
+ return Array.isArray(arr) && arr.every(guard);
661
+ }
662
+ //# sourceMappingURL=type-guards.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-guards.js","sourceRoot":"","sources":["../../src/utils/type-guards.ts"],"names":[],"mappings":"AAAA;;;;;;;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