Skip to content

Commit

Permalink
modernize Capitalize.popclipext
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotmoon committed May 10, 2024
1 parent e9150b7 commit 3cf2155
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 44 deletions.
6 changes: 0 additions & 6 deletions source/Capitalize.popclipext/Config.js

This file was deleted.

9 changes: 4 additions & 5 deletions source/Capitalize.popclipext/Config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"identifier" : "com.pilotmoon.popclip.extension.capitalize",
"name" : "Capitalize Words",
"popclipVersion" : 3543,
"identifier" : "xcom.pilotmoon.popclip.extension.capitalize",
"description" : "Start each selected word with a capital letter.",
"note": "Updated for macOS 12.0 Monterey.",
"icon" : "capitalize.png",
"title" : "Capitalize"
"icon" : "square filled Ab",
"popclipVersion" : 4151,
"module": "capitalize.js"
}
65 changes: 32 additions & 33 deletions source/Capitalize.popclipext/capitalize.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
function capitalizeWord(text) {
text = text.toLowerCase(); // first letter uppercase, others lowercase
return text.substr(0, 1).toUpperCase() + text.substr(1);
function capitalizeWord(text) {
text = text.toLowerCase(); // first letter uppercase, others lowercase
return text.substr(0, 1).toUpperCase() + text.substr(1);
}

function capitalizeAll(text) {
const regex=/(\p{L}+['’]\p{L}+|\p{L}+)/gu; // split into something approximating words
return text.replace(regex, (match) => capitalizeWord(match));
function capitalizeAll(text) {
const regex = /(\p{L}+['’]\p{L}+|\p{L}+)/gu; // split into something approximating words
return text.replace(regex, (match) => capitalizeWord(match));
}

if (typeof(define) !== 'undefined') { // when running in popclip, export the function
define(() => {
return (selection) => {
popclip.pasteText(capitalizeAll(selection.text));
}
})
}
else { // when running in jsc, perform tests
function test() {
const data = [
["blah", "Blah"],
["BLAH", "Blah"],
["'BLAH-blah'", "'Blah-Blah'"],
["\"BLAH-blah'", "\"Blah-Blah'"],
[" BLAH (blah-more", " Blah (Blah-More"],
["élan güt написанная!", "Élan Güt Написанная!"],
['"Nick\'s best dog\'s fur"', '"Nick\'s Best Dog\'s Fur"'],
["Nick's best dog's fur", "Nick's Best Dog's Fur"],
["😀nick's best dog’s fur", "😀Nick's Best Dog’s Fur"],
];
data.forEach((pair) => {
const [input, output]=pair;
const result=capitalizeAll(input);
print(`${output===result?'pass ':'fail *'} ${input} => ${result} (expected: ${output})`);
});
}
test();
exports.action = (input) => {
popclip.pasteText(capitalizeAll(input.text));
};

function test() {
const data = [
["blah", "Blah"],
["BLAH", "Blah"],
["'BLAH-blah'", "'Blah-Blah'"],
["\"BLAH-blah'", "\"Blah-Blah'"],
[" BLAH (blah-more", " Blah (Blah-More"],
["élan güt написанная!", "Élan Güt Написанная!"],
["\"Nick's best dog's fur\"", "\"Nick's Best Dog's Fur\""],
["Nick's best dog's fur", "Nick's Best Dog's Fur"],
["😀nick's best dog’s fur", "😀Nick's Best Dog’s Fur"],
];
data.forEach((pair) => {
const [input, output] = pair;
const result = capitalizeAll(input);
print(
`${
output === result ? "pass " : "fail *"
} ${input} => ${result} (expected: ${output})`,
);
});
}
test();
Binary file removed source/Capitalize.popclipext/capitalize.png
Binary file not shown.

0 comments on commit 3cf2155

Please sign in to comment.