[IMP] web: add "unique" array utility function
This commit adds "unique" array utility function which returns a copy of the given array but without any duplicates. closes odoo/odoo#76472 Related: odoo/enterprise#20833 Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
@@ -154,3 +154,15 @@ export function sections(array) {
|
||||
}
|
||||
return sections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing all elements of the given
|
||||
* array but without duplicates.
|
||||
*
|
||||
* @template T
|
||||
* @param {T[]} array
|
||||
* @returns {T[]}
|
||||
*/
|
||||
export function unique(array) {
|
||||
return Array.from(new Set(array));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import { cartesian, groupBy, sortBy } from "@web/core/utils/arrays";
|
||||
import { cartesian, groupBy, sortBy, unique } from "@web/core/utils/arrays";
|
||||
|
||||
QUnit.module("utils", () => {
|
||||
QUnit.module("Arrays");
|
||||
@@ -220,4 +220,9 @@ QUnit.module("utils", () => {
|
||||
QUnit.test("cartesian product of four arrays", function (assert) {
|
||||
assert.deepEqual(cartesian([1], [2], [3], [4]), [[1, 2, 3, 4]]);
|
||||
});
|
||||
|
||||
QUnit.test("unique array", function (assert) {
|
||||
assert.deepEqual(unique([1, 2, 3, 2, 4, 3, 1, 4]), [1, 2, 3, 4]);
|
||||
assert.deepEqual(unique("a d c a b c d b".split(" ")), "a d c b".split(" "));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user