table.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import Encodings from './encodings.js'
  2. import { UTF8Decoder, UTF8Encoder } from './utf8.js'
  3. import { UTF16Decoder, UTF16Encoder } from './utf16.js'
  4. import { GB18030Decoder, GB18030Encoder } from './gb18030.js'
  5. import { Big5Decoder, Big5Encoder } from './big5.js'
  6. import { EUCJPDecoder, EUCJPEncoder } from './euc-jp.js'
  7. import { EUCKRDecoder, EUCKREncoder } from './euc-kr.js'
  8. import { ISO2022JPDecoder, ISO2022JPEncoder } from './iso-2022-jp.js'
  9. import { XUserDefinedDecoder, XUserDefinedEncoder } from './x-user-defined.js'
  10. import { ShiftJISDecoder, ShiftJISEncoder } from './shift-jis.js'
  11. import { SingleByteDecoder, SingleByteEncoder } from './single-byte.js'
  12. import index from './text_decoder_indexes.js';
  13. // 5.2 Names and labels
  14. // TODO: Define @typedef for Encoding: {name:string,labels:Array.<string>}
  15. // https://github.com/google/closure-compiler/issues/247
  16. // Label to encoding registry.
  17. /** @type {Object.<string,{name:string,labels:Array.<string>}>} */
  18. export const label_to_encoding = {}
  19. Encodings.forEach(({ encodings }) => {
  20. encodings.forEach((encoding) => {
  21. encoding.labels.forEach((label) => {
  22. label_to_encoding[label] = encoding
  23. })
  24. })
  25. })
  26. // Registry of of encoder/decoder factories, by encoding name.
  27. export const encoders = {
  28. 'UTF-8'() { // 9.1 utf-8
  29. return new UTF8Encoder()
  30. },
  31. 'GBK'(options) { // 11.1.2 gbk encoder;
  32. // gbk's encoder is gb18030's encoder with its gbk flag set.
  33. return new GB18030Encoder(options, true)
  34. },
  35. 'gb18030'() {
  36. return new GB18030Encoder()
  37. },
  38. 'Big5'() {
  39. return new Big5Encoder()
  40. },
  41. 'EUC-JP'() {
  42. return new EUCJPEncoder()
  43. },
  44. 'EUC-KR'() {
  45. return new EUCKREncoder()
  46. },
  47. 'ISO-2022-JP'() {
  48. return new ISO2022JPEncoder()
  49. },
  50. 'UTF-16BE'() { // 15.3 utf-16be
  51. return new UTF16Encoder(true)
  52. },
  53. 'UTF-16LE'() { // 15.4 utf-16le
  54. return new UTF16Encoder()
  55. },
  56. 'x-user-defined'() {
  57. return new XUserDefinedEncoder()
  58. },
  59. 'Shift_JIS'() {
  60. return new ShiftJISEncoder()
  61. },
  62. }
  63. /** @type {Object.<string, function({fatal:boolean}): Decoder>} */
  64. export const decoders = {
  65. 'UTF-8'(options) { // 9.1.1 utf-8 decoder
  66. return new UTF8Decoder(options)
  67. },
  68. 'GBK'(options) { // 11.1.1 gbk decoder; gbk's decoder is gb18030's decoder.
  69. return new GB18030Decoder(options)
  70. },
  71. 'gb18030'(options) {
  72. return new GB18030Decoder(options)
  73. },
  74. 'Big5'(options) {
  75. return new Big5Decoder(options)
  76. },
  77. 'EUC-JP'(options) {
  78. return new EUCJPDecoder(options)
  79. },
  80. 'EUC-KR'(options) {
  81. return new EUCKRDecoder(options)
  82. },
  83. 'ISO-2022-JP'(options) {
  84. return new ISO2022JPDecoder(options)
  85. },
  86. 'UTF-16BE'(options) { // 15.3.1 utf-16be decoder
  87. return new UTF16Decoder(true, options)
  88. },
  89. 'UTF-16LE'(options) { // 15.4.1 utf-16le decoder
  90. return new UTF16Decoder(false, options)
  91. },
  92. 'x-user-defined'() {
  93. return new XUserDefinedDecoder()
  94. },
  95. 'Shift_JIS'(options) {
  96. return new ShiftJISDecoder(options)
  97. },
  98. }
  99. Encodings.forEach(({ heading, encodings }) => {
  100. if (heading != 'Legacy single-byte encodings')
  101. return
  102. encodings.forEach((encoding) => {
  103. const name = encoding.name
  104. const idx = index(name.toLowerCase())
  105. decoders[name] = (options) => {
  106. return new SingleByteDecoder(idx, options)
  107. }
  108. encoders[name] = (options) => {
  109. return new SingleByteEncoder(idx, options)
  110. }
  111. })
  112. })