UNPKG

tstosc

Version:

A transpiler that convert TypeScript to SuperCollider's SCLang.

124 lines (122 loc) 5.09 kB
const isValidPath__posix_default_option = { platform: "posix" }; const isValidPath__windows_default_option = { platform: "windows", length_limit: 260, is_forward_slash_valid_separator: true }; function isValidPath(s, option = {}) { if (s.length == 0) { return false; } option.type ??= "dir"; option.platform ??= process.platform == "win32" ? "windows" : "posix"; switch (option.platform) { case "posix": { option = { ...isValidPath__posix_default_option, ...option }; return checkPOSIXPathType( s, option ) != 3 /* invalid */; } case "windows": { option = { ...isValidPath__windows_default_option, ...option }; return checkWindowsPathType( s, option ) != 7 /* invalid */; } default: throw TypeError(`Unknown platform "${option.platform}".`); } } function isValidWindowsPath(s, option) { return isValidPath(s, { ...option, platform: "windows" }); } function checkPOSIXPathType(s, option) { if (s.length == 0 || s.length > (option.length_limit ?? Number.POSITIVE_INFINITY)) { return 3 /* invalid */; } const name_invalid_regex = /\n\0/; function isValidNameSlashNamePath(name) { const name_group = name.split("/"); for (const f of name_group.slice(0, -1)) { if (name_invalid_regex.test(f)) { return false; } } if (/\n/.test(name_group.at(-1))) { return false; } return true; } function returnIfValidNameSlashNamePath(name, type_when_valid) { return isValidNameSlashNamePath(name) ? type_when_valid : 3 /* invalid */; } if (s[0] == "/") { return returnIfValidNameSlashNamePath(s.slice(1), 0 /* absolute_path */); } if (s[0] == "~" && s[1] == "/") { return returnIfValidNameSlashNamePath(s.slice(2), 2 /* home_directory_relative_path */); } return returnIfValidNameSlashNamePath(s, 1 /* working_directory_relative_path */); } var POSIXPathType = /* @__PURE__ */ ((POSIXPathType2) => { POSIXPathType2[POSIXPathType2["absolute_path"] = 0] = "absolute_path"; POSIXPathType2[POSIXPathType2["working_directory_relative_path"] = 1] = "working_directory_relative_path"; POSIXPathType2[POSIXPathType2["home_directory_relative_path"] = 2] = "home_directory_relative_path"; POSIXPathType2[POSIXPathType2["invalid"] = 3] = "invalid"; return POSIXPathType2; })(POSIXPathType || {}); function checkWindowsPathType(s, option) { if (s.length == 0 || s.length > (option.length_limit ?? Number.POSITIVE_INFINITY)) { return 7 /* invalid */; } const name_invalid_regex = /[<>:"/|?*\x01-\x1f]/; const isSepChar = option.is_forward_slash_valid_separator ? (c) => c == "\\" || c == "/" : (c) => c == "\\"; const sep_char_regex = option.is_forward_slash_valid_separator ? /[\\/]/ : /[\\]/; function isInvalidName(name) { return name_invalid_regex.test(name); } function isValidNameSlashNamePath(name) { for (const f of name.split(sep_char_regex)) { if (isInvalidName(f)) { return false; } } return true; } function returnIfValidNameSlashNamePath(name, type_when_valid) { return isValidNameSlashNamePath(name) ? type_when_valid : 7 /* invalid */; } function isAlphabet(c) { return "a" < c && c < "z" || "A" < c && c < "Z"; } if (s[0] == "\\") { if (s[1] == "\\") { return (s[2] == "." || s[2] == "?") && s[3] == "\\" ? returnIfValidNameSlashNamePath(s.slice(4), 0 /* device_path */) : returnIfValidNameSlashNamePath(s.slice(2), 1 /* unc_path */); } return returnIfValidNameSlashNamePath(s.slice(1), 4 /* root_relative_path */); } if (isAlphabet(s[0]) && s[1] == ":") { return isSepChar(s[2]) ? returnIfValidNameSlashNamePath(s.slice(3), 2 /* dos_path */) : returnIfValidNameSlashNamePath(s.slice(2), 5 /* drive_relative_path */); } if (!isInvalidName(s[0])) { return /^(CON|PRN|AUX|NUL|LPT[1-9]|COM[1-9])$/i.test(s) ? 3 /* legacy_device */ : returnIfValidNameSlashNamePath(s, 6 /* working_directory_relative_path */); } return 7 /* invalid */; } var WindowsPathType = /* @__PURE__ */ ((WindowsPathType2) => { WindowsPathType2[WindowsPathType2["device_path"] = 0] = "device_path"; WindowsPathType2[WindowsPathType2["unc_path"] = 1] = "unc_path"; WindowsPathType2[WindowsPathType2["dos_path"] = 2] = "dos_path"; WindowsPathType2[WindowsPathType2["legacy_device"] = 3] = "legacy_device"; WindowsPathType2[WindowsPathType2["root_relative_path"] = 4] = "root_relative_path"; WindowsPathType2[WindowsPathType2["drive_relative_path"] = 5] = "drive_relative_path"; WindowsPathType2[WindowsPathType2["working_directory_relative_path"] = 6] = "working_directory_relative_path"; WindowsPathType2[WindowsPathType2["invalid"] = 7] = "invalid"; WindowsPathType2[WindowsPathType2["trivial_path"] = 2 /* dos_path */] = "trivial_path"; return WindowsPathType2; })(WindowsPathType || {}); export { POSIXPathType, WindowsPathType, checkPOSIXPathType, checkWindowsPathType, isValidPath, isValidPath__posix_default_option, isValidPath__windows_default_option, isValidWindowsPath };