CharEnc.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import Utils from "../Utils.js";
  2. import CryptoJS from "crypto-js";
  3. /**
  4. * Character encoding operations.
  5. *
  6. * @author n1474335 [n1474335@gmail.com]
  7. * @copyright Crown Copyright 2016
  8. * @license Apache-2.0
  9. *
  10. * @namespace
  11. */
  12. const CharEnc = {
  13. /**
  14. * @constant
  15. * @default
  16. */
  17. IO_FORMAT: ["UTF8", "UTF16", "UTF16LE", "UTF16BE", "Latin1", "Windows-1251", "Hex", "Base64"],
  18. /**
  19. * Text encoding operation.
  20. *
  21. * @param {string} input
  22. * @param {Object[]} args
  23. * @returns {string}
  24. */
  25. run: function(input, args) {
  26. let inputFormat = args[0],
  27. outputFormat = args[1];
  28. if (inputFormat === "Windows-1251") {
  29. input = Utils.win1251ToUnicode(input);
  30. input = CryptoJS.enc.Utf8.parse(input);
  31. } else {
  32. input = Utils.format[inputFormat].parse(input);
  33. }
  34. if (outputFormat === "Windows-1251") {
  35. input = CryptoJS.enc.Utf8.stringify(input);
  36. return Utils.unicodeToWin1251(input);
  37. } else {
  38. return Utils.format[outputFormat].stringify(input);
  39. }
  40. },
  41. };
  42. export default CharEnc;