Skip to content

Instantly share code, notes, and snippets.

@vv13
Last active November 22, 2021 16:02
Show Gist options
  • Save vv13/2bd79e7573857badced4279819f57c5b to your computer and use it in GitHub Desktop.
Save vv13/2bd79e7573857badced4279819f57c5b to your computer and use it in GitHub Desktop.
JavaScript 中英文标点转换
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