From 313052ffb80b55fcecc13f8e3d1b5285dc7129bd Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 23 Jun 2022 07:11:43 +0000 Subject: [PATCH] [IMP] web: object utils: add pluck function Part-of: odoo/odoo#92475 --- addons/web/static/src/core/utils/objects.js | 10 ++++++++++ addons/web/static/tests/core/utils/objects_tests.js | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/core/utils/objects.js b/addons/web/static/src/core/utils/objects.js index 4e7638b0b53..eaf24f0bf8d 100644 --- a/addons/web/static/src/core/utils/objects.js +++ b/addons/web/static/src/core/utils/objects.js @@ -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} + */ +export function pluck(object, ...properties) { + return Object.fromEntries(Object.entries(object).filter(([k]) => properties.includes(k))); +} diff --git a/addons/web/static/tests/core/utils/objects_tests.js b/addons/web/static/tests/core/utils/objects_tests.js index 27e14807ea4..41cde986927 100644 --- a/addons/web/static/tests/core/utils/objects_tests.js +++ b/addons/web/static/tests/core/utils/objects_tests.js @@ -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 }));