From ea1b8196ba240d2cc4c64a9079947028cb536bf8 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 18 Oct 2023 14:53:38 -0300 Subject: sugar: filterProperties: preserve original order if specified --- src/util/sugar.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/util/sugar.js') 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]; + } } } -- cgit 1.3.0-6-gf8a5