Skip to content

Commit 979f888

Browse files
committed
prettier tweaked this
1 parent 3562f50 commit 979f888

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Example/ios/*
2+
Example/android/*
3+
node_modules/*

.vscode/settings.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"cSpell.words": [
3-
"jrichardlai",
4-
"taskrabbit"
5-
]
6-
}
2+
"cSpell.words": ["jrichardlai", "taskrabbit"]
3+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"types": "src/ParsedText.d.ts",
77
"scripts": {
88
"prettier": "prettier",
9-
"prettier:all": "prettier --write ./*.json ./*.js ./**/*.js src/*.ts ",
9+
"prettier:all": "prettier --write '**/*.json' '**/*.js' '**/*.ts' ",
1010
"lint": "eslint",
1111
"test": "jest"
1212
},

src/lib/TextExtraction.js

+31-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TextExtraction {
66
* @param {RegExp} patterns[].pattern - RegExp to be used for parsing
77
*/
88
constructor(text, patterns) {
9-
this.text = text;
9+
this.text = text;
1010
this.patterns = patterns || [];
1111
}
1212

@@ -15,7 +15,7 @@ class TextExtraction {
1515
* @return {Object[]} - props for all the parts of the text
1616
*/
1717
parse() {
18-
let parsedTexts = [{children: this.text}];
18+
let parsedTexts = [{ children: this.text }];
1919
this.patterns.forEach((pattern) => {
2020
let newParts = [];
2121

@@ -27,27 +27,37 @@ class TextExtraction {
2727
return;
2828
}
2929

30-
let parts = [];
30+
let parts = [];
3131
let textLeft = parsedText.children;
3232
let indexOfMatchedString = 0;
3333

34-
while (textLeft) {
35-
let matches = pattern.pattern.exec(textLeft);
36-
37-
if (!matches) { break; }
38-
34+
/** @type {RegExpExecArray} */
35+
let matches;
36+
// Global RegExps are stateful, this makes it start at 0 if reused
37+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
38+
pattern.pattern.lastIndex = 0;
39+
while (textLeft && (matches = pattern.pattern.exec(textLeft))) {
3940
let previousText = textLeft.substr(0, matches.index);
40-
indexOfMatchedString += matches.index;
41+
indexOfMatchedString = matches.index;
4142

42-
parts.push({children: previousText});
43+
parts.push({ children: previousText });
4344

44-
parts.push(this.getMatchedPart(pattern, matches[0], matches, indexOfMatchedString));
45+
parts.push(
46+
this.getMatchedPart(
47+
pattern,
48+
matches[0],
49+
matches,
50+
indexOfMatchedString,
51+
),
52+
);
4553

4654
textLeft = textLeft.substr(matches.index + matches[0].length);
4755
indexOfMatchedString += matches[0].length;
56+
// Global RegExps are stateful, this makes it operate on the "remainder" of the string
57+
pattern.pattern.lastIndex = indexOfMatchedString;
4858
}
4959

50-
parts.push({children: textLeft});
60+
parts.push({ children: textLeft });
5161

5262
newParts.push(...parts);
5363
});
@@ -56,9 +66,9 @@ class TextExtraction {
5666
});
5767

5868
// Remove _matched key.
59-
parsedTexts.forEach((parsedText) => delete(parsedText._matched));
69+
parsedTexts.forEach((parsedText) => delete parsedText._matched);
6070

61-
return parsedTexts.filter(t => !!t.children);
71+
return parsedTexts.filter((t) => !!t.children);
6272
}
6373

6474
// private
@@ -75,7 +85,9 @@ class TextExtraction {
7585
let props = {};
7686

7787
Object.keys(matchedPattern).forEach((key) => {
78-
if (key === 'pattern' || key === 'renderText') { return; }
88+
if (key === 'pattern' || key === 'renderText') {
89+
return;
90+
}
7991

8092
if (typeof matchedPattern[key] === 'function') {
8193
props[key] = () => matchedPattern[key](text, index);
@@ -85,7 +97,10 @@ class TextExtraction {
8597
});
8698

8799
let children = text;
88-
if (matchedPattern.renderText && typeof matchedPattern.renderText === 'function') {
100+
if (
101+
matchedPattern.renderText &&
102+
typeof matchedPattern.renderText === 'function'
103+
) {
89104
children = matchedPattern.renderText(text, matches);
90105
}
91106

test/TextExtraction.test.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ describe('TextExtraction', () => {
7171
'https://long-gtld.xn--vermgensberatung-pwb',
7272
];
7373
const textExtraction = new TextExtraction(
74-
`this is my website ${urls[0]} and this is also ${
75-
urls[1]
76-
} and why not this one also ${urls[2]}`,
74+
`this is my website ${urls[0]} and this is also ${urls[1]} and why not this one also ${urls[2]}`,
7775
[
7876
{
7977
pattern: PATTERNS.url,

0 commit comments

Comments
 (0)