« 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.js11
1 files changed, 11 insertions, 0 deletions
diff --git a/general-util.js b/general-util.js
index 0f5bdd5..85ff8e5 100644
--- a/general-util.js
+++ b/general-util.js
@@ -324,3 +324,14 @@ module.exports.silenceEvents = async function(emitter, eventsToSilence, 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
+module.exports.parseWithoutPrototype = function(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
+  })
+}