Skip to content

Commit a054abe

Browse files
committed
binder: added a closing anchor
More accurate identification of the end of the key in difficult cases
1 parent d060974 commit a054abe

File tree

4 files changed

+104
-22
lines changed

4 files changed

+104
-22
lines changed

README.md

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,32 @@ The StringBinder allows you to replace keywords in plain text with ReactNode ele
88

99
## Usage
1010
### Keywords
11-
A keyword in plain text must start with `$` and contain only `A-Za-z` with out any spaces.
11+
A keyword in plain text must start & close with `$` and contain only `A-Za-z` with out any spaces (min len = 1).
1212

1313
```jsx
1414

15-
// keyword is $keyword
16-
const success = `This is a $keyword.`;
15+
// keyword is $keyword$
16+
const success = `This is a $keyword$.`;
1717

1818
// just not handled
1919
const fail = `This is not a $1keyword`;
2020

21-
// keyword is $key
21+
// just not handled
2222
const watch = `This is not a $key_word`;
2323

2424
// multiply keys: $flowers, $crons
25-
const multiple = "$flowers and $crowns.";
25+
const multiple = "$flowers$ and $crowns$.";
2626
```
2727
### Binds
28-
Bind object keys must be same as keywords but with out `$`
28+
Keys of bindable `ReactNode` | `string` must be same as keywords but without `$`
2929

