[FIX] web: pick a non-enumerable property

Before this commit, pick did not work on non-enumerable properties.

For example, getters in a class are non-enumerable properties. It was not
possible to pick the value of a getter in a Class with the pick function.

closes odoo/odoo#97558

Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
FrancoisGe
2022-08-23 16:28:58 +02:00
parent e1d60b9161
commit 771fdcc67c
2 changed files with 13 additions and 1 deletions
+3 -1
View File
@@ -29,5 +29,7 @@ export function deepCopy(obj) {
* @returns {Partial<T>}
*/
export function pick(object, ...properties) {
return Object.fromEntries(Object.entries(object).filter(([k]) => properties.includes(k)));
return Object.fromEntries(
properties.filter((prop) => prop in object).map((prop) => [prop, object[prop]])
);
}
@@ -11,6 +11,16 @@ QUnit.module("utils", () => {
assert.deepEqual(pick({ a: 3, b: "a", c: [] }, "a"), { a: 3 });
assert.deepEqual(pick({ a: 3, b: "a", c: [] }, "a", "c"), { a: 3, c: [] });
assert.deepEqual(pick({ a: 3, b: "a", c: [] }, "a", "b", "c"), { a: 3, b: "a", c: [] });
// Non enumerable property
class MyClass {
get a() {
return 1;
}
}
const myClass = new MyClass();
Object.defineProperty(myClass, "b", { enumerable: false, value: 2 });
assert.deepEqual(pick(myClass, "a", "b"), { a: 1, b: 2 });
});
QUnit.test("shallowEqual: simple valid cases", function (assert) {