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",
|
"toIterable": "readonly",
|
||||||
"stringReverse": "readonly",
|
"stringReverse": "readonly",
|
||||||
"promiseTimeout": "readonly",
|
"promiseTimeout": "readonly",
|
||||||
"stringReplaceAsync": "readonly",
|
|
||||||
"parseUrl": "readonly",
|
"parseUrl": "readonly",
|
||||||
"EventDispatcher": "readonly",
|
"EventDispatcher": "readonly",
|
||||||
"EventListenerCollection": "readonly",
|
"EventListenerCollection": "readonly",
|
||||||
|
@ -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(''));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user