1
0
mirror of synced 2025-02-21 04:26:43 +01:00

68 lines
1.3 KiB
JavaScript
Raw Normal View History

import Utils from "../Utils.js";
2016-11-28 10:42:58 +00:00
/**
* Unicode operations.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const Unicode = {
2016-11-28 10:42:58 +00:00
/**
* @constant
* @default
*/
PREFIXES: ["\\u", "%u", "U+"],
/**
* Unescape Unicode Characters operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runUnescape: function(input, args) {
2017-04-13 18:08:50 +01:00
let prefix = Unicode._prefixToRegex[args[0]],
2016-11-28 10:42:58 +00:00
regex = new RegExp(prefix+"([a-f\\d]{4,6})", "ig"),
output = "",
m,
i = 0;
2017-02-09 15:09:33 +00:00
2016-12-14 16:39:17 +00:00
while ((m = regex.exec(input))) {
2016-11-28 10:42:58 +00:00
// Add up to match
output += input.slice(i, m.index);
i = m.index;
2017-02-09 15:09:33 +00:00
2016-11-28 10:42:58 +00:00
// Add match
output += Utils.chr(parseInt(m[1], 16));
2017-02-09 15:09:33 +00:00
2016-11-28 10:42:58 +00:00
i = regex.lastIndex;
}
2017-02-09 15:09:33 +00:00
2016-11-28 10:42:58 +00:00
// Add all after final match
output += input.slice(i, input.length);
2017-02-09 15:09:33 +00:00
2016-11-28 10:42:58 +00:00
return output;
},
2017-02-09 15:09:33 +00:00
2016-11-28 10:42:58 +00:00
/**
* Lookup table to add prefixes to unicode delimiters so that they can be used in a regex.
*
* @private
* @constant
*/
_prefixToRegex: {
2016-11-28 10:42:58 +00:00
"\\u": "\\\\u",
"%u": "%u",
"U+": "U\\+"
},
};
export default Unicode;