unicoder.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const codesets = [
  2. {
  3. name: 'paren',
  4. sets: [
  5. { code: 0x1f110, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  6. { code: 0x249c, ref: 'abcdefghijklmnopqrstuvwxyz' },
  7. { code: 0x2474, ref: '123456789' }
  8. ]
  9. },
  10. {
  11. name: 'whiteSquare',
  12. sets: [
  13. { code: 0x1f130, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  14. { code: 0x1f130, ref: 'abcdefghijklmnopqrstuvwxyz' }
  15. ]
  16. },
  17. {
  18. name: 'dice',
  19. sets: [
  20. { code: 0x2680, ref: '123456'}
  21. ]
  22. },
  23. {
  24. name: 'round',
  25. sets: [
  26. { code: 0x2460, ref: '123456789' },
  27. { code: 0x24ea, ref: '0' },
  28. { code: 0x24b6, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  29. { code: 0x24d0, ref: 'abcdefghijklmnopqrstuvwxyz' }
  30. ],
  31. '10': String.fromCodePoint(0x2469),
  32. '11': String.fromCodePoint(0x246a),
  33. '12': String.fromCodePoint(0x246b),
  34. '13': String.fromCodePoint(0x246c),
  35. '14': String.fromCodePoint(0x246d),
  36. '15': String.fromCodePoint(0x246e),
  37. '16': String.fromCodePoint(0x246f),
  38. '17': String.fromCodePoint(0x2470),
  39. '18': String.fromCodePoint(0x2471),
  40. '19': String.fromCodePoint(0x2472),
  41. '20': String.fromCodePoint(0x2473)
  42. },
  43. {
  44. name: 'recycling',
  45. sets: [
  46. { code: 0x2673, ref: '1234567' }
  47. ]
  48. },
  49. {
  50. name: 'negRound',
  51. sets: [
  52. { code: 0x1f150, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  53. { code: 0x1f150, ref: 'abcdefghijklmnopqrstuvwxyz' },
  54. { code: 0x2776, ref: '123456789'},
  55. { code: 0x24ff, ref: '0' }
  56. ],
  57. '10': String.fromCodePoint(0x277f),
  58. '11': String.fromCodePoint(0x24eb),
  59. '12': String.fromCodePoint(0x24ec),
  60. '13': String.fromCodePoint(0x24ed),
  61. '14': String.fromCodePoint(0x24ee),
  62. '15': String.fromCodePoint(0x24ef),
  63. '16': String.fromCodePoint(0x24f0),
  64. '17': String.fromCodePoint(0x24f1),
  65. '18': String.fromCodePoint(0x24f2),
  66. '19': String.fromCodePoint(0x24f3),
  67. '20': String.fromCodePoint(0x24f4)
  68. },
  69. {
  70. name: 'negSquare',
  71. sets: [
  72. { code: 0x1f170, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  73. { code: 0x1f170, ref: 'abcdefghijklmnopqrstuvwxyz' }
  74. ]
  75. },
  76. {
  77. name: 'indicator',
  78. sets: [
  79. { code: 0x1f1e6, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  80. { code: 0x1f1e6, ref: 'abcdefghijklmnopqrstuvwxyz' }
  81. ]
  82. },
  83. {
  84. name: 'doubleStruck',
  85. sets: [
  86. { code: 0x1d538, ref: 'AB DEFG IJKLM O STUVWXY ' },
  87. { code: 0x1d552, ref: 'abcdefghijklmnopqrstuvwxyz' },
  88. { code: 0x1d7d8, ref: '0123456789'},
  89. { code: 0x2102, ref: 'C' },
  90. { code: 0x210D, ref: 'H' },
  91. { code: 0x2115, ref: 'N' },
  92. { code: 0x2119, ref: 'P' },
  93. { code: 0x211A, ref: 'Q' },
  94. { code: 0x211D, ref: 'R' },
  95. { code: 0x2124, ref: 'Z' }
  96. ]
  97. },
  98. {
  99. name: 'fraktur',
  100. sets: [
  101. { code: 0x1d56c, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  102. { code: 0x1d586, ref: 'abcdefghijklmnopqrstuvwxyz' }
  103. ]
  104. },
  105. {
  106. name: 'sansSerif',
  107. sets: [
  108. { code: 0x1d5a0, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  109. { code: 0x1d5ba, ref: 'abcdefghijklmnopqrstuvwxyz' },
  110. { code: 0x1d7e2, ref: '0123456789'}
  111. ]
  112. },
  113. {
  114. name: 'bold',
  115. sets: [
  116. { code: 0x1d400, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  117. { code: 0x1d41a, ref: 'abcdefghijklmnopqrstuvwxyz' },
  118. { code: 0x1d7ce, ref: '0123456789' }
  119. ]
  120. },
  121. {
  122. name: 'sansSerifBold',
  123. sets: [
  124. { code: 0x1d5d4, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  125. { code: 0x1d5ee, ref: 'abcdefghijklmnopqrstuvwxyz' },
  126. { code: 0x1d7ce, ref: '0123456789' }
  127. ]
  128. },
  129. {
  130. name: 'italic',
  131. sets: [
  132. { code: 0x1d434, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  133. { code: 0x1d622, ref: 'abcdefghijklmnopqrstuvwxyz' }
  134. ]
  135. },
  136. {
  137. name: 'sansSerifItalic',
  138. sets: [
  139. { code: 0x1d608, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  140. { code: 0x1d622, ref: 'abcdefghijklmnopqrstuvwxyz' }
  141. ]
  142. },
  143. {
  144. name: 'boldItalic',
  145. sets: [
  146. { code: 0x1d468, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  147. { code: 0x1d482, ref: 'abcdefghijklmnopqrstuvwxyz' }
  148. ]
  149. },
  150. {
  151. name: 'sansSerifBoldItalic',
  152. sets: [
  153. { code: 0x1d63c, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  154. { code: 0x1d656, ref: 'abcdefghijklmnopqrstuvwxyz' }
  155. ]
  156. },
  157. {
  158. name: 'monospace',
  159. sets: [
  160. { code: 0x1d670, ref: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
  161. { code: 0x1d68a, ref: 'abcdefghijklmnopqrstuvwxyz' },
  162. { code: 0x1d7f6, ref: '0123456789' }
  163. ]
  164. },
  165. {
  166. name: 'script',
  167. sets: [
  168. { code: 0x1d49c, ref: 'A CD G JK NOPQ STUVWXYZ' },
  169. { code: 0x1d4b6, ref: 'abcd f hijklmn pqrstuvwxyz' }
  170. ]
  171. },
  172. {
  173. name: 'circles',
  174. black: String.fromCodePoint(0x26ab),
  175. white: String.fromCodePoint(0x26aa),
  176. red: String.fromCodePoint(0x1f534),
  177. blue: String.fromCodePoint(0x1f535)
  178. }
  179. ]
  180. codesets.forEach(cs => {
  181. codesets[cs.name] = cs
  182. if (cs.sets) {
  183. cs.sets.forEach(set => {
  184. set.ref.split('').forEach((c, i) => {
  185. if (c !== ' ') {
  186. cs[c] = String.fromCodePoint(set.code + i)
  187. }
  188. })
  189. })
  190. }
  191. })
  192. const convertAlpha = (codeset, string) => {
  193. if (typeof codeset === 'string' || typeof codeset === 'number') codeset = codesets[codeset]
  194. if (!codeset) return null
  195. const codes = []
  196. for (let i = 0; i < string.length; i++) {
  197. const char = string[i]
  198. if (char === ' ') {
  199. codes.push(32)
  200. continue
  201. }
  202. let found = false
  203. for (var c = 0; c < codeset.sets.length; c++) {
  204. const set = codeset.sets[c]
  205. const index = set.ref.indexOf(char)
  206. if (index !== -1) {
  207. codes.push(set.code + index)
  208. found = true
  209. break
  210. }
  211. }
  212. if (!found) codes.push(char.charCodeAt())
  213. }
  214. return String.fromCodePoint(...codes)
  215. }
  216. module.exports = {
  217. codesets,
  218. convertAlpha
  219. }