Created
July 5, 2023 01:56
-
-
Save edcottrell/92ad791d3d62b8d87d3e19efcc2ee898 to your computer and use it in GitHub Desktop.
TextExpander Snippet: Slug to Title Case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toTitleCase(text) { | |
var wordsToLeaveInLowerCase = [ | |
'a', | |
'about', | |
'above', | |
'after', | |
'again', | |
'against', | |
'an', | |
'and', | |
'any', | |
'as', | |
'at', | |
'because', | |
'before', | |
'below', | |
'between', | |
'both', | |
'but', | |
'by', | |
'down', | |
'during', | |
'each', | |
'for', | |
'from', | |
'further', | |
'how', | |
'if', | |
'in', | |
'into', | |
'more', | |
'most', | |
'nor', | |
'of', | |
'on', | |
'or', | |
'out', | |
'over', | |
'she', | |
'so', | |
'than', | |
'that', | |
'that\'s', | |
'the', | |
'this', | |
'those', | |
'through', | |
'to', | |
'under', | |
'until', | |
'up', | |
'what', | |
'when', | |
'where', | |
'which', | |
'while', | |
'with' | |
], | |
wordsWithSpecialCase = [ | |
['adp', 'ADP'], | |
['amex', 'AmEx'], | |
['amg', 'AMG'], | |
['aws', 'AWS'], | |
['codepen', 'CodePen'], | |
['cpanel', 'cPanel'], | |
['fsa', 'FSA'], | |
['ebay', 'eBay'], | |
['intellij', 'IntelliJ'], | |
['iphone', 'iPhone'], | |
['mysql', 'MySQL'], | |
['php', 'PHP'], | |
['phpstorm', 'PHPStorm'], | |
['sql', 'SQL'], | |
['ssh', 'SSH'], | |
['ssl', 'SSL'], | |
['usa', 'USA'] | |
], | |
reducer = function(accumulator, currentValue) {return accumulator + '|' + currentValue[0];}, | |
wordsWithSpecialCaseRegEx = new RegExp(wordsWithSpecialCase.reduce(reducer).replace(/,\w+/, ''), 'i'); | |
// NOT PART OF THE NORMAL TITLE-CASE SNIPPET | |
// Strip dashes | |
text = text.replace(/-/g, ' '); | |
// Decode URI as needed | |
text = decodeURIComponent(text); | |
// Handle line breaks | |
text = text.replace(/([a-z])-[\r\n]([a-z])/g, "$1$2"); | |
text = text.replace(/[\r\n]+/g, " "); | |
// Convert to Title Case | |
// Logic modified from https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript | |
text = text.replace(/\w\S*/mg, function(txt) { | |
var txtLC = txt.toLowerCase(), | |
wordIndex, | |
specialCaseWordsCount = wordsWithSpecialCase.length; | |
for (wordIndex = 0; wordIndex < specialCaseWordsCount; wordIndex++) { | |
if (txtLC === wordsWithSpecialCase[wordIndex][0]) { | |
return wordsWithSpecialCase[wordIndex][1]; | |
} | |
} | |
if (wordsToLeaveInLowerCase.indexOf(txtLC) !== -1) { | |
return txtLC; | |
} | |
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); | |
}); | |
if (!wordsWithSpecialCaseRegEx.test(text)) { | |
text = text.charAt(0).toUpperCase() + text.substr(1); | |
} | |
// Handle subtitles | |
text = text.replace(/\W [a-z]/g, function (text) { | |
return text.toUpperCase().replace(/\s/, ': ').replace(/::/, ':'); | |
}); | |
// Handle hyphenated (or en-dash-hyphenated) words | |
text = text.replace(/\w[-–][a-z]/g, function (text) { | |
return text.charAt(0) + text.charAt(1) + text.charAt(2).toUpperCase(); | |
}); | |
return text; | |
} | |
// Start with the clipboard content | |
var result = TextExpander.pasteboardText; | |
result = toTitleCase(result); | |
// Return the result | |
TextExpander.appendOutput(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Converts a "slugified" (kebab-case) piece of text, such as
cool-file-i-just-downloaded.pdf
, to its (English) title-case equivalent, such asCool File I Just Downloaded.pdf
.This is imperfect and may break on acronyms, but it meets 99% of my needs when I want to give a file a more friendly name.