@isdk/util 0.1.1 → 0.1.3

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 (99) hide show
  1. package/dist/index.d.mts +344 -11
  2. package/dist/index.d.ts +344 -11
  3. package/dist/index.js +1 -1
  4. package/dist/index.mjs +1 -1
  5. package/docs/classes/ConfigFile.md +9 -9
  6. package/docs/functions/getMultiLevelExtname.md +1 -1
  7. package/docs/functions/glob.md +1 -1
  8. package/docs/functions/isStringIn.md +1 -1
  9. package/docs/functions/normalizeIncludeFiles.md +1 -1
  10. package/docs/functions/parseFrontMatter.md +1 -1
  11. package/docs/functions/parseYaml.md +1 -1
  12. package/docs/functions/registerYamlTag.md +1 -1
  13. package/docs/functions/removeLeadingEmptyLines.md +1 -1
  14. package/docs/functions/stringifyYaml.md +1 -1
  15. package/docs/functions/toCamelCase.md +37 -0
  16. package/docs/functions/toCapitalCase.md +38 -0
  17. package/docs/functions/toPascalCase.md +37 -0
  18. package/docs/functions/traverseFolder.md +1 -1
  19. package/docs/functions/traverseFolderSync.md +1 -1
  20. package/docs/globals.md +5 -0
  21. package/docs/interfaces/IncludeFiles.md +3 -3
  22. package/docs/interfaces/LoadConfigFileOptions.md +25 -0
  23. package/docs/type-aliases/StringifyFunc.md +21 -0
  24. package/docs/type-aliases/TraverseFolderHandler.md +1 -1
  25. package/docs/type-aliases/TraverseFolderSyncHandler.md +1 -1
  26. package/docs/variables/DefaultAllTextFiles.md +1 -1
  27. package/package.json +1 -1
  28. package/dist/chunk-3TCMKKX2.mjs +0 -1
  29. package/dist/chunk-46TT3NZV.mjs +0 -1
  30. package/dist/chunk-7APJBRA7.mjs +0 -1
  31. package/dist/chunk-7AQU3PBW.mjs +0 -1
  32. package/dist/chunk-D3BCI5SM.mjs +0 -1
  33. package/dist/chunk-FXF2KJLF.mjs +0 -1
  34. package/dist/chunk-GGOKSJK4.mjs +0 -1
  35. package/dist/chunk-JTTOXXPG.mjs +0 -1
  36. package/dist/chunk-LOMQZH4U.mjs +0 -292
  37. package/dist/chunk-OTEAFARC.mjs +0 -1
  38. package/dist/chunk-UJZVJIGI.mjs +0 -1
  39. package/dist/config-file.d.mts +0 -75
  40. package/dist/config-file.d.ts +0 -75
  41. package/dist/config-file.js +0 -1
  42. package/dist/config-file.mjs +0 -1
  43. package/dist/config-file.test.d.mts +0 -2
  44. package/dist/config-file.test.d.ts +0 -2
  45. package/dist/config-file.test.js +0 -292
  46. package/dist/config-file.test.mjs +0 -1
  47. package/dist/front-matter.d.mts +0 -6
  48. package/dist/front-matter.d.ts +0 -6
  49. package/dist/front-matter.js +0 -1
  50. package/dist/front-matter.mjs +0 -1
  51. package/dist/front-matter.test.d.mts +0 -2
  52. package/dist/front-matter.test.d.ts +0 -2
  53. package/dist/front-matter.test.js +0 -1
  54. package/dist/front-matter.test.mjs +0 -1
  55. package/dist/get-multi-level-extname.d.mts +0 -9
  56. package/dist/get-multi-level-extname.d.ts +0 -9
  57. package/dist/get-multi-level-extname.js +0 -1
  58. package/dist/get-multi-level-extname.mjs +0 -1
  59. package/dist/glob.d.mts +0 -28
  60. package/dist/glob.d.ts +0 -28
  61. package/dist/glob.js +0 -1
  62. package/dist/glob.mjs +0 -1
  63. package/dist/glob.test.d.mts +0 -2
  64. package/dist/glob.test.d.ts +0 -2
  65. package/dist/glob.test.js +0 -292
  66. package/dist/glob.test.mjs +0 -1
  67. package/dist/include-files.d.mts +0 -31
  68. package/dist/include-files.d.ts +0 -31
  69. package/dist/include-files.js +0 -1
  70. package/dist/include-files.mjs +0 -1
  71. package/dist/include-files.test.d.mts +0 -2
  72. package/dist/include-files.test.d.ts +0 -2
  73. package/dist/include-files.test.js +0 -292
  74. package/dist/include-files.test.mjs +0 -1
  75. package/dist/is-string-in.d.mts +0 -19
  76. package/dist/is-string-in.d.ts +0 -19
  77. package/dist/is-string-in.js +0 -1
  78. package/dist/is-string-in.mjs +0 -1
  79. package/dist/magic-string.es-TUA2PMWY.mjs +0 -1
  80. package/dist/remove-leading-empty-lines.d.mts +0 -18
  81. package/dist/remove-leading-empty-lines.d.ts +0 -18
  82. package/dist/remove-leading-empty-lines.js +0 -1
  83. package/dist/remove-leading-empty-lines.mjs +0 -1
  84. package/dist/remove-leading-empty-lines.test.d.mts +0 -2
  85. package/dist/remove-leading-empty-lines.test.d.ts +0 -2
  86. package/dist/remove-leading-empty-lines.test.js +0 -1
  87. package/dist/remove-leading-empty-lines.test.mjs +0 -1
  88. package/dist/traverse-folder.d.mts +0 -56
  89. package/dist/traverse-folder.d.ts +0 -56
  90. package/dist/traverse-folder.js +0 -1
  91. package/dist/traverse-folder.mjs +0 -1
  92. package/dist/traverse-folder.test.d.mts +0 -2
  93. package/dist/traverse-folder.test.d.ts +0 -2
  94. package/dist/traverse-folder.test.js +0 -292
  95. package/dist/traverse-folder.test.mjs +0 -1
  96. package/dist/yaml.d.mts +0 -59
  97. package/dist/yaml.d.ts +0 -59
  98. package/dist/yaml.js +0 -1
  99. package/dist/yaml.mjs +0 -1
