项目代码结构调整
This commit is contained in:
96
src/utils/transform.ts
Normal file
96
src/utils/transform.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { TransformTextResult } from "../type-definition/TransformTextResultType";
|
||||
|
||||
const logDebugInfo = false;
|
||||
|
||||
/**
|
||||
* 多选区分词
|
||||
*
|
||||
* @param multiSelectionInputs
|
||||
* @returns
|
||||
* @since 2024-04-03
|
||||
*/
|
||||
export function transformMutliSelectionText(selectionInputs: string[]): Array<TransformTextResult[]> {
|
||||
return selectionInputs.map(selectionInput => transformMutliLineText(selectionInput));
|
||||
}
|
||||
|
||||
/**
|
||||
* 多行内容分词(单一选区)
|
||||
*
|
||||
* @param multiLineInput
|
||||
* @returns
|
||||
* @since 2024-04-03
|
||||
*/
|
||||
export function transformMutliLineText(multiLineInput: string): TransformTextResult[] {
|
||||
const results: TransformTextResult[] = [];
|
||||
const lines = multiLineInput.split(/\r?\n/);
|
||||
for (const line of lines) {
|
||||
// console.log('line', '->' + line + '<-');
|
||||
results.push(transformText(line));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 独立段落单元分词 (不包含换行)
|
||||
*
|
||||
* @param str
|
||||
* @since 2024-04-02
|
||||
*/
|
||||
export function transformText(input: string): TransformTextResult {
|
||||
logDebugInfo && console.log('input ', '->' + input + '<-');
|
||||
|
||||
// 记录首尾空格
|
||||
const leadingSpaces = input.match(/^ +/);
|
||||
const trailingSpaces = /^[ ]+$/.test(input)
|
||||
? '' // 字符串全为空格时,将尾空格置为空字符串
|
||||
: input.match(/ +$/);
|
||||
|
||||
// 去除首尾空格
|
||||
// 不可以使用 input = input.trim(); 否则换行会被替换掉
|
||||
input = input.replace(/^ +| +$/g, '');
|
||||
|
||||
// 使用正则表达式匹配中英文字母、连字符、下划线、空格
|
||||
let result = input.replace(/([A-Za-z\-_ ]+)/g, (match: string) => {
|
||||
// 替换连字符为 '|' (如有多个则合并)
|
||||
match = match.replace(/[-_ ]+/g, '|');
|
||||
|
||||
// // 替换.时跳过连续点(例如Happy.. angry)
|
||||
// match = match.replace(/([^.])([.])([^.])/g, '$1|$3');
|
||||
|
||||
// 拆分连续的小写字母和大写字母为多个单词
|
||||
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, '');
|
||||
|
||||
// 还原首尾空格
|
||||
const leadingSpaceStr = leadingSpaces ? leadingSpaces[0] : '';
|
||||
const trailingSpaceStr = trailingSpaces ? trailingSpaces[0] : '';
|
||||
let noTrimResult = leadingSpaceStr + result + trailingSpaceStr;
|
||||
|
||||
logDebugInfo && console.log('output ', '->' + result + '<-');
|
||||
return {
|
||||
leadingSpace: leadingSpaceStr,
|
||||
trailingSpace: trailingSpaceStr,
|
||||
result: noTrimResult,
|
||||
trimResult: result,
|
||||
};
|
||||
}
|
20
src/utils/user-configuration.ts
Normal file
20
src/utils/user-configuration.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import vscode from 'vscode';
|
||||
|
||||
/**
|
||||
* 获取用户配置项
|
||||
*
|
||||
* @param configKey 配置项的键
|
||||
* @returns 配置项的值
|
||||
* @since 2024-07-29
|
||||
*/
|
||||
function getUserConfigurations<T>(configKey: string): T | undefined {
|
||||
const config = vscode.workspace.getConfiguration('variable-conversion');
|
||||
|
||||
const configValue = config.get<T>(configKey);
|
||||
// console.log('configValue:', configValue);
|
||||
return configValue;
|
||||
}
|
||||
|
||||
export {
|
||||
getUserConfigurations
|
||||
};
|
44
src/utils/utils.ts
Normal file
44
src/utils/utils.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 比较两个字符串数组 `Array<string>` 是否相同
|
||||
*
|
||||
* @param array1 数组1
|
||||
* @param array2 数组2
|
||||
* @returns
|
||||
* @since 2024-04-09
|
||||
*/
|
||||
export function isStringArrayEqual(array1: string[], array2: string[]) {
|
||||
if (array1.length !== array2.length) {
|
||||
return false;
|
||||
}
|
||||
for (let index = 0; index < array1.length; index++) {
|
||||
const element1 = array1[index];
|
||||
const element2 = array2[index];
|
||||
if (element1 !== element2) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除二维字符串数组中的重复数组元素
|
||||
*
|
||||
* 例如,输入 [["a", "b"], ["a", "b"], ["c", "d"]],会返回 [[ "a", "b"], ["c", "d"]],去除了重复出现的 ["a", "b"]。
|
||||
*
|
||||
* @param stringArr 要进行去重操作的二维字符串数组,即数组中每个元素又是一个字符串数组,代表一组相关的字符串元素集合。
|
||||
* @returns 返回一个二维字符串数组,其中已经去除了原输入二维数组中重复的元素组合,基于JSON序列化后的字符串比较来判定重复与否。
|
||||
* @since 2024-04-09
|
||||
*/
|
||||
export function stringListArrayDuplicateRemoval(stringArr: Array<string[]>): Array<string[]> {
|
||||
const tempArr: Array<string> = [];
|
||||
const newArr: Array<string[]> = [];
|
||||
for (let index = 0; index < stringArr.length; index++) {
|
||||
const element = stringArr[index];
|
||||
const elementStr = JSON.stringify(element);
|
||||
if (!tempArr.includes(elementStr)) {
|
||||
newArr.push(element);
|
||||
tempArr.push(elementStr);
|
||||
}
|
||||
}
|
||||
return newArr;
|
||||
}
|
Reference in New Issue
Block a user