Skip to content

Commit b8e7c3c

Browse files
committed
feature/NOTASK Initial functionality
0 parents  commit b8e7c3c

File tree

11 files changed

+6730
-0
lines changed

11 files changed

+6730
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# regexify-string
2+
3+
The way to decorate string by using regex with: React components, HTML tags etc
4+
5+
> perfectly works with: strings, html tags, react, react-native
6+
7+
8+
## Install
9+
10+
```
11+
$ npm install --save regexify-string
12+
```
13+
14+
## API
15+
16+
### regexifyString({ pattern, decorator, input })
17+
18+
#### pattern
19+
20+
Type: `RegExp`
21+
22+
#### decorator
23+
24+
Type: `(match: string, index: number) => string | JSX.Element`
25+
26+
- `match` string you would like to replace/decorate with something
27+
- `index` index number of the current match
28+
29+
**NOTE:** Try do not forget to use keys for React collections if needed
30+
31+
#### input
32+
33+
Type: `string`
34+
35+
## Usage
36+
37+
#### with strings
38+
39+
```js
40+
const result = regexifyString({
41+
pattern: /\[.*?\]/gim,
42+
decorator: (match, index) => {
43+
return `<${match}>`;
44+
},
45+
input: 'some [text] with simple example',
46+
});
47+
48+
console.log(result);
49+
// ['some ', '<[text]>', ' with simple example']
50+
```
51+
52+
#### with index keys
53+
54+
```js
55+
const result = regexifyString({
56+
pattern: /\[.*?\]/gim,
57+
decorator: (match, index) => {
58+
switch (index) {
59+
case 0: return `<FIRST ${match}>`;
60+
case 1: return `<SECOND ${match}>`;
61+
case 2: return `<THIRD ${match}>`;
62+
default: return match;
63+
}
64+
},
65+
input: 'Important text with [first link] and [second link] and much more [links]',
66+
});
67+
68+
console.log(result);
69+
/*
70+
[
71+
'Important text with ',
72+
'<FIRST [first link]>',
73+
' and ',
74+
'<SECOND [second link]>',
75+
' and much more ',
76+
'<THIRD [links]>',
77+
]
78+
*/
79+
```
80+
81+
#### with html
82+
83+
```js
84+
const result = regexifyString({
85+
pattern: /\[.*?\]/gim,
86+
decorator: (match, index) => {
87+
return <span>{match}</span>;
88+
},
89+
input: 'some [text] with simple example',
90+
});
91+
92+
console.log(result);
93+
// ['some ', <span>[text]</span>, ' with simple example']
94+
```
95+
96+
#### with React / React Native components
97+
98+
```js
99+
regexifyString({
100+
pattern: /\[.*?\]/gim,
101+
decorator: (match, index) => {
102+
return (
103+
<Link
104+
to={SOME_ROUTE}
105+
onClick={onClick}
106+
>
107+
{match}
108+
</Link>
109+
);
110+
},
111+
input: DisclaimerData.body,
112+
});
113+
```
114+
115+
```js
116+
regexifyString({
117+
pattern: /\[.*?\]/gim,
118+
decorator: (match, index) => {
119+
return <React.Fragment>{match}</React.Fragment>;
120+
},
121+
input: 'some [text] with simple example',
122+
});
123+
```
124+
125+
## License
126+
127+
MIT © [Artem Solovev](http://www.artemsolovev.com/)

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference types="react" />
2+
export interface IDecorateStringWithReactProps {
3+
decorator: (match: string, index: number) => string | JSX.Element;
4+
pattern: RegExp;
5+
input: string;
6+
}
7+
export default function regexifyString(props: IDecorateStringWithReactProps): Array<(string | JSX.Element)>;

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
function regexifyString(props) {
4+
var pattern = props.pattern, decorator = props.decorator, input = props.input;
5+
var output = [];
6+
var matchIndex = 0;
7+
var processedInput = input;
8+
var result = pattern.exec(processedInput);
9+
while (result !== null) {
10+
var matchStartAt = result.index;
11+
var match = result[0];
12+
var contentBeforeMatch = processedInput.substring(0, matchStartAt);
13+
var decoratedMatch = decorator(match, matchIndex);
14+
output.push(contentBeforeMatch);
15+
output.push(decoratedMatch);
16+
processedInput = processedInput.substring(matchStartAt + match.length, processedInput.length + 1);
17+
pattern.lastIndex = 0;
18+
result = pattern.exec(processedInput);
19+
++matchIndex;
20+
}
21+
if (processedInput) {
22+
output.push(processedInput);
23+
}
24+
return output;
25+
}
26+
exports.default = regexifyString;

jest.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
roots: [
3+
'<rootDir>/src'
4+
],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest',
7+
},
8+
testRegex: '(/__tests__/.*|(\\.|/)(test))\\.tsx?$',
9+
clearMocks: true,
10+
moduleFileExtensions: [
11+
'ts',
12+
'tsx',
13+
'js',
14+
'jsx',
15+
'json',
16+
'node'
17+
],
18+
moduleNameMapper: {
19+
'\~(.*)$': '<rootDir>/src/$1'
20+
},
21+
};

out/regexifyString.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
function regexifyString(props) {
4+
var pattern = props.pattern, decorator = props.decorator, input = props.input;
5+
var output = [];
6+
var matchIndex = 0;
7+
var processedInput = input;
8+
var result = pattern.exec(processedInput);
9+
while (result !== null) {
10+
var matchStartAt = result.index;
11+
var match = result[0];
12+
var contentBeforeMatch = processedInput.substring(0, matchStartAt);
13+
var decoratedMatch = decorator(match, matchIndex);
14+
output.push(contentBeforeMatch);
15+
output.push(decoratedMatch);
16+
processedInput = processedInput.substring(matchStartAt + match.length, processedInput.length + 1);
17+
pattern.lastIndex = 0;
18+
result = pattern.exec(processedInput);
19+
++matchIndex;
20+
}
21+
if (processedInput) {
22+
output.push(processedInput);
23+
}
24+
return output;
25+
}
26+
exports.default = regexifyString;

0 commit comments

Comments
 (0)