@isdk/util 0.1.5 → 0.1.6

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 (37) hide show
  1. package/dist/index.d.mts +81 -1
  2. package/dist/index.d.ts +81 -1
  3. package/dist/index.js +1 -1
  4. package/dist/index.mjs +1 -1
  5. package/docs/classes/ConfigFile.md +5 -5
  6. package/docs/functions/extNameLevel.md +43 -0
  7. package/docs/functions/filenameReservedRegex.md +20 -0
  8. package/docs/functions/getMultiLevelExtname.md +1 -1
  9. package/docs/functions/glob.md +1 -1
  10. package/docs/functions/isStringIn.md +1 -1
  11. package/docs/functions/isValidFilename.md +38 -0
  12. package/docs/functions/isValidFilepath.md +27 -0
  13. package/docs/functions/normalizeIncludeFiles.md +1 -1
  14. package/docs/functions/parseFrontMatter.md +1 -1
  15. package/docs/functions/parseYaml.md +1 -1
  16. package/docs/functions/reControlCharsRegex.md +19 -0
  17. package/docs/functions/registerYamlTag.md +1 -1
  18. package/docs/functions/removeLeadingEmptyLines.md +1 -1
  19. package/docs/functions/sanitizeFilename.md +39 -0
  20. package/docs/functions/sanitizeFilepath.md +33 -0
  21. package/docs/functions/stringifyYaml.md +1 -1
  22. package/docs/functions/toCamelCase.md +1 -1
  23. package/docs/functions/toCapitalCase.md +1 -1
  24. package/docs/functions/toPascalCase.md +1 -1
  25. package/docs/functions/traverseFolder.md +1 -1
  26. package/docs/functions/traverseFolderSync.md +1 -1
  27. package/docs/globals.md +10 -0
  28. package/docs/interfaces/IncludeFiles.md +3 -3
  29. package/docs/interfaces/LoadConfigFileOptions.md +3 -3
  30. package/docs/interfaces/SanitizeFilenameOptions.md +25 -0
  31. package/docs/type-aliases/StringifyFunc.md +1 -1
  32. package/docs/type-aliases/TraverseFolderHandler.md +1 -1
  33. package/docs/type-aliases/TraverseFolderSyncHandler.md +1 -1
  34. package/docs/variables/DefaultAllTextFiles.md +1 -1
  35. package/docs/variables/FilenameReservedRegex.md +14 -0
  36. package/docs/variables/WindowsReservedNameRegex.md +13 -0
  37. package/package.json +2 -1
package/dist/index.d.mts CHANGED
@@ -341,4 +341,84 @@ declare function toCamelCase(str: string): string;
341
341
  */
342
342
  declare function toCapitalCase(str: string): string;
343
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 };
344
+ /**
345
+ * Regular expression pattern for reserved characters in a filename.
346
+ * do not use /g global option here: when the regex is executed multiple times, it will always begin where it left off last time.
347
+ */
348
+ declare const FilenameReservedRegex: RegExp;
349
+ /**
350
+ * Regular expression pattern for reserved names on Windows systems.
351
+ */
352
+ declare const WindowsReservedNameRegex: RegExp;
353
+ /**
354
+ * Returns a new regular expression instance for reserved filename characters with the 'g' flag.
355
+ * use this to reset the with global option
356
+ * @returns A compiled regular expression for reserved filename characters.
357
+ */
358
+ declare function filenameReservedRegex(): RegExp;
359
+ /**
360
+ * Returns a new regular expression instance for control characters in a filename with the 'g' flag.
361
+ * @returns A compiled regular expression for control characters in a filename.
362
+ */
363
+ declare function reControlCharsRegex(): RegExp;
364
+ /**
365
+ * Validates if a given string is a valid filename.
366
+ * @param {string} filename - The filename to be validated.
367
+ * @returns True if the filename is valid, false otherwise.
368
+ * @throws {TypeError} If the input is not a string.
369
+ * @example
370
+ * isValidFilename('myFile.txt'); // Returns true
371
+ * isValidFilename('my<file.txt'); // Returns false
372
+ */
373
+ declare function isValidFilename(filename: string): boolean | "";
374
+ /**
375
+ * Validates whether the given filepath is valid.
376
+ * @param filepath - The filepath to be checked, represented as a string.
377
+ * @returns A boolean indicating whether the filepath is valid. Returns true if valid; false otherwise.
378
+ */
379
+ declare function isValidFilepath(filepath: string): boolean;
380
+ interface SanitizeFilenameOptions {
381
+ replacement?: string;
382
+ maxLength?: number;
383
+ }
384
+ /**
385
+ * Sanitizes a given filename by replacing invalid characters with a specified replacement character or a default.
386
+ * @param filename - The filename to sanitize, represented as a string.
387
+ * @param options - An optional object containing configuration options:
388
+ * - `replacement` {string} - The character to replace invalid characters with. Default is '!'. Cannot contain reserved filename characters.
389
+ * - `maxLength` {number} - The maximum length of the sanitized filename. Default is `MAX_FILENAME_LENGTH`.
390
+ * @returns The sanitized filename.
391
+ * @throws {Error} - If the `replacement` contains reserved filename characters or control characters.
392
+ */
393
+ declare function sanitizeFilename(filename: string, options?: SanitizeFilenameOptions): string;
394
+ /**
395
+ * Sanitizes each part of a file path and reassembles it, ensuring the path is valid according to provided options.
396
+ * @param filepath - The file path to sanitize, represented as a string.
397
+ * @param options - Optional settings for sanitization, extending `SanitizeFilenameOptions`. Allows customization of replacement characters and maximum filename length.
398
+ * @returns The sanitized file path as a string.
399
+ */
400
+ declare function sanitizeFilepath(filepath: string, options?: SanitizeFilenameOptions): string;
401
+ /**
402
+ * Calculates the level of an extension name.
403
+ *
404
+ * The level represents the number of dots ('.') in the extension name, excluding the first dot which separates
405
+ * the base filename from the extension. For example, the level of ".txt" is 1, and the level of ".tar.gz" is 2.
406
+ *
407
+ * @param extName - The extension name to analyze. It should start with a dot ('.').
408
+ * @returns The level of the extension name, which is the count of dots minus one.
409
+ *
410
+ * @example
411
+ * ```typescript
412
+ * // Returns 0
413
+ * extNameLevel("no-file-ext");
414
+ *
415
+ * // Returns 2
416
+ * extNameLevel(".tar.gz");
417
+ *
418
+ * // Returns 1
419
+ * extNameLevel(".json5");
420
+ * ```
421
+ */
422
+ declare function extNameLevel(extName: string): number;
423
+
424
+ export { ConfigFile, DefaultAllTextFiles, FilenameReservedRegex, type IncludeFiles, type LoadConfigFileOptions, type SanitizeFilenameOptions, type StringifyFunc, type TraverseFolderHandler, type TraverseFolderSyncHandler, WindowsReservedNameRegex, extNameLevel, filenameReservedRegex, getMultiLevelExtname, glob, isStringIn, isValidFilename, isValidFilepath, normalizeIncludeFiles, parseFrontMatter, parseYaml, reControlCharsRegex, registerYamlTag, removeLeadingEmptyLines, sanitizeFilename, sanitizeFilepath, stringifyYaml, toCamelCase, toCapitalCase, toPascalCase, traverseFolder, traverseFolderSync };
package/dist/index.d.ts CHANGED
@@ -341,4 +341,84 @@ declare function toCamelCase(str: string): string;
341
341
  */
342
342
  declare function toCapitalCase(str: string): string;
