util.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __read = (this && this.__read) || function (o, n) {
  13. var m = typeof Symbol === "function" && o[Symbol.iterator];
  14. if (!m) return o;
  15. var i = m.call(o), r, ar = [], e;
  16. try {
  17. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  18. }
  19. catch (error) { e = { error: error }; }
  20. finally {
  21. try {
  22. if (r && !r.done && (m = i["return"])) m.call(i);
  23. }
  24. finally { if (e) throw e.error; }
  25. }
  26. return ar;
  27. };
  28. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  29. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  30. if (ar || !(i in from)) {
  31. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  32. ar[i] = from[i];
  33. }
  34. }
  35. return to.concat(ar || Array.prototype.slice.call(from));
  36. };
  37. define(["require", "exports", "./constant"], function (require, exports, constant_1) {
  38. "use strict";
  39. Object.defineProperty(exports, "__esModule", { value: true });
  40. exports.compact = exports.compact2array = exports.combo = exports.combo2array = exports.convertUserOptions = exports.convertPinyinMode = exports.convertPinyinStyle = exports.hasKey = void 0;
  41. function hasKey(obj, key) {
  42. return Object.prototype.hasOwnProperty.call(obj, key);
  43. }
  44. exports.hasKey = hasKey;
  45. var pinyinStyleMap = new Map([
  46. ["tone", constant_1.ENUM_PINYIN_STYLE.TONE],
  47. ["TONE", constant_1.ENUM_PINYIN_STYLE.TONE],
  48. ["1", constant_1.ENUM_PINYIN_STYLE.TONE],
  49. ["tone2", constant_1.ENUM_PINYIN_STYLE.TONE2],
  50. ["TONE2", constant_1.ENUM_PINYIN_STYLE.TONE2],
  51. ["2", constant_1.ENUM_PINYIN_STYLE.TONE2],
  52. ["to3ne", constant_1.ENUM_PINYIN_STYLE.TO3NE],
  53. ["TO3NE", constant_1.ENUM_PINYIN_STYLE.TO3NE],
  54. ["5", constant_1.ENUM_PINYIN_STYLE.TO3NE],
  55. ["first_letter", constant_1.ENUM_PINYIN_STYLE.FIRST_LETTER],
  56. ["FIRST_LETTER", constant_1.ENUM_PINYIN_STYLE.FIRST_LETTER],
  57. ["4", constant_1.ENUM_PINYIN_STYLE.FIRST_LETTER],
  58. ["initials", constant_1.ENUM_PINYIN_STYLE.INITIALS],
  59. ["INITIALS", constant_1.ENUM_PINYIN_STYLE.INITIALS],
  60. ["3", constant_1.ENUM_PINYIN_STYLE.INITIALS],
  61. ["normal", constant_1.ENUM_PINYIN_STYLE.NORMAL],
  62. ["NORMAL", constant_1.ENUM_PINYIN_STYLE.NORMAL],
  63. ["0", constant_1.ENUM_PINYIN_STYLE.NORMAL],
  64. ["passport", constant_1.ENUM_PINYIN_STYLE.PASSPORT],
  65. ["PASSPORT", constant_1.ENUM_PINYIN_STYLE.PASSPORT],
  66. ["6", constant_1.ENUM_PINYIN_STYLE.PASSPORT],
  67. ]);
  68. function convertPinyinStyle(style) {
  69. var s = String(style);
  70. if (pinyinStyleMap.has(s)) {
  71. return pinyinStyleMap.get(s);
  72. }
  73. return constant_1.ENUM_PINYIN_STYLE.TONE;
  74. }
  75. exports.convertPinyinStyle = convertPinyinStyle;
  76. var pinyinModeMap = new Map([
  77. ["normal", constant_1.ENUM_PINYIN_MODE.NORMAL],
  78. ["NORMAL", constant_1.ENUM_PINYIN_MODE.NORMAL],
  79. ["surname", constant_1.ENUM_PINYIN_MODE.SURNAME],
  80. ["SURNAME", constant_1.ENUM_PINYIN_MODE.SURNAME],
  81. ]);
  82. function convertPinyinMode(mode) {
  83. var s = String(mode);
  84. if (pinyinModeMap.has(s)) {
  85. return pinyinModeMap.get(s);
  86. }
  87. return constant_1.ENUM_PINYIN_MODE.NORMAL;
  88. }
  89. exports.convertPinyinMode = convertPinyinMode;
  90. function convertUserOptions(options) {
  91. var segment = undefined;
  92. if (options === null || options === void 0 ? void 0 : options.segment) {
  93. if ((options === null || options === void 0 ? void 0 : options.segment) === true) {
  94. segment = "Intl.Segmenter";
  95. }
  96. else {
  97. segment = options.segment;
  98. }
  99. }
  100. var opt = __assign(__assign({}, constant_1.DEFAULT_OPTIONS), { style: convertPinyinStyle(options === null || options === void 0 ? void 0 : options.style), mode: convertPinyinMode(options === null || options === void 0 ? void 0 : options.mode), segment: segment, heteronym: (options === null || options === void 0 ? void 0 : options.heteronym) || false, group: (options === null || options === void 0 ? void 0 : options.group) || false });
  101. return opt;
  102. }
  103. exports.convertUserOptions = convertUserOptions;
  104. function combo2array(a1, a2) {
  105. var result = [];
  106. if (!a1.length) {
  107. return a2;
  108. }
  109. if (!a2.length) {
  110. return a1;
  111. }
  112. for (var i = 0, l = a1.length; i < l; i++) {
  113. for (var j = 0, m = a2.length; j < m; j++) {
  114. result.push(a1[i] + a2[j]);
  115. }
  116. }
  117. return result;
  118. }
  119. exports.combo2array = combo2array;
  120. function combo(arr) {
  121. if (arr.length === 0) {
  122. return [];
  123. }
  124. if (arr.length === 1) {
  125. return arr[0];
  126. }
  127. var result = combo2array(arr[0], arr[1]);
  128. for (var i = 2, l = arr.length; i < l; i++) {
  129. result = combo2array(result, arr[i]);
  130. }
  131. return result;
  132. }
  133. exports.combo = combo;
  134. function compact2array(a1, a2) {
  135. if (!Array.isArray(a1) || !Array.isArray(a2)) {
  136. throw new Error("compact2array expect two array as parameters");
  137. }
  138. if (!a1.length) {
  139. a1 = [""];
  140. }
  141. if (!a2.length) {
  142. a2 = [""];
  143. }
  144. var result = [];
  145. for (var i = 0, l = a1.length; i < l; i++) {
  146. for (var j = 0, m = a2.length; j < m; j++) {
  147. if (Array.isArray(a1[i])) {
  148. result.push(__spreadArray(__spreadArray([], __read(a1[i]), false), [a2[j]], false));
  149. }
  150. else {
  151. result.push([a1[i], a2[j]]);
  152. }
  153. }
  154. }
  155. return result;
  156. }
  157. exports.compact2array = compact2array;
  158. function compact(arr) {
  159. if (arr.length === 0) {
  160. return [];
  161. }
  162. if (arr.length === 1) {
  163. return [arr[0]];
  164. }
  165. var result = compact2array(arr[0], arr[1]);
  166. for (var i = 2, l = arr.length; i < l; ++i) {
  167. result = compact2array(result, arr[i]);
  168. }
  169. return result;
  170. }
  171. exports.compact = compact;
  172. });
  173. //# sourceMappingURL=util.js.map