summaryrefslogtreecommitdiff
path: root/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'util.js')
-rw-r--r--util.js19
1 files changed, 19 insertions, 0 deletions
diff --git a/util.js b/util.js
new file mode 100644
index 0000000..934ccf5
--- /dev/null
+++ b/util.js
@@ -0,0 +1,19 @@
+/* Adapted from: https://stackoverflow.com/a/45502134/5401763 under CC BY-SA 4.0 */
+exports.splitSegments = function (string, pattern) {
+ let segments = [];
+ let match;
+ let lastIndex = 0;
+ pattern.lastIndex = 0;
+
+ while (match = pattern.exec(string)) {
+ if (match.index > lastIndex) {
+ segments.push(string.substring(lastIndex, match.index));
+ }
+ segments.push({ match });
+ lastIndex = match.index + match[0].length;
+ }
+ if (lastIndex < string.length) {
+ segments.push(string.substr(lastIndex));
+ }
+ return segments;
+};