-
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.
Merge pull request #388 from Inist-CNRS/docs-tests-analytics
Cleanup analytics docs and test
- Loading branch information
Showing
18 changed files
with
2,647 additions
and
1,362 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,68 +1,95 @@ | ||
import { get } from 'lodash'; | ||
import core from './core'; | ||
|
||
/** | ||
* Take `Object` object getting value of fields (with json `path`) and throws an | ||
* object for each value | ||
* Pluck function see documentation at the end. | ||
* This part of the doc is used for jsdoc typing | ||
* @private | ||
* @param data {unknown} | ||
* @param feed {Feed} | ||
* @param ctx {import('../../core/src/engine').EngineScope} | ||
*/ | ||
const pluck = (data, feed, ctx) => { | ||
if (ctx.isLast()) { | ||
feed.close(); | ||
return; | ||
} | ||
let fields = ctx.getParam('path', 'id'); | ||
if (!Array.isArray(fields)) { | ||
fields = [fields]; | ||
} | ||
|
||
fields | ||
.filter((k) => typeof k === 'string') | ||
.map((key) => [key, get(data, key)]) | ||
.filter((x) => x[1]) | ||
.map((item) => ([item[0], (item[1] instanceof Array ? item[1] : [item[1]])])) | ||
.reduce((prev, cur) => prev.concat(cur[1].map((x) => ([cur[0], x]))), []) | ||
.forEach((item) => feed.write(core(item[0], item[1]))); | ||
feed.end(); | ||
}; | ||
|
||
/** | ||
* Extract the value of a given `path` and create a pair with the `path` as the `id` | ||
* and `path` value as the `value`. | ||
* | ||
* ```json | ||
* [ | ||
* { city: 'tokyo', year: 2000, count: 1 }, | ||
* { city: 'paris', year: 2001, count: 2 }, | ||
* { city: 'london', year: 2003, count: 3 }, | ||
* { city: 'nancy', year: 2005, count: 4 }, | ||
* { city: 'berlin', year: 2007, count: 5 }, | ||
* { city: 'madrid', year: 2009, count: 6 }, | ||
* { city: 'stockholm', year: 2011, count: 7 }, | ||
* { city: 'bruxelles', year: 2013, count: 8 }, | ||
* ] | ||
* ``` | ||
* Extrais la valeur d'un `path` donnée et créer un couple avec pour identifient le `path` | ||
* et comme `value` la valeur du `path`. | ||
* | ||
* Script: | ||
* ### Example / Exemple | ||
* | ||
* #### Script / Scénario | ||
* | ||
* ```ini | ||
* ; Import analytics plugin required to use "pluck" | ||
* ; Importation du plugin analytique nécessaire pour utiliser "pluck" | ||
* [use] | ||
* plugin = analytics | ||
* | ||
* ; Using "pluck" with 'year' as path setttings instead of 'id' how is the default value | ||
* ; Utilisation de "pluck" avec 'year' comme paramètres de path au lieux de la valeur par defaut qui et 'id' | ||
* [pluck] | ||
* path = year | ||
* | ||
* ``` | ||
* | ||
* Output: | ||
* #### Input / Entrée | ||
* | ||
* ```json | ||
* [ | ||
* { "city": "tokyo", "year": 2000, "count": 1 }, | ||
* { "city": "paris", "year": 2001, "count": 2 }, | ||
* { "city": "london", "year": 2003, "count": 3 }, | ||
* { "city": "nancy", "year": 2005, "count": 4 }, | ||
* { "city": "berlin", "year": 2007, "count": 5 }, | ||
* { "city": "madrid", "year": 2009, "count": 6 }, | ||
* { "city": "stockholm", "year": 2011, "count": 7 }, | ||
* { "city": "bruxelles", "year": 2013, "count": 8 } | ||
* ] | ||
* ``` | ||
* | ||
* #### Output / Sortie | ||
* | ||
* ```json | ||
* [ | ||
* { "id": "year", "value": 2000 }, | ||
* { "id": "year", "value": 2001 }, | ||
* { "id": "year", "value": 2003 }, | ||
* { "id": "year", "value": 2005 }, | ||
* { "id": "year", "value": 2007 }, | ||
* { "id": "year", "value": 2009 }, | ||
* { "id": "year", "value": 2011 }, | ||
* { "id": "year", "value": 2013 } | ||
* ] | ||
* [ | ||
* { "id": "year", "value": 2000 }, | ||
* { "id": "year", "value": 2001 }, | ||
* { "id": "year", "value": 2003 }, | ||
* { "id": "year", "value": 2005 }, | ||
* { "id": "year", "value": 2007 }, | ||
* { "id": "year", "value": 2009 }, | ||
* { "id": "year", "value": 2011 }, | ||
* { "id": "year", "value": 2013 } | ||
* ] | ||
* ``` | ||
* | ||
* @name pluck | ||
* @param {String} [path=id] path to use form group by | ||
* @returns {Object} | ||
* @param {String} [path=id] | ||
* <ul><li>path of the element who need to be extrated</li></ul> | ||
* <ul><li>chemin de l'élément qui doit être extrais</li></ul> | ||
* @returns {{ | ||
* id: String, | ||
* value: Object | ||
* }} | ||
*/ | ||
export default function pluck(data, feed) { | ||
if (this.isLast()) { | ||
feed.close(); | ||
return; | ||
} | ||
let fields = this.getParam('path', 'id'); | ||
if (!Array.isArray(fields)) { | ||
fields = [fields]; | ||
} | ||
|
||
fields | ||
.filter((k) => typeof k === 'string') | ||
.map((key) => [key, get(data, key)]) | ||
.filter((x) => x[1]) | ||
.map((item) => ([item[0], (item[1] instanceof Array ? item[1] : [item[1]])])) | ||
.reduce((prev, cur) => prev.concat(cur[1].map((x) => ([cur[0], x]))), []) | ||
.forEach((item) => feed.write(core(item[0], item[1]))); | ||
feed.end(); | ||
} | ||
export default pluck; |
Oops, something went wrong.