UNPKG

@raycast/eslint-plugin

Version:

ESLint plugin designed to help Raycast's extensions authors follow best practices

104 lines (103 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.titleCase = titleCase; function titleCase(s) { const noCaps = { and: true, but: true, or: true, nor: true, for: true, yet: true, so: true, to: true, as: true, at: true, by: true, from: true, in: true, into: true, of: true, off: true, on: true, onto: true, out: true, over: true, up: true, with: true, }; const articles = { a: true, an: true, the: true, }; const fixedCaseWords = { npm: "npm", "crates.io": "crates.io", dbt: "dbt", "pub.dev": "pub.dev", kubectx: "kubectx", "monday.com": "monday.com", "ray.so": "ray.so", flomo: "flomo", iterm: "iTerm", xkcd: "xkcd", macos: "macOS", iphone: "iPhone", github: "GitHub", ide: "IDE", url: "URL", vs: "VS", ai: "AI", json: "JSON", ios: "iOS", id: "ID", "plug-in": "Plug-in", "built-in": "Built-in", vscode: "VS Code", css: "CSS", html: "HTML", ip: "IP", svg: "SVG" }; s = s.replace(/\.\.\./g, "…"); const words = s.split(" ").map((x) => x.toString()); for (let i = 0; i < words.length; i++) { let word = words[i]; let leadingNonLetter = ""; let trailingNonLetter = ""; const leadingMatch = word.match(/^[^a-zA-Z]+/); if (leadingMatch) { leadingNonLetter = leadingMatch[0]; word = word.slice(leadingNonLetter.length); } const trailingMatch = word.match(/[^a-zA-Z]+$/); if (trailingMatch) { trailingNonLetter = trailingMatch[0]; word = word.slice(0, word.length - trailingNonLetter.length); } const lowerWord = word.toLowerCase(); const ok = noCaps[lowerWord]; const isArticle = articles[lowerWord]; const fixedCase = fixedCaseWords[lowerWord]; if (fixedCase) { words[i] = fixedCase; } else if (word.startsWith("http://") || word.startsWith("https://")) { words[i] = word; } else if ((!ok && !isArticle) || i === 0 || (isArticle && words[i - 1].endsWith(":"))) { words[i] = word .split("-") .map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()) .join("-"); } else { words[i] = word.toLowerCase(); } words[i] = leadingNonLetter + words[i] + trailingNonLetter; } return words.join(" "); }