diff options
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 |
commit | ea1b8196ba240d2cc4c64a9079947028cb536bf8 (patch) | |
tree | 2506e8439eacc9badd36f461590af2b8d30d1fe5 /src | |
parent | d8d877b63eec2e7c1d1afbca84b7f3cf6d24ca35 (diff) |
sugar: filterProperties: preserve original order if specified
Diffstat (limited to 'src')
-rw-r--r-- | src/util/sugar.js | 18 |
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]; + } } } |