« get me outta code hell

sugar: filterProperties: preserve original order if specified - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-10-18 14:53:38 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-10-18 14:53:38 -0300
commitea1b8196ba240d2cc4c64a9079947028cb536bf8 (patch)
tree2506e8439eacc9badd36f461590af2b8d30d1fe5 /src/util
parentd8d877b63eec2e7c1d1afbca84b7f3cf6d24ca35 (diff)
sugar: filterProperties: preserve original order if specified
Diffstat (limited to 'src/util')
-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 2e724ba..3e39e98 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];
+      }
     }
   }