@syware/jlib 0.18.0 → 0.18.1

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.
@@ -4,11 +4,13 @@ export declare const foldCharacter: (character: string) => string;
4
4
  /** A text as matching sees it: "Crème Brûlée" gives "creme brulee". */
5
5
  export declare const foldText: (text: string) => string;
6
6
  /**
7
- * Where a search first occurs in a text, ignoring case and accents, in the text's own indexes; null when it does
8
- * not, or when the search is empty.
7
+ * Where each word of a search occurs in a text, ignoring case and accents, in the text's own indexes: at the start of
8
+ * one of the text's words when it is there, else where it first occurs. In order, those that overlap or touch made
9
+ * one; empty when no word occurs, or the search has none. A page that finds options by words (`filter="none"`) gets
10
+ * each of them marked, wherever they are in the label.
9
11
  */
10
- export declare function findMatch(text: string, query: string): TextMatch | null;
11
- /** A text cut around its match, so the matched piece can be drawn highlighted. */
12
+ export declare function findMatches(text: string, query: string): TextMatch[];
13
+ /** A text cut around the words of a search it holds, so that they can be drawn highlighted. */
12
14
  export declare function matchParts(text: string, query: string): TextMatchPart[];
13
15
  /**
14
16
  * The options whose label contains the search, ignoring case and accents: those that start with it first, then
@@ -7,6 +7,7 @@ var RANK = {
7
7
  };
8
8
  var DIACRITICS = /\p{Diacritic}/gu;
9
9
  var WORD_CHARACTER = /[\p{L}\p{N}]/u;
10
+ var SPACES = /\s+/u;
10
11
  /** One character as matching sees it: lower case, without its accents ("É" gives "e"). */
11
12
  var foldCharacter = (character) => character.normalize("NFD").replace(DIACRITICS, "").toLowerCase();
12
13
  /** A text as matching sees it: "Crème Brûlée" gives "creme brulee". */
@@ -30,48 +31,77 @@ function foldPieces(text) {
30
31
  }
31
32
  return pieces;
32
33
  }
