[IMP] web: migrate math helpers

We move some util functions from the legacy file math_utils.js to the
file arrays.js. Those funcions will be used in the new implementation of
the pivot view.
This commit is contained in:
Aaron Bohy
2021-08-16 12:53:51 +00:00
committed by Mathieu Duckerts-Antoine
parent b134c1e45e
commit 24c643bc4f
6 changed files with 132 additions and 132 deletions
-1
View File
@@ -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',
@@ -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;
}
@@ -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,
};
});
@@ -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';
@@ -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]]);
});
});
@@ -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]]);
});
});
});