pinyin.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { pinyin } from "pinyin-pro";
  2. import { isArray } from "./is/index";
  3. import { get } from "lodash-es";
  4. /**
  5. * @description 判断字符串str是否包含目标字符串tgStr的拼音形式。
  6. * @param str 待检查的字符串
  7. * @param tgStr 目标字符串,用于拼音匹配
  8. * @returns 如果str的拼音形式包含tgStr,则返回true;否则返回false。
  9. */
  10. const getMatch = (str: string, tgStr: string): boolean => {
  11. const result = pinyin(str, { toneType: "none", type: "array" });
  12. // 将拼音数组连接成字符串,用于完整拼音匹配
  13. let res = result.join("");
  14. if (res.includes(tgStr)) return true;
  15. // 将拼音数组的每个元素的首字母提取出来,然后连接成字符串,用于首字母拼音匹配
  16. res = result
  17. .map(p => p[0])
  18. .join("")
  19. .toLowerCase();
  20. return res.includes(tgStr);
  21. };
  22. /**
  23. * @description 拼音模糊查询数值
  24. * @param inputValue {string} 识别文本内容
  25. * @param totalList {array} 数据源,单层数组
  26. * @param props {array/string} 识别文本字段名
  27. * @returns array
  28. */
  29. export function pinyinFilter(inputValue: string, totalList: any, props: string | string[]) {
  30. // 完整拼音
  31. const pyInput = pinyin(inputValue, { toneType: "none", type: "array" }).join("");
  32. // Helper函数来检查节点是否匹配
  33. function isNodeMatch(node: any, prop: string): boolean {
  34. const value = String(get(node, prop, ""));
  35. return value.includes(inputValue) || getMatch(value, pyInput);
  36. }
  37. return totalList.filter((e: any) => {
  38. let ifMatch = false;
  39. if (isArray(props)) {
  40. for (let i = 0; i < props.length; i++) {
  41. if (isNodeMatch(e, props[i])) {
  42. ifMatch = true;
  43. break;
  44. }
  45. }
  46. } else {
  47. ifMatch = isNodeMatch(e, props);
  48. }
  49. return ifMatch;
  50. });
  51. }
  52. /**
  53. * @description 拼音模糊查询数值
  54. * @param inputValue {string} 识别文本内容
  55. * @param totalList {array} 数据源,支持多层数组
  56. * @param props {array/string} 识别文本字段名,支持“a.b"
  57. * @param childAttr {string} 子层级字段名, 默认为children
  58. * @returns array
  59. */
  60. export function pinyinFilterTree(inputValue: string, totalList: any, props: string | string[], childAttr: string = "children") {
  61. // 完整拼音
  62. const pyInput = pinyin(inputValue, { toneType: "none", type: "array" }).join("");
  63. // Helper函数来检查节点是否匹配
  64. function isNodeMatch(node: any, prop: string): boolean {
  65. const value = get(node, prop, "") as string;
  66. return value.includes(inputValue) || getMatch(value, pyInput);
  67. }
  68. return totalList
  69. .map((e: any) => {
  70. let ifMatch = false;
  71. if (isArray(props)) {
  72. for (let i = 0; i < props.length; i++) {
  73. if (isNodeMatch(e, props[i])) {
  74. ifMatch = true;
  75. break;
  76. }
  77. }
  78. } else {
  79. ifMatch = isNodeMatch(e, props);
  80. }
  81. if (ifMatch) return e;
  82. // 如果当前节点不匹配,但存在子节点,则递归处理子节点
  83. if (e.hasOwnProperty(childAttr) && e[childAttr].length > 0) {
  84. const filteredChildren = pinyinFilterTree(inputValue, e[childAttr], props, childAttr);
  85. if (filteredChildren.length > 0) {
  86. let _data = { ...e };
  87. _data[childAttr] = filteredChildren;
  88. return _data;
  89. }
  90. }
  91. return null;
  92. })
  93. .filter((node: any) => node !== null); // 过滤掉 null 节点
  94. }