encoding-factory.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. // This is free and unencumbered software released into the public domain.
  3. // See LICENSE.md for more information.
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. var big5_1 = require("../coders/big5");
  6. var euc_jp_1 = require("../coders/euc-jp");
  7. var euc_kr_1 = require("../coders/euc-kr");
  8. var gb18030_1 = require("../coders/gb18030");
  9. var iso_2022_jp_1 = require("../coders/iso-2022-jp");
  10. var shift_jis_1 = require("../coders/shift-jis");
  11. var single_byte_1 = require("../coders/single-byte");
  12. var utf_16_1 = require("../coders/utf-16");
  13. var utf_8_1 = require("../coders/utf-8");
  14. var x_user_defined_1 = require("../coders/x-user-defined");
  15. var encodings_1 = require("./encodings");
  16. var indexes_1 = require("./indexes");
  17. var encoding_indexes_provider_1 = require("./encoding-indexes-provider");
  18. //
  19. // Utilities
  20. //
  21. // import './encoding/utilities';
  22. //
  23. // Implementation of Encoding specification
  24. // https://encoding.spec.whatwg.org/
  25. //
  26. //
  27. // 4. Terminology
  28. //
  29. // import './encoding/terminology';
  30. //
  31. // 5. Encodings
  32. //
  33. // import "./encoding/encodings";
  34. //
  35. // 6. Indexes
  36. //
  37. // import './encoding/indexes';
  38. var encodingIndexes = encoding_indexes_provider_1.getEncodingIndexes();
  39. // Registry of of encoder/decoder factories, by encoding name.
  40. /** @type {Object.<string, function({fatal:boolean}): Encoder>} */
  41. // const encoders: Encoders = {};
  42. /** @type {Object.<string, function({fatal:boolean}): Decoder>} */
  43. // const decoders: Decoders = {};
  44. //
  45. // 10. Legacy single-byte encodings
  46. //
  47. // 10.1 single-byte decoder
  48. // 10.2 single-byte encoder
  49. exports.encoders = {
  50. // 9.1 utf-8
  51. // 9.1.1 utf-8 decoder
  52. // 9.1.2 utf-8 encoder
  53. /** @param {{fatal: boolean}} options */
  54. 'UTF-8': function (options) { return new utf_8_1.UTF8Encoder(options); },
  55. //
  56. // 11. Legacy multi-byte Chinese (simplified) encodings
  57. //
  58. // 11.1 gbk
  59. // 11.1.1 gbk decoder
  60. // gbk's decoder is gb18030's decoder.
  61. // 11.1.2 gbk encoder
  62. // gbk's encoder is gb18030's encoder with its gbk flag set.
  63. /** @param {{fatal: boolean}} options */
  64. 'GBK': function (options) { return new gb18030_1.GB18030Encoder(options, true); },
  65. // 11.2 gb18030
  66. // 11.2.1 gb18030 decoder
  67. // 11.2.2 gb18030 encoder
  68. /** @param {{fatal: boolean}} options */
  69. 'gb18030': function (options) { return new gb18030_1.GB18030Encoder(options); },
  70. //
  71. // 12. Legacy multi-byte Chinese (traditional) encodings
  72. //
  73. // 12.1 Big5
  74. // 12.1.1 Big5 decoder
  75. // 12.1.2 Big5 encoder
  76. /** @param {{fatal: boolean}} options */
  77. 'Big5': function (options) { return new big5_1.Big5Encoder(options); },
  78. //
  79. // 13. Legacy multi-byte Japanese encodings
  80. //
  81. // 13.1 euc-jp
  82. // 13.1.1 euc-jp decoder
  83. // 13.1.2 euc-jp encoder
  84. /** @param {{fatal: boolean}} options */
  85. 'EUC-JP': function (options) { return new euc_jp_1.EUCJPEncoder(options); },
  86. // 13.2 iso-2022-jp
  87. // 13.2.1 iso-2022-jp decoder
  88. // 13.2.2 iso-2022-jp encoder
  89. /** @param {{fatal: boolean}} options */
  90. 'ISO-2022-JP': function (options) { return new iso_2022_jp_1.ISO2022JPEncoder(options); },
  91. // 13.3 Shift_JIS
  92. // 13.3.1 Shift_JIS decoder
  93. // 13.3.2 Shift_JIS encoder
  94. /** @param {{fatal: boolean}} options */
  95. 'Shift_JIS': function (options) { return new shift_jis_1.ShiftJISEncoder(options); },
  96. //
  97. // 14. Legacy multi-byte Korean encodings
  98. //
  99. // 14.1 euc-kr
  100. // 14.1.1 euc-kr decoder
  101. // 14.1.2 euc-kr encoder
  102. /** @param {{fatal: boolean}} options */
  103. 'EUC-KR': function (options) { return new euc_kr_1.EUCKREncoder(options); },
  104. //
  105. // 15. Legacy miscellaneous encodings
  106. //
  107. // 15.1 replacement
  108. // Not needed - API throws RangeError
  109. // 15.2 Common infrastructure for utf-16be and utf-16le
  110. // 15.2.1 shared utf-16 decoder
  111. // 15.2.2 shared utf-16 encoder
  112. // 15.3 utf-16be
  113. // 15.3.1 utf-16be decoder
  114. /** @param {{fatal: boolean}} options */
  115. 'UTF-16BE': function (options) { return new utf_16_1.UTF16Encoder(true, options); },
  116. // 15.3.2 utf-16be encoder
  117. // 15.4 utf-16le
  118. // 15.4.1 utf-16le decoder
  119. /** @param {{fatal: boolean}} options */
  120. 'UTF-16LE': function (options) { return new utf_16_1.UTF16Encoder(false, options); },
  121. // 15.4.2 utf-16le encoder
  122. // 15.5 x-user-defined
  123. // 15.5.1 x-user-defined decoder
  124. // 15.5.2 x-user-defined encoder
  125. /** @param {{fatal: boolean}} options */
  126. 'x-user-defined': function (options) { return new x_user_defined_1.XUserDefinedEncoder(options); },
  127. };
  128. exports.decoders = {
  129. /** @param {{fatal: boolean}} options */
  130. 'UTF-8': function (options) { return new utf_8_1.UTF8Decoder(options); },
  131. /** @param {{fatal: boolean}} options */
  132. 'GBK': function (options) { return new gb18030_1.GB18030Decoder(options); },
  133. /** @param {{fatal: boolean}} options */
  134. 'gb18030': function (options) { return new gb18030_1.GB18030Decoder(options); },
  135. /** @param {{fatal: boolean}} options */
  136. 'Big5': function (options) { return new big5_1.Big5Decoder(options); },
  137. /** @param {{fatal: boolean}} options */
  138. 'EUC-JP': function (options) { return new euc_jp_1.EUCJPDecoder(options); },
  139. /** @param {{fatal: boolean}} options */
  140. 'ISO-2022-JP': function (options) { return new iso_2022_jp_1.ISO2022JPDecoder(options); },
  141. /** @param {{fatal: boolean}} options */
  142. 'Shift_JIS': function (options) { return new shift_jis_1.ShiftJISDecoder(options); },
  143. /** @param {{fatal: boolean}} options */
  144. 'EUC-KR': function (options) { return new euc_kr_1.EUCKRDecoder(options); },
  145. /** @param {{fatal: boolean}} options */
  146. 'UTF-16BE': function (options) { return new utf_16_1.UTF16Decoder(true, options); },
  147. /** @param {{fatal: boolean}} options */
  148. 'UTF-16LE': function (options) { return new utf_16_1.UTF16Decoder(false, options); },
  149. /** @param {{fatal: boolean}} options */
  150. 'x-user-defined': function (options) { return new x_user_defined_1.XUserDefinedDecoder(options); },
  151. };
  152. if (encodingIndexes) {
  153. encodings_1.encodings.forEach(function (category) {
  154. if (category.heading !== 'Legacy single-byte encodings')
  155. return;
  156. category.encodings.forEach(function (encoding) {
  157. var name = encoding.name;
  158. var idx = indexes_1.index(name.toLowerCase());
  159. /** @param {{fatal: boolean}} options */
  160. exports.decoders[name] = function (options) {
  161. return new single_byte_1.SingleByteDecoder(idx, options);
  162. };
  163. /** @param {{fatal: boolean}} options */
  164. exports.encoders[name] = function (options) {
  165. return new single_byte_1.SingleByteEncoder(idx, options);
  166. };
  167. });
  168. });
  169. }
  170. //# sourceMappingURL=encoding-factory.js.map