package/dist/index.d.mts CHANGED
@@ -1,11 +1,344 @@
1
- export { ConfigFile } from './config-file.mjs';
2
- export { glob } from './glob.mjs';
3
- export { isStringIn } from './is-string-in.mjs';
4
- export { DefaultAllTextFiles, IncludeFiles, normalizeIncludeFiles } from './include-files.mjs';
5
- export { parseFrontMatter } from './front-matter.mjs';
6
- export { getMultiLevelExtname } from './get-multi-level-extname.mjs';
7
- export { removeLeadingEmptyLines } from './remove-leading-empty-lines.mjs';
8
- export { TraverseFolderHandler, TraverseFolderSyncHandler, traverseFolder, traverseFolderSync } from './traverse-folder.mjs';
9
- export { parseYaml, registerYamlTag, stringifyYaml } from './yaml.mjs';
10
- import 'fs';
11
- import 'yaml';
1
+ import { Dirent } from 'fs';
2
+ import { ParseOptions, DocumentOptions, SchemaOptions, ToJSOptions, CreateNodeOptions, ToStringOptions } from 'yaml';
3
+
4
+ type StringifyFunc = (content: any) => string;
5
+ interface LoadConfigFileOptions {
6
+ extLevel?: number;
7
+ externalFile?: string;
8
+ }
9
+ /**
10
+ * Represents a configuration file utility class that provides methods to load and save configuration files.
11
+ * It supports multiple file formats such as YAML, JSON, etc., by registering corresponding parsers and stringifiers.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // Register a custom file type handler
16
+ * ConfigFile.register('.custom', (content) => {
17
+ * return { data: content.toUpperCase() }; // Example parser
18
+ * }, (obj) => {
19
+ * return obj.data.toLowerCase(); // Example stringifier
20
+ * });
21
+ *
22
+ * // Save a configuration file
23
+ * ConfigFile.save('config.custom', { key: 'value' });
24
+ *
25
+ * // Load a configuration file
26
+ * const config = ConfigFile.load('config.custom');
27
+ * console.log(config); // Output: { key: 'value' }
28
+ * ```
29
+ */
30
+ declare class ConfigFile {
31
+ /**
32
+ * A record of registered stringify functions for different file extensions.
33
+ */
34
+ static stringifys: Record<string, StringifyFunc>;
35
+ /**
36
+ * Registers a parser and stringifier for specific file extensions.
37
+ *
38
+ * @param extname - The file extension(s) to register the parser and stringifier for.
39
+ * @param parser - A function that parses the file content into an object.
40
+ * @param stringify - A function that converts an object back into file content.
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * ConfigFile.register(['.json'], JSON.parse, (obj) => JSON.stringify(obj, null, 2));
45
+ * ```
46
+ */
47
+ static register(extname: string | string[], parser: (content: string) => any, stringify: StringifyFunc): void;
48
+ /**
49
+ * Loads a configuration file based on the provided filename and options.
50
+ *
51
+ * @param filename - The path to the configuration file.
52
+ * @param options - Additional options for loading the configuration file.
53
+ * @returns The parsed configuration object.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const config = ConfigFile.load('config.yaml');
58
+ * console.log(config); // Output: { key: 'value' }
59
+ * ```
60
+ */
61
+ static load(filename: string, options?: LoadConfigFileOptions): any;
62
+ /**
63
+ * Saves a configuration object to a file with the specified filename and options.
64
+ *
65
+ * @param filename - The path where the configuration file should be saved.
66
+ * @param config - The configuration object to save.
67
+ * @param options - Additional options for saving the configuration file.
68
+ * @returns The final filename where the configuration was saved.
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * ConfigFile.save('config.json', { key: 'value' });
73
+ * ```
74
+ */
75
+ static save(filename: string, config: any, options?: LoadConfigFileOptions): string;
76
+ }
77
+
78
+ /**
79
+ * Matches a file path against a list of glob patterns.
80
+ *
81
+ * This function checks if the given `filepath` matches any of the provided glob `pattern`s.
82
+ * If a `rootDir` is specified, the `filepath` will first be converted to a relative path
83
+ * based on the `rootDir`.
84
+ *
85
+ * @param filepath - The file path to match.
86
+ * @param pattern - An array of glob patterns to match against.
87
+ * @param rootDir - (Optional) The root directory used to calculate the relative path of `filepath`.
88
+ *
89
+ * @returns Returns `true` if the `filepath` matches any of the patterns, otherwise `false`.
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * import { glob } from './glob';
94
+ *
95
+ * const filepath = '/home/user/project/src/index.ts';
96
+ * const patterns = ['src/*.ts', '!src/test.ts'];
97
+ * const rootDir = '/home/user/project';
98
+ *
99
+ * const result = glob(filepath, patterns, rootDir);
100
+ * console.log(result); // true
101
+ * ```
102
+ */
103
+ declare function glob(filepath: string, pattern: string[], rootDir?: string): boolean | undefined;
104
+
105
+ /**
106
+ * Checks if a given string exists within an array of strings or a single string.
107
+ *
108
+ * @param str - The string to search for.
109
+ * @param arr - An array of strings or a single string to search within.
110
+ * @returns Returns `true` if the string is found, otherwise `false`.
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * const result = isStringIn("apple", ["banana", "apple", "cherry"]);
115
+ * console.log(result); // true
116
+ *
117
+ * const singleResult = isStringIn("hello", "hello");
118
+ * console.log(singleResult); // true
119
+ * ```
120
+ */
121
+ declare function isStringIn(str: string, arr: string[] | string): boolean;
122
+
123
+ declare const DefaultAllTextFiles: string[];
124
+ interface IncludeFiles {
125
+ include?: string[];
126
+ exclude?: string[];
127
+ }
128
+ /**
129
+ * Normalizes a list of file patterns for glob matching.
130
+ *
131
+ * This function takes either an array of file patterns or an object containing `include` and `exclude` arrays,
132
+ * and returns a normalized array of file patterns. If no patterns are provided, it defaults to including
133
+ * all text-based files as defined in `DefaultAllTextFiles`.
134
+ *
135
+ * @param files - Either an array of file patterns or an object with `include` and `exclude` properties.
136
+ * @param defaultFiles - An optional array of default file patterns to use if no include patterns are specified.
137
+ * Defaults to `DefaultAllTextFiles`.
138
+ * @returns A normalized array of file patterns, with excluded patterns prefixed by `!`.
139
+ *
140
+ * @example
141
+ *
142
+ * ```typescript
143
+ * const result = normalizeIncludeFiles({
144
+ * include: ['*.ts', '*.js'],
145
+ * exclude: ['node_modules/**']
146
+ * });
147
+ * console.log(result);
148
+ * // Output: ['*.ts', '*.js', '!node_modules/**']
149
+ * ```
150
+ */
151
+ declare function normalizeIncludeFiles(files?: string[] | IncludeFiles, defaultFiles?: string[]): string[];
152
+
153
+ declare function parseFrontMatter(value: string, delimiter?: string): {
154
+ data: any;
155
+ content: string;
156
+ };
157
+
158
+ /**
159
+ * Retrieves multi-level file extension
160
+ * @param filename The file name
161
+ * @param level The level, default is 1, indicating the number of extension levels to retrieve
162
+ * @returns Returns a concatenated string of multiple extensions, or an empty string if no extension is found
163
+ */
164
+ declare function getMultiLevelExtname(filename: string, level?: number): string;
165
+
166
+ /**
167
+ * Removes all leading empty lines or "#" comments line from the given string.
168
+ *
169
+ * This function trims any sequence of empty lines that appear at the beginning
170
+ * of the input string. It accounts for different newline characters including
171
+ * '\n', '\r\n', and also considers lines that contain only whitespace characters.
172
+ *
173
+ * @param text - The string from which to remove leading empty lines.
174
+ * @returns The string with leading empty lines removed.
175
+ *
176
+ * @example
177
+ * const sampleText = '# This is a comment\n\nHello, world!\n';
178
+ * const cleanedText = removeLeadingEmptyLines(sampleText);
179
+ * // cleanedText is now 'Hello, world!\n'
180
+ */
181
+ declare function removeLeadingEmptyLines(text: string): string;
182
+
183
+ /**
184
+ * A handler function for asynchronous folder traversal.
185
+ * @param filePath - The full path of the file or directory.
186
+ * @param entry - The directory entry (file or folder).
187
+ * @returns Returns `true` to stop further traversal, or `false`/`void` to continue.
188
+ */
189
+ type TraverseFolderHandler = (filePath: string, entry: Dirent) => boolean | void | Promise<boolean | void>;
190
+ /**
191
+ * A handler function for synchronous folder traversal.
192
+ * @param filePath - The full path of the file or directory.
193
+ * @param entry - The directory entry (file or folder).
194
+ * @returns Returns `true` to stop further traversal, or `false` to continue.
195
+ */
196
+ type TraverseFolderSyncHandler = (filePath: string, entry: Dirent) => boolean | void;
197
+ /**
198
+ * Traverses a folder asynchronously and applies a handler to each file or directory.
199
+ *
200
+ * @param directoryPath - The root directory to start traversal.
201
+ * @param fileHandler - A handler function called for each file/directory.
202
+ *
203
+ * @example
204
+ * ```typescript
205
+ * await traverseFolder('/path/to/folder', async (filePath, entry) => {
206
+ * console.log(`Found: ${filePath}`);
207
+ * if (entry.name === 'stopfile.txt') {
208
+ * console.log('Stopping traversal...');
209
+ * return true; // Stops further traversal
210
+ * }
211
+ * return false; // Continues traversal
212
+ * });
213
+ * ```
214
+ */
215
+ declare function traverseFolder(directoryPath: string, fileHandler: TraverseFolderHandler): Promise<void>;
216
+ /**
217
+ * Traverses a folder synchronously and applies a handler to each file or directory.
218
+ *
219
+ * @param directoryPath - The root directory to start traversal.
220
+ * @param fileHandler - A handler function called for each file/directory.
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * traverseFolderSync('/path/to/folder', (filePath, entry) => {
225
+ * console.log(`Found: ${filePath}`);
226
+ * if (entry.name === 'stopfile.txt') {
227
+ * console.log('Stopping traversal...');
228
+ * return true; // Stops further traversal
229
+ * }
230
+ * return false; // Continues traversal
231
+ * });
232
+ * ```
233
+ */
234
+ declare function traverseFolderSync(directoryPath: string, fileHandler: TraverseFolderSyncHandler): void;
235
+
236
+ /**
237
+ * Registers custom YAML tags to be used in parsing and stringifying YAML content.
238
+ *
239
+ * @param tags - A single tag or an array of tags to register.
240
+ *
241
+ * @example
242
+ * ```typescript
243
+ * import { registerYamlTag } from './yaml';
244
+ *
245
+ * const customTag = {
246
+ * tag: '!custom',
247
+ * resolve: (value) => `resolved-${value}`,
248
+ * };
249
+ * registerYamlTag(customTag);
250
+ * ```
251
+ */
252
+ declare function registerYamlTag(tags: any): void;
253
+ /**
254
+ * Parses a YAML string into a JavaScript object with optional custom tags.
255
+ *
256
+ * @param content - The YAML string to parse.
257
+ * @param options - Optional parsing options, including custom tags.
258
+ * @returns The parsed JavaScript object.
259
+ *
260
+ * @example
261
+ * ```typescript
262
+ * import { parseYaml } from './yaml';
263
+ *
264
+ * const yamlContent = `
265
+ * example: !custom value
266
+ * `;
267
+ * const result = parseYaml(yamlContent);
268
+ * console.log(result); // { example: 'resolved-value' }
269
+ * ```
270
+ */
271
+ declare function parseYaml(content: string, options?: ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions): any;
272
+ /**
273
+ * Converts a JavaScript object into a YAML string with optional custom tags.
274
+ *
275
+ * @param content - The JavaScript object to convert to YAML.
276
+ * @param options - Optional stringification options, including custom tags.
277
+ * @returns The YAML string representation of the input object.
278
+ *
279
+ * @example
280
+ * ```typescript
281
+ * import { stringifyYaml } from './yaml';
282
+ *
283
+ * const data = {
284
+ * example: 'value',
285
+ * };
286
+ * const yamlString = stringifyYaml(data);
287
+ * console.log(yamlString); // "example: value\n"
288
+ * ```
289
+ */
290
+ declare function stringifyYaml(content: any, options?: DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions & ToStringOptions): string;
291
+
292
+ /**
293
+ * Converts a string to PascalCase.
294
+ *
295
+ * @param str - The string to convert.
296
+ * @returns The PascalCase version of the string.
297
+ *
298
+ * @example
299
+ * ```typescript
300
+ * console.log(toPascalCase("some-word")); // Output: "SomeWord"
301
+ * console.log(toPascalCase("some_Word")); // Output: "SomeWord"
302
+ * console.log(toPascalCase("someWord")); // Output: "SomeWord"
303
+ * console.log(toPascalCase("some word")); // Output: "SomeWord"
304
+ * console.log(toPascalCase("SOME_WORD")); // Output: "SomeWord"
305
+ * ```
306
+ */
307
+ declare function toPascalCase(str: string): string;
308
+
309
+ /**
310
+ * Converts a string to camelCase.
311
+ *
312
+ * @param str - The string to convert.
313
+ * @returns The camelCase version of the string.
314
+ *
315
+ * @example
316
+ * ```typescript
317
+ * console.log(toCamelCase("some-word")); // Output: "someWord"
318
+ * console.log(toCamelCase("some_Word")); // Output: "someWord"
319
+ * console.log(toCamelCase("Some Word")); // Output: "someWord"
320
+ * console.log(toCamelCase("SomeWord")); // Output: "someWord"
321
+ * console.log(toCamelCase("")); // Output: ""
322
+ * ```
323
+ */
324
+ declare function toCamelCase(str: string): string;
325
+
326
+ /**
327
+ * Converts a string to capital case, where the first letter of each word is capitalized
328
+ * and the rest are in lowercase. Words are separated by spaces, hyphens, or underscores.
329
+ *
330
+ * @param str - The input string to convert.
331
+ * @returns The converted string in capital case.
332
+ *
333
+ * @example
334
+ * ```typescript
335
+ * toCapitalCase('hello world'); // Returns 'Hello World'
336
+ * toCapitalCase('HELLO-WORLD'); // Returns 'Hello World'
337
+ * toCapitalCase('hello_world'); // Returns 'Hello World'
338
+ * toCapitalCase('hElLo_wOrLd-TeSt cAsE'); // Returns 'H El Lo W Or Ld Te St C As E'
339
+ * toCapitalCase(''); // Returns ''
340
+ * ```
341
+ */
342
+ declare function toCapitalCase(str: string): string;
343
+
344
+ export { ConfigFile, DefaultAllTextFiles, type IncludeFiles, type LoadConfigFileOptions, type StringifyFunc, type TraverseFolderHandler, type TraverseFolderSyncHandler, getMultiLevelExtname, glob, isStringIn, normalizeIncludeFiles, parseFrontMatter, parseYaml, registerYamlTag, removeLeadingEmptyLines, stringifyYaml, toCamelCase, toCapitalCase, toPascalCase, traverseFolder, traverseFolderSync };