« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/general-util.js
diff options
context:
space:
mode:
Diffstat (limited to 'general-util.js')
-rw-r--r--general-util.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/general-util.js b/general-util.js
index bb0574a..536b3fd 100644
--- a/general-util.js
+++ b/general-util.js
@@ -329,3 +329,28 @@ export async function parseOptions(options, optionDescriptorMap) {
 }
 
 parseOptions.handleDashless = Symbol()
+
+export async function silenceEvents(emitter, eventsToSilence, callback) {
+  const oldEmit = emitter.emit
+
+  emitter.emit = function(event, ...data) {
+    if (!eventsToSilence.includes(event)) {
+      oldEmit.apply(emitter, [event, ...data])
+    }
+  }
+
+  await callback()
+
+  emitter.emit = oldEmit
+}
+
+// Kindly stolen from ESDiscuss:
+// https://esdiscuss.org/topic/proposal-add-an-option-to-omit-prototype-of-objects-created-by-json-parse#content-1
+export function parseWithoutPrototype(string) {
+  return JSON.parse(string, function(k, v) {
+    if (v && typeof v === 'object' && !Array.isArray(v)) {
+      return Object.assign(Object.create(null), v)
+    }
+    return v
+  })
+}