3030
```jsx
3131
import StringBinderInstance from "@rejchev/react-string-binder";
3232

33-
const text = "This is a simple $example";
33+
const text = "This is a simple $nl$";
3434

3535
StringBinderInstance.bind(text, {
36-
example: <hr />
36+
nl: <hr />
3737
})
3838

3939
```
@@ -44,13 +44,57 @@ StringBinderInstance.bind(text, {
4444

4545
import StringBinderInstance from "@rejchev/react-string-binder";
4646

47-
const text =
48-
"The spring sun warms the awakening nature. " +
49-
"Birds chirp joyfully in the tree $crowns, $flowers stretch towards the heavenly surface. " +
50-
"The air is filled with freshness and the aroma of the first flowers."
51-
52-
const element = StringBinderInstance.bind(text, {
53-
crowns: <a href={'https://en.wikipedia.org/wiki/Crown_(botany)'} target={'_blank'}>crowns</a>,
54-
flowers: <a href={'https://en.wikipedia.org/wiki/Flower'} target={'_blank'}>flowers</a>
55-
})
47+
function App() {
48+
49+
const text =
50+
"The spring sun warms the awakening nature. $n$" +
51+
"Birds chirp joyfully in the tree $crowns$, $flowers$ stretch towards the heavenly surface. $n$" +
52+
"The air is filled with freshness and the aroma of the first flowers."
53+
54+
const unknownKeys =
55+
// Test variant <br/>
56+
"Test $variant$ $n$" +
57+
58+
// Test another_variant <br/>
59+
"Test another_$variant$ $n$" +
60+
61+
// Test third_variant_variant
62+
"Test third_$variant$_variant"
63+
64+
const oneMore =
65+
// Test <a href="//yandex.ru" target="_blank">flowers</a>flowers <br/>
66+
"Test $flowers$flowers $n$" +
67+
68+
// Test <a href="//yandex.ru" target="_blank">flowers</a>flowers <br/>
69+
"Test $flowers$flowers$ $n$" +
70+
71+
// Test <a href="//yandex.ru" target="_blank">flowers</a><a href="//yandex.ru" target="_blank">flowers</a> <br/>
72+
"Test $flowers$$flowers$ $n$"
73+
74+
return (
75+
<>
76+
<p className="read-the-docs">
77+
{StringBinderInstance.bind(text, {
78+
crowns: <a href={'//google.com'} target={'_blank'}>crowns</a>,
79+
flowers: <a href={'//yandex.ru'} target={'_blank'}>flowers</a>,
80+
n: <br/>,
81+
})}
82+
</p>
83+
<p className="read-the-docs">
84+
{StringBinderInstance.bind(unknownKeys, {
85+
crowns: <a href={'//google.com'} target={'_blank'}>crowns</a>,
86+
flowers: <a href={'//yandex.ru'} target={'_blank'}>flowers</a>,
87+
n: <br/>,
88+
})}
89+
</p>
90+
<p className="read-the-docs">
91+
{StringBinderInstance.bind(oneMore, {
92+
crowns: <a href={'//google.com'} target={'_blank'}>crowns</a>,
93+
flowers: <a href={'//yandex.ru'} target={'_blank'}>flowers</a>,
94+
n: <br/>,
95+
})}
96+
</p>
97+
</>
98+
)
99+
}
56100
```

example/App.tsx

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,51 @@ import StringBinderInstance from "../lib";
44
function App() {
55

66
const text =
7-
"The spring sun warms the awakening nature. " +
8-
"Birds chirp joyfully in the tree $crowns, $flowers stretch towards the heavenly surface. " +
7+
"The spring sun warms the awakening nature. $n$" +
8+
"Birds chirp joyfully in the tree $crowns$, $flowers$ stretch towards the heavenly surface. $n$" +
99
"The air is filled with freshness and the aroma of the first flowers."
1010

11+
const unknownKeys =
12+
// Test variant <br/>
13+
"Test $variant$ $n$" +
14+
15+
// Test another_variant <br/>
16+
"Test another_$variant$ $n$" +
17+
18+
// Test third_variant_variant
19+
"Test third_$variant$_variant"
20+
21+
const oneMore =
22+
// Test <a href="//yandex.ru" target="_blank">flowers</a>flowers <br/>
23+
"Test $flowers$flowers $n$" +
24+
25+
// Test <a href="//yandex.ru" target="_blank">flowers</a>flowers <br/>
26+
"Test $flowers$flowers$ $n$" +
27+
28+
// Test <a href="//yandex.ru" target="_blank">flowers</a><a href="//yandex.ru" target="_blank">flowers</a> <br/>
29+
"Test $flowers$$flowers$ $n$"
30+
1131
return (
1232
<>
1333
<p className="read-the-docs">
1434
{StringBinderInstance.bind(text, {
1535
crowns: <a href={'//google.com'} target={'_blank'}>crowns</a>,
16-
flowers: <a href={'//yandex.ru'} target={'_blank'}>flowers</a>
36+
flowers: <a href={'//yandex.ru'} target={'_blank'}>flowers</a>,
37+
n: <br/>,
38+
})}
39+
</p>
40+
<p className="read-the-docs">
41+
{StringBinderInstance.bind(unknownKeys, {
42+
crowns: <a href={'//google.com'} target={'_blank'}>crowns</a>,
43+
flowers: <a href={'//yandex.ru'} target={'_blank'}>flowers</a>,
44+
n: <br/>,
45+
})}
46+
</p>
47+
<p className="read-the-docs">
48+
{StringBinderInstance.bind(oneMore, {
49+
crowns: <a href={'//google.com'} target={'_blank'}>crowns</a>,
50+
flowers: <a href={'//yandex.ru'} target={'_blank'}>flowers</a>,
51+
n: <br/>,
1752
})}
1853
</p>
1954
</>

lib/StringBinder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ class StringBinder {
8484

8585
keyIdx = -1;
8686

87+
if (j >= len || str[j] !== StringBinder.SpecialSymbol)
88+
continue;
89+
8790
if(!sub.length) {
8891
buf += str[i];
8992
continue;
@@ -92,7 +95,7 @@ class StringBinder {
9295
chain.push(sub);
9396
buf += StringBinder.Anchor;
9497

95-
i += sub.length;
98+
i += sub.length + 1;
9699
}
97100

98101
return {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@rejchev/react-string-binder",
33
"private": false,
4-
"version": "1.0.7",
4+
"version": "1.1.0",
55
"type": "module",
66

77
"author": {

0 commit comments

Comments
 (0)