Skip to content

Commit

Permalink
兼容大小写
Browse files Browse the repository at this point in the history
  • Loading branch information
baiy committed Sep 16, 2023
1 parent 992c738 commit 381c35c
Showing 1 changed file with 47 additions and 31 deletions.
78 changes: 47 additions & 31 deletions packages/ctool-core/src/tools/unicode/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,76 @@ export const _typeLists = [
"unicode_number",
"html_entity_10",
"html_entity_16",
"css_entity"
] as const
"css_entity",
] as const;

export type TypeLists = typeof _typeLists[number]
export type TypeLists = (typeof _typeLists)[number];
export default {
decode(str: string, type: TypeLists = "unicode_point_default") {
const errorListener = (item, callback) => {
try {
return callback && callback()
return callback && callback();
} catch (e) {
throw new Error(`${item} decode error:${$error(e)}`)
throw new Error(`${item} decode error:${$error(e)}`);
}
}
};

switch (type) {
case "unicode_point_default":
return str.replace(/\\u[0-9a-fA-F]{4}/g, (item) => {
return errorListener(item, () => String.fromCodePoint(parseInt(`0x${item.toLowerCase().replace("\\u", "")}`)))
return str.replace(/\\[Uu]{1}[0-9a-fA-F]{4}/g, item => {
return errorListener(item, () =>
String.fromCodePoint(parseInt(`0x${item.toLowerCase().replace("\\u", "")}`)),
);
});
case "unicode_point_wide":
return str.replace(/\\u[0-9a-fA-F]{1,6}/g, (item) => {
return errorListener(item, () => String.fromCodePoint(parseInt(`0x${item.toLowerCase().replace("\\u", "")}`)))
return str.replace(/\\[Uu]{1}[0-9a-fA-F]{1,6}/g, item => {
return errorListener(item, () =>
String.fromCodePoint(parseInt(`0x${item.toLowerCase().replace("\\u", "")}`)),
);
});
case "unicode_point_wide_brace":
return str.replace(/\\u{[0-9a-fA-F]{1,6}}/g, (item) => {
return errorListener(item, () => String.fromCodePoint(parseInt(`0x${item.toLowerCase().replace("\\u", "").replace("{", "").replace("}", "")}`)))
return str.replace(/\\[Uu]{1}{[0-9a-fA-F]{1,6}}/g, item => {
return errorListener(item, () =>
String.fromCodePoint(
parseInt(`0x${item.toLowerCase().replace("\\u", "").replace("{", "").replace("}", "")}`),
),
);
});
case "unicode_number":
return str.replace(/U\+[0-9a-fA-F]{1,6}/g, (item) => {
return errorListener(item, () => String.fromCodePoint(parseInt(`0x${item.replace("U", "").toLowerCase().replace("+", "")}`)))
return str.replace(/U\+[0-9a-fA-F]{1,6}/g, item => {
return errorListener(item, () =>
String.fromCodePoint(parseInt(`0x${item.replace("U", "").toLowerCase().replace("+", "")}`)),
);
});
case "html_entity_10":
return str.replace(/&#[0-9]+;/g, (item) => {
return errorListener(item, () => String.fromCodePoint(parseInt(`${item.replace("&#", "").replace(";", "")}`)))
return str.replace(/&#[0-9]+;/g, item => {
return errorListener(item, () =>
String.fromCodePoint(parseInt(`${item.replace("&#", "").replace(";", "")}`)),
);
});
case "html_entity_16":
return str.replace(/&#x[0-9a-fA-F]{1,6};/g, (item) => {
return errorListener(item, () => String.fromCodePoint(parseInt(`0x${item.replace("&#x", "").toLowerCase().replace(";", "")}`)))
return str.replace(/&#x[0-9a-fA-F]{1,6};/g, item => {
return errorListener(item, () =>
String.fromCodePoint(parseInt(`0x${item.replace("&#x", "").toLowerCase().replace(";", "")}`)),
);
});
case "css_entity":
return str.replace(/\\[0-9a-fA-F]{1,6}/g, (item) => {
return errorListener(item, () => String.fromCodePoint(parseInt(`0x${item.replace("\\", "").toLowerCase()}`)))
return str.replace(/\\[0-9a-fA-F]{1,6}/g, item => {
return errorListener(item, () =>
String.fromCodePoint(parseInt(`0x${item.replace("\\", "").toLowerCase()}`)),
);
});
}
throw new Error("decode type error")
throw new Error("decode type error");
},
encode(str: string, type: TypeLists = "unicode_point_default", ignore_ascii = false) {
let code: string[] = []
let code: string[] = [];
for (let s of str) {
let decimalStr = s.codePointAt(0)?.toString(10) || "";
let hexStr = s.codePointAt(0)?.toString(16) || "";
if (hexStr.length < 3 && ignore_ascii) {
// 忽略ascii字符
code.push(s)
code.push(s);
continue;
}
// 补零
Expand All @@ -67,7 +83,7 @@ export default {
case "unicode_point_default":
if (hexStr.length > 4) {
// 宽字符处理
code.push(...this.charToUtf16(s).map((item) => `\\u${item}`))
code.push(...this.charToUtf16(s).map(item => `\\u${item}`));
} else {
code.push(`\\u${hexRepairStr}`);
}
Expand All @@ -91,19 +107,19 @@ export default {
code.push(`\\${hexRepairStr}`);
break;
default:
throw new Error("encode type error")
throw new Error("encode type error");
}
}
return code.join("");
},
charToUtf16(str: string) {
let arr: string[] = []
let arr: string[] = [];
for (let i = 0; i < str.length; i++) {
arr[i] = this.repair(str.charCodeAt(i).toString(16))
arr[i] = this.repair(str.charCodeAt(i).toString(16));
}
return arr
return arr;
},
repair(str: string) {
return str.length > 3 ? str : `${'0'.repeat(4 - str.length)}${str}`;
}
}
return str.length > 3 ? str : `${"0".repeat(4 - str.length)}${str}`;
},
};

0 comments on commit 381c35c

Please sign in to comment.