Last active
November 28, 2024 06:36
-
-
Save vv13/2bd79e7573857badced4279819f57c5b to your computer and use it in GitHub Desktop.
JavaScript 中英文标点转换
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const en = ["\"", "'", ",", ".", "?", "!", "(", ")", "[", "]", "{", "}", ";"]; | |
const cn = ["“", "‘", ",", "。", "?", "!", "(", ")", "【", "】", "「", "」", ";"]; | |
const converter = (source, from, target) => { | |
const map = new Map(from.map((item, index) => [item, target[index]])); | |
return source.split('').map(item => map.get(item) || item).join('') | |
}; | |
export const cn2enSymbol = (source) => converter(source, cn, en); | |
export const en2cnSymbol = (source) => converter(source, en, cn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment