@@ -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 }));
|
||||
|
||||
Reference in New Issue
Block a user