File size: 2,580 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { pinyin } from "pinyin-pro";

const HAN_IDEOGRAPH_RE = /\p{Unified_Ideograph}/u;

export const normalizeStr = (s) => (s ?? "").toString().toLowerCase().trim();

const normalizeLooseFromNormalized = (normalized) =>
  normalized.replace(/[\s_-]+/g, "").replace(/[()()【】\[\]{}·•]+/g, "");

export const normalizeLoose = (s) =>
  normalizeLooseFromNormalized(normalizeStr(s));

const memoizeStringFn = (fn) => {
  const cache = new Map();

  return (raw) => {
    const key = (raw ?? "").toString();
    if (cache.has(key)) {
      return cache.get(key);
    }

    const value = fn(key);
    cache.set(key, value);
    return value;
  };
};

const getNormalizedText = memoizeStringFn(normalizeStr);

const getLooseText = memoizeStringFn((text) =>
  normalizeLooseFromNormalized(getNormalizedText(text)),
);

export const toPinyinText = memoizeStringFn((text) =>
  pinyin(text, { toneType: "none" })
    .toLowerCase()
    .replace(/\s+/g, ""),
);

export const toInitials = memoizeStringFn((text) =>
  pinyin(text, { pattern: "first", toneType: "none" })
    .toLowerCase()
    .replace(/\s+/g, ""),
);

export const buildSearchQuery = (raw) => {
  const norm = getNormalizedText(raw);
  if (!norm) return null;
  return {
    norm,
    loose: getLooseText(raw),
  };
};

export const matchesText = (value, query) => {
  if (value == null || !query?.norm) return false;
  const text = String(value);

  const normalizedValue = getNormalizedText(text);
  const looseValue = query.loose ? getLooseText(text) : null;

  if (normalizedValue.includes(query.norm)) return true;
  if (query.loose && looseValue?.includes(query.loose)) return true;

  if (!HAN_IDEOGRAPH_RE.test(text)) return false;

  const pinyinValue = toPinyinText(text);
  if (pinyinValue.includes(query.norm)) return true;

  const initialsValue = toInitials(text);
  if (initialsValue.includes(query.norm)) return true;

  return false;
};

export const getPluginSearchFields = (plugin) => {
  const supportPlatforms = Array.isArray(plugin?.support_platforms)
    ? plugin.support_platforms.join(" ")
    : "";
  const tags = Array.isArray(plugin?.tags) ? plugin.tags.join(" ") : "";

  return [
    plugin?.name,
    plugin?.trimmedName,
    plugin?.display_name,
    plugin?.desc,
    plugin?.author,
    plugin?.repo,
    plugin?.version,
    plugin?.astrbot_version,
    supportPlatforms,
    tags,
  ];
};

export const matchesPluginSearch = (plugin, query) => {
  if (!query) return true;

  return getPluginSearchFields(plugin).some((candidate) =>
    matchesText(candidate, query),
  );
};