编写测试用例
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { transformText } from './text-split';
|
||||
import { transformText } from './text-transform';
|
||||
|
||||
/**
|
||||
* 转小驼峰 to Camel Case
|
||||
*
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export function toCamelCase(str: string): string {
|
||||
@@ -19,9 +19,9 @@ export function toCamelCase(str: string): string {
|
||||
|
||||
/**
|
||||
* 转大驼峰 to Pascal Case
|
||||
*
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export function toPascalCase(str: string): string {
|
||||
@@ -30,9 +30,9 @@ export function toPascalCase(str: string): string {
|
||||
|
||||
/**
|
||||
* 转大写 to Upper Case
|
||||
*
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export function toUpperCase(str: string): string {
|
||||
@@ -41,9 +41,9 @@ export function toUpperCase(str: string): string {
|
||||
|
||||
/**
|
||||
* 转小写 to Lower Case
|
||||
*
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export function toLowerCase(str: string): string {
|
||||
|
@@ -1,56 +1,56 @@
|
||||
|
||||
const logDebugInfo = false;
|
||||
|
||||
/**
|
||||
* 分词
|
||||
*
|
||||
* @param str
|
||||
* @since 2024-04-02
|
||||
*/
|
||||
export function transformText(input: string): string {
|
||||
logDebugInfo && console.log('input ', '->' + input + '<-');
|
||||
|
||||
// 记录首尾空格
|
||||
const leadingSpaces = input.match(/^ +/);
|
||||
const trailingSpaces = input.match(/ +$/);
|
||||
|
||||
// 去除首尾空格
|
||||
input = input.trim();
|
||||
|
||||
// 使用正则表达式匹配中英文字母、连字符、下划线和空格
|
||||
let result = input.replace(/([A-Za-z\-_ ]+)/g, (match: string) => {
|
||||
|
||||
// 替换连字符为 '|' (如有多个则合并)
|
||||
match = match.replace(/[-_ ]+/g, '|');
|
||||
|
||||
// 拆分连续的小写字母和大写字母为多个单词
|
||||
match = match.replace(/([a-z])([A-Z])/g, '$1|$2');
|
||||
|
||||
// 分割
|
||||
let words = match.split('|');
|
||||
|
||||
// 处理特殊情况,如 'ENFADADO' 不应该被拆分
|
||||
words = words.map(word => {
|
||||
if (word.toUpperCase() === word && word.length > 1) {
|
||||
return word.toLowerCase();
|
||||
}
|
||||
return word.replace(/([A-Z])/g, '|$1').toLowerCase();
|
||||
});
|
||||
|
||||
// 重新组合单词
|
||||
return '|' + words.join('|') + '|';
|
||||
});
|
||||
|
||||
// 如果有多个 | 将其合并
|
||||
result = result.replace(/[\|]+/g, '|');
|
||||
|
||||
// 如果首尾有 | 将其替换掉
|
||||
result = result.replace(/(^[\|]+|[\|]+$)/g, '');
|
||||
|
||||
// 还原首尾空格
|
||||
// result = (leadingSpaces ? (leadingSpaces[0] + '|') : '') + result + (trailingSpaces ? ('|' + trailingSpaces[0]) : '');
|
||||
result = (leadingSpaces ? leadingSpaces[0] : '') + result + (trailingSpaces ? trailingSpaces[0] : '');
|
||||
|
||||
logDebugInfo && console.log('output ', '->' + result + '<-');
|
||||
return result;
|
||||
}
|
||||
|
||||
const logDebugInfo = false;
|
||||
|
||||
/**
|
||||
* 分词
|
||||
*
|
||||
* @param str
|
||||
* @since 2024-04-02
|
||||
*/
|
||||
export function transformText(input: string): string {
|
||||
logDebugInfo && console.log('input ', '->' + input + '<-');
|
||||
|
||||
// 记录首尾空格
|
||||
const leadingSpaces = input.match(/^ +/);
|
||||
const trailingSpaces = input.match(/ +$/);
|
||||
|
||||
// 去除首尾空格
|
||||
input = input.trim();
|
||||
|
||||
// 使用正则表达式匹配中英文字母、连字符、下划线和空格
|
||||
let result = input.replace(/([A-Za-z\-_ ]+)/g, (match: string) => {
|
||||
|
||||
// 替换连字符为 '|' (如有多个则合并)
|
||||
match = match.replace(/[-_ ]+/g, '|');
|
||||
|
||||
// 拆分连续的小写字母和大写字母为多个单词
|
||||
match = match.replace(/([a-z])([A-Z])/g, '$1|$2');
|
||||
|
||||
// 分割
|
||||
let words = match.split('|');
|
||||
|
||||
// 处理特殊情况,如 'ENFADADO' 不应该被拆分
|
||||
words = words.map(word => {
|
||||
if (word.toUpperCase() === word && word.length > 1) {
|
||||
return word.toLowerCase();
|
||||
}
|
||||
return word.replace(/([A-Z])/g, '|$1').toLowerCase();
|
||||
});
|
||||
|
||||
// 重新组合单词
|
||||
return '|' + words.join('|') + '|';
|
||||
});
|
||||
|
||||
// 如果有多个 | 将其合并
|
||||
result = result.replace(/[\|]+/g, '|');
|
||||
|
||||
// 如果首尾有 | 将其替换掉
|
||||
result = result.replace(/(^[\|]+|[\|]+$)/g, '');
|
||||
|
||||
// 还原首尾空格
|
||||
// result = (leadingSpaces ? (leadingSpaces[0] + '|') : '') + result + (trailingSpaces ? ('|' + trailingSpaces[0]) : '');
|
||||
result = (leadingSpaces ? leadingSpaces[0] : '') + result + (trailingSpaces ? trailingSpaces[0] : '');
|
||||
|
||||
logDebugInfo && console.log('output ', '->' + result + '<-');
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user