Skip to content

Commit a9652b0

Browse files
committed
Type definitions and naming tweaks
1 parent 55d795f commit a9652b0

4 files changed

Lines changed: 40 additions & 20 deletions

File tree

index.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Replaces text in a string, using a regular expression or search string.
3+
* @param string The input string.
4+
* @param searchValue A string to search for.
5+
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
6+
*/
7+
declare function replaceAsync(
8+
string: string,
9+
searchValue: string | RegExp,
10+
replaceValue: string
11+
): Promise<string>;
12+
13+
/**
14+
* Replaces text in a string, using a regular expression or search string.
15+
* @param string The input string.
16+
* @param searchValue A string to search for.
17+
* @param replacer An async function that returns the replacement text.
18+
*/
19+
declare function replaceAsync(
20+
string: string,
21+
searchValue: string | RegExp,
22+
replacer: (substring: string, ...args: any[]) => Promise<string> | string
23+
): Promise<string>;
24+
25+
export = replaceAsync;

index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
module.exports = function stringReplaceAsync(
2-
string,
3-
searchValue,
4-
replaceValue
5-
) {
1+
module.exports = function replaceAsync(string, searchValue, replacer) {
62
try {
7-
if (typeof replaceValue === "function") {
8-
// 1. Run fake pass of `replace`, collect values from `replaceValue` calls
3+
if (typeof replacer === "function") {
4+
// 1. Run fake pass of `replace`, collect values from `replacer` calls
95
// 2. Resolve them with `Promise.all`
106
// 3. Run `replace` with resolved values
117
var values = [];
128
String.prototype.replace.call(string, searchValue, function () {
13-
values.push(replaceValue.apply(undefined, arguments));
9+
values.push(replacer.apply(undefined, arguments));
1410
return "";
1511
});
1612
return Promise.all(values).then(function (resolvedValues) {
@@ -20,7 +16,7 @@ module.exports = function stringReplaceAsync(
2016
});
2117
} else {
2218
return Promise.resolve(
23-
String.prototype.replace.call(string, searchValue, replaceValue)
19+
String.prototype.replace.call(string, searchValue, replacer)
2420
);
2521
}
2622
} catch (error) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"test": "jest"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"string",

readme.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ await replaceAsync("#rebeccapurple", /#(\w+)/g, async (match, name) => {
2121

2222
## The Why
2323

24-
Say you have a task of replacing color names in a string with their respective hex codes.
24+
Say you have a task of replacing color names with their respective hex codes.
2525

2626
```js
2727
let spec = "I want background to be #papayawhip and borders #rebeccapurple.";
28-
// convert to "I want background to be #FFEFD5 (papayawhip) and borders #663399 (rebeccapurple).";
28+
// make it "I want background to be #FFEFD5 (papayawhip) and borders #663399 (rebeccapurple).";
2929
```
3030

31-
Luckily, strings in JavaScript have this handy method built in, so you use it.
31+
Luckily, strings in JavaScript have this handy `replace` method built in, so you use it.
3232

3333
```js
3434
spec.replace(/#(\w+)/g, (match, name) => {
@@ -37,7 +37,7 @@ spec.replace(/#(\w+)/g, (match, name) => {
3737
});
3838
```
3939

40-
Time passes, a new requirement comes in: now you have to query a database for custom colors. This is an async operation, so naturally you convert `getColorByName` into async function.
40+
Time passes, a new requirement emerges: now you have to query a database for custom colors. This is an async operation, so naturally you convert `getColorByName` into async function.
4141

4242
Turns out it has a cost: now all the code above should also be async. You try this:
4343

@@ -68,11 +68,9 @@ Yay!
6868
API is
6969
[String.prototype.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), except the first argument is a string itself.
7070

71-
### replaceAsync(string, searchValue, replaceValue)
71+
### replaceAsync(string, searchValue, replace)
7272

73-
Runs `replaceValue` and waits for it to resolve before replacing `searchValue`
74-
with results. If `searchValue` is a _global_ RegExp, `replaceValue` will be
75-
called concurrently for every match.
73+
Runs `replace` and waits for it to resolve before replacing `searchValue` with results. If `searchValue` is a _global_ RegExp, `replace` will be called concurrently for every match.
7674

7775
#### string
7876

@@ -87,15 +85,15 @@ Type: `regexp`, `string`
8785

8886
An expression to match substrings to replace.
8987

90-
#### replaceValue
88+
#### replace
9189

9290
Type: `function`, `string`
9391

9492
A `function` that takes [several arguments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter) and returns a `promise`. Resolved value will be used as _replacement substring_.
9593

9694
## A Note on Concurrency
9795

98-
Previously this module had an aditional menhod `seq()` that ran `replaceValue` functions one by one instead of all at once. We decided to remove it to cut more weight from the package. Here's a snippet that achieves the same effect:
96+
Previously this module had aditional menhod `seq()` that ran `replace` functions one by one instead of all at once. We decided to remove it to narrow our scope. Here's a snippet that achieves the same effect:
9997

10098
```js
10199
let sequence = Promise.resolve();

0 commit comments

Comments
 (0)