From a6cd6d5a892f5a0501c0e81fe583bd64befff5d0 Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Fri, 19 Jun 2020 07:06:28 +0000 Subject: [IMP] web: utils: add groupBy function This commit introduces a new utility function: 'groupBy'. It is similar in API and result as the underscore method of the same name. X-original-commit: 834a1967161dc537cb2d5543314a062dbac08f9c --- addons/web/static/tests/core/util_tests.js | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'addons/web/static/tests/core/util_tests.js') diff --git a/addons/web/static/tests/core/util_tests.js b/addons/web/static/tests/core/util_tests.js index da6ac0d7737..6115833e4f2 100644 --- a/addons/web/static/tests/core/util_tests.js +++ b/addons/web/static/tests/core/util_tests.js @@ -7,6 +7,59 @@ QUnit.module('core', {}, function () { QUnit.module('utils'); + QUnit.test('groupBy', function (assert) { + assert.expect(7); + + const { groupBy } = utils; + + // Invalid + assert.throws( + () => groupBy({}), + new TypeError(`list is not iterable`) + ); + assert.throws( + () => groupBy([], true), + new Error(`Expected criterion of type 'string' or 'function' and got 'boolean'`) + ); + assert.throws( + () => groupBy([], 3), + new Error(`Expected criterion of type 'string' or 'function' and got 'number'`) + ); + assert.throws( + () => groupBy([], {}), + new Error(`Expected criterion of type 'string' or 'function' and got 'object'`) + ); + + // criterion = default + assert.deepEqual( + groupBy(["a", "b", 1, true]), + { + 1: [1], + a: ["a"], + b: ["b"], + true: [true], + } + ); + // criterion = string + assert.deepEqual( + groupBy([{ x: "a" }, { x: "a" }, { x: "b" }], "x"), + { + a: [{ x: "a" }, { x: "a" }], + b: [{ x: "b" }], + } + ); + // criterion = function + assert.deepEqual( + groupBy(["a", "b", 1, true], (x) => `el${x}`), + { + ela: ["a"], + elb: ["b"], + el1: [1], + eltrue: [true], + } + ); + }); + QUnit.test('intersperse', function (assert) { assert.expect(27); -- cgit v1.2.3