1 | import invariant from 'invariant';
|
2 | import stripDiacritics from './stripDiacritics';
|
3 | var CASE_INSENSITIVE = 'i';
|
4 | var COMBINING_MARKS = /[\u0300-\u036F]/;
|
5 |
|
6 | export function escapeStringRegexp(str) {
|
7 | !(typeof str === 'string') ? process.env.NODE_ENV !== "production" ? invariant(false, '`escapeStringRegexp` expected a string.') : invariant(false) : void 0;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
|
13 | }
|
14 | export default function getMatchBounds(subject, str) {
|
15 | var search = new RegExp(escapeStringRegexp(stripDiacritics(str)), CASE_INSENSITIVE);
|
16 | var matches = search.exec(stripDiacritics(subject));
|
17 |
|
18 | if (!matches) {
|
19 | return null;
|
20 | }
|
21 |
|
22 | var start = matches.index;
|
23 | var matchLength = matches[0].length;
|
24 |
|
25 | if (COMBINING_MARKS.test(subject)) {
|
26 |
|
27 |
|
28 | for (var ii = 0; ii <= start; ii++) {
|
29 | if (COMBINING_MARKS.test(subject[ii])) {
|
30 | start += 1;
|
31 | }
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 | for (var _ii = start; _ii <= start + matchLength; _ii++) {
|
37 | if (COMBINING_MARKS.test(subject[_ii])) {
|
38 | matchLength += 1;
|
39 | }
|
40 | }
|
41 | }
|
42 |
|
43 | return {
|
44 | end: start + matchLength,
|
45 | start: start
|
46 | };
|
47 | } |
\ | No newline at end of file |