« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/util/sugar.js18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index 2e724bae..3e39e98f 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -168,7 +168,9 @@ export function setIntersection(set1, set2) {
   return intersection;
 }
 
-export function filterProperties(object, properties) {
+export function filterProperties(object, properties, {
+  preserveOriginalOrder = false,
+} = {}) {
   if (typeof object !== 'object' || object === null) {
     throw new TypeError(`Expected object to be an object, got ${typeAppearance(object)}`);
   }
@@ -179,9 +181,17 @@ export function filterProperties(object, properties) {
 
   const filteredObject = {};
 
-  for (const property of properties) {
-    if (Object.hasOwn(object, property)) {
-      filteredObject[property] = object[property];
+  if (preserveOriginalOrder) {
+    for (const property of Object.keys(object)) {
+      if (properties.includes(property)) {
+        filteredObject[property] = object[property];
+      }
+    }
+  } else {
+    for (const property of properties) {
+      if (Object.hasOwn(object, property)) {
+        filteredObject[property] = object[property];
+      }
     }
   }