-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor (analytics): update summing doc and add test
- Loading branch information
1 parent
e5399fe
commit df570cd
Showing
6 changed files
with
303 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.