343
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 };
344
+ /**
345
+ * Regular expression pattern for reserved characters in a filename.
346
+ * do not use /g global option here: when the regex is executed multiple times, it will always begin where it left off last time.
347
+ */
348
+ declare const FilenameReservedRegex: RegExp;
349
+ /**
350
+ * Regular expression pattern for reserved names on Windows systems.
351
+ */
352
+ declare const WindowsReservedNameRegex: RegExp;
353
+ /**
354
+ * Returns a new regular expression instance for reserved filename characters with the 'g' flag.
355
+ * use this to reset the with global option
356
+ * @returns A compiled regular expression for reserved filename characters.
357
+ */
358
+ declare function filenameReservedRegex(): RegExp;
359
+ /**
360
+ * Returns a new regular expression instance for control characters in a filename with the 'g' flag.
361
+ * @returns A compiled regular expression for control characters in a filename.
362
+ */
363
+ declare function reControlCharsRegex(): RegExp;
364
+ /**
365
+ * Validates if a given string is a valid filename.
366
+ * @param {string} filename - The filename to be validated.
367
+ * @returns True if the filename is valid, false otherwise.
368
+ * @throws {TypeError} If the input is not a string.
369
+ * @example
370
+ * isValidFilename('myFile.txt'); // Returns true
371
+ * isValidFilename('my<file.txt'); // Returns false
372
+ */
373
+ declare function isValidFilename(filename: string): boolean | "";
374
+ /**
375
+ * Validates whether the given filepath is valid.
376
+ * @param filepath - The filepath to be checked, represented as a string.
377
+ * @returns A boolean indicating whether the filepath is valid. Returns true if valid; false otherwise.
378
+ */
379
+ declare function isValidFilepath(filepath: string): boolean;
380
+ interface SanitizeFilenameOptions {
381
+ replacement?: string;
382
+ maxLength?: number;
383
+ }
384
+ /**
385
+ * Sanitizes a given filename by replacing invalid characters with a specified replacement character or a default.
386
+ * @param filename - The filename to sanitize, represented as a string.
387
+ * @param options - An optional object containing configuration options:
388
+ * - `replacement` {string} - The character to replace invalid characters with. Default is '!'. Cannot contain reserved filename characters.
389
+ * - `maxLength` {number} - The maximum length of the sanitized filename. Default is `MAX_FILENAME_LENGTH`.
390
+ * @returns The sanitized filename.
391
+ * @throws {Error} - If the `replacement` contains reserved filename characters or control characters.
392
+ */
393
+ declare function sanitizeFilename(filename: string, options?: SanitizeFilenameOptions): string;
394
+ /**
395
+ * Sanitizes each part of a file path and reassembles it, ensuring the path is valid according to provided options.
396
+ * @param filepath - The file path to sanitize, represented as a string.
397
+ * @param options - Optional settings for sanitization, extending `SanitizeFilenameOptions`. Allows customization of replacement characters and maximum filename length.
398
+ * @returns The sanitized file path as a string.
399
+ */
400
+ declare function sanitizeFilepath(filepath: string, options?: SanitizeFilenameOptions): string;
401
+ /**
402
+ * Calculates the level of an extension name.
403
+ *
404
+ * The level represents the number of dots ('.') in the extension name, excluding the first dot which separates
405
+ * the base filename from the extension. For example, the level of ".txt" is 1, and the level of ".tar.gz" is 2.
406
+ *
407
+ * @param extName - The extension name to analyze. It should start with a dot ('.').
408
+ * @returns The level of the extension name, which is the count of dots minus one.
409
+ *
410
+ * @example
411
+ * ```typescript
412
+ * // Returns 0
413
+ * extNameLevel("no-file-ext");
414
+ *
415
+ * // Returns 2
416
+ * extNameLevel(".tar.gz");
417
+ *
418
+ * // Returns 1
419
+ * extNameLevel(".json5");
420
+ * ```
421
+ */
422
+ declare function extNameLevel(extName: string): number;
423
+
424
+ export { ConfigFile, DefaultAllTextFiles, FilenameReservedRegex, type IncludeFiles, type LoadConfigFileOptions, type SanitizeFilenameOptions, type StringifyFunc, type TraverseFolderHandler, type TraverseFolderSyncHandler, WindowsReservedNameRegex, extNameLevel, filenameReservedRegex, getMultiLevelExtname, glob, isStringIn, isValidFilename, isValidFilepath, normalizeIncludeFiles, parseFrontMatter, parseYaml, reControlCharsRegex, registerYamlTag, removeLeadingEmptyLines, sanitizeFilename, sanitizeFilepath, stringifyYaml, toCamelCase, toCapitalCase, toPascalCase, traverseFolder, traverseFolderSync };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e,t=Object.create,r=Object.defineProperty,n=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.getPrototypeOf,i=Object.prototype.hasOwnProperty,a=(e,t,s,a)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let c of o(t))i.call(e,c)||c===s||r(e,c,{get:()=>t[c],enumerable:!(a=n(t,c))||a.enumerable});return e},c=(e,n,o)=>(o=null!=e?t(s(e)):{},a(!n&&e&&e.__esModule?o:r(o,"default",{value:e,enumerable:!0}),e)),f={};((e,t)=>{for(var n in t)r(e,n,{get:t[n],enumerable:!0})})(f,{ConfigFile:()=>w,DefaultAllTextFiles:()=>C,getMultiLevelExtname:()=>y,glob:()=>$,isStringIn:()=>A,normalizeIncludeFiles:()=>T,parseFrontMatter:()=>q,parseYaml:()=>h,registerYamlTag:()=>b,removeLeadingEmptyLines:()=>j,stringifyYaml:()=>d,toCamelCase:()=>M,toCapitalCase:()=>Y,toPascalCase:()=>_,traverseFolder:()=>z,traverseFolderSync:()=>S}),module.exports=(e=f,a(r({},"__esModule",{value:!0}),e));var l=require("fs"),u=c(require("path")),m=require("load-config-file"),p=c(require("path"));function y(e,t=1){let r="";for(;t--;){const t=p.default.extname(e);if(!t)break;r=t+r,e=p.default.basename(e,t)}return r}var g=require("yaml"),v=[];function b(e){Array.isArray(e)||(e=[e]);for(const t of e){-1===v.indexOf(t)&&v.push(t)}}function h(e,t){if(t)if(t.customTags){if(Array.isArray(t.customTags))t.customTags=v.concat(t.customTags);else if("function"==typeof t.customTags){const e=t.customTags;t.customTags=t=>e(v.concat(t))}}else t.customTags=v;else t={customTags:v};return(0,g.parse)(e,t)}function d(e,t){if(t)if(t.customTags){if(Array.isArray(t.customTags))t.customTags=v.concat(t.customTags);else if("function"==typeof t.customTags){const e=t.customTags;t.customTags=t=>e(v.concat(t))}}else t.customTags=v;else t={customTags:v};return(0,g.stringify)(e,t)}function j(e){const t=/^\s*(#[^\r\n]*)?[\r\n]+/;let r;for(;null!==(r=t.exec(e))&&((e=e.substring(r[0].length)).startsWith("\n")||e.startsWith("\r")||e.trimStart().startsWith("#")););return e}var x="---";function q(e,t=x){const r=t.length,n=j(e);if(n.startsWith(t)&&("\n"===n[t.length]||"\r"===n[t.length])){let e=n.indexOf("\n"+t,r);if(-1!==e){const o=n.slice(r,e);for(e+=t.length+1;"\n"===n[e]||"\r"===n[e];)e++;const s=n.slice(e);return{data:h(o)||{},content:s}}}return{data:{},content:e}}var w=class{static register(e,t,r){m.Config.register(e,t),"string"==typeof e&&(e=[e]);for(const t of e)this.stringifys[t]=r}static load(e,t){return function(e,{extLevel:t=1,externalFile:r}={}){"."===e[0]&&t++;const n=y(e,t);n&&n.split(".").length>1&&(e=e.slice(0,-n.length));let o=m.Config.loadSync(e);if(!o&&r){if(!u.default.isAbsolute(r)){const t=u.default.dirname(e);r=u.default.join(t,r)}if((0,l.existsSync)(r)){const e=q((0,l.readFileSync)(r,"utf8")).data;Object.keys(e).length&&(o=e)}}return o}(e,t)}static save(e,t,r){return function(e,t,{extLevel:r=1}={}){"."===e[0]&&r++;let n=y(e,r);(!n||n.split(".").length<=1)&&(e+=".yaml",n=".yaml");const o=w.stringifys[n];if(!o)throw new Error(`${e} unsupported mime type: ${n}`);t=o(t);const s=u.default.dirname(e);(0,l.existsSync)(s)||(0,l.mkdirSync)(s,{recursive:!0});return(0,l.writeFileSync)(e,t,{encoding:"utf8"}),e}(e,t,r)}};w.stringifys={},w.register([".yml",".yaml"],h,d),w.register([".json"],(function(e){return JSON.parse(e)}),(e=>JSON.stringify(e,null,2)));var F=require("@isdk/glob"),O=c(require("path"));function $(e,t,r){return r&&(e=O.default.relative(r,e)),(0,F.globMatch)(e,t)}function A(e,t){return"string"==typeof t&&(t=[t]),-1!==t.indexOf(e)}var C=["**/*.((j|t)s?(x)|m(j|t)s)?(x)","**/*.(md|markdown|txt|?(x)htm?(l)|yaml|yml|xml|json|bat|sh|bash|zsh|ini|css|scss|less|sass|py|rb|php|go|java|c|cpp|h|hpp|hxx|rust|zig)"];function T(e,t=C){if(e)if(Array.isArray(e))e=[...e];else{const r=e.include||[],n=e.exclude||[];0===r.length&&r.push(...t),e=[...r];for(const t of n)e.push(`!${t}`)}else e=[...t];return 0===e.length&&e.push(...t),e}var k=require("fs/promises"),E=require("fs"),L=c(require("path"));async function z(e,t){const r=await(0,k.readdir)(e,{withFileTypes:!0});for(const n of r){const r=L.default.join(e,n.name);try{if(n.isDirectory()){await t(r,n)||await z(r,t)}else{if(!0===await t(r,n))break}}catch(e){console.error(`Error processing file: ${r}`),console.error(e)}}}function S(e,t){const r=(0,E.readdirSync)(e,{withFileTypes:!0});for(const n of r){const r=L.default.join(e,n.name);try{if(n.isDirectory()){t(r,n)||z(r,t)}else{if(!0===t(r,n))break}}catch(e){console.error(`Error processing file: ${r}`),console.error(e)}}}function _(e){if(!e)return"";return e.replace(/[-_ ]+/g," ").split(" ").filter(Boolean).map((e=>e.charAt(0).toUpperCase()+e.slice(1))).join("")}function M(e){return(e=_(e)).charAt(0).toLowerCase()+e.slice(1)}function Y(e){if(!e)return"";return e.replace(/[-_ ]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").split(" ").filter(Boolean).map((e=>e.charAt(0).toUpperCase()+e.slice(1).toLowerCase())).join(" ")}
1
+ "use strict";var e,t=Object.create,n=Object.defineProperty,r=Object.getOwnPropertyDescriptor,i=Object.getOwnPropertyNames,o=Object.getPrototypeOf,s=Object.prototype.hasOwnProperty,a=(e,t,o,a)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let u of i(t))s.call(e,u)||u===o||n(e,u,{get:()=>t[u],enumerable:!(a=r(t,u))||a.enumerable});return e},u=(e,r,i)=>(i=null!=e?t(o(e)):{},a(!r&&e&&e.__esModule?i:n(i,"default",{value:e,enumerable:!0}),e)),c={};((e,t)=>{for(var r in t)n(e,r,{get:t[r],enumerable:!0})})(c,{ConfigFile:()=>q,DefaultAllTextFiles:()=>O,FilenameReservedRegex:()=>B,WindowsReservedNameRegex:()=>D,extNameLevel:()=>U,filenameReservedRegex:()=>W,getMultiLevelExtname:()=>g,glob:()=>R,isStringIn:()=>E,isValidFilename:()=>G,isValidFilepath:()=>H,normalizeIncludeFiles:()=>C,parseFrontMatter:()=>j,parseYaml:()=>h,reControlCharsRegex:()=>Z,registerYamlTag:()=>v,removeLeadingEmptyLines:()=>b,sanitizeFilename:()=>K,sanitizeFilepath:()=>Q,stringifyYaml:()=>F,toCamelCase:()=>M,toCapitalCase:()=>S,toPascalCase:()=>N,traverseFolder:()=>L,traverseFolderSync:()=>T}),module.exports=(e=c,a(n({},"__esModule",{value:!0}),e));var l=require("fs"),f=u(require("path")),m=require("load-config-file"),p=u(require("path"));function g(e,t=1){let n="";for(;t--;){const t=p.default.extname(e);if(!t)break;n=t+n,e=p.default.basename(e,t)}return n}var y=require("yaml"),d=[];function v(e){Array.isArray(e)||(e=[e]);for(const t of e){-1===d.indexOf(t)&&d.push(t)}}function h(e,t){if(t)if(t.customTags){if(Array.isArray(t.customTags))t.customTags=d.concat(t.customTags);else if("function"==typeof t.customTags){const e=t.customTags;t.customTags=t=>e(d.concat(t))}}else t.customTags=d;else t={customTags:d};return(0,y.parse)(e,t)}function F(e,t){if(t)if(t.customTags){if(Array.isArray(t.customTags))t.customTags=d.concat(t.customTags);else if("function"==typeof t.customTags){const e=t.customTags;t.customTags=t=>e(d.concat(t))}}else t.customTags=d;else t={customTags:d};return(0,y.stringify)(e,t)}function b(e){const t=/^\s*(#[^\r\n]*)?[\r\n]+/;let n;for(;null!==(n=t.exec(e))&&((e=e.substring(n[0].length)).startsWith("\n")||e.startsWith("\r")||e.trimStart().startsWith("#")););return e}var x="---";function j(e,t=x){const n=t.length,r=b(e);if(r.startsWith(t)&&("\n"===r[t.length]||"\r"===r[t.length])){let e=r.indexOf("\n"+t,n);if(-1!==e){const i=r.slice(n,e);for(e+=t.length+1;"\n"===r[e]||"\r"===r[e];)e++;const o=r.slice(e);return{data:h(i)||{},content:o}}}return{data:{},content:e}}var q=class{static register(e,t,n){m.Config.register(e,t),"string"==typeof e&&(e=[e]);for(const t of e)this.stringifys[t]=n}static load(e,t){return function(e,{extLevel:t=1,externalFile:n}={}){"."===e[0]&&t++;const r=g(e,t);r&&r.split(".").length>1&&(e=e.slice(0,-r.length));let i=m.Config.loadSync(e);if(!i&&n){if(!f.default.isAbsolute(n)){const t=f.default.dirname(e);n=f.default.join(t,n)}if((0,l.existsSync)(n)){const e=j((0,l.readFileSync)(n,"utf8")).data;Object.keys(e).length&&(i=e)}}return i}(e,t)}static save(e,t,n){return function(e,t,{extLevel:n=1}={}){"."===e[0]&&n++;let r=g(e,n);(!r||r.split(".").length<=1)&&(e+=".yaml",r=".yaml");const i=q.stringifys[r];if(!i)throw new Error(`${e} unsupported mime type: ${r}`);t=i(t);const o=f.default.dirname(e);(0,l.existsSync)(o)||(0,l.mkdirSync)(o,{recursive:!0});return(0,l.writeFileSync)(e,t,{encoding:"utf8"}),e}(e,t,n)}};q.stringifys={},q.register([".yml",".yaml"],h,F),q.register([".json"],(function(e){return JSON.parse(e)}),(e=>JSON.stringify(e,null,2)));var w=require("@isdk/glob"),$=u(require("path"));function R(e,t,n){return n&&(e=$.default.relative(n,e)),(0,w.globMatch)(e,t)}function E(e,t){return"string"==typeof t&&(t=[t]),-1!==t.indexOf(e)}var O=["**/*.((j|t)s?(x)|m(j|t)s)?(x)","**/*.(md|markdown|txt|?(x)htm?(l)|yaml|yml|xml|json|bat|sh|bash|zsh|ini|css|scss|less|sass|py|rb|php|go|java|c|cpp|h|hpp|hxx|rust|zig)"];function C(e,t=O){if(e)if(Array.isArray(e))e=[...e];else{const n=e.include||[],r=e.exclude||[];0===n.length&&n.push(...t),e=[...n];for(const t of r)e.push(`!${t}`)}else e=[...t];return 0===e.length&&e.push(...t),e}var z=require("fs/promises"),A=require("fs"),k=u(require("path"));async function L(e,t){const n=await(0,z.readdir)(e,{withFileTypes:!0});for(const r of n){const n=k.default.join(e,r.name);try{if(r.isDirectory()){await t(n,r)||await L(n,t)}else{if(!0===await t(n,r))break}}catch(e){console.error(`Error processing file: ${n}`),console.error(e)}}}function T(e,t){const n=(0,A.readdirSync)(e,{withFileTypes:!0});for(const r of n){const n=k.default.join(e,r.name);try{if(r.isDirectory()){t(n,r)||L(n,t)}else{if(!0===t(n,r))break}}catch(e){console.error(`Error processing file: ${n}`),console.error(e)}}}function N(e){if(!e)return"";return e.replace(/[-_ ]+/g," ").split(" ").filter(Boolean).map((e=>e.charAt(0).toUpperCase()+e.slice(1))).join("")}function M(e){return(e=N(e)).charAt(0).toLowerCase()+e.slice(1)}function S(e){if(!e)return"";return e.replace(/[-_ ]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").split(" ").filter(Boolean).map((e=>e.charAt(0).toUpperCase()+e.slice(1).toLowerCase())).join(" ")}var _=u(require("path")),Y=require("@isdk/common-error"),B=/[<>:"/\\|?*\u0000-\u001F]/,D=/^(con|prn|aux|nul|com\d|lpt\d)$/i,I=100,J=/^\.+(\\|\/)|^\.+$/,V=/\.+$/,P=/[\u0000-\u001F\u0080-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/;function W(){return new RegExp(B.source,"g")}function Z(){return new RegExp(P.source,"g")}function G(e){return e&&!(B.test(e)||Z().test(e)||J.test(e)||V.test(e))}function H(e){const t=e.split(_.default.sep);return("/"===e[0]||t[0]&&_.default.dirname(t[0])===t[0])&&t.shift(),t.every(G)}function K(e,t={}){const n=t.replacement||"!";if((B.test(n)||P.test(n))&&(0,Y.throwError)("Replacement string cannot contain reserved filename characters","sanitizeFilename",Y.ErrorCode.InvalidArgument),n.length>0){const t=/([<>:"/\\|?*\u0000-\u001F]){2,}/;e=e.replace(t,"$1")}if(e=(e=(e=(e=(e=e.normalize("NFD")).replace(J,n)).replace(W(),n)).replace(Z(),n)).replace(V,""),n.length>0){"."===e[0]||"."!==e[0]||(e=n+e),"."===e[e.length-1]&&(e+=n)}e=D.test(e)?e+n:e;const r="number"==typeof t.maxLength?t.maxLength:I;if(e.length>r){const t=e.lastIndexOf(".");if(-1===t)e=e.slice(0,r);else{const n=e.slice(0,t),i=e.slice(t);e=n.slice(0,Math.max(1,r-i.length))+i}}return e}function Q(e,t={}){const n=e.split(_.default.sep);let r;("/"===e[0]||n[0]&&_.default.dirname(n[0])===n[0])&&(r=n.shift());const i=n.map((e=>K(e,t)));return void 0!==r&&i.unshift(r),i.join(_.default.sep)}function U(e){return e.split(".").length-1}
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{existsSync as t,mkdirSync as n,readFileSync as o,writeFileSync as r}from"fs";import e from"path";import{Config as s}from"load-config-file";import i from"path";function f(t,n=1){let o="";for(;n--;){const n=i.extname(t);if(!n)break;o=n+o,t=i.basename(t,n)}return o}import{parse as c,stringify as a}from"yaml";var l=[];function u(t){Array.isArray(t)||(t=[t]);for(const n of t){-1===l.indexOf(n)&&l.push(n)}}function m(t,n){if(n)if(n.customTags){if(Array.isArray(n.customTags))n.customTags=l.concat(n.customTags);else if("function"==typeof n.customTags){const t=n.customTags;n.customTags=n=>t(l.concat(n))}}else n.customTags=l;else n={customTags:l};return c(t,n)}function p(t,n){if(n)if(n.customTags){if(Array.isArray(n.customTags))n.customTags=l.concat(n.customTags);else if("function"==typeof n.customTags){const t=n.customTags;n.customTags=n=>t(l.concat(n))}}else n.customTags=l;else n={customTags:l};return a(t,n)}function y(t){const n=/^\s*(#[^\r\n]*)?[\r\n]+/;let o;for(;null!==(o=n.exec(t))&&((t=t.substring(o[0].length)).startsWith("\n")||t.startsWith("\r")||t.trimStart().startsWith("#")););return t}function h(t,n="---"){const o=n.length,r=y(t);if(r.startsWith(n)&&("\n"===r[n.length]||"\r"===r[n.length])){let t=r.indexOf("\n"+n,o);if(-1!==t){const e=r.slice(o,t);for(t+=n.length+1;"\n"===r[t]||"\r"===r[t];)t++;const s=r.slice(t);return{data:m(e)||{},content:s}}}return{data:{},content:t}}var g=class{static register(t,n,o){s.register(t,n),"string"==typeof t&&(t=[t]);for(const n of t)this.stringifys[n]=o}static load(n,r){return function(n,{extLevel:r=1,externalFile:i}={}){"."===n[0]&&r++;const c=f(n,r);c&&c.split(".").length>1&&(n=n.slice(0,-c.length));let a=s.loadSync(n);if(!a&&i){if(!e.isAbsolute(i)){const t=e.dirname(n);i=e.join(t,i)}if(t(i)){const t=h(o(i,"utf8")).data;Object.keys(t).length&&(a=t)}}return a}(n,r)}static save(o,s,i){return function(o,s,{extLevel:i=1}={}){"."===o[0]&&i++;let c=f(o,i);(!c||c.split(".").length<=1)&&(o+=".yaml",c=".yaml");const a=g.stringifys[c];if(!a)throw new Error(`${o} unsupported mime type: ${c}`);s=a(s);const l=e.dirname(o);t(l)||n(l,{recursive:!0});return r(o,s,{encoding:"utf8"}),o}(o,s,i)}};g.stringifys={},g.register([".yml",".yaml"],m,p),g.register([".json"],(function(t){return JSON.parse(t)}),(t=>JSON.stringify(t,null,2)));import{globMatch as x}from"@isdk/glob";import d from"path";function w(t,n,o){return o&&(t=d.relative(o,t)),x(t,n)}function b(t,n){return"string"==typeof n&&(n=[n]),-1!==n.indexOf(t)}var v=["**/*.((j|t)s?(x)|m(j|t)s)?(x)","**/*.(md|markdown|txt|?(x)htm?(l)|yaml|yml|xml|json|bat|sh|bash|zsh|ini|css|scss|less|sass|py|rb|php|go|java|c|cpp|h|hpp|hxx|rust|zig)"];function $(t,n=v){if(t)if(Array.isArray(t))t=[...t];else{const o=t.include||[],r=t.exclude||[];0===o.length&&o.push(...n),t=[...o];for(const n of r)t.push(`!${n}`)}else t=[...n];return 0===t.length&&t.push(...n),t}import{readdir as j}from"fs/promises";import{readdirSync as k}from"fs";import A from"path";async function T(t,n){const o=await j(t,{withFileTypes:!0});for(const r of o){const o=A.join(t,r.name);try{if(r.isDirectory()){await n(o,r)||await T(o,n)}else{if(!0===await n(o,r))break}}catch(t){console.error(`Error processing file: ${o}`),console.error(t)}}}function z(t,n){const o=k(t,{withFileTypes:!0});for(const r of o){const o=A.join(t,r.name);try{if(r.isDirectory()){n(o,r)||T(o,n)}else{if(!0===n(o,r))break}}catch(t){console.error(`Error processing file: ${o}`),console.error(t)}}}function E(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").split(" ").filter(Boolean).map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join("")}function F(t){return(t=E(t)).charAt(0).toLowerCase()+t.slice(1)}function O(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").split(" ").filter(Boolean).map((t=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase())).join(" ")}export{g as ConfigFile,v as DefaultAllTextFiles,f as getMultiLevelExtname,w as glob,b as isStringIn,$ as normalizeIncludeFiles,h as parseFrontMatter,m as parseYaml,u as registerYamlTag,y as removeLeadingEmptyLines,p as stringifyYaml,F as toCamelCase,O as toCapitalCase,E as toPascalCase,T as traverseFolder,z as traverseFolderSync};
1
+ import{existsSync as t,mkdirSync as n,readFileSync as o,writeFileSync as r}from"fs";import e from"path";import{Config as i}from"load-config-file";import s from"path";function f(t,n=1){let o="";for(;n--;){const n=s.extname(t);if(!n)break;o=n+o,t=s.basename(t,n)}return o}import{parse as c,stringify as u}from"yaml";var a=[];function l(t){Array.isArray(t)||(t=[t]);for(const n of t){-1===a.indexOf(n)&&a.push(n)}}function m(t,n){if(n)if(n.customTags){if(Array.isArray(n.customTags))n.customTags=a.concat(n.customTags);else if("function"==typeof n.customTags){const t=n.customTags;n.customTags=n=>t(a.concat(n))}}else n.customTags=a;else n={customTags:a};return c(t,n)}function p(t,n){if(n)if(n.customTags){if(Array.isArray(n.customTags))n.customTags=a.concat(n.customTags);else if("function"==typeof n.customTags){const t=n.customTags;n.customTags=n=>t(a.concat(n))}}else n.customTags=a;else n={customTags:a};return u(t,n)}function y(t){const n=/^\s*(#[^\r\n]*)?[\r\n]+/;let o;for(;null!==(o=n.exec(t))&&((t=t.substring(o[0].length)).startsWith("\n")||t.startsWith("\r")||t.trimStart().startsWith("#")););return t}function g(t,n="---"){const o=n.length,r=y(t);if(r.startsWith(n)&&("\n"===r[n.length]||"\r"===r[n.length])){let t=r.indexOf("\n"+n,o);if(-1!==t){const e=r.slice(o,t);for(t+=n.length+1;"\n"===r[t]||"\r"===r[t];)t++;const i=r.slice(t);return{data:m(e)||{},content:i}}}return{data:{},content:t}}var h=class{static register(t,n,o){i.register(t,n),"string"==typeof t&&(t=[t]);for(const n of t)this.stringifys[n]=o}static load(n,r){return function(n,{extLevel:r=1,externalFile:s}={}){"."===n[0]&&r++;const c=f(n,r);c&&c.split(".").length>1&&(n=n.slice(0,-c.length));let u=i.loadSync(n);if(!u&&s){if(!e.isAbsolute(s)){const t=e.dirname(n);s=e.join(t,s)}if(t(s)){const t=g(o(s,"utf8")).data;Object.keys(t).length&&(u=t)}}return u}(n,r)}static save(o,i,s){return function(o,i,{extLevel:s=1}={}){"."===o[0]&&s++;let c=f(o,s);(!c||c.split(".").length<=1)&&(o+=".yaml",c=".yaml");const u=h.stringifys[c];if(!u)throw new Error(`${o} unsupported mime type: ${c}`);i=u(i);const a=e.dirname(o);t(a)||n(a,{recursive:!0});return r(o,i,{encoding:"utf8"}),o}(o,i,s)}};h.stringifys={},h.register([".yml",".yaml"],m,p),h.register([".json"],(function(t){return JSON.parse(t)}),(t=>JSON.stringify(t,null,2)));import{globMatch as d}from"@isdk/glob";import x from"path";function v(t,n,o){return o&&(t=x.relative(o,t)),d(t,n)}function w(t,n){return"string"==typeof n&&(n=[n]),-1!==n.indexOf(t)}var $=["**/*.((j|t)s?(x)|m(j|t)s)?(x)","**/*.(md|markdown|txt|?(x)htm?(l)|yaml|yml|xml|json|bat|sh|bash|zsh|ini|css|scss|less|sass|py|rb|php|go|java|c|cpp|h|hpp|hxx|rust|zig)"];function F(t,n=$){if(t)if(Array.isArray(t))t=[...t];else{const o=t.include||[],r=t.exclude||[];0===o.length&&o.push(...n),t=[...o];for(const n of r)t.push(`!${n}`)}else t=[...n];return 0===t.length&&t.push(...n),t}import{readdir as b}from"fs/promises";import{readdirSync as E}from"fs";import j from"path";async function k(t,n){const o=await b(t,{withFileTypes:!0});for(const r of o){const o=j.join(t,r.name);try{if(r.isDirectory()){await n(o,r)||await k(o,n)}else{if(!0===await n(o,r))break}}catch(t){console.error(`Error processing file: ${o}`),console.error(t)}}}function A(t,n){const o=E(t,{withFileTypes:!0});for(const r of o){const o=j.join(t,r.name);try{if(r.isDirectory()){n(o,r)||k(o,n)}else{if(!0===n(o,r))break}}catch(t){console.error(`Error processing file: ${o}`),console.error(t)}}}function z(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").split(" ").filter(Boolean).map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join("")}function T(t){return(t=z(t)).charAt(0).toLowerCase()+t.slice(1)}function N(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").split(" ").filter(Boolean).map((t=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase())).join(" ")}import O from"path";import{ErrorCode as R,throwError as B}from"@isdk/common-error";var J=/[<>:"/\\|?*\u0000-\u001F]/,L=/^(con|prn|aux|nul|com\d|lpt\d)$/i,S=/^\.+(\\|\/)|^\.+$/,_=/\.+$/,C=/[\u0000-\u001F\u0080-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/;function D(){return new RegExp(J.source,"g")}function M(){return new RegExp(C.source,"g")}function Z(t){return t&&!(J.test(t)||M().test(t)||S.test(t)||_.test(t))}function q(t){const n=t.split(O.sep);return("/"===t[0]||n[0]&&O.dirname(n[0])===n[0])&&n.shift(),n.every(Z)}function G(t,n={}){const o=n.replacement||"!";if((J.test(o)||C.test(o))&&B("Replacement string cannot contain reserved filename characters","sanitizeFilename",R.InvalidArgument),o.length>0){const n=/([<>:"/\\|?*\u0000-\u001F]){2,}/;t=t.replace(n,"$1")}if(t=(t=(t=(t=(t=t.normalize("NFD")).replace(S,o)).replace(D(),o)).replace(M(),o)).replace(_,""),o.length>0){"."===t[0]||"."!==t[0]||(t=o+t),"."===t[t.length-1]&&(t+=o)}t=L.test(t)?t+o:t;const r="number"==typeof n.maxLength?n.maxLength:100;if(t.length>r){const n=t.lastIndexOf(".");if(-1===n)t=t.slice(0,r);else{const o=t.slice(0,n),e=t.slice(n);t=o.slice(0,Math.max(1,r-e.length))+e}}return t}function H(t,n={}){const o=t.split(O.sep);let r;("/"===t[0]||o[0]&&O.dirname(o[0])===o[0])&&(r=o.shift());const e=o.map((t=>G(t,n)));return void 0!==r&&e.unshift(r),e.join(O.sep)}function I(t){return t.split(".").length-1}export{h as ConfigFile,$ as DefaultAllTextFiles,J as FilenameReservedRegex,L as WindowsReservedNameRegex,I as extNameLevel,D as filenameReservedRegex,f as getMultiLevelExtname,v as glob,w as isStringIn,Z as isValidFilename,q as isValidFilepath,F as normalizeIncludeFiles,g as parseFrontMatter,m as parseYaml,M as reControlCharsRegex,l as registerYamlTag,y as removeLeadingEmptyLines,G as sanitizeFilename,H as sanitizeFilepath,p as stringifyYaml,T as toCamelCase,N as toCapitalCase,z as toPascalCase,k as traverseFolder,A as traverseFolderSync};
@@ -6,7 +6,7 @@
6
6
 
7
7
  # Class: ConfigFile
8
8
 
9
- Defined in: [config-file.ts:46](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L46)
9
+ Defined in: [config-file.ts:46](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L46)
10
10
 
11
11
  Represents a configuration file utility class that provides methods to load and save configuration files.
12
12
  It supports multiple file formats such as YAML, JSON, etc., by registering corresponding parsers and stringifiers.
@@ -45,7 +45,7 @@ console.log(config); // Output: { key: 'value' }
45
45
 
46
46
  > `static` **stringifys**: `Record`\<`string`, [`StringifyFunc`](../type-aliases/StringifyFunc.md)\> = `{}`
47
47
 
48
- Defined in: [config-file.ts:50](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L50)
48
+ Defined in: [config-file.ts:50](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L50)
49
49
 
50
50
  A record of registered stringify functions for different file extensions.
51
51
 
@@ -55,7 +55,7 @@ A record of registered stringify functions for different file extensions.
55
55
 
56
56
  > `static` **load**(`filename`, `options`?): `any`
57
57
 
58
- Defined in: [config-file.ts:85](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L85)
58
+ Defined in: [config-file.ts:85](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L85)
59
59
 
60
60
  Loads a configuration file based on the provided filename and options.
61
61
 
@@ -92,7 +92,7 @@ console.log(config); // Output: { key: 'value' }
92
92
 
93
93
  > `static` **register**(`extname`, `parser`, `stringify`): `void`
94
94
 
95
- Defined in: [config-file.ts:64](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L64)
95
+ Defined in: [config-file.ts:64](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L64)
96
96
 
97
97
  Registers a parser and stringifier for specific file extensions.
98
98
 
@@ -132,7 +132,7 @@ ConfigFile.register(['.json'], JSON.parse, (obj) => JSON.stringify(obj, null, 2)
132
132
 
133
133
  > `static` **save**(`filename`, `config`, `options`?): `string`
134
134
 
135
- Defined in: [config-file.ts:102](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L102)
135
+ Defined in: [config-file.ts:102](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L102)
136
136
 
137
137
  Saves a configuration object to a file with the specified filename and options.
138
138
 
@@ -0,0 +1,43 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / extNameLevel
6
+
7
+ # Function: extNameLevel()
8
+
9
+ > **extNameLevel**(`extName`): `number`
10
+
11
+ Defined in: [filename.ts:189](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L189)
12
+
13
+ Calculates the level of an extension name.
14
+
15
+ The level represents the number of dots ('.') in the extension name, excluding the first dot which separates
16
+ the base filename from the extension. For example, the level of ".txt" is 1, and the level of ".tar.gz" is 2.
17
+
18
+ ## Parameters
19
+
20
+ ### extName
21
+
22
+ `string`
23
+
24
+ The extension name to analyze. It should start with a dot ('.').
25
+
26
+ ## Returns
27
+
28
+ `number`
29
+
30
+ The level of the extension name, which is the count of dots minus one.
31
+
32
+ ## Example
33
+
34
+ ```typescript
35
+ // Returns 0
36
+ extNameLevel("no-file-ext");
37
+
38
+ // Returns 2
39
+ extNameLevel(".tar.gz");
40
+
41
+ // Returns 1
42
+ extNameLevel(".json5");
43
+ ```
@@ -0,0 +1,20 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / filenameReservedRegex
6
+
7
+ # Function: filenameReservedRegex()
8
+
9
+ > **filenameReservedRegex**(): `RegExp`
10
+
11
+ Defined in: [filename.ts:42](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L42)
12
+
13
+ Returns a new regular expression instance for reserved filename characters with the 'g' flag.
14
+ use this to reset the with global option
15
+
16
+ ## Returns
17
+
18
+ `RegExp`
19
+
20
+ A compiled regular expression for reserved filename characters.
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **getMultiLevelExtname**(`filename`, `level`): `string`
10
10
 
11
- Defined in: [get-multi-level-extname.ts:9](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/get-multi-level-extname.ts#L9)
11
+ Defined in: [get-multi-level-extname.ts:9](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/get-multi-level-extname.ts#L9)
12
12
 
13
13
  Retrieves multi-level file extension
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **glob**(`filepath`, `pattern`, `rootDir`?): `undefined` \| `boolean`
10
10
 
11
- Defined in: [glob.ts:29](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/glob.ts#L29)
11
+ Defined in: [glob.ts:29](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/glob.ts#L29)
12
12
 
13
13
  Matches a file path against a list of glob patterns.
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **isStringIn**(`str`, `arr`): `boolean`
10
10
 
11
- Defined in: [is-string-in.ts:17](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/is-string-in.ts#L17)
11
+ Defined in: [is-string-in.ts:17](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/is-string-in.ts#L17)
12
12
 
13
13
  Checks if a given string exists within an array of strings or a single string.
14
14
 
@@ -0,0 +1,38 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / isValidFilename
6
+
7
+ # Function: isValidFilename()
8
+
9
+ > **isValidFilename**(`filename`): `boolean` \| `""`
10
+
11
+ Defined in: [filename.ts:63](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L63)
12
+
13
+ Validates if a given string is a valid filename.
14
+
15
+ ## Parameters
16
+
17
+ ### filename
18
+
19
+ `string`
20
+
21
+ The filename to be validated.
22
+
23
+ ## Returns
24
+
25
+ `boolean` \| `""`
26
+
27
+ True if the filename is valid, false otherwise.
28
+
29
+ ## Throws
30
+
31
+ If the input is not a string.
32
+
33
+ ## Example
34
+
35
+ ```ts
36
+ isValidFilename('myFile.txt'); // Returns true
37
+ isValidFilename('my<file.txt'); // Returns false
38
+ ```
@@ -0,0 +1,27 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / isValidFilepath
6
+
7
+ # Function: isValidFilepath()
8
+
9
+ > **isValidFilepath**(`filepath`): `boolean`
10
+
11
+ Defined in: [filename.ts:72](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L72)
12
+
13
+ Validates whether the given filepath is valid.
14
+
15
+ ## Parameters
16
+
17
+ ### filepath
18
+
19
+ `string`
20
+
21
+ The filepath to be checked, represented as a string.
22
+
23
+ ## Returns
24
+
25
+ `boolean`
26
+
27
+ A boolean indicating whether the filepath is valid. Returns true if valid; false otherwise.
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **normalizeIncludeFiles**(`files`?, `defaultFiles`?): `string`[]
10
10
 
11
- Defined in: [include-files.ts:34](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/include-files.ts#L34)
11
+ Defined in: [include-files.ts:34](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/include-files.ts#L34)
12
12
 
13
13
  Normalizes a list of file patterns for glob matching.
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **parseFrontMatter**(`value`, `delimiter`): `object`
10
10
 
11
- Defined in: [front-matter.ts:6](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/front-matter.ts#L6)
11
+ Defined in: [front-matter.ts:6](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/front-matter.ts#L6)
12
12
 
13
13
  ## Parameters
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **parseYaml**(`content`, `options`?): `any`
10
10
 
11
- Defined in: [yaml.ts:51](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/yaml.ts#L51)
11
+ Defined in: [yaml.ts:51](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/yaml.ts#L51)
12
12
 
13
13
  Parses a YAML string into a JavaScript object with optional custom tags.
14
14
 
@@ -0,0 +1,19 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / reControlCharsRegex
6
+
7
+ # Function: reControlCharsRegex()
8
+
9
+ > **reControlCharsRegex**(): `RegExp`
10
+
11
+ Defined in: [filename.ts:50](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L50)
12
+
13
+ Returns a new regular expression instance for control characters in a filename with the 'g' flag.
14
+
15
+ ## Returns
16
+
17
+ `RegExp`
18
+
19
+ A compiled regular expression for control characters in a filename.
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **registerYamlTag**(`tags`): `void`
10
10
 
11
- Defined in: [yaml.ts:23](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/yaml.ts#L23)
11
+ Defined in: [yaml.ts:23](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/yaml.ts#L23)
12
12
 
13
13
  Registers custom YAML tags to be used in parsing and stringifying YAML content.
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **removeLeadingEmptyLines**(`text`): `string`
10
10
 
11
- Defined in: [remove-leading-empty-lines.ts:16](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/remove-leading-empty-lines.ts#L16)
11
+ Defined in: [remove-leading-empty-lines.ts:16](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/remove-leading-empty-lines.ts#L16)
12
12
 
13
13
  Removes all leading empty lines or "#" comments line from the given string.
14
14
 
@@ -0,0 +1,39 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / sanitizeFilename
6
+
7
+ # Function: sanitizeFilename()
8
+
9
+ > **sanitizeFilename**(`filename`, `options`): `string`
10
+
11
+ Defined in: [filename.ts:97](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L97)
12
+
13
+ Sanitizes a given filename by replacing invalid characters with a specified replacement character or a default.
14
+
15
+ ## Parameters
16
+
17
+ ### filename
18
+
19
+ `string`
20
+
21
+ The filename to sanitize, represented as a string.
22
+
23
+ ### options
24
+
25
+ [`SanitizeFilenameOptions`](../interfaces/SanitizeFilenameOptions.md) = `{}`
26
+
27
+ An optional object containing configuration options:
28
+ - `replacement` {string} - The character to replace invalid characters with. Default is '!'. Cannot contain reserved filename characters.
29
+ - `maxLength` {number} - The maximum length of the sanitized filename. Default is `MAX_FILENAME_LENGTH`.
30
+
31
+ ## Returns
32
+
33
+ `string`
34
+
35
+ The sanitized filename.
36
+
37
+ ## Throws
38
+
39
+ - If the `replacement` contains reserved filename characters or control characters.
@@ -0,0 +1,33 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / sanitizeFilepath
6
+
7
+ # Function: sanitizeFilepath()
8
+
9
+ > **sanitizeFilepath**(`filepath`, `options`): `string`
10
+
11
+ Defined in: [filename.ts:149](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L149)
12
+
13
+ Sanitizes each part of a file path and reassembles it, ensuring the path is valid according to provided options.
14
+
15
+ ## Parameters
16
+
17
+ ### filepath
18
+
19
+ `string`
20
+
21
+ The file path to sanitize, represented as a string.
22
+
23
+ ### options
24
+
25
+ [`SanitizeFilenameOptions`](../interfaces/SanitizeFilenameOptions.md) = `{}`
26
+
27
+ Optional settings for sanitization, extending `SanitizeFilenameOptions`. Allows customization of replacement characters and maximum filename length.
28
+
29
+ ## Returns
30
+
31
+ `string`
32
+
33
+ The sanitized file path as a string.
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **stringifyYaml**(`content`, `options`?): `string`
10
10
 
11
- Defined in: [yaml.ts:85](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/yaml.ts#L85)
11
+ Defined in: [yaml.ts:85](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/yaml.ts#L85)
12
12
 
13
13
  Converts a JavaScript object into a YAML string with optional custom tags.
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **toCamelCase**(`str`): `string`
10
10
 
11
- Defined in: [to-camel-case.ts:17](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/to-camel-case.ts#L17)
11
+ Defined in: [to-camel-case.ts:17](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/to-camel-case.ts#L17)
12
12
 
13
13
  Converts a string to camelCase.
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **toCapitalCase**(`str`): `string`
10
10
 
11
- Defined in: [to-capital-case.ts:17](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/to-capital-case.ts#L17)
11
+ Defined in: [to-capital-case.ts:17](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/to-capital-case.ts#L17)
12
12
 
13
13
  Converts a string to capital case, where the first letter of each word is capitalized
14
14
  and the rest are in lowercase. Words are separated by spaces, hyphens, or underscores.
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **toPascalCase**(`str`): `string`
10
10
 
11
- Defined in: [to-pascal-case.ts:16](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/to-pascal-case.ts#L16)
11
+ Defined in: [to-pascal-case.ts:16](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/to-pascal-case.ts#L16)
12
12
 
13
13
  Converts a string to PascalCase.
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **traverseFolder**(`directoryPath`, `fileHandler`): `Promise`\<`void`\>
10
10
 
11
- Defined in: [traverse-folder.ts:38](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/traverse-folder.ts#L38)
11
+ Defined in: [traverse-folder.ts:38](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/traverse-folder.ts#L38)
12
12
 
13
13
  Traverses a folder asynchronously and applies a handler to each file or directory.
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **traverseFolderSync**(`directoryPath`, `fileHandler`): `void`
10
10
 
11
- Defined in: [traverse-folder.ts:76](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/traverse-folder.ts#L76)
11
+ Defined in: [traverse-folder.ts:76](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/traverse-folder.ts#L76)
12
12
 
13
13
  Traverses a folder synchronously and applies a handler to each file or directory.
14
14
 
package/docs/globals.md CHANGED
@@ -12,6 +12,7 @@
12
12
 
13
13
  - [IncludeFiles](interfaces/IncludeFiles.md)
14
14
  - [LoadConfigFileOptions](interfaces/LoadConfigFileOptions.md)
15
+ - [SanitizeFilenameOptions](interfaces/SanitizeFilenameOptions.md)
15
16
 
16
17
  ## Type Aliases
17
18
 
@@ -22,17 +23,26 @@
22
23
  ## Variables
23
24
 
24
25
  - [DefaultAllTextFiles](variables/DefaultAllTextFiles.md)
26
+ - [FilenameReservedRegex](variables/FilenameReservedRegex.md)
27
+ - [WindowsReservedNameRegex](variables/WindowsReservedNameRegex.md)
25
28
 
26
29
  ## Functions
27
30
 
31
+ - [extNameLevel](functions/extNameLevel.md)
32
+ - [filenameReservedRegex](functions/filenameReservedRegex.md)
28
33
  - [getMultiLevelExtname](functions/getMultiLevelExtname.md)
29
34
  - [glob](functions/glob.md)
30
35
  - [isStringIn](functions/isStringIn.md)
36
+ - [isValidFilename](functions/isValidFilename.md)
37
+ - [isValidFilepath](functions/isValidFilepath.md)
31
38
  - [normalizeIncludeFiles](functions/normalizeIncludeFiles.md)
32
39
  - [parseFrontMatter](functions/parseFrontMatter.md)
33
40
  - [parseYaml](functions/parseYaml.md)
41
+ - [reControlCharsRegex](functions/reControlCharsRegex.md)
34
42
  - [registerYamlTag](functions/registerYamlTag.md)
35
43
  - [removeLeadingEmptyLines](functions/removeLeadingEmptyLines.md)
44
+ - [sanitizeFilename](functions/sanitizeFilename.md)
45
+ - [sanitizeFilepath](functions/sanitizeFilepath.md)
36
46
  - [stringifyYaml](functions/stringifyYaml.md)
37
47
  - [toCamelCase](functions/toCamelCase.md)
38
48
  - [toCapitalCase](functions/toCapitalCase.md)
@@ -6,7 +6,7 @@
6
6
 
7
7
  # Interface: IncludeFiles
8
8
 
9
- Defined in: [include-files.ts:6](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/include-files.ts#L6)
9
+ Defined in: [include-files.ts:6](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/include-files.ts#L6)
10
10
 
11
11
  ## Properties
12
12
 
@@ -14,7 +14,7 @@ Defined in: [include-files.ts:6](https://github.com/isdk/util.js/blob/d57e048e4f
14
14
 
15
15
  > `optional` **exclude**: `string`[]
16
16
 
17
- Defined in: [include-files.ts:8](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/include-files.ts#L8)
17
+ Defined in: [include-files.ts:8](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/include-files.ts#L8)
18
18
 
19
19
  ***
20
20
 
@@ -22,4 +22,4 @@ Defined in: [include-files.ts:8](https://github.com/isdk/util.js/blob/d57e048e4f
22
22
 
23
23
  > `optional` **include**: `string`[]
24
24
 
25
- Defined in: [include-files.ts:7](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/include-files.ts#L7)
25
+ Defined in: [include-files.ts:7](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/include-files.ts#L7)
@@ -6,7 +6,7 @@
6
6
 
7
7
  # Interface: LoadConfigFileOptions
8
8
 
9
- Defined in: [config-file.ts:20](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L20)
9
+ Defined in: [config-file.ts:20](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L20)
10
10
 
11
11
  ## Properties
12
12
 
@@ -14,7 +14,7 @@ Defined in: [config-file.ts:20](https://github.com/isdk/util.js/blob/d57e048e4f7
14
14
 
15
15
  > `optional` **externalFile**: `string`
16
16
 
17
- Defined in: [config-file.ts:22](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L22)
17
+ Defined in: [config-file.ts:22](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L22)
18
18
 
19
19
  ***
20
20
 
@@ -22,4 +22,4 @@ Defined in: [config-file.ts:22](https://github.com/isdk/util.js/blob/d57e048e4f7
22
22
 
23
23
  > `optional` **extLevel**: `number`
24
24
 
25
- Defined in: [config-file.ts:21](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L21)
25
+ Defined in: [config-file.ts:21](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L21)
@@ -0,0 +1,25 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / SanitizeFilenameOptions
6
+
7
+ # Interface: SanitizeFilenameOptions
8
+
9
+ Defined in: [filename.ts:83](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L83)
10
+
11
+ ## Properties
12
+
13
+ ### maxLength?
14
+
15
+ > `optional` **maxLength**: `number`
16
+
17
+ Defined in: [filename.ts:85](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L85)
18
+
19
+ ***
20
+
21
+ ### replacement?
22
+
23
+ > `optional` **replacement**: `string`
24
+
25
+ Defined in: [filename.ts:84](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L84)
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **StringifyFunc** = (`content`) => `string`
10
10
 
11
- Defined in: [config-file.ts:18](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/config-file.ts#L18)
11
+ Defined in: [config-file.ts:18](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/config-file.ts#L18)
12
12
 
13
13
  ## Parameters
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **TraverseFolderHandler** = (`filePath`, `entry`) => `boolean` \| `void` \| `Promise`\<`boolean` \| `void`\>
10
10
 
11
- Defined in: [traverse-folder.ts:11](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/traverse-folder.ts#L11)
11
+ Defined in: [traverse-folder.ts:11](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/traverse-folder.ts#L11)
12
12
 
13
13
  A handler function for asynchronous folder traversal.
14
14
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  > **TraverseFolderSyncHandler** = (`filePath`, `entry`) => `boolean` \| `void`
10
10
 
11
- Defined in: [traverse-folder.ts:18](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/traverse-folder.ts#L18)
11
+ Defined in: [traverse-folder.ts:18](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/traverse-folder.ts#L18)
12
12
 
13
13
  A handler function for synchronous folder traversal.
14
14
 
@@ -8,4 +8,4 @@
8
8
 
9
9
  > `const` **DefaultAllTextFiles**: `string`[]
10
10
 
11
- Defined in: [include-files.ts:1](https://github.com/isdk/util.js/blob/d57e048e4f751b04d987b4327c0ccab1379da1c3/src/include-files.ts#L1)
11
+ Defined in: [include-files.ts:1](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/include-files.ts#L1)
@@ -0,0 +1,14 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / FilenameReservedRegex
6
+
7
+ # Variable: FilenameReservedRegex
8
+
9
+ > `const` **FilenameReservedRegex**: `RegExp`
10
+
11
+ Defined in: [filename.ts:8](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L8)
12
+
13
+ Regular expression pattern for reserved characters in a filename.
14
+ do not use /g global option here: when the regex is executed multiple times, it will always begin where it left off last time.
@@ -0,0 +1,13 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / WindowsReservedNameRegex
6
+
7
+ # Variable: WindowsReservedNameRegex
8
+
9
+ > `const` **WindowsReservedNameRegex**: `RegExp`
10
+
11
+ Defined in: [filename.ts:13](https://github.com/isdk/util.js/blob/337b47688186bc271c622eb5b7ca550ac681e127/src/filename.ts#L13)
12
+
13
+ Regular expression pattern for reserved names on Windows systems.
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@isdk/util",
3
3
  "description": "a set of utility functions",
4
- "version": "0.1.5",
4
+ "version": "0.1.6",
5
5
  "author": "Riceball LEE @snowyu",
6
6
  "bugs": "https://github.com/isdk/util.js/issues",
7
7
  "dependencies": {
8
+ "@isdk/common-error": "^0.1.4",
8
9
  "@isdk/glob": "^0.1.0",
9
10
  "load-config-file": "2.0.0",
10
11
  "yaml": "^2.7.0"