33
- /**
34
- * Where a search first occurs in a text, ignoring case and accents, in the text's own indexes; null when it does
35
- * not, or when the search is empty.
36
- */
37
- function findMatch(text, query) {
38
- const needle = foldText(query.trim());
39
- if (needle === "") return null;
40
- const at = foldText(text).indexOf(needle);
41
- if (at < 0) return null;
42
- const end = at + needle.length;
43
- const pieces = foldPieces(text);
44
- const first = pieces.find((piece) => piece.foldedEnd > at);
45
- const last = pieces.findLast((piece) => piece.foldedStart < end || piece.foldedEnd === end);
34
+ /** The search's words as matching sees them: what was typed, folded, cut at its spaces. */
35
+ var searchWords = (query) => foldText(query).split(SPACES).filter((word) => word !== "");
36
+ /** A range of a text's folded form in the text's own indexes; an accent written as a mark goes with its letter. */
37
+ function originalRange(pieces, foldedStart, foldedEnd) {
38
+ const first = pieces.find((piece) => piece.foldedEnd > foldedStart);
39
+ const last = pieces.findLast((piece) => piece.foldedStart < foldedEnd || piece.foldedEnd === foldedEnd);
46
40
  if (first === void 0 || last === void 0) return null;
47
41
  return {
48
42
  end: last.end,
49
43
  start: first.start
50
44
  };
51
45
  }
52
- /** A text cut around its match, so the matched piece can be drawn highlighted. */
46
+ /** Ranges in order, those that overlap or touch made one. */
47
+ function mergedRanges(ranges) {
48
+ const merged = [];
49
+ for (const range of [...ranges].sort((a, b) => a.start - b.start)) {
50
+ const last = merged.at(-1);
51
+ if (last !== void 0 && range.start <= last.end) {
52
+ merged[merged.length - 1] = {
53
+ end: Math.max(last.end, range.end),
54
+ start: last.start
55
+ };
56
+ continue;
57
+ }
58
+ merged.push(range);
59
+ }
60
+ return merged;
61
+ }
62
+ /**
63
+ * Where each word of a search occurs in a text, ignoring case and accents, in the text's own indexes: at the start of
64
+ * one of the text's words when it is there, else where it first occurs. In order, those that overlap or touch made
65
+ * one; empty when no word occurs, or the search has none. A page that finds options by words (`filter="none"`) gets
66
+ * each of them marked, wherever they are in the label.
67
+ */
68
+ function findMatches(text, query) {
69
+ const folded = foldText(text);
70
+ const pieces = foldPieces(text);
71
+ return mergedRanges(searchWords(query).flatMap((word) => {
72
+ const at = wordIndex(folded, word);
73
+ const range = at < 0 ? null : originalRange(pieces, at, at + word.length);
74
+ return range === null ? [] : [range];
75
+ }));
76
+ }
77
+ /** A text cut around the words of a search it holds, so that they can be drawn highlighted. */
53
78
  function matchParts(text, query) {
54
- const match = findMatch(text, query);
55
- if (match === null) return text === "" ? [] : [{
79
+ const matches = findMatches(text, query);
80
+ const parts = matches.flatMap((match, index) => [{
56
81
  matched: false,
57
- text
58
- }];
59
- return [
60
- {
61
- matched: false,
62
- text: text.slice(0, match.start)
63
- },
64
- {
65
- matched: true,
66
- text: text.slice(match.start, match.end)
67
- },
68
- {
69
- matched: false,
70
- text: text.slice(match.end)
71
- }
72
- ].filter((part) => part.text !== "");
82
+ text: text.slice(matches[index - 1]?.end ?? 0, match.start)
83
+ }, {
84
+ matched: true,
85
+ text: text.slice(match.start, match.end)
86
+ }]);
87
+ const rest = {
88
+ matched: false,
89
+ text: text.slice(matches.at(-1)?.end ?? 0)
90
+ };
91
+ return [...parts, rest].filter((part) => part.text !== "");
73
92
  }
74
93
  var startsAWord = (folded, index) => index === 0 || !WORD_CHARACTER.test(folded.charAt(index - 1));
94
+ /** Every place a folded word occurs in a folded text, in order. */
95
+ function occurrences(folded, word) {
96
+ const found = [];
97
+ for (let at = folded.indexOf(word); at >= 0; at = folded.indexOf(word, at + 1)) found.push(at);
98
+ return found;
99
+ }
100
+ /** Where a folded word occurs in a folded text: at the start of one of its words if it can, else first; -1 if not. */
101
+ function wordIndex(folded, word) {
102
+ const found = occurrences(folded, word);
103
+ return found.find((index) => startsAWord(folded, index)) ?? found[0] ?? -1;
104
+ }
75
105
  /** How well a label matches a folded search, or null when it does not contain it. */
76
106
  function rankOf(label, needle) {
77
107
  const folded = foldText(label);
@@ -105,4 +135,4 @@ function nextEnabledIndex(options, current, direction) {
105
135
  }
106
136
  /** The first (1) or last (-1) option that can be chosen; -1 when none can. */
107
137
  var edgeEnabledIndex = (options, from) => nextEnabledIndex(options, -1, from);
108
- export { edgeEnabledIndex, filterOptions, findMatch, foldCharacter, foldText, matchParts, nextEnabledIndex };
138
+ export { edgeEnabledIndex, filterOptions, findMatches, foldCharacter, foldText, matchParts, nextEnabledIndex };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syware/jlib",
3
- "version": "0.18.0",
3
+ "version": "0.18.1",
4
4
  "description": "A tree-shakeable component library on Jadis and native Web Components, organised in atomic design",
5
5
  "keywords": [
6
6
  "web-components",