util.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { ENUM_PINYIN_STYLE, ENUM_PINYIN_MODE, DEFAULT_OPTIONS } from "./constant";
  2. export function hasKey(obj, key) {
  3. return Object.prototype.hasOwnProperty.call(obj, key);
  4. }
  5. const pinyinStyleMap = new Map([
  6. ["tone", ENUM_PINYIN_STYLE.TONE],
  7. ["TONE", ENUM_PINYIN_STYLE.TONE],
  8. ["1", ENUM_PINYIN_STYLE.TONE],
  9. ["tone2", ENUM_PINYIN_STYLE.TONE2],
  10. ["TONE2", ENUM_PINYIN_STYLE.TONE2],
  11. ["2", ENUM_PINYIN_STYLE.TONE2],
  12. ["to3ne", ENUM_PINYIN_STYLE.TO3NE],
  13. ["TO3NE", ENUM_PINYIN_STYLE.TO3NE],
  14. ["5", ENUM_PINYIN_STYLE.TO3NE],
  15. ["first_letter", ENUM_PINYIN_STYLE.FIRST_LETTER],
  16. ["FIRST_LETTER", ENUM_PINYIN_STYLE.FIRST_LETTER],
  17. ["4", ENUM_PINYIN_STYLE.FIRST_LETTER],
  18. ["initials", ENUM_PINYIN_STYLE.INITIALS],
  19. ["INITIALS", ENUM_PINYIN_STYLE.INITIALS],
  20. ["3", ENUM_PINYIN_STYLE.INITIALS],
  21. ["normal", ENUM_PINYIN_STYLE.NORMAL],
  22. ["NORMAL", ENUM_PINYIN_STYLE.NORMAL],
  23. ["0", ENUM_PINYIN_STYLE.NORMAL],
  24. ["passport", ENUM_PINYIN_STYLE.PASSPORT],
  25. ["PASSPORT", ENUM_PINYIN_STYLE.PASSPORT],
  26. ["6", ENUM_PINYIN_STYLE.PASSPORT],
  27. ]);
  28. export function convertPinyinStyle(style) {
  29. const s = String(style);
  30. if (pinyinStyleMap.has(s)) {
  31. return pinyinStyleMap.get(s);
  32. }
  33. return ENUM_PINYIN_STYLE.TONE;
  34. }
  35. const pinyinModeMap = new Map([
  36. ["normal", ENUM_PINYIN_MODE.NORMAL],
  37. ["NORMAL", ENUM_PINYIN_MODE.NORMAL],
  38. ["surname", ENUM_PINYIN_MODE.SURNAME],
  39. ["SURNAME", ENUM_PINYIN_MODE.SURNAME],
  40. ]);
  41. export function convertPinyinMode(mode) {
  42. const s = String(mode);
  43. if (pinyinModeMap.has(s)) {
  44. return pinyinModeMap.get(s);
  45. }
  46. return ENUM_PINYIN_MODE.NORMAL;
  47. }
  48. export function convertUserOptions(options) {
  49. let segment = undefined;
  50. if (options?.segment) {
  51. if (options?.segment === true) {
  52. segment = "Intl.Segmenter";
  53. }
  54. else {
  55. segment = options.segment;
  56. }
  57. }
  58. const opt = {
  59. ...DEFAULT_OPTIONS,
  60. style: convertPinyinStyle(options?.style),
  61. mode: convertPinyinMode(options?.mode),
  62. segment,
  63. heteronym: options?.heteronym || false,
  64. group: options?.group || false,
  65. };
  66. return opt;
  67. }
  68. export function combo2array(a1, a2) {
  69. const result = [];
  70. if (!a1.length) {
  71. return a2;
  72. }
  73. if (!a2.length) {
  74. return a1;
  75. }
  76. for (let i = 0, l = a1.length; i < l; i++) {
  77. for (let j = 0, m = a2.length; j < m; j++) {
  78. result.push(a1[i] + a2[j]);
  79. }
  80. }
  81. return result;
  82. }
  83. export function combo(arr) {
  84. if (arr.length === 0) {
  85. return [];
  86. }
  87. if (arr.length === 1) {
  88. return arr[0];
  89. }
  90. let result = combo2array(arr[0], arr[1]);
  91. for (let i = 2, l = arr.length; i < l; i++) {
  92. result = combo2array(result, arr[i]);
  93. }
  94. return result;
  95. }
  96. export function compact2array(a1, a2) {
  97. if (!Array.isArray(a1) || !Array.isArray(a2)) {
  98. throw new Error("compact2array expect two array as parameters");
  99. }
  100. if (!a1.length) {
  101. a1 = [""];
  102. }
  103. if (!a2.length) {
  104. a2 = [""];
  105. }
  106. const result = [];
  107. for (let i = 0, l = a1.length; i < l; i++) {
  108. for (let j = 0, m = a2.length; j < m; j++) {
  109. if (Array.isArray(a1[i])) {
  110. result.push([...a1[i], a2[j]]);
  111. }
  112. else {
  113. result.push([a1[i], a2[j]]);
  114. }
  115. }
  116. }
  117. return result;
  118. }
  119. export function compact(arr) {
  120. if (arr.length === 0) {
  121. return [];
  122. }
  123. if (arr.length === 1) {
  124. return [arr[0]];
  125. }
  126. let result = compact2array(arr[0], arr[1]);
  127. for (let i = 2, l = arr.length; i < l; ++i) {
  128. result = compact2array(result, arr[i]);
  129. }
  130. return result;
  131. }
  132. //# sourceMappingURL=util.js.map