[IMP] web: object utils: add pluck function

Part-of: odoo/odoo#92475
This commit is contained in:
Aaron Bohy
2022-06-30 09:29:41 +02:00
parent 5501744a83
commit 313052ffb8
2 changed files with 19 additions and 1 deletions
@@ -21,3 +21,13 @@ export function shallowEqual(obj1, obj2) {
export function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* @template {T}
* @param {T} object
* @param {...(keyof T)} properties
* @returns {Partial<T>}
*/
export function pluck(object, ...properties) {
return Object.fromEntries(Object.entries(object).filter(([k]) => properties.includes(k)));
}
@@ -1,10 +1,18 @@
/** @odoo-module **/
import { shallowEqual } from "@web/core/utils/objects";
import { pluck, shallowEqual } from "@web/core/utils/objects";
QUnit.module("utils", () => {
QUnit.module("Objects");
QUnit.test("pluck", function (assert) {
assert.deepEqual(pluck({}), {});
assert.deepEqual(pluck({}, "a"), {});
assert.deepEqual(pluck({ a: 3, b: "a", c: [] }, "a"), { a: 3 });
assert.deepEqual(pluck({ a: 3, b: "a", c: [] }, "a", "c"), { a: 3, c: [] });
assert.deepEqual(pluck({ a: 3, b: "a", c: [] }, "a", "b", "c"), { a: 3, b: "a", c: [] });
});
QUnit.test("shallowEqual: simple valid cases", function (assert) {
assert.ok(shallowEqual({}, {}));
assert.ok(shallowEqual({ a: 1 }, { a: 1 }));