i18-fe-automator
Version:
前端代码提取中文并替换成$t函数
41 lines (40 loc) • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.classifyString = void 0;
function formatString(input) {
// 使用正则表达式将每个单词的首字母大写
return input.replace(/\b\w/g, function (match) {
return match.toUpperCase();
});
}
function classifyString(input) {
// 使用正则表达式检查输入是否为单词
const wordPattern = /^[a-zA-Z]+$/;
const phrasePattern = /[.,;!]/;
if (wordPattern.test(input)) {
return formatString(input);
}
else if (!phrasePattern.test(input)) {
const words = input.split(/\s+/);
if (words.length >= 4) {
return input.charAt(0).toUpperCase() + input.slice(1);
}
else {
return formatString(input);
}
}
else {
return input.charAt(0).toUpperCase() + input.slice(1);
}
}
exports.classifyString = classifyString;
// 测试示例
// console.log(classifyString('dog')) // 输出: Dog
// console.log(classifyString('in the morning')) // 输出: In The Morning
// console.log(classifyString('in the morning dd')) // 输出: In The Morning
// console.log(classifyString('I love programming.')) // 输出: I love programming.
// console.log(classifyString('in the {slot1}'))
// console.log(classifyString('in abc {aaaa}'))
// console.log(classifyString('in {slot1}')) // 输出: This is {a sentence} with {curly braces}.
// console.log(classifyString('This is {a sentence} with {curly braces}.')) // 输出: This is {a sentence} with {curly braces}.
// console.log(classifyString('This is a long sentence with more than four words.')) // 输出: This is a long sentence with more than four words.