Skip to content

Commit 9ef59f4

Browse files
committed
Make README more informative
1 parent 6c95e7c commit 9ef59f4

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

README.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,99 @@ Interpolate regular expression captures and match variables (i.e. `$1`, `$&`) in
88
var Interpolation = require('regexp-capture-interpolation');
99

1010
Interpolation.interpolate(
11-
['key\tvalue', 'key', 'value', 0, 'key\tvalue\n'],
11+
['key\tvalue', 'key', 'value', 0, 'key\tvalue\nkey2\tvalue2\n'],
1212
'$1: $2,'
1313
); // returns "key: value,"
1414
```
1515

16+
# Exports
17+
18+
```js
19+
require('regexp-capture-interpolation')
20+
```
21+
22+
Returns an object which has three functions, with the keys `interpolate`, `interpolateSlash`, and `interpolateDollar`.
23+
24+
These three functions each accept two arguments: a match array and a replacement string.
25+
26+
The match array corresponds to the arguments passed into the replacement function in `String.prototype.replace()`, that is, it should have the following values:
27+
28+
- Matched substring
29+
- First capture
30+
- Second capture
31+
- ...
32+
- The offset of the matched substring within the whole string being examined.
33+
- The whole string being examined
34+
35+
Thus, the array **should** have at least three arguments, as captures being optional, their number being determined by the regular expression pattern.
36+
37+
### `interpolateDollar`
38+
39+
This function is intended to correspond exactly to JavaScript's own replacement behaviour, i.e., the following should have the same result:
40+
41+
```js
42+
originalString.replace( pattern, replacement );
43+
44+
originalString.replace( pattern, function () {
45+
return Interpolation.interpolateDollar(arguments, replacement);
46+
} );
47+
```
48+
49+
Thus, it perfoms the following interpolations:
50+
51+
```
52+
$& Matched text
53+
$` The portion of the string that precedes the matched substring.
54+
$' The portion of the string that follows the matched substring.
55+
$1 The contents of the first captured substring.
56+
$2 The contents of the second captured substring.
57+
...
58+
$$ A literal dollar character
59+
```
60+
61+
Note that `$1`, `$2`... `$n`. are only interpolated if the pattern has at least _n_ captures.
62+
63+
### `interpolateSlash`
64+
65+
This function uses backslash as an escape character, which is useful for situations where you are replacing from user input.
66+
67+
It perfoms the following interpolations, corresponding broadly to JavaScript's String Literal syntax:
68+
69+
```
70+
\1 The contents of the first captured substring.
71+
\2 The contents of the second captured substring.
72+
...
73+
\\ A literal backslash character.
74+
\0 A literal NUL (0x00) character.
75+
\n A newline.
76+
\r A carriage return.
77+
\t A tab character.
78+
\v A vertical tab character.
79+
\$ A literal dollar character (this works for any non-word character).
80+
\xhh The codepoint at 0xhh.
81+
\uhhhh The codepoint at 0xhh.
82+
\u{h}, \u{hhhh}, \x{hhh}, etc.
83+
```
84+
85+
Note that `\1`, `\2`... `\n`. are only interpolated if the pattern has at least _n_ captures.
86+
87+
Note also that if writing the replacement in JavaScript source, your backslashes may need escaping, i.e. the following are equivalent:
88+
89+
```js
90+
Interpolation.interpolateSlash(match, "\\1");
91+
Interpolation.interpolateDollar(match, "$1");
92+
```
93+
94+
### `interpolate`
95+
96+
This function interpolates both slash-escaped and dollar-escaped tokens.
97+
98+
This function is designed to be used in a user-facing environment where DWIM (do what I mean) is an important principle, i.e. the user might expect to use dollar or slash syntax, or even both.
99+
100+
# Author & Contributors
101+
102+
Daniel Perrett <perrettdl@googlemail.com>
103+
104+
# Copyright & License
105+
106+
This package is available under a MIT License - see the included `LICENSE` file for more information.

0 commit comments

Comments
 (0)