From df570cd859aadf2d3716d596564b5746c044a4b9 Mon Sep 17 00:00:00 2001
From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com>
Date: Thu, 25 Jan 2024 10:07:28 +0100
Subject: [PATCH] refactor (analytics): update summing doc and add test
---
docs/plugin-analytics.md | 69 ++++++++-----
packages/analytics/README.md | 69 ++++++++-----
packages/analytics/src/summing.js | 117 +++++++++++++--------
packages/analytics/test/summing.spec.js | 130 ++++++++++++++++++++++++
packages/analytics/test/tune.spec.js | 13 +--
packages/analytics/test/value.spec.js | 18 +---
6 files changed, 303 insertions(+), 113 deletions(-)
create mode 100644 packages/analytics/test/summing.spec.js
diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md
index bbe1466ea..5ff986f49 100644
--- a/docs/plugin-analytics.md
+++ b/docs/plugin-analytics.md
@@ -1459,48 +1459,65 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
### summing
-Take special `Object` like `{id, value}` and replace `value` with the sum of
-`value`s
+Create an id, value pair from two given path and apply a sum to the value
-```json
-[
- { "id": "A", "value": [1, 1, 1] },
- { "id": "B", "value": [1] },
- { "id": "C", "value": [1, 1, 1, 1] },
-]
-```
+Créer un couple id, value à partir de chemin et applique un somme sur la valeur
-Script:
+#### Example / Exemple
+
+##### Script / Scénario
```ini
+; Import analytics plugin required to use tune
+; Importation du plugin analytique nécessaire pour utiliser tune
[use]
plugin = analytics
-[summing]
+; Using "tune" with default settings
+; Utilisation de "tune" avec les paramètres par défaut
+[tune]
```
-Output:
+##### Input / Entrée
```json
-[{
- "id": "A", "value": 3
-},
-{
- "id": "B",
- "value": 1
-},
-{
- "id": "C",
- "value": 4
-}]
+ [
+ {
+ "id": 1,
+ "value": [1, 1, 1],
+ "hello": "world"
+ },
+ {
+ "id": 2,
+ "value": [2, 2, 2],
+ "hello": "world"
+ }
+ ]
+```
+
+##### Output / Sortie
+
+```json
+[
+ {
+ "id": 1,
+ "value": 3
+ },
+ {
+ "id": 2,
+ "value": 6
+ }
+]
```
#### Parameters
-- `id` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use for id (optional, default `id`)
-- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use for value (optional, default `value`)
+- `id` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
- path of the element used to create the new identifier
+ - chemin de l'élément utilisé pour créer le nouvel identifiant
(optional, default `id`)
+- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** - path of the element to be summed
+ - chemin de l'élément qui doit être sommé
(optional, default `value`)
-Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
+Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), value: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}**
### tune
diff --git a/packages/analytics/README.md b/packages/analytics/README.md
index bbe1466ea..5ff986f49 100644
--- a/packages/analytics/README.md
+++ b/packages/analytics/README.md
@@ -1459,48 +1459,65 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
### summing
-Take special `Object` like `{id, value}` and replace `value` with the sum of
-`value`s
+Create an id, value pair from two given path and apply a sum to the value
-```json
-[
- { "id": "A", "value": [1, 1, 1] },
- { "id": "B", "value": [1] },
- { "id": "C", "value": [1, 1, 1, 1] },
-]
-```
+Créer un couple id, value à partir de chemin et applique un somme sur la valeur
-Script:
+#### Example / Exemple
+
+##### Script / Scénario
```ini
+; Import analytics plugin required to use tune
+; Importation du plugin analytique nécessaire pour utiliser tune
[use]
plugin = analytics
-[summing]
+; Using "tune" with default settings
+; Utilisation de "tune" avec les paramètres par défaut
+[tune]
```
-Output:
+##### Input / Entrée
```json
-[{
- "id": "A", "value": 3
-},
-{
- "id": "B",
- "value": 1
-},
-{
- "id": "C",
- "value": 4
-}]
+ [
+ {
+ "id": 1,
+ "value": [1, 1, 1],
+ "hello": "world"
+ },
+ {
+ "id": 2,
+ "value": [2, 2, 2],
+ "hello": "world"
+ }
+ ]
+```
+
+##### Output / Sortie
+
+```json
+[
+ {
+ "id": 1,
+ "value": 3
+ },
+ {
+ "id": 2,
+ "value": 6
+ }
+]
```
#### Parameters
-- `id` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use for id (optional, default `id`)
-- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use for value (optional, default `value`)
+- `id` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** - path of the element used to create the new identifier
+ - chemin de l'élément utilisé pour créer le nouvel identifiant
(optional, default `id`)
+- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** - path of the element to be summed
+ - chemin de l'élément qui doit être sommé
(optional, default `value`)
-Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
+Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), value: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}**
### tune
diff --git a/packages/analytics/src/summing.js b/packages/analytics/src/summing.js
index 01c46de2f..ba127f22f 100644
--- a/packages/analytics/src/summing.js
+++ b/packages/analytics/src/summing.js
@@ -1,58 +1,95 @@
import get from 'lodash.get';
import core from './core';
+
/**
- * Take special `Object` like `{id, value}` and replace `value` with the sum of
- * `value`s
+ * Summing function see documentation at the end.
+ * This part of the doc is use for jsdoc typing
+ * @private
+ * @param data {unknown}
+ * @param feed {Feed}
+ * @param ctx
+ */
+const summing = (data, feed, ctx) => {
+ if (ctx.isLast()) {
+ feed.close();
+ return;
+ }
+
+ const id = get(data, ctx.getParam('id', 'id')) || ctx.getIndex();
+ const value = get(data, ctx.getParam('value', 'value'));
+
+ const values = Array.isArray(value) ? value : [value];
+
+ if (id && value) {
+ feed.write(core(id, values.reduce((sum, x) => sum + Number(x), 0)));
+ }
+
+ feed.end();
+};
+
+/**
+ * Create an id, value pair from two given path and apply a sum to the value
*
- * ```json
- * [
- * { "id": "A", "value": [1, 1, 1] },
- * { "id": "B", "value": [1] },
- * { "id": "C", "value": [1, 1, 1, 1] },
- * ]
- * ```
+ * Créer un couple id, value à partir de chemin et applique un somme sur la valeur
*
- * Script:
+ * ### Example / Exemple
+ *
+ * #### Script / Scénario
*
* ```ini
+ * ; Import analytics plugin required to use tune
+ * ; Importation du plugin analytique nécessaire pour utiliser tune
* [use]
* plugin = analytics
*
- * [summing]
+ * ; Using "tune" with default settings
+ * ; Utilisation de "tune" avec les paramètres par défaut
+ * [tune]
*
* ```
*
- * Output:
+ * #### Input / Entrée
*
* ```json
- * [{
- * "id": "A", "value": 3
- * },
- * {
- * "id": "B",
- * "value": 1
- * },
- * {
- * "id": "C",
- * "value": 4
- * }]
- * ```
+ * [
+ * {
+ * "id": 1,
+ * "value": [1, 1, 1],
+ * "hello": "world"
+ * },
+ * {
+ * "id": 2,
+ * "value": [2, 2, 2],
+ * "hello": "world"
+ * }
+ * ]
+ * ```
+ *
+ * #### Output / Sortie
+ *
+ * ```json
+ * [
+ * {
+ * "id": 1,
+ * "value": 3
+ * },
+ * {
+ * "id": 2,
+ * "value": 6
+ * }
+ * ]
+ * ```
*
* @name summing
- * @param {String} [id=id] path to use for id
- * @param {String} [value=value] path to use for value
- * @returns {Object}
+ * @param {String} [id=id]
+ * - path of the element used to create the new identifier
+ * - chemin de l'élément utilisé pour créer le nouvel identifiant
+ * @param {String} [value=value]
+ * - path of the element to be summed
+ * - chemin de l'élément qui doit être sommé
+ * @returns {{
+ * id: String,
+ * value: Object
+ * }}
*/
-export default function summing(data, feed) {
- if (this.isLast()) {
- feed.close();
- return;
- }
- const id = get(data, this.getParam('id', 'id')) || this.getIndex();
- const value = get(data, this.getParam('value', 'value'));
- const values = Array.isArray(value) ? value : [value];
- if (id && value) {
- feed.write(core(id, values.reduce((sum, x) => sum + Number(x), 0)));
- }
- feed.end();
-}
+export default summing;
diff --git a/packages/analytics/test/summing.spec.js b/packages/analytics/test/summing.spec.js
new file mode 100644
index 000000000..7769c1960
--- /dev/null
+++ b/packages/analytics/test/summing.spec.js
@@ -0,0 +1,130 @@
+import ezs from '../../core/src';
+import summing from '../src/summing';
+import runEzs from '../test-utils/runEzs';
+
+ezs.addPath(__dirname);
+
+describe('summing', () => {
+
+ beforeAll(() => {
+ ezs.use({ summing });
+ });
+
+ describe('summing, simple object', () => {
+ const simpleData = [
+ {
+ 'id': 1,
+ 'value': [1, 1, 1]
+ },
+ {
+ 'id': 2,
+ 'value': [2, 2, 2]
+ }
+ ];
+
+ it('should sum value (id = id, value = value)', async () => {
+ const result = await runEzs(ezs, simpleData, 'summing');
+
+ expect(result).not.toBeNull();
+
+ expect(result).toHaveLength(2);
+
+ expect(result[0]).not.toBeNull();
+ expect(result[0].id).not.toBeNull();
+ expect(result[0].id).toStrictEqual(1);
+ expect(result[0].value).not.toBeNull();
+ expect(result[0].value).toStrictEqual(3);
+
+ expect(result[1]).not.toBeNull();
+ expect(result[1].id).not.toBeNull();
+ expect(result[1].id).toStrictEqual(2);
+ expect(result[1].value).not.toBeNull();
+ expect(result[1].value).toStrictEqual(6);
+ });
+
+ it('should not sum id (id = value, value = id)', async () => {
+ const result = await runEzs(ezs, simpleData, 'summing', {
+ id : 'value',
+ value: 'id',
+ });
+
+ expect(result).not.toBeNull();
+
+ expect(result).toHaveLength(2);
+
+ expect(result[0]).not.toBeNull();
+ expect(result[0].id).not.toBeNull();
+ expect(result[0].id).toStrictEqual([1, 1, 1]);
+ expect(result[0].value).not.toBeNull();
+ expect(result[0].value).toStrictEqual(1);
+
+ expect(result[1]).not.toBeNull();
+ expect(result[1].id).not.toBeNull();
+ expect(result[1].id).toStrictEqual([2, 2, 2]);
+ expect(result[1].value).not.toBeNull();
+ expect(result[1].value).toStrictEqual(2);
+ });
+ });
+
+ describe('summing, noisy object', () => {
+ const noisyData = [
+ {
+ 'id': 2,
+ 'value': [10, 10, 10],
+ 'hello': 'world'
+ },
+ {
+ 'id': 4,
+ 'value': [20, 20, 20],
+ 'hello': 'world'
+ }
+ ];
+
+ it('should sum value and remove noise (id = id, value = value)', async () => {
+ const result = await runEzs(ezs, noisyData, 'summing');
+
+ expect(result).not.toBeNull();
+
+ expect(result).toHaveLength(2);
+
+ expect(result[0]).not.toBeNull();
+ expect(result[0].id).not.toBeNull();
+ expect(result[0].id).toStrictEqual(2);
+ expect(result[0].value).not.toBeNull();
+ expect(result[0].value).toStrictEqual(30);
+ expect(result[0]).not.toHaveProperty('hello');
+
+ expect(result[1]).not.toBeNull();
+ expect(result[1].id).not.toBeNull();
+ expect(result[1].id).toStrictEqual(4);
+ expect(result[1].value).not.toBeNull();
+ expect(result[1].value).toStrictEqual(60);
+ expect(result[1]).not.toHaveProperty('hello');
+ });
+
+ it('should not sum id and remove noise (id = value, value = id)', async () => {
+ const result = await runEzs(ezs, noisyData, 'summing', {
+ id : 'value',
+ value: 'id',
+ });
+
+ expect(result).not.toBeNull();
+
+ expect(result).toHaveLength(2);
+
+ expect(result[0]).not.toBeNull();
+ expect(result[0].id).not.toBeNull();
+ expect(result[0].id).toStrictEqual([10, 10, 10]);
+ expect(result[0].value).not.toBeNull();
+ expect(result[0].value).toStrictEqual(2);
+ expect(result[0]).not.toHaveProperty('hello');
+
+ expect(result[1]).not.toBeNull();
+ expect(result[1].id).not.toBeNull();
+ expect(result[1].id).toStrictEqual([20, 20, 20]);
+ expect(result[1].value).not.toBeNull();
+ expect(result[1].value).toStrictEqual(4);
+ expect(result[1]).not.toHaveProperty('hello');
+ });
+ });
+});
\ No newline at end of file
diff --git a/packages/analytics/test/tune.spec.js b/packages/analytics/test/tune.spec.js
index 8f18d7d6f..e82109071 100644
--- a/packages/analytics/test/tune.spec.js
+++ b/packages/analytics/test/tune.spec.js
@@ -5,6 +5,11 @@ import runEzs from '../test-utils/runEzs';
ezs.addPath(__dirname);
describe('tune', () => {
+
+ beforeAll(() => {
+ ezs.use({ tune });
+ });
+
describe('normalize, simple object', () => {
const simpleData = [
{
@@ -18,8 +23,6 @@ describe('tune', () => {
];
it('should normalize (path = id)', async () => {
- ezs.use({ tune });
-
const result = await runEzs(ezs, simpleData, 'tune');
expect(result[0]).not.toBeNull();
@@ -33,8 +36,6 @@ describe('tune', () => {
});
it('should normalize (path = value)', async () => {
- ezs.use({ tune });
-
const result = await runEzs(ezs, simpleData, 'tune', { path: 'value' });
expect(result[0]).not.toBeNull();
@@ -61,8 +62,6 @@ describe('tune', () => {
];
it('should normalize (path = id)', async () => {
- ezs.use({ tune });
-
const result = await runEzs(ezs, arrayData, 'tune');
expect(result[0]).not.toBeNull();
@@ -76,8 +75,6 @@ describe('tune', () => {
});
it('should normalize (path = value)', async () => {
- ezs.use({ tune });
-
const result = await runEzs(ezs, arrayData, 'tune', { path: 'value' });
expect(result[0]).not.toBeNull();
diff --git a/packages/analytics/test/value.spec.js b/packages/analytics/test/value.spec.js
index 75eca870e..4fcbed8bf 100644
--- a/packages/analytics/test/value.spec.js
+++ b/packages/analytics/test/value.spec.js
@@ -5,6 +5,11 @@ import runEzs from '../test-utils/runEzs';
ezs.addPath(__dirname);
describe('value', () => {
+
+ beforeAll(() => {
+ ezs.use({ value });
+ });
+
describe('simple data', () => {
const simpleData = [
{
@@ -18,7 +23,6 @@ describe('value', () => {
];
it('should extract the id (path = id)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, simpleData, 'value', { path: 'id' });
expect(result).toHaveLength(2);
@@ -28,7 +32,6 @@ describe('value', () => {
});
it('should extract the value (path = value)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, simpleData, 'value', { path: 'value' });
expect(result).toHaveLength(2);
@@ -38,7 +41,6 @@ describe('value', () => {
});
it('should extract the value (path = undefined)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, simpleData, 'value');
expect(result).toHaveLength(2);
@@ -61,7 +63,6 @@ describe('value', () => {
];
it('should extract the id (path = id)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, arrayData, 'value', { path: 'id' });
expect(result).toHaveLength(2);
@@ -71,7 +72,6 @@ describe('value', () => {
});
it('should extract the value (path = value)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, arrayData, 'value', { path: 'value' });
expect(result).toHaveLength(2);
@@ -98,7 +98,6 @@ describe('value', () => {
];
it('should extract the id (path = id)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, objectData, 'value', { path: 'id' });
expect(result).toHaveLength(2);
@@ -108,7 +107,6 @@ describe('value', () => {
});
it('should extract the value (path = value)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, objectData, 'value', { path: 'value' });
expect(result).toHaveLength(2);
@@ -155,7 +153,6 @@ describe('value', () => {
];
it('should extract the id (path = id)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, deepObjectData, 'value', { path: 'id' });
expect(result).toHaveLength(2);
@@ -165,7 +162,6 @@ describe('value', () => {
});
it('should extract the value (path = value)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, deepObjectData, 'value', { path: 'value' });
expect(result).toHaveLength(2);
@@ -247,7 +243,6 @@ describe('value', () => {
];
it('should extract the id (path = id)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, animalia100Data, 'value', { path: 'id' });
expect(result).toHaveLength(5);
@@ -260,7 +255,6 @@ describe('value', () => {
});
it('should extract the value (path = value)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, animalia100Data, 'value', { path: 'value' });
expect(result).toHaveLength(5);
@@ -309,7 +303,6 @@ describe('value', () => {
});
it('should extract the id (path = id)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, genratedData, 'value', { path: 'id' });
expect(result).toHaveLength(LENGTH);
@@ -320,7 +313,6 @@ describe('value', () => {
});
it('should extract the value (path = value)', async () => {
- ezs.use({ value });
const result = await runEzs(ezs, genratedData, 'value', { path: 'value' });
expect(result).toHaveLength(LENGTH);