diff --git a/addons/web/__manifest__.py b/addons/web/__manifest__.py index 7e8648b81b6..e13682633a6 100644 --- a/addons/web/__manifest__.py +++ b/addons/web/__manifest__.py @@ -161,7 +161,6 @@ This module provides the core of the Odoo Web Client. 'web/static/src/legacy/js/core/py_utils.js', 'web/static/src/legacy/js/core/context.js', 'web/static/src/legacy/js/core/data_comparison_utils.js', - 'web/static/src/legacy/js/core/math_utils.js', 'web/static/src/legacy/js/core/misc.js', 'web/static/src/legacy/js/fields/*', 'web/static/src/legacy/js/report/utils.js', diff --git a/addons/web/static/src/core/utils/arrays.js b/addons/web/static/src/core/utils/arrays.js index 79d1d8a9fed..f44326f3e9d 100644 --- a/addons/web/static/src/core/utils/arrays.js +++ b/addons/web/static/src/core/utils/arrays.js @@ -1,5 +1,32 @@ /** @odoo-module **/ +/** + * Same values returned as those returned by cartesian function for case n = 0 + * and n > 1. For n = 1, brackets are put around the unique parameter elements. + * + * @returns {Array} + */ +function _cartesian() { + const args = Array.prototype.slice.call(arguments); + if (args.length === 0) { + return [undefined]; + } + const firstArray = args[0].map(function (elem) { + return [elem]; + }); + if (args.length === 1) { + return firstArray; + } + const productOfOtherArrays = _cartesian.apply(null, args.slice(1)); + return firstArray.reduce(function (acc, elem) { + return acc.concat( + productOfOtherArrays.map(function (tuple) { + return elem.concat(tuple); + }) + ); + }, []); +} + /** * Helper function returning an extraction handler to use on array elements to * return a certain attribute or mutated form of the element. @@ -92,3 +119,38 @@ export function symmetricalDifference(arrayA, arrayB) { .filter((id) => !arrayB.includes(id)) .concat(arrayB.filter((id) => !arrayA.includes(id))); } + +/** + * Returns the product of any number n of arrays. + * The internal structures of their elements is preserved. + * For n = 1, no brackets are put around the unique parameter elements + * For n = 0, [undefined] is returned since it is the unit + * of the cartesian product (up to isomorphism). + * + * @returns {Array} + */ +export function cartesian() { + const args = Array.prototype.slice.call(arguments); + if (args.length === 0) { + return [undefined]; + } else if (args.length === 1) { + return args[0]; + } else { + return _cartesian.apply(null, args); + } +} + +/** + * Returns all initial sections of a given array, e.g. for [1, 2] the array + * [[], [1], [1, 2]] is returned. + * + * @param {Array} array + * @returns {Array[]} + */ +export function sections(array) { + const sections = []; + for (let i = 0; i < array.length + 1; i++) { + sections.push(array.slice(0, i)); + } + return sections; +} diff --git a/addons/web/static/src/legacy/js/core/math_utils.js b/addons/web/static/src/legacy/js/core/math_utils.js deleted file mode 100644 index 6f560624ffc..00000000000 --- a/addons/web/static/src/legacy/js/core/math_utils.js +++ /dev/null @@ -1,73 +0,0 @@ -odoo.define('web.mathUtils', function () { -"use strict"; - -/** - * Same values returned as those returned by cartesian function for case n = 0 - * and n > 1. For n = 1, brackets are put around the unique parameter elements. - * - * @returns {Array} - */ -function _cartesian() { - var args = Array.prototype.slice.call(arguments); - if (args.length === 0) { - return [undefined]; - } - var firstArray = args[0].map(function (elem) { - return [elem]; - }); - if (args.length === 1) { - return firstArray; - } - var productOfOtherArrays = _cartesian.apply(null, args.slice(1)); - var result = firstArray.reduce( - function (acc, elem) { - return acc.concat(productOfOtherArrays.map(function (tuple) { - return elem.concat(tuple); - })); - }, - [] - ); - return result; -} - -/** - * Returns the product of any number n of arrays. - * The internal structures of their elements is preserved. - * For n = 1, no brackets are put around the unique parameter elements - * For n = 0, [undefined] is returned since it is the unit - * of the cartesian product (up to isomorphism). - * - * @returns {Array} - */ -function cartesian() { - var args = Array.prototype.slice.call(arguments); - if (args.length === 0) { - return [undefined]; - } else if (args.length === 1) { - return args[0]; - } else { - return _cartesian.apply(null, args); - } -} - -/** - * Returns all initial sections of a given array, e.g. for [1, 2] the array - * [[], [1], [1, 2]] is returned. - * - * @param {Array} array - * @returns {Array[]} - */ -function sections(array) { - var sections = []; - for (var i = 0; i < array.length + 1; i++) { - sections.push(array.slice(0, i)); - } - return sections; -} - -return { - cartesian: cartesian, - sections: sections, -}; - -}); diff --git a/addons/web/static/src/legacy/js/views/pivot/pivot_model.js b/addons/web/static/src/legacy/js/views/pivot/pivot_model.js index ab5e4de309e..c7c174cda28 100644 --- a/addons/web/static/src/legacy/js/views/pivot/pivot_model.js +++ b/addons/web/static/src/legacy/js/views/pivot/pivot_model.js @@ -275,7 +275,7 @@ import concurrency from 'web.concurrency'; import core from 'web.core'; import dataComparisonUtils from 'web.dataComparisonUtils'; import Domain from 'web.Domain'; -import mathUtils from 'web.mathUtils'; +import * as mathUtils from '@web/core/utils/arrays'; import session from 'web.session'; diff --git a/addons/web/static/tests/core/utils/arrays_tests.js b/addons/web/static/tests/core/utils/arrays_tests.js index 85e7dee61da..6a5fd1f12a0 100644 --- a/addons/web/static/tests/core/utils/arrays_tests.js +++ b/addons/web/static/tests/core/utils/arrays_tests.js @@ -1,6 +1,6 @@ /** @odoo-module **/ -import { groupBy, sortBy } from "@web/core/utils/arrays"; +import { cartesian, groupBy, sortBy } from "@web/core/utils/arrays"; QUnit.module("utils", () => { QUnit.module("Arrays"); @@ -152,4 +152,72 @@ QUnit.module("utils", () => { { x: "a" }, ]); }); + + QUnit.test("cartesian product of zero arrays", function (assert) { + assert.deepEqual(cartesian(), [undefined], "the unit of the product is a singleton"); + }); + + QUnit.test("cartesian product of a single array", function (assert) { + assert.deepEqual(cartesian([]), []); + assert.deepEqual(cartesian([1]), [1], "we don't want unecessary brackets"); + assert.deepEqual(cartesian([1, 2]), [1, 2]); + assert.deepEqual( + cartesian([[1, 2]]), + [[1, 2]], + "the internal structure of elements should be preserved" + ); + assert.deepEqual( + cartesian([ + [1, 2], + [3, [2]], + ]), + [ + [1, 2], + [3, [2]], + ], + "the internal structure of elements should be preserved" + ); + }); + + QUnit.test("cartesian product of two arrays", function (assert) { + assert.deepEqual(cartesian([], []), []); + assert.deepEqual(cartesian([1], []), []); + assert.deepEqual(cartesian([1], [2]), [[1, 2]]); + assert.deepEqual(cartesian([1, 2], [3]), [ + [1, 3], + [2, 3], + ]); + assert.deepEqual( + cartesian([[1], 4], [2, [3]]), + [ + [[1], 2], + [[1], [3]], + [4, 2], + [4, [3]], + ], + "the internal structure of elements should be preserved" + ); + }); + + QUnit.test("cartesian product of three arrays", function (assert) { + assert.deepEqual(cartesian([], [], []), []); + assert.deepEqual(cartesian([1], [], [2, 5]), []); + assert.deepEqual( + cartesian([1], [2], [3]), + [[1, 2, 3]], + "we should have no unecessary brackets, we want elements to be 'triples'" + ); + assert.deepEqual( + cartesian([[1], 2], [3], [4]), + [ + [[1], 3, 4], + [2, 3, 4], + ], + "the internal structure of elements should be preserved" + ); + }); + + QUnit.test("cartesian product of four arrays", function (assert) { + assert.deepEqual(cartesian([1], [2], [3], [4]), [[1, 2, 3, 4]]); + }); }); diff --git a/addons/web/static/tests/legacy/core/math_utils_tests.js b/addons/web/static/tests/legacy/core/math_utils_tests.js deleted file mode 100644 index 1029bdd062d..00000000000 --- a/addons/web/static/tests/legacy/core/math_utils_tests.js +++ /dev/null @@ -1,56 +0,0 @@ -odoo.define('web.math_utils_tests', function(require) { -"use strict"; - -var mathUtils = require('web.mathUtils'); -var cartesian = mathUtils.cartesian; - -QUnit.module('mathUtils', function () { - - QUnit.module('cartesian'); - - - QUnit.test('cartesian product of zero arrays', function(assert) { - assert.expect(1); - assert.deepEqual(cartesian(), [undefined], - "the unit of the product is a singleton"); - }); - - QUnit.test('cartesian product of a single array', function(assert) { - assert.expect(5); - assert.deepEqual(cartesian([]), []); - assert.deepEqual(cartesian([1]), [1], - "we don't want unecessary brackets"); - assert.deepEqual(cartesian([1, 2]), [1, 2]); - assert.deepEqual(cartesian([[1, 2]]), [[1, 2]], - "the internal structure of elements should be preserved"); - assert.deepEqual(cartesian([[1, 2], [3, [2]]]), [[1, 2], [3, [2]]], - "the internal structure of elements should be preserved"); - }); - - QUnit.test('cartesian product of two arrays', function(assert) { - assert.expect(5); - assert.deepEqual(cartesian([], []), []); - assert.deepEqual(cartesian([1], []), []); - assert.deepEqual(cartesian([1], [2]), [[1, 2]]); - assert.deepEqual(cartesian([1, 2], [3]), [[1, 3], [2, 3]]); - assert.deepEqual(cartesian([[1], 4], [2, [3]]), [[[1], 2], [[1], [3]], [4, 2], [4, [3]] ], - "the internal structure of elements should be preserved"); - }); - - QUnit.test('cartesian product of three arrays', function(assert) { - assert.expect(4); - assert.deepEqual(cartesian([], [], []), []); - assert.deepEqual(cartesian([1], [], [2, 5]), []); - assert.deepEqual(cartesian([1], [2], [3]), [[1, 2, 3]], - "we should have no unecessary brackets, we want elements to be 'triples'"); - assert.deepEqual(cartesian([[1], 2], [3], [4]), [[[1], 3, 4], [2, 3, 4]], - "the internal structure of elements should be preserved"); - }); - - QUnit.test('cartesian product of four arrays', function(assert) { - assert.expect(1); - assert.deepEqual(cartesian([1], [2], [3], [4]), [[1, 2, 3, 4]]); - }); - -}); -});