unicode.js 4.8 KB
Newer Older
B
baiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
const unicode_number = "unicode_number";
const html_entity_10 = "html_entity_10";
const html_entity_16 = "html_entity_16";
const unicode_point_default = "unicode_point_default";
const unicode_point_wide = "unicode_point_wide";
const unicode_point_wide_brace = "unicode_point_wide_brace";
const css_entitie = "css_entitie";

export default {
    type: {
        unicode_point_default,
        unicode_point_wide,
        unicode_point_wide_brace,
        unicode_number,
        html_entity_10,
        html_entity_16,
        css_entitie
    },
B
baiy 已提交
19
    decode(str, type) {
B
baiy 已提交
20 21 22 23
        const errorListener = (item, callback) => {
            try {
                return callback && callback()
            } catch (e) {
B
baiy 已提交
24
                throw new Error(`${item} 解码异常:${e.message}`)
B
baiy 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
            }
        }

        switch (type) {
            case this.type.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", "")}`)))
                });
            case this.type.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", "")}`)))
                });
            case this.type.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("}", "")}`)))
                });
            case this.type.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("+", "")}`)))
                });
            case this.type.html_entity_10:
                return str.replace(/&#[0-9]+;/g, (item) => {
                    return errorListener(item, () => String.fromCodePoint(parseInt(`${item.replace("&#", "").replace(";", "")}`)))
                });
            case this.type.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(";", "")}`)))
                });
            case this.type.css_entitie:
                return str.replace(/\\[0-9a-fA-F]{1,6}/g, (item) => {
                    return errorListener(item, () => String.fromCodePoint(parseInt(`0x${item.replace("\\", "").toLowerCase()}`)))
                });
        }
B
baiy 已提交
58
        throw new Error("解码类型异常")
B
baiy 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    },
    encode(string, type = unicode_point_default, ignore_ascii = false) {
        let code = []
        for (let s of string) {
            let decimalStr = s.codePointAt(0).toString(10);
            let hexStr = s.codePointAt(0).toString(16);
            if (hexStr.length < 3 && ignore_ascii) {
                // 忽略ascii字符
                code.push(s)
                continue;
            }
            // 补零
            let hexRepairStr = this.repair(hexStr);
            switch (type) {
                case this.type.unicode_point_default:
                    if (hexStr.length > 4) {
                        // 宽字符处理
                        code.push(...this.charToUtf16(s).map((item) => `\\u${item}`))
                    } else {
                        code.push(`\\u${hexRepairStr}`);
                    }
                    break;
                case this.type.unicode_point_wide:
                    code.push(`\\u${hexRepairStr}`);
                    break;
                case this.type.unicode_point_wide_brace:
                    code.push(`\\u{${hexStr}}`);
                    break;
                case this.type.unicode_number:
                    code.push(`U+${hexRepairStr.toUpperCase()}`);
                    break;
                case this.type.html_entity_10:
                    code.push(`&#${decimalStr};`);
                    break;
                case this.type.html_entity_16:
                    code.push(`&#x${hexStr};`);
                    break;
                case this.type.css_entitie:
                    code.push(`\\${hexRepairStr}`);
                    break;
                default:
B
baiy 已提交
100
                    throw new Error("编码类型异常")
B
baiy 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114
            }
        }
        return code.join("");
    },
    charToUtf16(str) {
        let arr = []
        for (let i = 0; i < str.length; i++) {
            arr[i] = this.repair(str.charCodeAt(i).toString(16))
        }
        return arr
    },
    repair(str) {
        return str.length > 3 ? str : `${'0'.repeat(4 - str.length)}${str}`;
    }
B
baiy 已提交
115
}