Move stringReplaceAsync
It is only used in AnkiNoteBuilder and it was originally created for this purpose.
This commit is contained in:
parent
7ac1c843a9
commit
8f9b6534c6
@ -86,7 +86,6 @@
|
||||
"toIterable": "readonly",
|
||||
"stringReverse": "readonly",
|
||||
"promiseTimeout": "readonly",
|
||||
"stringReplaceAsync": "readonly",
|
||||
"parseUrl": "readonly",
|
||||
"EventDispatcher": "readonly",
|
||||
"EventListenerCollection": "readonly",
|
||||
|
@ -93,7 +93,7 @@ class AnkiNoteBuilder {
|
||||
};
|
||||
const markers = this._markers;
|
||||
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)) {
|
||||
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(''));
|
||||
}
|
||||
}
|
||||
|
@ -175,21 +175,6 @@ function promiseTimeout(delay, resolveValue) {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user