Move stringReplaceAsync

It is only used in AnkiNoteBuilder and it was originally created
for this purpose.
This commit is contained in:
toasted-nutbread 2020-03-07 15:23:32 -05:00
parent 7ac1c843a9
commit 8f9b6534c6
3 changed files with 16 additions and 17 deletions

View File

@ -86,7 +86,6 @@
"toIterable": "readonly", "toIterable": "readonly",
"stringReverse": "readonly", "stringReverse": "readonly",
"promiseTimeout": "readonly", "promiseTimeout": "readonly",
"stringReplaceAsync": "readonly",
"parseUrl": "readonly", "parseUrl": "readonly",
"EventDispatcher": "readonly", "EventDispatcher": "readonly",
"EventListenerCollection": "readonly", "EventListenerCollection": "readonly",

View File

@ -93,7 +93,7 @@ class AnkiNoteBuilder {
}; };
const markers = this._markers; const markers = this._markers;
const pattern = /\{([\w-]+)\}/g; const pattern = /\{([\w-]+)\}/g;
return await stringReplaceAsync(field, pattern, async (g0, marker) => { return await AnkiNoteBuilder.stringReplaceAsync(field, pattern, async (g0, marker) => {
if (!markers.has(marker)) { if (!markers.has(marker)) {
return g0; return g0;
} }
@ -106,4 +106,19 @@ class AnkiNoteBuilder {
} }
}); });
} }
static stringReplaceAsync(str, regex, replacer) {
let match;
let index = 0;
const parts = [];
while ((match = regex.exec(str)) !== null) {
parts.push(str.substring(index, match.index), replacer(...match, match.index, str));
index = regex.lastIndex;
}
if (parts.length === 0) {
return Promise.resolve(str);
}
parts.push(str.substring(index));
return Promise.all(parts).then((v) => v.join(''));
}
} }

View File

@ -175,21 +175,6 @@ function promiseTimeout(delay, resolveValue) {
return promise; return promise;
} }
function stringReplaceAsync(str, regex, replacer) {
let match;
let index = 0;
const parts = [];
while ((match = regex.exec(str)) !== null) {
parts.push(str.substring(index, match.index), replacer(...match, match.index, str));
index = regex.lastIndex;
}
if (parts.length === 0) {
return Promise.resolve(str);
}
parts.push(str.substring(index));
return Promise.all(parts).then((v) => v.join(''));
}
/* /*
* Common events * Common events