2017-03-23 17:52:20 +00:00
|
|
|
import Utils from "../Utils.js";
|
|
|
|
import CryptoJS from "crypto-js";
|
2017-03-06 12:45:51 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Character encoding operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const CharEnc = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
IO_FORMAT: ["UTF8", "UTF16", "UTF16LE", "UTF16BE", "Latin1", "Windows-1251", "Hex", "Base64"],
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Text encoding operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run: function(input, args) {
|
2017-01-31 18:24:56 +00:00
|
|
|
var inputFormat = args[0],
|
|
|
|
outputFormat = args[1];
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (inputFormat === "Windows-1251") {
|
|
|
|
input = Utils.win1251ToUnicode(input);
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Utf8.parse(input);
|
|
|
|
} else {
|
2017-01-31 18:24:56 +00:00
|
|
|
input = Utils.format[inputFormat].parse(input);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (outputFormat === "Windows-1251") {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Utf8.stringify(input);
|
2017-01-31 18:24:56 +00:00
|
|
|
return Utils.unicodeToWin1251(input);
|
2016-11-28 10:42:58 +00:00
|
|
|
} else {
|
2017-01-31 18:24:56 +00:00
|
|
|
return Utils.format[outputFormat].stringify(input);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default CharEnc;
|