You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

encoding.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var iconvLite = require('iconv-lite');
  3. // Expose to the world
  4. module.exports.convert = convert;
  5. /**
  6. * Convert encoding of an UTF-8 string or a buffer
  7. *
  8. * @param {String|Buffer} str String to be converted
  9. * @param {String} to Encoding to be converted to
  10. * @param {String} [from='UTF-8'] Encoding to be converted from
  11. * @return {Buffer} Encoded string
  12. */
  13. function convert(str, to, from) {
  14. from = checkEncoding(from || 'UTF-8');
  15. to = checkEncoding(to || 'UTF-8');
  16. str = str || '';
  17. var result;
  18. if (from !== 'UTF-8' && typeof str === 'string') {
  19. str = Buffer.from(str, 'binary');
  20. }
  21. if (from === to) {
  22. if (typeof str === 'string') {
  23. result = Buffer.from(str);
  24. } else {
  25. result = str;
  26. }
  27. } else {
  28. try {
  29. result = convertIconvLite(str, to, from);
  30. } catch (E) {
  31. console.error(E);
  32. result = str;
  33. }
  34. }
  35. if (typeof result === 'string') {
  36. result = Buffer.from(result, 'utf-8');
  37. }
  38. return result;
  39. }
  40. /**
  41. * Convert encoding of astring with iconv-lite
  42. *
  43. * @param {String|Buffer} str String to be converted
  44. * @param {String} to Encoding to be converted to
  45. * @param {String} [from='UTF-8'] Encoding to be converted from
  46. * @return {Buffer} Encoded string
  47. */
  48. function convertIconvLite(str, to, from) {
  49. if (to === 'UTF-8') {
  50. return iconvLite.decode(str, from);
  51. } else if (from === 'UTF-8') {
  52. return iconvLite.encode(str, to);
  53. } else {
  54. return iconvLite.encode(iconvLite.decode(str, from), to);
  55. }
  56. }
  57. /**
  58. * Converts charset name if needed
  59. *
  60. * @param {String} name Character set
  61. * @return {String} Character set name
  62. */
  63. function checkEncoding(name) {
  64. return (name || '')
  65. .toString()
  66. .trim()
  67. .replace(/^latin[\-_]?(\d+)$/i, 'ISO-8859-$1')
  68. .replace(/^win(?:dows)?[\-_]?(\d+)$/i, 'WINDOWS-$1')
  69. .replace(/^utf[\-_]?(\d+)$/i, 'UTF-$1')
  70. .replace(/^ks_c_5601\-1987$/i, 'CP949')
  71. .replace(/^us[\-_]?ascii$/i, 'ASCII')
  72. .toUpperCase();
  73. }