Skip to content

Instantly share code, notes, and snippets.

@MakotoE MakotoE/titleCase.jsee
Last active Dec 27, 2018

Embed
What would you like to do?
// Words to exclude
var excludeWords = ['a', 'an', 'and', 'for', 'or', 'as', 'at', 'of'];
/*
Choose between:
eeCaseLowerCase
eeCaseUpperCase
eeCaseCapitalize
*/
var convertTo = eeCaseCapitalize;
function coords() {
return {
x: document.selection.GetActivePointX(eePosLogical),
y: document.selection.GetActivePointY(eePosLogical),
};
}
function includes(arr, find) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === find) {
return true;
}
}
return false;
}
var matchNonwhitespace = new RegExp('\\S');
var left = {
x: document.selection.GetTopPointX(eePosLogical),
y: document.selection.GetTopPointY(eePosLogical),
};
var selected = {
x: document.selection.GetBottomPointX(eePosLogical),
y: document.selection.GetBottomPointY(eePosLogical),
};
if (left.x > 1) {
document.selection.SetActivePoint(eePosLogical, left.x, left.y);
document.selection.SetActivePoint(eePosLogical, left.x - 1, left.y, true);
if (document.selection.Text !== ' ') {
document.selection.WordRight();
left = coords();
}
}
while (left.y < selected.y || left.x < selected.x) {
document.selection.SetActivePoint(eePosLogical, left.x, left.y);
document.selection.WordRight(true);
var right = coords();
if (includes(['’', '\''], document.selection.Text)) { // Skip apostrophe
document.selection.WordRight(true);
right = coords();
} else if (matchNonwhitespace.test(document.selection.Text)) {
if (document.selection.Text.slice(-1) === ' ') {
right = coords();
document.selection.SetActivePoint(eePosLogical, left.x, left.y);
document.selection.SetActivePoint(eePosLogical, right.x - 1, right.y, true);
}
if (!includes(excludeWords, document.selection.Text)) {
document.selection.ChangeCase( convertTo );
}
}
left = right;
}
document.selection.CharRight();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.