Skip to content

Commit

Permalink
refactor (analytics): update summing doc and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlasDiablo committed Jan 25, 2024
1 parent e5399fe commit df570cd
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 113 deletions.
69 changes: 43 additions & 26 deletions docs/plugin-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)** <ul><li>path of the element used to create the new identifier</li></ul>
<ul><li>chemin de l'élément utilisé pour créer le nouvel identifiant</li></ul> (optional, default `id`)
- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** <ul><li>path of the element to be summed</li></ul>
<ul><li>chemin de l'élément qui doit être sommé</li></ul> (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

Expand Down
69 changes: 43 additions & 26 deletions packages/analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)** <ul><li>path of the element used to create the new identifier</li></ul>
<ul><li>chemin de l'élément utilisé pour créer le nouvel identifiant</li></ul> (optional, default `id`)
- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** <ul><li>path of the element to be summed</li></ul>
<ul><li>chemin de l'élément qui doit être sommé</li></ul> (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

Expand Down
117 changes: 77 additions & 40 deletions packages/analytics/src/summing.js
Original file line number Diff line number Diff line change
@@ -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]
* <ul><li>path of the element used to create the new identifier</li></ul>
* <ul><li>chemin de l'élément utilisé pour créer le nouvel identifiant</li></ul>
* @param {String} [value=value]
* <ul><li>path of the element to be summed</li></ul>
* <ul><li>chemin de l'élément qui doit être sommé</li></ul>
* @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;
Loading

0 comments on commit df570cd

Please sign in to comment.