aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-07 15:23:32 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-07 21:47:48 -0500
commit8f9b6534c64dd871db92729714b46d400a52e30e (patch)
treeb07759046d5f90f72878d4b0725383c76994889b
parent7ac1c843a92cbefd0a625f06b5093217b585f7cf (diff)
Move stringReplaceAsync
It is only used in AnkiNoteBuilder and it was originally created for this purpose.
-rw-r--r--.eslintrc.json1
-rw-r--r--ext/bg/js/anki-note-builder.js17
-rw-r--r--ext/mixed/js/core.js15
3 files changed, 16 insertions, 17 deletions
diff --git a/.eslintrc.json b/.eslintrc.json
index 4ee1f982..2730acb5 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -86,7 +86,6 @@
"toIterable": "readonly",
"stringReverse": "readonly",
"promiseTimeout": "readonly",
- "stringReplaceAsync": "readonly",
"parseUrl": "readonly",
"EventDispatcher": "readonly",
"EventListenerCollection": "readonly",
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index be39ff43..2a15b20d 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -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(''));
+ }
}
diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js
index 0e22b9ac..0d50e915 100644
--- a/ext/mixed/js/core.js
+++ b/ext/mixed/js/core.js
@@ -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