diff options
| author | Mathieu Duckerts-Antoine <dam@odoo.com> | 2019-04-23 15:05:27 +0000 |
|---|---|---|
| committer | Mathieu Duckerts-Antoine <dam@odoo.com> | 2019-08-02 18:34:18 +0000 |
| commit | 5894df57d34c8d9d41db9bf901502d42dac9d884 (patch) | |
| tree | ef999088a48b8b0f2f846c84f304939cdccaea1a /addons/web/static/src | |
| parent | e488f208d21b7cff86d22ac12e894d953ad65943 (diff) | |
[IMP] web: add math_utils to core
math_utils is a small library that exports two
functions:
- cartesian: allows to compute the cartesian
products of any numbers of arrays
while preserving the internal
structures of their elements.
- sections: returns an array with all
the initial sections of the
array passed as parameter.
Diffstat (limited to 'addons/web/static/src')
| -rw-r--r-- | addons/web/static/src/js/core/math_utils.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/addons/web/static/src/js/core/math_utils.js b/addons/web/static/src/js/core/math_utils.js new file mode 100644 index 00000000000..0eeb631125b --- /dev/null +++ b/addons/web/static/src/js/core/math_utils.js @@ -0,0 +1,73 @@ +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, the singleton [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, +}; + +}); |
