highlight-words 1.2.2 → 2.1.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.
- package/Readme.md +6 -3
- package/dist/highlight-words.d.mts +29 -24
- package/dist/highlight-words.d.ts +29 -24
- package/dist/highlight-words.js +99 -127
- package/dist/highlight-words.min.js +2 -2
- package/dist/highlight-words.min.js.map +1 -1
- package/dist/highlight-words.min.mjs +60 -2
- package/dist/highlight-words.min.mjs.map +1 -1
- package/dist/highlight-words.mjs +95 -123
- package/package.json +52 -60
package/Readme.md
CHANGED
|
@@ -7,7 +7,7 @@ Give it a piece of text and a search query, and it splits it into chunks separat
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
9
|
```
|
|
10
|
-
|
|
10
|
+
pnpm add highlight-words
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
```
|
|
@@ -51,6 +51,7 @@ console.log(chunks);
|
|
|
51
51
|
[Play with this example on Code Sandbox.][sandbox-vanilla]
|
|
52
52
|
|
|
53
53
|
Alternatively, you can also use a regular expression as part of the query string to search for.
|
|
54
|
+
|
|
54
55
|
```js
|
|
55
56
|
const chunks = highlightWords({
|
|
56
57
|
text: 'The quick brown fox jumped over the lazy dog',
|
|
@@ -102,12 +103,14 @@ console.log(chunks);
|
|
|
102
103
|
> If using a regular expresssion, you can choose to either enclose the pattern in a string or not. In both cases, you need to format the regular expression properly, i.e. enclose the pattern between slashes, **and** you need to create capture groups, so that the match is remembered for later use.
|
|
103
104
|
>
|
|
104
105
|
> **Valid regular expression usage**
|
|
106
|
+
>
|
|
105
107
|
> ```
|
|
106
108
|
> query: '/(quick|brown|fox)/'
|
|
107
109
|
> query: /(quick|brown|fox)/
|
|
108
110
|
> ```
|
|
109
111
|
>
|
|
110
|
-
|
|
112
|
+
> **Invalid regular expression usage**
|
|
113
|
+
>
|
|
111
114
|
> ```
|
|
112
115
|
> query: '(quick|brown|fox)'
|
|
113
116
|
> query: '/quick|brown|fox/'
|
|
@@ -269,7 +272,7 @@ MIT License - fork, modify and use however you want.
|
|
|
269
272
|
[npm-version-badge]: https://img.shields.io/npm/v/highlight-words.svg?style=flat-square
|
|
270
273
|
[highlight-words-npm]: https://www.npmjs.com/package/highlight-words
|
|
271
274
|
[npm-downloads-badge]: https://img.shields.io/npm/dt/highlight-words.svg?style=flat-square
|
|
272
|
-
[build-status-badge]: https://
|
|
275
|
+
[build-status-badge]: https://github.com/tricinel/highlight-words/actions/workflows/ci.yml/badge.svg
|
|
273
276
|
[sandbox-react]: https://codesandbox.io/s/highlight-words-react-1h7qw
|
|
274
277
|
[sandbox-angular]: https://codesandbox.io/s/highlight-words-angular-xpp46
|
|
275
278
|
[sandbox-vue]: https://codesandbox.io/s/highlight-words-vue-zopni
|
|
@@ -1,27 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
interface Chunk {
|
|
3
|
-
key: string;
|
|
4
|
-
text: string;
|
|
5
|
-
match: boolean;
|
|
6
|
-
}
|
|
7
|
-
interface Options {
|
|
8
|
-
text: string;
|
|
9
|
-
query: string;
|
|
10
|
-
clipBy?: number;
|
|
11
|
-
matchExactly?: boolean;
|
|
12
|
-
}
|
|
13
|
-
interface Clip {
|
|
14
|
-
curr: Chunk;
|
|
15
|
-
next?: Chunk;
|
|
16
|
-
prev?: Chunk;
|
|
17
|
-
clipBy?: number;
|
|
18
|
-
}
|
|
19
|
-
interface Query {
|
|
20
|
-
terms: string;
|
|
21
|
-
matchExactly?: boolean;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
24
2
|
|
|
3
|
+
export declare namespace HighlightWords {
|
|
4
|
+
interface Chunk {
|
|
5
|
+
key: string;
|
|
6
|
+
text: string;
|
|
7
|
+
match: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface Options {
|
|
10
|
+
text: string;
|
|
11
|
+
query: string;
|
|
12
|
+
clipBy?: number;
|
|
13
|
+
matchExactly?: boolean;
|
|
14
|
+
}
|
|
15
|
+
interface Clip {
|
|
16
|
+
curr: Chunk;
|
|
17
|
+
next?: Chunk;
|
|
18
|
+
prev?: Chunk;
|
|
19
|
+
clipBy?: number;
|
|
20
|
+
}
|
|
21
|
+
interface Query {
|
|
22
|
+
terms: string;
|
|
23
|
+
matchExactly?: boolean;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
25
26
|
/**
|
|
26
27
|
* Split a text into chunks denoting which are a match and which are not based on a user search term.
|
|
27
28
|
* @param text String The text to split.
|
|
@@ -31,4 +32,8 @@ declare namespace HighlightWords {
|
|
|
31
32
|
*/
|
|
32
33
|
declare const highlightWords: ({ text, query, clipBy, matchExactly }: Readonly<HighlightWords.Options>) => HighlightWords.Chunk[];
|
|
33
34
|
|
|
34
|
-
export {
|
|
35
|
+
export {
|
|
36
|
+
highlightWords as default,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export {};
|
|
@@ -1,27 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
interface Chunk {
|
|
3
|
-
key: string;
|
|
4
|
-
text: string;
|
|
5
|
-
match: boolean;
|
|
6
|
-
}
|
|
7
|
-
interface Options {
|
|
8
|
-
text: string;
|
|
9
|
-
query: string;
|
|
10
|
-
clipBy?: number;
|
|
11
|
-
matchExactly?: boolean;
|
|
12
|
-
}
|
|
13
|
-
interface Clip {
|
|
14
|
-
curr: Chunk;
|
|
15
|
-
next?: Chunk;
|
|
16
|
-
prev?: Chunk;
|
|
17
|
-
clipBy?: number;
|
|
18
|
-
}
|
|
19
|
-
interface Query {
|
|
20
|
-
terms: string;
|
|
21
|
-
matchExactly?: boolean;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
24
2
|
|
|
3
|
+
export declare namespace HighlightWords {
|
|
4
|
+
interface Chunk {
|
|
5
|
+
key: string;
|
|
6
|
+
text: string;
|
|
7
|
+
match: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface Options {
|
|
10
|
+
text: string;
|
|
11
|
+
query: string;
|
|
12
|
+
clipBy?: number;
|
|
13
|
+
matchExactly?: boolean;
|
|
14
|
+
}
|
|
15
|
+
interface Clip {
|
|
16
|
+
curr: Chunk;
|
|
17
|
+
next?: Chunk;
|
|
18
|
+
prev?: Chunk;
|
|
19
|
+
clipBy?: number;
|
|
20
|
+
}
|
|
21
|
+
interface Query {
|
|
22
|
+
terms: string;
|
|
23
|
+
matchExactly?: boolean;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
25
26
|
/**
|
|
26
27
|
* Split a text into chunks denoting which are a match and which are not based on a user search term.
|
|
27
28
|
* @param text String The text to split.
|
|
@@ -31,4 +32,8 @@ declare namespace HighlightWords {
|
|
|
31
32
|
*/
|
|
32
33
|
declare const highlightWords: ({ text, query, clipBy, matchExactly }: Readonly<HighlightWords.Options>) => HighlightWords.Chunk[];
|
|
33
34
|
|
|
34
|
-
export {
|
|
35
|
+
export {
|
|
36
|
+
highlightWords as default,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export {};
|
package/dist/highlight-words.js
CHANGED
|
@@ -1,133 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
//#region src/clip.ts
|
|
6
|
+
var hasProp = (prop) => (obj) => obj !== null && typeof obj === "object" && prop in obj;
|
|
7
|
+
var hasMatch = hasProp("match");
|
|
8
|
+
var chunkExists = (chunk) => typeof chunk !== "undefined";
|
|
9
|
+
/**
|
|
10
|
+
* This provides context around a chunk's text, based on the next and previous chunks.
|
|
11
|
+
* e.g. If we have the string "The quick brown fox jumped over the lazy dog",
|
|
12
|
+
* and the search term "fox jumped", with a padding of 2, we want to have the end result be:
|
|
13
|
+
* "... quick brown fox jumped over the ..."
|
|
14
|
+
* The search term, "fox jumped" is left as is, and the left and right chunks, instead of having
|
|
15
|
+
* the text in full, will be clipped.
|
|
16
|
+
*/
|
|
17
|
+
function clip({ curr, next, prev, clipBy = 3 }) {
|
|
18
|
+
const words = curr.text.split(" ");
|
|
19
|
+
const len = words.length;
|
|
20
|
+
if (curr.match || clipBy >= len) return curr.text;
|
|
21
|
+
const ellipsis = "...";
|
|
22
|
+
if (chunkExists(next) && chunkExists(prev) && hasMatch(prev) && hasMatch(next)) {
|
|
23
|
+
if (len > clipBy * 2) return [
|
|
24
|
+
...words.slice(0, clipBy),
|
|
25
|
+
ellipsis,
|
|
26
|
+
...words.slice(-clipBy)
|
|
27
|
+
].join(" ");
|
|
28
|
+
return curr.text;
|
|
29
|
+
}
|
|
30
|
+
if (chunkExists(next) && hasMatch(next)) return [ellipsis, ...words.slice(-clipBy)].join(" ");
|
|
31
|
+
if (chunkExists(prev) && hasMatch(prev)) return [...words.slice(0, clipBy), ellipsis].join(" ");
|
|
32
|
+
return curr.text;
|
|
9
33
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const escapeRegexp = (term) => term.replace(/[|\\{}()[\]^$+*?.-]/g, (char) => `\\${char}`);
|
|
20
|
-
const termsToRegExpString = (terms) => terms.replace(/\s{2,}/g, " ").split(" ").join("|");
|
|
21
|
-
const regexpQuery = ({
|
|
22
|
-
terms,
|
|
23
|
-
matchExactly = false
|
|
24
|
-
}) => {
|
|
25
|
-
if (typeof terms !== "string") {
|
|
26
|
-
throw new TypeError("Expected a string");
|
|
27
|
-
}
|
|
28
|
-
const escapedTerms = escapeRegexp(terms.trim());
|
|
29
|
-
return `(${matchExactly ? escapedTerms : termsToRegExpString(escapedTerms)})`;
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/regexp.ts
|
|
36
|
+
var escapeRegexp = (term) => term.replace(/[|\\{}()[\]^$+*?.-]/g, (char) => `\\${char}`);
|
|
37
|
+
var termsToRegExpString = (terms) => terms.replace(/\s{2,}/g, " ").split(" ").join("|");
|
|
38
|
+
var regexpQuery = ({ terms, matchExactly = false }) => {
|
|
39
|
+
if (typeof terms !== "string") throw new TypeError("Expected a string");
|
|
40
|
+
const escapedTerms = escapeRegexp(terms.trim());
|
|
41
|
+
return `(${matchExactly ? escapedTerms : termsToRegExpString(escapedTerms)})`;
|
|
30
42
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
throw new TypeError("Expected terms to be either a string or a RegExp!");
|
|
43
|
-
}
|
|
43
|
+
var buildRegexp = ({ terms, matchExactly = false }) => {
|
|
44
|
+
try {
|
|
45
|
+
const fromString = /^([/~@;%#'])(.*?)\1([gimsuy]*)$/.exec(terms);
|
|
46
|
+
if (fromString) return new RegExp(fromString[2], fromString[3]);
|
|
47
|
+
return new RegExp(regexpQuery({
|
|
48
|
+
terms,
|
|
49
|
+
matchExactly
|
|
50
|
+
}), "ig");
|
|
51
|
+
} catch {
|
|
52
|
+
throw new TypeError("Expected terms to be either a string or a RegExp!");
|
|
53
|
+
}
|
|
44
54
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const words = curr.text.split(" ");
|
|
56
|
-
const len = words.length;
|
|
57
|
-
if (curr.match || clipBy >= len) {
|
|
58
|
-
return curr.text;
|
|
59
|
-
}
|
|
60
|
-
const ellipsis = "...";
|
|
61
|
-
if (chunkExists(next) && chunkExists(prev) && hasMatch(prev) && hasMatch(next)) {
|
|
62
|
-
if (len > clipBy * 2) {
|
|
63
|
-
return [
|
|
64
|
-
...words.slice(0, clipBy),
|
|
65
|
-
ellipsis,
|
|
66
|
-
...words.slice(-clipBy)
|
|
67
|
-
].join(" ");
|
|
68
|
-
}
|
|
69
|
-
return curr.text;
|
|
70
|
-
}
|
|
71
|
-
if (chunkExists(next) && hasMatch(next)) {
|
|
72
|
-
return [ellipsis, ...words.slice(-clipBy)].join(" ");
|
|
73
|
-
}
|
|
74
|
-
if (chunkExists(prev) && hasMatch(prev)) {
|
|
75
|
-
return [...words.slice(0, clipBy), ellipsis].join(" ");
|
|
76
|
-
}
|
|
77
|
-
return curr.text;
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/uid.ts
|
|
57
|
+
var IDX = 36;
|
|
58
|
+
var HEX = "";
|
|
59
|
+
while (IDX--) HEX += IDX.toString(36);
|
|
60
|
+
function uid(len = 11) {
|
|
61
|
+
let str = "";
|
|
62
|
+
let num = len;
|
|
63
|
+
while (num--) str += HEX[Math.random() * 36 | 0];
|
|
64
|
+
return str;
|
|
78
65
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
var
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const searchRegexp = buildRegexp({ terms: query, matchExactly });
|
|
117
|
-
return text.split(searchRegexp).filter(hasLength).map((str) => ({
|
|
118
|
-
// Compose the object for a match
|
|
119
|
-
key: uid(),
|
|
120
|
-
text: str,
|
|
121
|
-
match: matchExactly ? str.toLowerCase() === safeQuery.toLowerCase() : searchRegexp.test(str)
|
|
122
|
-
})).map((chunk, index, chunks) => __spreadValues(__spreadValues({}, chunk), typeof clipBy === "number" && {
|
|
123
|
-
// We only overwrite the text if there is a clip
|
|
124
|
-
text: clip(__spreadProps(__spreadValues(__spreadValues({
|
|
125
|
-
curr: chunk
|
|
126
|
-
}, index < chunks.length - 1 && { next: chunks[index + 1] }), index > 0 && { prev: chunks[index - 1] }), {
|
|
127
|
-
// If this wasn't the first chunk, set the previous chunk
|
|
128
|
-
clipBy
|
|
129
|
-
}))
|
|
130
|
-
}));
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/index.ts
|
|
68
|
+
var hasLength = (str) => str.length > 0;
|
|
69
|
+
/**
|
|
70
|
+
* Split a text into chunks denoting which are a match and which are not based on a user search term.
|
|
71
|
+
* @param text String The text to split.
|
|
72
|
+
* @param query String The query to split by. This can contain multiple words.
|
|
73
|
+
* @param clipBy Number Clip the non-matches by a certain number of words to provide context around the matches.
|
|
74
|
+
* @param matchExactly Boolean If we have multiple words in the query, we will match any of the words if exact is false. For example, searching for "brown fox" in "the brown cute fox" will yield both "brown" and "fox" as matches. While if exact is true, the same search will return no results.
|
|
75
|
+
*/
|
|
76
|
+
var highlightWords = ({ text, query, clipBy, matchExactly = false }) => {
|
|
77
|
+
const safeQuery = typeof query === "string" ? query.trim() : query;
|
|
78
|
+
if (safeQuery === "") return [{
|
|
79
|
+
key: uid(),
|
|
80
|
+
text,
|
|
81
|
+
match: false
|
|
82
|
+
}];
|
|
83
|
+
const searchRegexp = buildRegexp({
|
|
84
|
+
terms: query,
|
|
85
|
+
matchExactly
|
|
86
|
+
});
|
|
87
|
+
return text.split(searchRegexp).filter(hasLength).map((str) => {
|
|
88
|
+
searchRegexp.lastIndex = 0;
|
|
89
|
+
return {
|
|
90
|
+
key: uid(),
|
|
91
|
+
text: str,
|
|
92
|
+
match: matchExactly ? str.toLowerCase() === safeQuery.toLowerCase() : searchRegexp.test(str)
|
|
93
|
+
};
|
|
94
|
+
}).map((chunk, index, chunks) => ({
|
|
95
|
+
...chunk,
|
|
96
|
+
...typeof clipBy === "number" && { text: clip({
|
|
97
|
+
curr: chunk,
|
|
98
|
+
...index < chunks.length - 1 && { next: chunks[index + 1] },
|
|
99
|
+
...index > 0 && { prev: chunks[index - 1] },
|
|
100
|
+
clipBy
|
|
101
|
+
}) }
|
|
102
|
+
}));
|
|
131
103
|
};
|
|
132
|
-
|
|
104
|
+
//#endregion
|
|
133
105
|
exports.default = highlightWords;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=highlight-words.min.js.map
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=(e=>t=>typeof t==`object`&&!!t&&e in t)(`match`),t=e=>e!==void 0;function n({curr:n,next:r,prev:i,clipBy:a=3}){let o=n.text.split(` `),s=o.length;return n.match||a>=s?n.text:t(r)&&t(i)&&e(i)&&e(r)?s>a*2?[...o.slice(0,a),`...`,...o.slice(-a)].join(` `):n.text:t(r)&&e(r)?[`...`,...o.slice(-a)].join(` `):t(i)&&e(i)?[...o.slice(0,a),`...`].join(` `):n.text}for(var r=e=>e.replace(/[|\\{}()[\]^$+*?.-]/g,e=>`\\${e}`),i=e=>e.replace(/\s{2,}/g,` `).split(` `).join(`|`),a=({terms:e,matchExactly:t=!1})=>{if(typeof e!=`string`)throw TypeError(`Expected a string`);let n=r(e.trim());return`(${t?n:i(n)})`},o=({terms:e,matchExactly:t=!1})=>{try{let n=/^([/~@;%#'])(.*?)\1([gimsuy]*)$/.exec(e);return n?new RegExp(n[2],n[3]):new RegExp(a({terms:e,matchExactly:t}),`ig`)}catch{throw TypeError(`Expected terms to be either a string or a RegExp!`)}},s=36,c=``;s--;)c+=s.toString(36);function l(e=11){let t=``,n=e;for(;n--;)t+=c[Math.random()*36|0];return t}var u=e=>e.length>0,d=({text:e,query:t,clipBy:r,matchExactly:i=!1})=>{let a=typeof t==`string`?t.trim():t;if(a===``)return[{key:l(),text:e,match:!1}];let s=o({terms:t,matchExactly:i});return e.split(s).filter(u).map(e=>(s.lastIndex=0,{key:l(),text:e,match:i?e.toLowerCase()===a.toLowerCase():s.test(e)})).map((e,t,i)=>({...e,...typeof r==`number`&&{text:n({curr:e,...t<i.length-1&&{next:i[t+1]},...t>0&&{prev:i[t-1]},clipBy:r})}}))};exports.default=d;
|
|
2
|
+
//# sourceMappingURL=highlight-words.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"highlight-words.min.js","sources":["../src/uid.ts","../src/regexp.ts","../src/clip.ts","../src/index.ts"],"sourcesContent":["// https://github.com/lukeed/uid/blob/master/src/index.js\n/* eslint no-bitwise: 'off' */\n/* eslint no-plusplus: 'off' */\n/* eslint @typescript-eslint/strict-boolean-expressions: 'off' */\nlet IDX = 36;\nlet HEX = '';\nwhile (IDX--) {\n HEX += IDX.toString(36);\n}\n\nexport default function uid(len = 11): string {\n let str = '';\n let num = len;\n while (num--) {\n str += HEX[(Math.random() * 36) | 0];\n }\n return str;\n}\n","import type { HighlightWords } from '.'; // eslint-disable-line import/no-cycle\n\n// We need escape certain characters before creating the RegExp\n// https://github.com/sindresorhus/escape-string-regexp\nconst escapeRegexp = (term: string): string =>\n term.replace(/[|\\\\{}()[\\]^$+*?.-]/g, (char: string) => `\\\\${char}`);\n\nconst termsToRegExpString = (terms: string): string =>\n terms\n .replace(/\\s{2,}/g, ' ')\n .split(' ')\n .join('|');\n\nconst regexpQuery = ({\n terms,\n matchExactly = false\n}: Readonly<HighlightWords.Query>): string => {\n if (typeof terms !== 'string') {\n throw new TypeError('Expected a string');\n }\n\n const escapedTerms = escapeRegexp(terms.trim());\n return `(${matchExactly ? escapedTerms : termsToRegExpString(escapedTerms)})`;\n};\n\nconst buildRegexp = ({\n terms,\n matchExactly = false\n}: Readonly<HighlightWords.Query>): RegExp => {\n try {\n const fromString = /^([/~@;%#'])(.*?)\\1([gimsuy]*)$/.exec(terms);\n if (fromString) {\n return new RegExp(fromString[2], fromString[3]);\n }\n\n return new RegExp(regexpQuery({ terms, matchExactly }), 'ig');\n } catch {\n throw new TypeError('Expected terms to be either a string or a RegExp!');\n }\n};\n\nexport { regexpQuery };\n\nexport default buildRegexp;\n","/* eslint complexity: [\"error\", { \"max\": 12 }] */\nimport type { HighlightWords } from '.'; // eslint-disable-line import/no-cycle\n\nconst hasProp =\n <T>(prop: string) =>\n (obj: T) =>\n obj !== null && typeof obj === 'object' && prop in obj;\nconst hasMatch = hasProp<HighlightWords.Chunk>('match');\n\nconst chunkExists = (\n chunk: HighlightWords.Chunk | undefined\n): chunk is HighlightWords.Chunk => typeof chunk !== 'undefined';\n\n/**\n * This provides context around a chunk's text, based on the next and previous chunks.\n * e.g. If we have the string \"The quick brown fox jumped over the lazy dog\",\n * and the search term \"fox jumped\", with a padding of 2, we want to have the end result be:\n * \"... quick brown fox jumped over the ...\"\n * The search term, \"fox jumped\" is left as is, and the left and right chunks, instead of having\n * the text in full, will be clipped.\n */\nexport default function clip({\n curr,\n next,\n prev,\n clipBy = 3\n}: HighlightWords.Clip): string {\n const words = curr.text.split(' ');\n const len = words.length;\n\n // If the current is a match, we leave it alone\n // Or if the clipBy is greater than or equal to the length of the words, there's nothing to clip\n if (curr.match || clipBy >= len) {\n return curr.text;\n }\n\n // If we have a clipBy greater than the length of the words in the current match,\n // it means we can clip the words in the current chunk\n const ellipsis = '...';\n\n if (\n chunkExists(next) &&\n chunkExists(prev) &&\n hasMatch(prev) &&\n hasMatch(next)\n ) {\n // Both the previous and the next chunks are a match\n // Let's check if we have enough words to clip by on both sides\n if (len > clipBy * 2) {\n return [\n ...words.slice(0, clipBy),\n ellipsis,\n ...words.slice(-clipBy)\n ].join(' ');\n }\n\n return curr.text;\n }\n\n // We start to check the next and previous matches in order to\n // properly position the elipsis\n if (chunkExists(next) && hasMatch(next)) {\n // The chunk right after this one is a match\n // So we need the elipsis at the start of the returned text\n // so that it sticks correctly to the next (match)'s text\n return [ellipsis, ...words.slice(-clipBy)].join(' ');\n }\n\n if (chunkExists(prev) && hasMatch(prev)) {\n // The chunk right before this one is a match\n // So we need the elipsis at the end of the returned text\n // so that it sticks correctly to the previous (match)'s text\n return [...words.slice(0, clipBy), ellipsis].join(' ');\n }\n\n // If we made it this far, just return the text\n return curr.text;\n}\n","import uid from './uid';\nimport buildRegexp from './regexp'; // eslint-disable-line import/no-cycle\nimport clip from './clip'; // eslint-disable-line import/no-cycle\n\n/* eslint-disable import/no-unused-modules */\ndeclare namespace HighlightWords {\n export interface Chunk {\n key: string;\n text: string;\n match: boolean;\n }\n\n export interface Options {\n text: string;\n query: string;\n clipBy?: number;\n matchExactly?: boolean;\n }\n\n export interface Clip {\n curr: Chunk;\n next?: Chunk;\n prev?: Chunk;\n clipBy?: number;\n }\n\n export interface Query {\n terms: string;\n matchExactly?: boolean;\n }\n}\n\nexport { HighlightWords };\n/* eslint-enable import/no-unused-modules */\n\nconst hasLength = (str: string): boolean => str.length > 0;\n\n/**\n * Split a text into chunks denoting which are a match and which are not based on a user search term.\n * @param text String The text to split.\n * @param query String The query to split by. This can contain multiple words.\n * @param clipBy Number Clip the non-matches by a certain number of words to provide context around the matches.\n * @param matchExactly Boolean If we have multiple words in the query, we will match any of the words if exact is false. For example, searching for \"brown fox\" in \"the brown cute fox\" will yield both \"brown\" and \"fox\" as matches. While if exact is true, the same search will return no results.\n */\nconst highlightWords = ({\n text,\n query,\n clipBy,\n matchExactly = false\n}: Readonly<HighlightWords.Options>): HighlightWords.Chunk[] => {\n // Let's make sure that the user cannot pass in just a bunch of spaces\n const safeQuery = typeof query === 'string' ? query.trim() : query;\n\n if (safeQuery === '') {\n return [\n {\n key: uid(),\n text,\n match: false\n }\n ];\n }\n\n const searchRegexp = buildRegexp({ terms: query, matchExactly });\n\n type ReadonlyChunk = Readonly<HighlightWords.Chunk>; // eslint-disable-line @typescript-eslint/no-type-alias\n\n return text\n .split(searchRegexp) // Split the entire thing into an array of matches and non-matches\n .filter(hasLength) // Filter any matches that have the text with length of 0\n .map((str) => ({\n // Compose the object for a match\n key: uid(),\n text: str,\n match: matchExactly\n ? str.toLowerCase() === safeQuery.toLowerCase()\n : searchRegexp.test(str)\n }))\n .map((chunk: ReadonlyChunk, index, chunks: readonly ReadonlyChunk[]) => ({\n // For each chunk, clip the text if needed\n ...chunk, // All the props first\n ...(typeof clipBy === 'number' && {\n // We only overwrite the text if there is a clip\n text: clip({\n curr: chunk, // We need the current chunk\n ...(index < chunks.length - 1 && { next: chunks[index + 1] }), // If this wasn't the last chunk, set the next chunk\n ...(index > 0 && { prev: chunks[index - 1] }), // If this wasn't the first chunk, set the previous chunk\n clipBy\n })\n })\n }));\n};\n\nexport default highlightWords;\n"],"names":["IDX","HEX","uid","len","str","num","escapeRegexp","term","char","termsToRegExpString","terms","regexpQuery","matchExactly","escapedTerms","buildRegexp","fromString","hasProp","prop","obj","hasMatch","chunkExists","chunk","clip","curr","next","prev","clipBy","words","ellipsis","u","f","y","p","d","C","g","n","m","hasLength","highlightWords","text","query","safeQuery","searchRegexp","index","chunks","__spreadValues","__spreadProps"],"mappings":"oEAIA,IAAIA,EAAM,GACNC,EAAM,GACV,KAAOD,KACLC,GAAOD,EAAI,SAAS,EAAE,EAGAE,SAAAA,EAAIC,EAAM,GAAY,CAC5C,IAAIC,EAAM,GACNC,EAAMF,EACV,KAAOE,KACLD,GAAOH,EAAK,KAAK,OAAW,EAAA,GAAM,CAAC,EAErC,OAAOG,CACT,CCbA,MAAME,EAAgBC,GACpBA,EAAK,QAAQ,uBAAyBC,GAAiB,KAAKA,GAAM,EAE9DC,EAAuBC,GAC3BA,EACG,QAAQ,UAAW,GAAG,EACtB,MAAM,GAAG,EACT,KAAK,GAAG,EAEPC,EAAc,CAAC,CACnB,MAAAD,EACA,aAAAE,EAAe,EACjB,IAA8C,CAC5C,GAAI,OAAOF,GAAU,SACnB,MAAM,IAAI,U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B,EAAsBuB,EAAOC,IAAsCC,EAAAA,EAAA,CAAA,EAEpEzB,CAAAA,EACC,OAAOK,GAAW,UAAY,CAEhC,KAAMJ,EAAKyB,EAAAD,EAAAV,EAAA,CACT,KAAMf,CAAAA,EACFuB,EAAQC,EAAO,OAAS,GAAK,CAAE,KAAMA,EAAOD,EAAQ,CAAC,CAAE,CACvDA,EAAAA,EAAQ,GAAK,CAAE,KAAMC,EAAOD,EAAQ,CAAC,CAAE,CAHlC,EAAA,CAIT,OAAAlB,CACF,CAAC,CAAA,CACH,CACA,CAAA,CACN"}
|
|
1
|
+
{"version":3,"file":"highlight-words.min.js","names":[],"sources":["../src/clip.ts","../src/regexp.ts","../src/uid.ts","../src/index.ts"],"sourcesContent":["/* eslint complexity: [\"error\", { \"max\": 12 }] */\nimport type { HighlightWords } from '.';\n\nconst hasProp =\n <T>(prop: string) =>\n (obj: T) =>\n obj !== null && typeof obj === 'object' && prop in obj;\nconst hasMatch = hasProp<HighlightWords.Chunk>('match');\n\nconst chunkExists = (\n chunk: HighlightWords.Chunk | undefined\n): chunk is HighlightWords.Chunk => typeof chunk !== 'undefined';\n\n/**\n * This provides context around a chunk's text, based on the next and previous chunks.\n * e.g. If we have the string \"The quick brown fox jumped over the lazy dog\",\n * and the search term \"fox jumped\", with a padding of 2, we want to have the end result be:\n * \"... quick brown fox jumped over the ...\"\n * The search term, \"fox jumped\" is left as is, and the left and right chunks, instead of having\n * the text in full, will be clipped.\n */\nexport default function clip({\n curr,\n next,\n prev,\n clipBy = 3\n}: HighlightWords.Clip): string {\n const words = curr.text.split(' ');\n const len = words.length;\n\n // If the current is a match, we leave it alone\n // Or if the clipBy is greater than or equal to the length of the words, there's nothing to clip\n if (curr.match || clipBy >= len) {\n return curr.text;\n }\n\n // If we have a clipBy greater than the length of the words in the current match,\n // it means we can clip the words in the current chunk\n const ellipsis = '...';\n\n if (\n chunkExists(next) &&\n chunkExists(prev) &&\n hasMatch(prev) &&\n hasMatch(next)\n ) {\n // Both the previous and the next chunks are a match\n // Let's check if we have enough words to clip by on both sides\n if (len > clipBy * 2) {\n return [\n ...words.slice(0, clipBy),\n ellipsis,\n ...words.slice(-clipBy)\n ].join(' ');\n }\n\n return curr.text;\n }\n\n // We start to check the next and previous matches in order to\n // properly position the elipsis\n if (chunkExists(next) && hasMatch(next)) {\n // The chunk right after this one is a match\n // So we need the elipsis at the start of the returned text\n // so that it sticks correctly to the next (match)'s text\n return [ellipsis, ...words.slice(-clipBy)].join(' ');\n }\n\n if (chunkExists(prev) && hasMatch(prev)) {\n // The chunk right before this one is a match\n // So we need the elipsis at the end of the returned text\n // so that it sticks correctly to the previous (match)'s text\n return [...words.slice(0, clipBy), ellipsis].join(' ');\n }\n\n // If we made it this far, just return the text\n return curr.text;\n}\n","import type { HighlightWords } from '.';\n\n// We need escape certain characters before creating the RegExp\n// https://github.com/sindresorhus/escape-string-regexp\nconst escapeRegexp = (term: string): string =>\n term.replace(/[|\\\\{}()[\\]^$+*?.-]/g, (char: string) => `\\\\${char}`);\n\nconst termsToRegExpString = (terms: string): string =>\n terms\n .replace(/\\s{2,}/g, ' ')\n .split(' ')\n .join('|');\n\nconst regexpQuery = ({\n terms,\n matchExactly = false\n}: Readonly<HighlightWords.Query>): string => {\n if (typeof terms !== 'string') {\n throw new TypeError('Expected a string');\n }\n\n const escapedTerms = escapeRegexp(terms.trim());\n return `(${matchExactly ? escapedTerms : termsToRegExpString(escapedTerms)})`;\n};\n\nconst buildRegexp = ({\n terms,\n matchExactly = false\n}: Readonly<HighlightWords.Query>): RegExp => {\n try {\n const fromString = /^([/~@;%#'])(.*?)\\1([gimsuy]*)$/.exec(terms);\n if (fromString) {\n return new RegExp(fromString[2], fromString[3]);\n }\n\n return new RegExp(regexpQuery({ terms, matchExactly }), 'ig');\n } catch {\n throw new TypeError('Expected terms to be either a string or a RegExp!');\n }\n};\n\nexport { regexpQuery };\n\nexport default buildRegexp;\n","// https://github.com/lukeed/uid/blob/master/src/index.js\n/* eslint no-bitwise: 'off' */\n/* eslint no-plusplus: 'off' */\n/* eslint @typescript-eslint/strict-boolean-expressions: 'off' */\nlet IDX = 36;\nlet HEX = '';\nwhile (IDX--) {\n HEX += IDX.toString(36);\n}\n\nexport default function uid(len = 11): string {\n let str = '';\n let num = len;\n while (num--) {\n str += HEX[(Math.random() * 36) | 0];\n }\n return str;\n}\n","import clip from './clip';\nimport buildRegexp from './regexp';\nimport uid from './uid';\n\ndeclare namespace HighlightWords {\n export interface Chunk {\n key: string;\n text: string;\n match: boolean;\n }\n\n export interface Options {\n text: string;\n query: string;\n clipBy?: number;\n matchExactly?: boolean;\n }\n\n export interface Clip {\n curr: Chunk;\n next?: Chunk;\n prev?: Chunk;\n clipBy?: number;\n }\n\n export interface Query {\n terms: string;\n matchExactly?: boolean;\n }\n}\n\nexport { HighlightWords };\n/* eslint-enable import/no-unused-modules */\n\nconst hasLength = (str: string): boolean => str.length > 0;\n\n/**\n * Split a text into chunks denoting which are a match and which are not based on a user search term.\n * @param text String The text to split.\n * @param query String The query to split by. This can contain multiple words.\n * @param clipBy Number Clip the non-matches by a certain number of words to provide context around the matches.\n * @param matchExactly Boolean If we have multiple words in the query, we will match any of the words if exact is false. For example, searching for \"brown fox\" in \"the brown cute fox\" will yield both \"brown\" and \"fox\" as matches. While if exact is true, the same search will return no results.\n */\nconst highlightWords = ({\n text,\n query,\n clipBy,\n matchExactly = false\n}: Readonly<HighlightWords.Options>): HighlightWords.Chunk[] => {\n // Let's make sure that the user cannot pass in just a bunch of spaces\n const safeQuery = typeof query === 'string' ? query.trim() : query;\n\n if (safeQuery === '') {\n return [\n {\n key: uid(),\n text,\n match: false\n }\n ];\n }\n\n const searchRegexp = buildRegexp({ terms: query, matchExactly });\n\n type ReadonlyChunk = Readonly<HighlightWords.Chunk>;\n\n return text\n .split(searchRegexp) // Split the entire thing into an array of matches and non-matches\n .filter(hasLength) // Filter any matches that have the text with length of 0\n .map((str) => {\n searchRegexp.lastIndex = 0;\n return {\n // Compose the object for a match\n key: uid(),\n text: str,\n match: matchExactly\n ? str.toLowerCase() === safeQuery.toLowerCase()\n : searchRegexp.test(str)\n };\n })\n .map((chunk: ReadonlyChunk, index, chunks: readonly ReadonlyChunk[]) => ({\n // For each chunk, clip the text if needed\n ...chunk, // All the props first\n ...(typeof clipBy === 'number' && {\n // We only overwrite the text if there is a clip\n text: clip({\n curr: chunk, // We need the current chunk\n ...(index < chunks.length - 1 && { next: chunks[index + 1] }), // If this wasn't the last chunk, set the next chunk\n ...(index > 0 && { prev: chunks[index - 1] }), // If this wasn't the first chunk, set the previous chunk\n clipBy\n })\n })\n }));\n};\n\nexport default highlightWords;\n"],"mappings":"+FAOA,IAAM,GAHA,GACH,GACiB,OAAO,GAAQ,YAA/B,GAA2C,KAAQ,EACtC,CAA8B,OAAO,EAEhD,EACJ,GACyC,IAAU,OAUrD,SAAwB,EAAK,CAC3B,OACA,OACA,OACA,SAAS,GACqB,CAC9B,IAAM,EAAQ,EAAK,KAAK,MAAM,GAAG,EAC3B,EAAM,EAAM,OAgDlB,OA5CI,EAAK,OAAS,GAAU,EACnB,EAAK,KAQZ,EAAY,CAAI,GAChB,EAAY,CAAI,GAChB,EAAS,CAAI,GACb,EAAS,CAAI,EAIT,EAAM,EAAS,EACV,CACL,GAAG,EAAM,MAAM,EAAG,CAAM,EACxB,MACA,GAAG,EAAM,MAAM,CAAC,CAAM,CACxB,CAAC,CAAC,KAAK,GAAG,EAGL,EAAK,KAKV,EAAY,CAAI,GAAK,EAAS,CAAI,EAI7B,CAAC,MAAU,GAAG,EAAM,MAAM,CAAC,CAAM,CAAC,CAAC,CAAC,KAAK,GAAG,EAGjD,EAAY,CAAI,GAAK,EAAS,CAAI,EAI7B,CAAC,GAAG,EAAM,MAAM,EAAG,CAAM,EAAG,KAAQ,CAAC,CAAC,KAAK,GAAG,EAIhD,EAAK,IACd,CEvEA,IDFA,IAAM,EAAgB,GACpB,EAAK,QAAQ,uBAAyB,GAAiB,KAAK,GAAM,EAE9D,EAAuB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AEvE,GAAG,EACH,GAAI,OAAO,GAAW,UAAY,CAEhC,KAAM,EAAK,CACT,KAAM,EACN,GAAI,EAAQ,EAAO,OAAS,GAAK,CAAE,KAAM,EAAO,EAAQ,EAAG,EAC3D,GAAI,EAAQ,GAAK,CAAE,KAAM,EAAO,EAAQ,EAAG,EAC3C,QACF,CAAC,CACH,CACF,EAAE,CACN"}
|
|
@@ -1,2 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var e = ((e) => (t) => typeof t == "object" && !!t && e in t)("match"), t = (e) => e !== void 0;
|
|
2
|
+
function n({ curr: n, next: r, prev: i, clipBy: a = 3 }) {
|
|
3
|
+
let o = n.text.split(" "), s = o.length;
|
|
4
|
+
return n.match || a >= s ? n.text : t(r) && t(i) && e(i) && e(r) ? s > a * 2 ? [
|
|
5
|
+
...o.slice(0, a),
|
|
6
|
+
"...",
|
|
7
|
+
...o.slice(-a)
|
|
8
|
+
].join(" ") : n.text : t(r) && e(r) ? ["...", ...o.slice(-a)].join(" ") : t(i) && e(i) ? [...o.slice(0, a), "..."].join(" ") : n.text;
|
|
9
|
+
}
|
|
10
|
+
for (var r = (e) => e.replace(/[|\\{}()[\]^$+*?.-]/g, (e) => `\\${e}`), i = (e) => e.replace(/\s{2,}/g, " ").split(" ").join("|"), a = ({ terms: e, matchExactly: t = !1 }) => {
|
|
11
|
+
if (typeof e != "string") throw TypeError("Expected a string");
|
|
12
|
+
let n = r(e.trim());
|
|
13
|
+
return `(${t ? n : i(n)})`;
|
|
14
|
+
}, o = ({ terms: e, matchExactly: t = !1 }) => {
|
|
15
|
+
try {
|
|
16
|
+
let n = /^([/~@;%#'])(.*?)\1([gimsuy]*)$/.exec(e);
|
|
17
|
+
return n ? new RegExp(n[2], n[3]) : new RegExp(a({
|
|
18
|
+
terms: e,
|
|
19
|
+
matchExactly: t
|
|
20
|
+
}), "ig");
|
|
21
|
+
} catch {
|
|
22
|
+
throw TypeError("Expected terms to be either a string or a RegExp!");
|
|
23
|
+
}
|
|
24
|
+
}, s = 36, c = ""; s--;) c += s.toString(36);
|
|
25
|
+
function l(e = 11) {
|
|
26
|
+
let t = "", n = e;
|
|
27
|
+
for (; n--;) t += c[Math.random() * 36 | 0];
|
|
28
|
+
return t;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/index.ts
|
|
32
|
+
var u = (e) => e.length > 0, d = ({ text: e, query: t, clipBy: r, matchExactly: i = !1 }) => {
|
|
33
|
+
let a = typeof t == "string" ? t.trim() : t;
|
|
34
|
+
if (a === "") return [{
|
|
35
|
+
key: l(),
|
|
36
|
+
text: e,
|
|
37
|
+
match: !1
|
|
38
|
+
}];
|
|
39
|
+
let s = o({
|
|
40
|
+
terms: t,
|
|
41
|
+
matchExactly: i
|
|
42
|
+
});
|
|
43
|
+
return e.split(s).filter(u).map((e) => (s.lastIndex = 0, {
|
|
44
|
+
key: l(),
|
|
45
|
+
text: e,
|
|
46
|
+
match: i ? e.toLowerCase() === a.toLowerCase() : s.test(e)
|
|
47
|
+
})).map((e, t, i) => ({
|
|
48
|
+
...e,
|
|
49
|
+
...typeof r == "number" && { text: n({
|
|
50
|
+
curr: e,
|
|
51
|
+
...t < i.length - 1 && { next: i[t + 1] },
|
|
52
|
+
...t > 0 && { prev: i[t - 1] },
|
|
53
|
+
clipBy: r
|
|
54
|
+
}) }
|
|
55
|
+
}));
|
|
56
|
+
};
|
|
57
|
+
//#endregion
|
|
58
|
+
export { d as default };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=highlight-words.min.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"highlight-words.min.mjs","sources":["../src/uid.ts","../src/regexp.ts","../src/clip.ts","../src/index.ts"],"sourcesContent":["// https://github.com/lukeed/uid/blob/master/src/index.js\n/* eslint no-bitwise: 'off' */\n/* eslint no-plusplus: 'off' */\n/* eslint @typescript-eslint/strict-boolean-expressions: 'off' */\nlet IDX = 36;\nlet HEX = '';\nwhile (IDX--) {\n HEX += IDX.toString(36);\n}\n\nexport default function uid(len = 11): string {\n let str = '';\n let num = len;\n while (num--) {\n str += HEX[(Math.random() * 36) | 0];\n }\n return str;\n}\n","import type { HighlightWords } from '.'; // eslint-disable-line import/no-cycle\n\n// We need escape certain characters before creating the RegExp\n// https://github.com/sindresorhus/escape-string-regexp\nconst escapeRegexp = (term: string): string =>\n term.replace(/[|\\\\{}()[\\]^$+*?.-]/g, (char: string) => `\\\\${char}`);\n\nconst termsToRegExpString = (terms: string): string =>\n terms\n .replace(/\\s{2,}/g, ' ')\n .split(' ')\n .join('|');\n\nconst regexpQuery = ({\n terms,\n matchExactly = false\n}: Readonly<HighlightWords.Query>): string => {\n if (typeof terms !== 'string') {\n throw new TypeError('Expected a string');\n }\n\n const escapedTerms = escapeRegexp(terms.trim());\n return `(${matchExactly ? escapedTerms : termsToRegExpString(escapedTerms)})`;\n};\n\nconst buildRegexp = ({\n terms,\n matchExactly = false\n}: Readonly<HighlightWords.Query>): RegExp => {\n try {\n const fromString = /^([/~@;%#'])(.*?)\\1([gimsuy]*)$/.exec(terms);\n if (fromString) {\n return new RegExp(fromString[2], fromString[3]);\n }\n\n return new RegExp(regexpQuery({ terms, matchExactly }), 'ig');\n } catch {\n throw new TypeError('Expected terms to be either a string or a RegExp!');\n }\n};\n\nexport { regexpQuery };\n\nexport default buildRegexp;\n","/* eslint complexity: [\"error\", { \"max\": 12 }] */\nimport type { HighlightWords } from '.'; // eslint-disable-line import/no-cycle\n\nconst hasProp =\n <T>(prop: string) =>\n (obj: T) =>\n obj !== null && typeof obj === 'object' && prop in obj;\nconst hasMatch = hasProp<HighlightWords.Chunk>('match');\n\nconst chunkExists = (\n chunk: HighlightWords.Chunk | undefined\n): chunk is HighlightWords.Chunk => typeof chunk !== 'undefined';\n\n/**\n * This provides context around a chunk's text, based on the next and previous chunks.\n * e.g. If we have the string \"The quick brown fox jumped over the lazy dog\",\n * and the search term \"fox jumped\", with a padding of 2, we want to have the end result be:\n * \"... quick brown fox jumped over the ...\"\n * The search term, \"fox jumped\" is left as is, and the left and right chunks, instead of having\n * the text in full, will be clipped.\n */\nexport default function clip({\n curr,\n next,\n prev,\n clipBy = 3\n}: HighlightWords.Clip): string {\n const words = curr.text.split(' ');\n const len = words.length;\n\n // If the current is a match, we leave it alone\n // Or if the clipBy is greater than or equal to the length of the words, there's nothing to clip\n if (curr.match || clipBy >= len) {\n return curr.text;\n }\n\n // If we have a clipBy greater than the length of the words in the current match,\n // it means we can clip the words in the current chunk\n const ellipsis = '...';\n\n if (\n chunkExists(next) &&\n chunkExists(prev) &&\n hasMatch(prev) &&\n hasMatch(next)\n ) {\n // Both the previous and the next chunks are a match\n // Let's check if we have enough words to clip by on both sides\n if (len > clipBy * 2) {\n return [\n ...words.slice(0, clipBy),\n ellipsis,\n ...words.slice(-clipBy)\n ].join(' ');\n }\n\n return curr.text;\n }\n\n // We start to check the next and previous matches in order to\n // properly position the elipsis\n if (chunkExists(next) && hasMatch(next)) {\n // The chunk right after this one is a match\n // So we need the elipsis at the start of the returned text\n // so that it sticks correctly to the next (match)'s text\n return [ellipsis, ...words.slice(-clipBy)].join(' ');\n }\n\n if (chunkExists(prev) && hasMatch(prev)) {\n // The chunk right before this one is a match\n // So we need the elipsis at the end of the returned text\n // so that it sticks correctly to the previous (match)'s text\n return [...words.slice(0, clipBy), ellipsis].join(' ');\n }\n\n // If we made it this far, just return the text\n return curr.text;\n}\n","import uid from './uid';\nimport buildRegexp from './regexp'; // eslint-disable-line import/no-cycle\nimport clip from './clip'; // eslint-disable-line import/no-cycle\n\n/* eslint-disable import/no-unused-modules */\ndeclare namespace HighlightWords {\n export interface Chunk {\n key: string;\n text: string;\n match: boolean;\n }\n\n export interface Options {\n text: string;\n query: string;\n clipBy?: number;\n matchExactly?: boolean;\n }\n\n export interface Clip {\n curr: Chunk;\n next?: Chunk;\n prev?: Chunk;\n clipBy?: number;\n }\n\n export interface Query {\n terms: string;\n matchExactly?: boolean;\n }\n}\n\nexport { HighlightWords };\n/* eslint-enable import/no-unused-modules */\n\nconst hasLength = (str: string): boolean => str.length > 0;\n\n/**\n * Split a text into chunks denoting which are a match and which are not based on a user search term.\n * @param text String The text to split.\n * @param query String The query to split by. This can contain multiple words.\n * @param clipBy Number Clip the non-matches by a certain number of words to provide context around the matches.\n * @param matchExactly Boolean If we have multiple words in the query, we will match any of the words if exact is false. For example, searching for \"brown fox\" in \"the brown cute fox\" will yield both \"brown\" and \"fox\" as matches. While if exact is true, the same search will return no results.\n */\nconst highlightWords = ({\n text,\n query,\n clipBy,\n matchExactly = false\n}: Readonly<HighlightWords.Options>): HighlightWords.Chunk[] => {\n // Let's make sure that the user cannot pass in just a bunch of spaces\n const safeQuery = typeof query === 'string' ? query.trim() : query;\n\n if (safeQuery === '') {\n return [\n {\n key: uid(),\n text,\n match: false\n }\n ];\n }\n\n const searchRegexp = buildRegexp({ terms: query, matchExactly });\n\n type ReadonlyChunk = Readonly<HighlightWords.Chunk>; // eslint-disable-line @typescript-eslint/no-type-alias\n\n return text\n .split(searchRegexp) // Split the entire thing into an array of matches and non-matches\n .filter(hasLength) // Filter any matches that have the text with length of 0\n .map((str) => ({\n // Compose the object for a match\n key: uid(),\n text: str,\n match: matchExactly\n ? str.toLowerCase() === safeQuery.toLowerCase()\n : searchRegexp.test(str)\n }))\n .map((chunk: ReadonlyChunk, index, chunks: readonly ReadonlyChunk[]) => ({\n // For each chunk, clip the text if needed\n ...chunk, // All the props first\n ...(typeof clipBy === 'number' && {\n // We only overwrite the text if there is a clip\n text: clip({\n curr: chunk, // We need the current chunk\n ...(index < chunks.length - 1 && { next: chunks[index + 1] }), // If this wasn't the last chunk, set the next chunk\n ...(index > 0 && { prev: chunks[index - 1] }), // If this wasn't the first chunk, set the previous chunk\n clipBy\n })\n })\n }));\n};\n\nexport default highlightWords;\n"],"names":["IDX","HEX","uid","len","str","num","escapeRegexp","term","char","termsToRegExpString","terms","regexpQuery","matchExactly","escapedTerms","buildRegexp","fromString","hasProp","prop","obj","hasMatch","chunkExists","chunk","clip","curr","next","prev","clipBy","words","ellipsis","u","f","y","p","d","C","n","m","hasLength","highlightWords","text","query","safeQuery","searchRegexp","index","chunks","__spreadValues","__spreadProps"],"mappings":"AAIA,IAAIA,EAAM,GACNC,EAAM,GACV,KAAOD,KACLC,GAAOD,EAAI,SAAS,EAAE,EAGAE,SAAAA,EAAIC,EAAM,GAAY,CAC5C,IAAIC,EAAM,GACNC,EAAMF,EACV,KAAOE,KACLD,GAAOH,EAAK,KAAK,OAAW,EAAA,GAAM,CAAC,EAErC,OAAOG,CACT,CCbA,MAAME,EAAgBC,GACpBA,EAAK,QAAQ,uBAAyBC,GAAiB,KAAKA,GAAM,EAE9DC,EAAuBC,GAC3BA,EACG,QAAQ,UAAW,GAAG,EACtB,MAAM,GAAG,EACT,KAAK,GAAG,EAEPC,EAAc,CAAC,CACnB,MAAAD,EACA,aAAAE,EAAe,EACjB,IAA8C,CAC5C,GAAI,OAAOF,GAAU,SACnB,MAAM,IAAI,UAAU,mBAAmB,EAGzC,MAAMG,EAAeP,EAAaI,EAAM,KAAK,CAAC,EAC9C,MAAO,IAAIE,EAAeC,EAAeJ,EAAoBI,CAAY,IAC3E,EAEMC,EAAc,CAAC,CACnB,MAAAJ,EACA,aAAAE,EAAe,EACjB,IAA8C,CAC5C,GAAI,CACF,MAAMG,EAAa,kCAAkC,KAAKL,CAAK,EAC/D,OAAIK,EACK,IAAI,OAAOA,EAAW,CAAC,EAAGA,EAAW,CAAC,CAAC,EAGzC,IAAI,OAAOJ,EAAY,CAAE,MAAAD,EAAO,aAAAE,CAAa,CAAC,EAAG,IAAI,CAC9D,OACE,MAAM,IAAI,UAAU,mDAAmD,CACzE,CACF,ECpCMI,EACAC,GACHC,GACCA,IAAQ,MAAQ,OAAOA,GAAQ,UAAYD,KAAQC,EACjDC,EAAWH,EAA8B,OAAO,EAEhDI,EACJC,GACkC,OAAOA,EAAU,IAUrD,SAAwBC,EAAK,CAC3B,KAAAC,E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OAAA,CAAA,EAAAE,EAAA,CAAA,EAAA,IAAAN,EAAA,EAAAC,EAAA,CAAA,CAAA,EAmCMM,MAAAA,EAAajC,GAAyBA,EAAI,OAAS,EASnDkC,EAAiB,CAAC,CACtB,KAAAC,EACA,MAAAC,EACA,OAAAd,EACA,aAAAd,EAAe,EACjB,IAAgE,CAE9D,MAAM6B,EAAY,OAAOD,GAAU,SAAWA,EAAM,KAASA,EAAAA,EAE7D,GAAIC,IAAc,GAChB,MAAO,CACL,CACE,IAAKvC,IACL,KAAAqC,EACA,MAAO,EACT,CACF,EAGF,MAAMG,EAAe5B,EAAY,CAAE,MAAO0B,EAAO,aAAA5B,CAAa,CAAC,EAI/D,OAAO2B,EACJ,MAAMG,CAAY,EAClB,OAAOL,CAAS,EAChB,IAAKjC,IAAS,CAEb,IAAKF,IACL,KAAME,EACN,MAAOQ,EACHR,EAAI,YAAkBqC,IAAAA,EAAU,cAChCC,EAAa,KAAKtC,CAAG,CAC3B,EAAE,EACD,IAAI,CAACiB,EAAsBsB,EAAOC,IAAsCC,EAAAA,EAAA,CAAA,EAEpExB,CAAAA,EACC,OAAOK,GAAW,UAAY,CAEhC,KAAMJ,EAAKwB,EAAAD,EAAAV,EAAA,CACT,KAAMd,CAAAA,EACFsB,EAAQC,EAAO,OAAS,GAAK,CAAE,KAAMA,EAAOD,EAAQ,CAAC,CAAE,CACvDA,EAAAA,EAAQ,GAAK,CAAE,KAAMC,EAAOD,EAAQ,CAAC,CAAE,CAHlC,EAAA,CAIT,OAAAjB,CACF,CAAC,CAAA,CACH,CACA,CAAA,CACN"}
|
|
1
|
+
{"version":3,"file":"highlight-words.min.mjs","names":[],"sources":["../src/clip.ts","../src/regexp.ts","../src/uid.ts","../src/index.ts"],"sourcesContent":["/* eslint complexity: [\"error\", { \"max\": 12 }] */\nimport type { HighlightWords } from '.';\n\nconst hasProp =\n <T>(prop: string) =>\n (obj: T) =>\n obj !== null && typeof obj === 'object' && prop in obj;\nconst hasMatch = hasProp<HighlightWords.Chunk>('match');\n\nconst chunkExists = (\n chunk: HighlightWords.Chunk | undefined\n): chunk is HighlightWords.Chunk => typeof chunk !== 'undefined';\n\n/**\n * This provides context around a chunk's text, based on the next and previous chunks.\n * e.g. If we have the string \"The quick brown fox jumped over the lazy dog\",\n * and the search term \"fox jumped\", with a padding of 2, we want to have the end result be:\n * \"... quick brown fox jumped over the ...\"\n * The search term, \"fox jumped\" is left as is, and the left and right chunks, instead of having\n * the text in full, will be clipped.\n */\nexport default function clip({\n curr,\n next,\n prev,\n clipBy = 3\n}: HighlightWords.Clip): string {\n const words = curr.text.split(' ');\n const len = words.length;\n\n // If the current is a match, we leave it alone\n // Or if the clipBy is greater than or equal to the length of the words, there's nothing to clip\n if (curr.match || clipBy >= len) {\n return curr.text;\n }\n\n // If we have a clipBy greater than the length of the words in the current match,\n // it means we can clip the words in the current chunk\n const ellipsis = '...';\n\n if (\n chunkExists(next) &&\n chunkExists(prev) &&\n hasMatch(prev) &&\n hasMatch(next)\n ) {\n // Both the previous and the next chunks are a match\n // Let's check if we have enough words to clip by on both sides\n if (len > clipBy * 2) {\n return [\n ...words.slice(0, clipBy),\n ellipsis,\n ...words.slice(-clipBy)\n ].join(' ');\n }\n\n return curr.text;\n }\n\n // We start to check the next and previous matches in order to\n // properly position the elipsis\n if (chunkExists(next) && hasMatch(next)) {\n // The chunk right after this one is a match\n // So we need the elipsis at the start of the returned text\n // so that it sticks correctly to the next (match)'s text\n return [ellipsis, ...words.slice(-clipBy)].join(' ');\n }\n\n if (chunkExists(prev) && hasMatch(prev)) {\n // The chunk right before this one is a match\n // So we need the elipsis at the end of the returned text\n // so that it sticks correctly to the previous (match)'s text\n return [...words.slice(0, clipBy), ellipsis].join(' ');\n }\n\n // If we made it this far, just return the text\n return curr.text;\n}\n","import type { HighlightWords } from '.';\n\n// We need escape certain characters before creating the RegExp\n// https://github.com/sindresorhus/escape-string-regexp\nconst escapeRegexp = (term: string): string =>\n term.replace(/[|\\\\{}()[\\]^$+*?.-]/g, (char: string) => `\\\\${char}`);\n\nconst termsToRegExpString = (terms: string): string =>\n terms\n .replace(/\\s{2,}/g, ' ')\n .split(' ')\n .join('|');\n\nconst regexpQuery = ({\n terms,\n matchExactly = false\n}: Readonly<HighlightWords.Query>): string => {\n if (typeof terms !== 'string') {\n throw new TypeError('Expected a string');\n }\n\n const escapedTerms = escapeRegexp(terms.trim());\n return `(${matchExactly ? escapedTerms : termsToRegExpString(escapedTerms)})`;\n};\n\nconst buildRegexp = ({\n terms,\n matchExactly = false\n}: Readonly<HighlightWords.Query>): RegExp => {\n try {\n const fromString = /^([/~@;%#'])(.*?)\\1([gimsuy]*)$/.exec(terms);\n if (fromString) {\n return new RegExp(fromString[2], fromString[3]);\n }\n\n return new RegExp(regexpQuery({ terms, matchExactly }), 'ig');\n } catch {\n throw new TypeError('Expected terms to be either a string or a RegExp!');\n }\n};\n\nexport { regexpQuery };\n\nexport default buildRegexp;\n","// https://github.com/lukeed/uid/blob/master/src/index.js\n/* eslint no-bitwise: 'off' */\n/* eslint no-plusplus: 'off' */\n/* eslint @typescript-eslint/strict-boolean-expressions: 'off' */\nlet IDX = 36;\nlet HEX = '';\nwhile (IDX--) {\n HEX += IDX.toString(36);\n}\n\nexport default function uid(len = 11): string {\n let str = '';\n let num = len;\n while (num--) {\n str += HEX[(Math.random() * 36) | 0];\n }\n return str;\n}\n","import clip from './clip';\nimport buildRegexp from './regexp';\nimport uid from './uid';\n\ndeclare namespace HighlightWords {\n export interface Chunk {\n key: string;\n text: string;\n match: boolean;\n }\n\n export interface Options {\n text: string;\n query: string;\n clipBy?: number;\n matchExactly?: boolean;\n }\n\n export interface Clip {\n curr: Chunk;\n next?: Chunk;\n prev?: Chunk;\n clipBy?: number;\n }\n\n export interface Query {\n terms: string;\n matchExactly?: boolean;\n }\n}\n\nexport { HighlightWords };\n/* eslint-enable import/no-unused-modules */\n\nconst hasLength = (str: string): boolean => str.length > 0;\n\n/**\n * Split a text into chunks denoting which are a match and which are not based on a user search term.\n * @param text String The text to split.\n * @param query String The query to split by. This can contain multiple words.\n * @param clipBy Number Clip the non-matches by a certain number of words to provide context around the matches.\n * @param matchExactly Boolean If we have multiple words in the query, we will match any of the words if exact is false. For example, searching for \"brown fox\" in \"the brown cute fox\" will yield both \"brown\" and \"fox\" as matches. While if exact is true, the same search will return no results.\n */\nconst highlightWords = ({\n text,\n query,\n clipBy,\n matchExactly = false\n}: Readonly<HighlightWords.Options>): HighlightWords.Chunk[] => {\n // Let's make sure that the user cannot pass in just a bunch of spaces\n const safeQuery = typeof query === 'string' ? query.trim() : query;\n\n if (safeQuery === '') {\n return [\n {\n key: uid(),\n text,\n match: false\n }\n ];\n }\n\n const searchRegexp = buildRegexp({ terms: query, matchExactly });\n\n type ReadonlyChunk = Readonly<HighlightWords.Chunk>;\n\n return text\n .split(searchRegexp) // Split the entire thing into an array of matches and non-matches\n .filter(hasLength) // Filter any matches that have the text with length of 0\n .map((str) => {\n searchRegexp.lastIndex = 0;\n return {\n // Compose the object for a match\n key: uid(),\n text: str,\n match: matchExactly\n ? str.toLowerCase() === safeQuery.toLowerCase()\n : searchRegexp.test(str)\n };\n })\n .map((chunk: ReadonlyChunk, index, chunks: readonly ReadonlyChunk[]) => ({\n // For each chunk, clip the text if needed\n ...chunk, // All the props first\n ...(typeof clipBy === 'number' && {\n // We only overwrite the text if there is a clip\n text: clip({\n curr: chunk, // We need the current chunk\n ...(index < chunks.length - 1 && { next: chunks[index + 1] }), // If this wasn't the last chunk, set the next chunk\n ...(index > 0 && { prev: chunks[index - 1] }), // If this wasn't the first chunk, set the previous chunk\n clipBy\n })\n })\n }));\n};\n\nexport default highlightWords;\n"],"mappings":"AAOA,IAAM,MAHA,OACH,MACiB,OAAO,KAAQ,cAA/B,KAA2C,KAAQ,EACtC,CAA8B,OAAO,GAEhD,KACJ,MACyC,MAAU;AAUrD,SAAwB,EAAK,EAC3B,SACA,SACA,SACA,YAAS,KACqB;CAC9B,IAAM,IAAQ,EAAK,KAAK,M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M,IAAY;CAC5C,IAAI,IAAM,IACN,IAAM;CACV,OAAO,MACL,KAAO,EAAK,KAAK,OAAO,IAAI,KAAM;CAEpC,OAAO;AACT;;;ACiBA,IAAM,KAAa,MAAyB,EAAI,SAAS,GASnD,KAAkB,EACtB,SACA,UACA,WACA,kBAAe,SAC+C;CAE9D,IAAM,IAAY,OAAO,KAAU,WAAW,EAAM,KAAK,IAAI;CAE7D,IAAI,MAAc,IAChB,OAAO,CACL;EACE,KAAK,EAAI;EACT;EACA,OAAO;CACT,CACF;CAGF,IAAM,IAAe,EAAY;EAAE,OAAO;EAAO;CAAa,CAAC;CAI/D,OAAO,EACJ,MAAM,CAAY,CAAC,CACnB,OAAO,CAAS,CAAC,CACjB,KAAK,OACJ,EAAa,YAAY,GAClB;EAEL,KAAK,EAAI;EACT,MAAM;EACN,OAAO,IACH,EAAI,YAAY,MAAM,EAAU,YAAY,IAC5C,EAAa,KAAK,CAAG;CAC3B,EACD,CAAC,CACD,KAAK,GAAsB,GAAO,OAAsC;EAEvE,GAAG;EACH,GAAI,OAAO,KAAW,YAAY,EAEhC,MAAM,EAAK;GACT,MAAM;GACN,GAAI,IAAQ,EAAO,SAAS,KAAK,EAAE,MAAM,EAAO,IAAQ,GAAG;GAC3D,GAAI,IAAQ,KAAK,EAAE,MAAM,EAAO,IAAQ,GAAG;GAC3C;EACF,CAAC,EACH;CACF,EAAE;AACN"}
|
package/dist/highlight-words.mjs
CHANGED
|
@@ -1,129 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
//#region src/clip.ts
|
|
2
|
+
var hasProp = (prop) => (obj) => obj !== null && typeof obj === "object" && prop in obj;
|
|
3
|
+
var hasMatch = hasProp("match");
|
|
4
|
+
var chunkExists = (chunk) => typeof chunk !== "undefined";
|
|
5
|
+
/**
|
|
6
|
+
* This provides context around a chunk's text, based on the next and previous chunks.
|
|
7
|
+
* e.g. If we have the string "The quick brown fox jumped over the lazy dog",
|
|
8
|
+
* and the search term "fox jumped", with a padding of 2, we want to have the end result be:
|
|
9
|
+
* "... quick brown fox jumped over the ..."
|
|
10
|
+
* The search term, "fox jumped" is left as is, and the left and right chunks, instead of having
|
|
11
|
+
* the text in full, will be clipped.
|
|
12
|
+
*/
|
|
13
|
+
function clip({ curr, next, prev, clipBy = 3 }) {
|
|
14
|
+
const words = curr.text.split(" ");
|
|
15
|
+
const len = words.length;
|
|
16
|
+
if (curr.match || clipBy >= len) return curr.text;
|
|
17
|
+
const ellipsis = "...";
|
|
18
|
+
if (chunkExists(next) && chunkExists(prev) && hasMatch(prev) && hasMatch(next)) {
|
|
19
|
+
if (len > clipBy * 2) return [
|
|
20
|
+
...words.slice(0, clipBy),
|
|
21
|
+
ellipsis,
|
|
22
|
+
...words.slice(-clipBy)
|
|
23
|
+
].join(" ");
|
|
24
|
+
return curr.text;
|
|
25
|
+
}
|
|
26
|
+
if (chunkExists(next) && hasMatch(next)) return [ellipsis, ...words.slice(-clipBy)].join(" ");
|
|
27
|
+
if (chunkExists(prev) && hasMatch(prev)) return [...words.slice(0, clipBy), ellipsis].join(" ");
|
|
28
|
+
return curr.text;
|
|
5
29
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const escapeRegexp = (term) => term.replace(/[|\\{}()[\]^$+*?.-]/g, (char) => `\\${char}`);
|
|
16
|
-
const termsToRegExpString = (terms) => terms.replace(/\s{2,}/g, " ").split(" ").join("|");
|
|
17
|
-
const regexpQuery = ({
|
|
18
|
-
terms,
|
|
19
|
-
matchExactly = false
|
|
20
|
-
}) => {
|
|
21
|
-
if (typeof terms !== "string") {
|
|
22
|
-
throw new TypeError("Expected a string");
|
|
23
|
-
}
|
|
24
|
-
const escapedTerms = escapeRegexp(terms.trim());
|
|
25
|
-
return `(${matchExactly ? escapedTerms : termsToRegExpString(escapedTerms)})`;
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/regexp.ts
|
|
32
|
+
var escapeRegexp = (term) => term.replace(/[|\\{}()[\]^$+*?.-]/g, (char) => `\\${char}`);
|
|
33
|
+
var termsToRegExpString = (terms) => terms.replace(/\s{2,}/g, " ").split(" ").join("|");
|
|
34
|
+
var regexpQuery = ({ terms, matchExactly = false }) => {
|
|
35
|
+
if (typeof terms !== "string") throw new TypeError("Expected a string");
|
|
36
|
+
const escapedTerms = escapeRegexp(terms.trim());
|
|
37
|
+
return `(${matchExactly ? escapedTerms : termsToRegExpString(escapedTerms)})`;
|
|
26
38
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
throw new TypeError("Expected terms to be either a string or a RegExp!");
|
|
39
|
-
}
|
|
39
|
+
var buildRegexp = ({ terms, matchExactly = false }) => {
|
|
40
|
+
try {
|
|
41
|
+
const fromString = /^([/~@;%#'])(.*?)\1([gimsuy]*)$/.exec(terms);
|
|
42
|
+
if (fromString) return new RegExp(fromString[2], fromString[3]);
|
|
43
|
+
return new RegExp(regexpQuery({
|
|
44
|
+
terms,
|
|
45
|
+
matchExactly
|
|
46
|
+
}), "ig");
|
|
47
|
+
} catch {
|
|
48
|
+
throw new TypeError("Expected terms to be either a string or a RegExp!");
|
|
49
|
+
}
|
|
40
50
|
};
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const words = curr.text.split(" ");
|
|
52
|
-
const len = words.length;
|
|
53
|
-
if (curr.match || clipBy >= len) {
|
|
54
|
-
return curr.text;
|
|
55
|
-
}
|
|
56
|
-
const ellipsis = "...";
|
|
57
|
-
if (chunkExists(next) && chunkExists(prev) && hasMatch(prev) && hasMatch(next)) {
|
|
58
|
-
if (len > clipBy * 2) {
|
|
59
|
-
return [
|
|
60
|
-
...words.slice(0, clipBy),
|
|
61
|
-
ellipsis,
|
|
62
|
-
...words.slice(-clipBy)
|
|
63
|
-
].join(" ");
|
|
64
|
-
}
|
|
65
|
-
return curr.text;
|
|
66
|
-
}
|
|
67
|
-
if (chunkExists(next) && hasMatch(next)) {
|
|
68
|
-
return [ellipsis, ...words.slice(-clipBy)].join(" ");
|
|
69
|
-
}
|
|
70
|
-
if (chunkExists(prev) && hasMatch(prev)) {
|
|
71
|
-
return [...words.slice(0, clipBy), ellipsis].join(" ");
|
|
72
|
-
}
|
|
73
|
-
return curr.text;
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/uid.ts
|
|
53
|
+
var IDX = 36;
|
|
54
|
+
var HEX = "";
|
|
55
|
+
while (IDX--) HEX += IDX.toString(36);
|
|
56
|
+
function uid(len = 11) {
|
|
57
|
+
let str = "";
|
|
58
|
+
let num = len;
|
|
59
|
+
while (num--) str += HEX[Math.random() * 36 | 0];
|
|
60
|
+
return str;
|
|
74
61
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
var
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const searchRegexp = buildRegexp({ terms: query, matchExactly });
|
|
113
|
-
return text.split(searchRegexp).filter(hasLength).map((str) => ({
|
|
114
|
-
// Compose the object for a match
|
|
115
|
-
key: uid(),
|
|
116
|
-
text: str,
|
|
117
|
-
match: matchExactly ? str.toLowerCase() === safeQuery.toLowerCase() : searchRegexp.test(str)
|
|
118
|
-
})).map((chunk, index, chunks) => __spreadValues(__spreadValues({}, chunk), typeof clipBy === "number" && {
|
|
119
|
-
// We only overwrite the text if there is a clip
|
|
120
|
-
text: clip(__spreadProps(__spreadValues(__spreadValues({
|
|
121
|
-
curr: chunk
|
|
122
|
-
}, index < chunks.length - 1 && { next: chunks[index + 1] }), index > 0 && { prev: chunks[index - 1] }), {
|
|
123
|
-
// If this wasn't the first chunk, set the previous chunk
|
|
124
|
-
clipBy
|
|
125
|
-
}))
|
|
126
|
-
}));
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/index.ts
|
|
64
|
+
var hasLength = (str) => str.length > 0;
|
|
65
|
+
/**
|
|
66
|
+
* Split a text into chunks denoting which are a match and which are not based on a user search term.
|
|
67
|
+
* @param text String The text to split.
|
|
68
|
+
* @param query String The query to split by. This can contain multiple words.
|
|
69
|
+
* @param clipBy Number Clip the non-matches by a certain number of words to provide context around the matches.
|
|
70
|
+
* @param matchExactly Boolean If we have multiple words in the query, we will match any of the words if exact is false. For example, searching for "brown fox" in "the brown cute fox" will yield both "brown" and "fox" as matches. While if exact is true, the same search will return no results.
|
|
71
|
+
*/
|
|
72
|
+
var highlightWords = ({ text, query, clipBy, matchExactly = false }) => {
|
|
73
|
+
const safeQuery = typeof query === "string" ? query.trim() : query;
|
|
74
|
+
if (safeQuery === "") return [{
|
|
75
|
+
key: uid(),
|
|
76
|
+
text,
|
|
77
|
+
match: false
|
|
78
|
+
}];
|
|
79
|
+
const searchRegexp = buildRegexp({
|
|
80
|
+
terms: query,
|
|
81
|
+
matchExactly
|
|
82
|
+
});
|
|
83
|
+
return text.split(searchRegexp).filter(hasLength).map((str) => {
|
|
84
|
+
searchRegexp.lastIndex = 0;
|
|
85
|
+
return {
|
|
86
|
+
key: uid(),
|
|
87
|
+
text: str,
|
|
88
|
+
match: matchExactly ? str.toLowerCase() === safeQuery.toLowerCase() : searchRegexp.test(str)
|
|
89
|
+
};
|
|
90
|
+
}).map((chunk, index, chunks) => ({
|
|
91
|
+
...chunk,
|
|
92
|
+
...typeof clipBy === "number" && { text: clip({
|
|
93
|
+
curr: chunk,
|
|
94
|
+
...index < chunks.length - 1 && { next: chunks[index + 1] },
|
|
95
|
+
...index > 0 && { prev: chunks[index - 1] },
|
|
96
|
+
clipBy
|
|
97
|
+
}) }
|
|
98
|
+
}));
|
|
127
99
|
};
|
|
128
|
-
|
|
100
|
+
//#endregion
|
|
129
101
|
export { highlightWords as default };
|
package/package.json
CHANGED
|
@@ -1,84 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "highlight-words",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Split a piece text into multiple chunks based on a search query, allowing you to highlight the matches.",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"highlight",
|
|
7
|
+
"regex",
|
|
8
|
+
"search",
|
|
9
|
+
"string"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/tricinel/highlight-words#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/tricinel/highlight-words/issues"
|
|
8
14
|
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Bogdan Lazar",
|
|
9
17
|
"repository": {
|
|
10
18
|
"type": "git",
|
|
11
19
|
"url": "https://github.com/tricinel/highlight-words.git"
|
|
12
20
|
},
|
|
13
|
-
"bugs": {
|
|
14
|
-
"url": "https://github.com/tricinel/highlight-words/issues"
|
|
15
|
-
},
|
|
16
|
-
"homepage": "https://github.com/tricinel/highlight-words#readme",
|
|
17
|
-
"author": "Bogdan Lazar",
|
|
18
|
-
"license": "MIT",
|
|
19
|
-
"keywords": [
|
|
20
|
-
"string",
|
|
21
|
-
"search",
|
|
22
|
-
"highlight",
|
|
23
|
-
"regex"
|
|
24
|
-
],
|
|
25
21
|
"files": [
|
|
26
22
|
"dist"
|
|
27
23
|
],
|
|
28
|
-
"eslintIgnore": [
|
|
29
|
-
"node_modules",
|
|
30
|
-
"dist",
|
|
31
|
-
"coverage"
|
|
32
|
-
],
|
|
33
24
|
"type": "commonjs",
|
|
34
|
-
"typings": "dist/highlight-words.d.ts",
|
|
35
25
|
"main": "dist/highlight-words.js",
|
|
36
26
|
"module": "dist/highlight-words.mjs",
|
|
27
|
+
"typings": "dist/highlight-words.d.ts",
|
|
37
28
|
"exports": {
|
|
38
29
|
".": {
|
|
39
30
|
"require": "./dist/highlight-words.js",
|
|
40
31
|
"default": "./dist/highlight-words.mjs"
|
|
41
32
|
}
|
|
42
33
|
},
|
|
43
|
-
"devDependencies": {
|
|
44
|
-
"@babel/core": "^7.21.5",
|
|
45
|
-
"@babel/plugin-transform-typescript": "^7.21.3",
|
|
46
|
-
"@babel/preset-env": "^7.21.5",
|
|
47
|
-
"@babel/preset-typescript": "^7.21.5",
|
|
48
|
-
"@stryker-mutator/core": "^6.4.2",
|
|
49
|
-
"@stryker-mutator/jest-runner": "^6.4.2",
|
|
50
|
-
"@types/jest": "^29.5.1",
|
|
51
|
-
"@typescript-eslint/eslint-plugin": "^5.59.1",
|
|
52
|
-
"@typescript-eslint/parser": "^5.59.1",
|
|
53
|
-
"cross-env": "^7.0.3",
|
|
54
|
-
"esbuild": "^0.17.18",
|
|
55
|
-
"eslint": "^8.39.0",
|
|
56
|
-
"eslint-config-frontwerk-typescript": "^1.2.2",
|
|
57
|
-
"eslint-config-prettier": "^8.8.0",
|
|
58
|
-
"eslint-plugin-import": "^2.27.5",
|
|
59
|
-
"eslint-plugin-jest": "^27.2.1",
|
|
60
|
-
"eslint-plugin-prettier": "^4.2.1",
|
|
61
|
-
"husky": "^8.0.3",
|
|
62
|
-
"jest": "^29.5.0",
|
|
63
|
-
"jest-watch-typeahead": "^2.2.2",
|
|
64
|
-
"lint-staged": "^13.2.2",
|
|
65
|
-
"npm-run-all": "^4.1.5",
|
|
66
|
-
"precise-commits": "^1.0.2",
|
|
67
|
-
"prettier": "^2.8.8",
|
|
68
|
-
"rimraf": "^5.0.0",
|
|
69
|
-
"rollup": "^3.21.2",
|
|
70
|
-
"rollup-plugin-dts": "^5.3.0",
|
|
71
|
-
"rollup-plugin-esbuild": "^5.0.0",
|
|
72
|
-
"typescript": "^5.0.4"
|
|
73
|
-
},
|
|
74
34
|
"scripts": {
|
|
35
|
+
"prepare": "lefthook install || true",
|
|
75
36
|
"prebuild": "cross-env rimraf dist",
|
|
76
|
-
"build": "
|
|
77
|
-
"
|
|
37
|
+
"build:lib:normal": "vite build",
|
|
38
|
+
"build:lib:min": "cross-env MINIFY=true vite build",
|
|
39
|
+
"build:lib": "npm-run-all build:lib:normal build:lib:min",
|
|
40
|
+
"build:types": "dts-bundle-generator -o dist/highlight-words.d.ts src/index.ts && dts-bundle-generator -o dist/highlight-words.d.mts src/index.ts",
|
|
41
|
+
"build:dist:normal": "vite build",
|
|
42
|
+
"build:dist:min": "cross-env MINIFY=true vite build",
|
|
43
|
+
"build:dist": "npm-run-all prebuild build:dist:normal build:dist:min",
|
|
44
|
+
"build": "npm-run-all prebuild build:lib build:types",
|
|
45
|
+
"watch": "vite build --watch",
|
|
78
46
|
"typecheck": "tsc --noEmit",
|
|
79
|
-
"
|
|
80
|
-
"format": "
|
|
81
|
-
"
|
|
82
|
-
"test:
|
|
47
|
+
"format": "oxfmt --check .",
|
|
48
|
+
"format:write": "oxfmt --write .",
|
|
49
|
+
"lint": "oxlint -c .oxlintrc.json .",
|
|
50
|
+
"test:dev": "vitest run -c vitest.config.mts",
|
|
51
|
+
"test:dev:watch": "vitest",
|
|
52
|
+
"test:dist": "vitest run -c vitest.dist.config.mts",
|
|
53
|
+
"test": "npm-run-all -p test:dev test:dist",
|
|
54
|
+
"validate": "npm-run-all typecheck lint format test:dev",
|
|
55
|
+
"pretest:dist": "npm-run-all build:dist",
|
|
56
|
+
"prepublishOnly": "npm-run-all validate format:write build"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/node": "^25.7.0",
|
|
60
|
+
"cross-env": "^10.1.0",
|
|
61
|
+
"dts-bundle-generator": "^9.5.1",
|
|
62
|
+
"lefthook": "^2.1.6",
|
|
63
|
+
"npm-run-all": "^4.1.5",
|
|
64
|
+
"oxfmt": "^0.49.0",
|
|
65
|
+
"oxlint": "^1.64.0",
|
|
66
|
+
"rimraf": "^6.0.1",
|
|
67
|
+
"tsc-files": "^1.1.4",
|
|
68
|
+
"typescript": "^6.0.3",
|
|
69
|
+
"vite": "^8.0.12",
|
|
70
|
+
"vitest": "^4.1.6"
|
|
71
|
+
},
|
|
72
|
+
"engines": {
|
|
73
|
+
"node": ">= 22",
|
|
74
|
+
"npm": ">= 10"
|
|
83
75
|
}
|
|
84
|
-
}
|
|
76
|
+
}
|