From 4d7940e8e4ceb0b90439fc9726339f796ee23940 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 8 Nov 2019 22:08:11 -0500 Subject: Optimize dictFieldFormat --- ext/mixed/js/extension.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'ext/mixed') diff --git a/ext/mixed/js/extension.js b/ext/mixed/js/extension.js index 54862e19..12ed9c1f 100644 --- a/ext/mixed/js/extension.js +++ b/ext/mixed/js/extension.js @@ -133,3 +133,18 @@ 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('')); +} -- cgit v1.2.3