utils.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { __assign, __awaiter, __generator, __spreadArray } from "tslib";
  2. import { ZDATAS } from './zdatas';
  3. /**
  4. * json转字符串
  5. * @param {string} data 需要转json的字符串
  6. * @return {object} json 字符串
  7. */
  8. export function safeJSONparse(data) {
  9. var result;
  10. try {
  11. result = JSON.parse(data);
  12. }
  13. catch (_a) {
  14. result = {};
  15. }
  16. return result || {};
  17. }
  18. /**
  19. * 判断数组是否为空
  20. */
  21. export function isWordsDataEmpty(arr) {
  22. var _a;
  23. if (!arr)
  24. return true;
  25. if (!Array.isArray(arr))
  26. return true;
  27. if (arr.length === 0)
  28. return true;
  29. // 数据合法性校验
  30. if (!((_a = arr === null || arr === void 0 ? void 0 : arr[0]) === null || _a === void 0 ? void 0 : _a.charId))
  31. return true;
  32. return false;
  33. }
  34. /**
  35. * 清除字符串里的数字
  36. */
  37. export function clearNumberInStr(str) {
  38. return str.replace(/[0-9]/gi, '');
  39. }
  40. /**
  41. * 格式化字库数据
  42. * @param datas ZDatas 数据
  43. * @return {IWordsData} 字库
  44. */
  45. export function formatZDatas(datas) {
  46. if (datas === void 0) { datas = []; }
  47. return datas.map(function (item) {
  48. return __assign(__assign({}, item), { pinYinChars: item.pinYinChars.map(function (i) { return i.char; }), splitChars: item.splitChars.map(function (i) { return i.char; }) });
  49. });
  50. }
  51. /**
  52. * 候选字推荐序函数
  53. * 考虑两个维度,一个是输入值和生僻字的匹配程度,比如你输入YA 雅是完全匹配,炎是模糊匹配,排列的时候肯定”雅“在前面,
  54. * 如果除了”雅“还有一个”亚“,两个都是完全匹配,这个时候就看哪个字占比高,哪个就排在前面
  55. * @param {IWordsData} wordsData 字库数据
  56. * @param {string} inputValue 当前输入的值
  57. * @param {string} filterKey 过滤依据的key值
  58. * @return {IWordsData} 返回符合要求并且排序好的候选项列表
  59. */
  60. export function matchWordsRecommend(wordsData, inputValue, filterKey) {
  61. if (wordsData === void 0) { wordsData = []; }
  62. if (inputValue === void 0) { inputValue = ''; }
  63. if (filterKey === void 0) { filterKey = 'all'; }
  64. return wordsSorter(wordsFilter(wordsData, inputValue, filterKey), inputValue, filterKey);
  65. }
  66. /**
  67. * 字库过滤,只挑选符合要求的候选字
  68. * @param {IWordsData} wordsData 字库数据
  69. * @param {string} inputValue 当前输入的值
  70. * @param {string} filterKey 过滤依据的key值
  71. * @return {IWordsData} 返回符合要求并且排序好的候选项列表
  72. */
  73. export function wordsFilter(wordsData, inputValue, filterKey) {
  74. if (wordsData === void 0) { wordsData = []; }
  75. if (inputValue === void 0) { inputValue = ''; }
  76. if (filterKey === void 0) { filterKey = 'all'; }
  77. // 字库数据为空降级为使用本地数据
  78. if (!wordsData || isWordsDataEmpty(wordsData))
  79. wordsData = formatZDatas(ZDATAS.datas);
  80. if (!inputValue)
  81. return [];
  82. switch (filterKey) {
  83. case 'all':
  84. /* eslint-disable-next-line no-case-declarations */
  85. var matchPinyinArr = filterByPinyin(wordsData, inputValue);
  86. /* eslint-disable-next-line no-case-declarations */
  87. var matchSplitArr = filterBySplitWord(wordsData, inputValue);
  88. return mergeMatchWordsArr(matchPinyinArr, matchSplitArr);
  89. case 'pinyin':
  90. return filterByPinyin(wordsData, inputValue);
  91. case 'split':
  92. return filterBySplitWord(wordsData, inputValue);
  93. default:
  94. return [];
  95. break;
  96. }
  97. }
  98. /**
  99. * 根据拼音过滤候选项
  100. * @param {IWordsData} wordsData 字库数据
  101. * @param {string} inputValue 当前输入的值
  102. * @return {IWordsData} 返回符合要求并候选项列表
  103. */
  104. function filterByPinyin(wordsData, inputValue) {
  105. if (wordsData === void 0) { wordsData = []; }
  106. if (inputValue === void 0) { inputValue = ''; }
  107. var keyTranslate = inputValue.toUpperCase();
  108. return wordsData.filter(function (item) {
  109. var pinYinChars = (item === null || item === void 0 ? void 0 : item.pinYinChars) || [];
  110. if (pinYinChars.length === 0)
  111. return false;
  112. return (pinYinChars.filter(function (pinyinItem) {
  113. return pinyinItem.indexOf(keyTranslate) > -1;
  114. }).length > 0);
  115. });
  116. }
  117. /**
  118. * 根据拆字过滤候选项
  119. * @param {IWordsData} wordsData 字库数据
  120. * @param {string} inputValue 当前输入的值
  121. * @return {IWordsData} 返回符合要求并候选项列表
  122. */
  123. function filterBySplitWord(wordsData, inputValue) {
  124. if (wordsData === void 0) { wordsData = []; }
  125. if (inputValue === void 0) { inputValue = ''; }
  126. return wordsData.filter(function (item) {
  127. var splitChars = item.splitChars || [];
  128. if (splitChars.length === 0) {
  129. return false;
  130. }
  131. return (splitChars.filter(function (splitItem) {
  132. return splitItem.indexOf(inputValue) > -1;
  133. }).length > 0);
  134. });
  135. }
  136. /**
  137. * 合并多个候选项数组
  138. * @param {IWordsData} pinyinMatchArr 拼音匹配的候选项
  139. * @param {IWordsData} splitMatchArr 拼音匹配的候选项
  140. * @return {IWordsData} 返回合并后的候选项列表
  141. */
  142. function mergeMatchWordsArr(pinyinMatchArr, splitMatchArr) {
  143. var unDuplicate = __spreadArray(__spreadArray([], pinyinMatchArr, true), splitMatchArr, true);
  144. if (unDuplicate.length === 0)
  145. return unDuplicate;
  146. var results = [];
  147. unDuplicate.forEach(function (item) {
  148. var findDuplicateWords = results.filter(function (item2) {
  149. return item.unicodeCodePoint === item2.unicodeCodePoint;
  150. });
  151. if (findDuplicateWords.length === 0)
  152. results.push(item);
  153. });
  154. return results;
  155. }
  156. /**
  157. * 候选项排序,用户选择可能性高的候选项排在前面
  158. * @param {IWordsData} wordsData 字库数据
  159. * @param {string} inputValue 当前输入的值
  160. * @param {string} filterKey 过滤依据的key值
  161. * @return {IWordsData} 返回符合要求并且排序好的候选项列表
  162. */
  163. export function wordsSorter(wordsData, inputValue, filterKey) {
  164. if (filterKey === void 0) { filterKey = 'all'; }
  165. switch (filterKey) {
  166. case 'all':
  167. // 当输入值以字母开头使用拼音排序
  168. if (/^[a-zA-Z0-9]+$/.test(inputValue)) {
  169. return sortByPinyin(wordsData, inputValue);
  170. }
  171. return sortBySplitWord(wordsData, inputValue);
  172. case 'pinyin':
  173. return sortByPinyin(wordsData, inputValue);
  174. case 'split':
  175. return sortBySplitWord(wordsData, inputValue);
  176. default:
  177. return [];
  178. break;
  179. }
  180. }
  181. /**
  182. * 根据拼音给候选项排序
  183. * @param {IWordsData} wordsData 字库数据
  184. * @param {string} inputValue 当前输入的值
  185. * @return {IWordsData} 返回符合要求并候选项列表
  186. */
  187. function sortByPinyin(wordsData, inputValue) {
  188. if (wordsData === void 0) { wordsData = []; }
  189. if (inputValue === void 0) { inputValue = ''; }
  190. var arr = wordsData.slice();
  191. // 清除输入值中的数字
  192. var keyTranslate = clearNumberInStr(inputValue.toUpperCase());
  193. arr.forEach(function (item) {
  194. var sort = 0;
  195. var pinYinChars = (item.pinYinChars || []).map(function (pinyin) {
  196. return clearNumberInStr(pinyin.toUpperCase());
  197. });
  198. // 拼音完全匹配 + 10000
  199. if (pinYinChars.indexOf(keyTranslate) > -1)
  200. sort += 10000;
  201. // 拼音模糊匹配 + 5000
  202. if (pinYinChars.filter(function (splitKey) { return splitKey.indexOf(keyTranslate) === 0; })
  203. .length > 0) {
  204. sort += 5000;
  205. }
  206. // 加上当前字的权重
  207. sort += item.weight || 0;
  208. /* eslint-disable no-param-reassign */
  209. item.sort = sort;
  210. });
  211. // 根据最终排序值排序
  212. arr.sort(function (item1, item2) { return (item2.sort || 0) - (item1.sort || 0); });
  213. return arr;
  214. }
  215. /**
  216. * 根据拆字给候选项排序
  217. * @param {IWordsData} wordsData 字库数据
  218. * @param {string} inputValue 当前输入的值
  219. * @return {IWordsData} 返回符合要求并候选项列表
  220. */
  221. function sortBySplitWord(wordsData, inputValue) {
  222. if (wordsData === void 0) { wordsData = []; }
  223. if (inputValue === void 0) { inputValue = ''; }
  224. var arr = wordsData.slice();
  225. arr.forEach(function (item) {
  226. var sort = 0;
  227. var p = item.splitChars || [];
  228. // 拆字完全匹配 + 10000
  229. if (p.indexOf(inputValue) > -1)
  230. sort += 10000;
  231. // 拆字模糊匹配 + 5000
  232. if (p.filter(function (splitKey) { return splitKey.indexOf(inputValue) === 0; }).length > 0) {
  233. sort += 5000;
  234. }
  235. // 加上当前字的权重
  236. sort += item.weight || 0;
  237. /* eslint-disable no-param-reassign */
  238. item.sort = sort;
  239. });
  240. // 根据最终排序值排序
  241. arr.sort(function (item1, item2) { return (item2.sort || 0) - (item1.sort || 0); });
  242. return arr;
  243. }
  244. /**
  245. * 加载远程字体
  246. */
  247. export function loadFontFace() {
  248. return __awaiter(this, void 0, void 0, function () {
  249. var fontName;
  250. return __generator(this, function (_a) {
  251. fontName = "url(\"".concat(ZDATAS.fontUrl, "\")");
  252. return [2 /*return*/, new Promise(function (resolve, reject) {
  253. my.loadFontFace({
  254. family: 'rare-words-font',
  255. source: fontName,
  256. global: true,
  257. success: function () {
  258. resolve(true);
  259. },
  260. fail: function (err) {
  261. reject(err);
  262. },
  263. });
  264. })];
  265. });
  266. });
  267. }