@jetbrains/ring-ui 5.0.53 → 5.0.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,11 @@
1
1
  /** https://github.com/bevacqua/fuzzysearch + highlighting with Markdown */
2
+ declare type Match = {
3
+ from: number;
4
+ to: number;
5
+ };
2
6
  export default function fuzzyHighlight(needle: string, haystack: string, caseSensitive?: boolean): {
3
7
  matched: boolean;
8
+ matches: Match[];
4
9
  highlight: string;
5
10
  };
11
+ export {};
@@ -1,9 +1,20 @@
1
1
  /** https://github.com/bevacqua/fuzzysearch + highlighting with Markdown */
2
- const marker = '**';
3
2
  export default function fuzzyHighlight(needle, haystack, caseSensitive = false) {
4
3
  const ndl = caseSensitive ? needle : needle.toLowerCase();
5
4
  const hstck = caseSensitive ? haystack : haystack.toLowerCase();
6
- const result = (matched, highlight = haystack) => ({ matched, highlight });
5
+ const result = (matched, matches = []) => {
6
+ let highlight = haystack;
7
+ if (matches.length > 0) {
8
+ highlight = '';
9
+ let prevMatch = { to: 0 };
10
+ for (const match of matches) {
11
+ highlight += `${haystack.slice(prevMatch.to, match.from)}**${haystack.slice(match.from, match.to)}**`;
12
+ prevMatch = match;
13
+ }
14
+ highlight += haystack.slice(prevMatch.to);
15
+ }
16
+ return ({ matched, matches, highlight });
17
+ };
7
18
  const hlen = hstck.length;
8
19
  const nlen = ndl.length;
9
20
  if (nlen > hlen) {
@@ -11,11 +22,12 @@ export default function fuzzyHighlight(needle, haystack, caseSensitive = false)
11
22
  }
12
23
  if (nlen === hlen) {
13
24
  const matched = ndl === hstck;
14
- return result(matched, matched ? `${marker}${haystack}${marker}` : haystack);
25
+ return result(matched, matched ? [{ from: 0, to: haystack.length }] : []);
15
26
  }
16
- let highlight = '';
17
27
  let on = false;
18
28
  let j = 0;
29
+ const matches = [];
30
+ let from = 0;
19
31
  /* eslint-disable no-labels */
20
32
  outer: for (let i = 0; i < nlen; i++) {
21
33
  const nch = ndl[i];
@@ -24,10 +36,13 @@ export default function fuzzyHighlight(needle, haystack, caseSensitive = false)
24
36
  const match = hch === nch;
25
37
  // Don't turn highlight on for space characters
26
38
  const nextOn = match && /\S/.test(hch);
27
- if (nextOn !== on) {
28
- highlight += marker;
39
+ if (nextOn && !on) {
40
+ from = j;
41
+ }
42
+ else if (!nextOn && on) {
43
+ matches.push({ from, to: j });
29
44
  }
30
- highlight += haystack[j++];
45
+ j++;
31
46
  on = nextOn;
32
47
  if (match) {
33
48
  continue outer;
@@ -37,8 +52,7 @@ export default function fuzzyHighlight(needle, haystack, caseSensitive = false)
37
52
  }
38
53
  /* eslint-enable */
39
54
  if (on) {
40
- highlight += marker;
55
+ matches.push({ from, to: j });
41
56
  }
42
- highlight += haystack.slice(j);
43
- return result(true, highlight);
57
+ return result(true, matches);
44
58
  }
@@ -1,5 +1,11 @@
1
1
  /** https://github.com/bevacqua/fuzzysearch + highlighting with Markdown */
2
+ declare type Match = {
3
+ from: number;
4
+ to: number;
5
+ };
2
6
  export default function fuzzyHighlight(needle: string, haystack: string, caseSensitive?: boolean): {
3
7
  matched: boolean;
8
+ matches: Match[];
4
9
  highlight: string;
5
10
  };
11
+ export {};
@@ -1,13 +1,25 @@
1
1
  /** https://github.com/bevacqua/fuzzysearch + highlighting with Markdown */
2
- const marker = '**';
3
2
  function fuzzyHighlight(needle, haystack) {
4
3
  let caseSensitive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
5
4
  const ndl = caseSensitive ? needle : needle.toLowerCase();
6
5
  const hstck = caseSensitive ? haystack : haystack.toLowerCase();
7
6
  const result = function (matched) {
8
- let highlight = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : haystack;
7
+ let matches = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
8
+ let highlight = haystack;
9
+ if (matches.length > 0) {
10
+ highlight = '';
11
+ let prevMatch = {
12
+ to: 0
13
+ };
14
+ for (const match of matches) {
15
+ highlight += `${haystack.slice(prevMatch.to, match.from)}**${haystack.slice(match.from, match.to)}**`;
16
+ prevMatch = match;
17
+ }
18
+ highlight += haystack.slice(prevMatch.to);
19
+ }
9
20
  return {
10
21
  matched,
22
+ matches,
11
23
  highlight
12
24
  };
13
25
  };
@@ -18,11 +30,15 @@ function fuzzyHighlight(needle, haystack) {
18
30
  }
19
31
  if (nlen === hlen) {
20
32
  const matched = ndl === hstck;
21
- return result(matched, matched ? `${marker}${haystack}${marker}` : haystack);
33
+ return result(matched, matched ? [{
34
+ from: 0,
35
+ to: haystack.length
36
+ }] : []);
22
37
  }
23
- let highlight = '';
24
38
  let on = false;
25
39
  let j = 0;
40
+ const matches = [];
41
+ let from = 0;
26
42
  /* eslint-disable no-labels */
27
43
  outer: for (let i = 0; i < nlen; i++) {
28
44
  const nch = ndl[i];
@@ -31,10 +47,15 @@ function fuzzyHighlight(needle, haystack) {
31
47
  const match = hch === nch;
32
48
  // Don't turn highlight on for space characters
33
49
  const nextOn = match && /\S/.test(hch);
34
- if (nextOn !== on) {
35
- highlight += marker;
50
+ if (nextOn && !on) {
51
+ from = j;
52
+ } else if (!nextOn && on) {
53
+ matches.push({
54
+ from,
55
+ to: j
56
+ });
36
57
  }
37
- highlight += haystack[j++];
58
+ j++;
38
59
  on = nextOn;
39
60
  if (match) {
40
61
  continue outer;
@@ -44,10 +65,12 @@ function fuzzyHighlight(needle, haystack) {
44
65
  }
45
66
  /* eslint-enable */
46
67
  if (on) {
47
- highlight += marker;
68
+ matches.push({
69
+ from,
70
+ to: j
71
+ });
48
72
  }
49
- highlight += haystack.slice(j);
50
- return result(true, highlight);
73
+ return result(true, matches);
51
74
  }
52
75
 
53
76
  export { fuzzyHighlight as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetbrains/ring-ui",
3
- "version": "5.0.53",
3
+ "version": "5.0.54",
4
4
  "description": "JetBrains UI library",
5
5
  "author": "JetBrains",
6
6
  "license": "Apache-2.0",
@@ -259,5 +259,5 @@
259
259
  "node": ">=14.0",
260
260
  "npm": ">=6.0.0"
261
261
  },
262
- "gitHead": "21c1f2143dec4335b5094f69720307af583d39cf"
262
+ "gitHead": "738bf4ba27a6a7ac082c070a845f7b992f87c350"
263
263
  }