From 640c8e36c2ce9011d5121d669e716491749bef4f Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:43:22 +0100 Subject: [PATCH 01/26] refactor: first draft of a small docs and test cleanup --- docs/plugin-analytics.md | 67 +++++++++++++++++++++----- packages/analytics/README.md | 67 +++++++++++++++++++++----- packages/analytics/src/tune.js | 70 +++++++++++++++++++++++----- packages/analytics/test/tune.spec.js | 42 +++++++++++++++++ 4 files changed, 211 insertions(+), 35 deletions(-) create mode 100644 packages/analytics/test/tune.spec.js diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index 1cf8d07ce..5cb3366c1 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1504,34 +1504,77 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### tune -Take all `Object` and sort them with selected field +Create and replace the id with a unify id that can be used with [sort](#sort) -```json -[{ -}] -``` +Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort) -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 +; Using "tune" with default settings +; Utilisation de "tune" avec les paramettre par defaut [tune] ``` -Output: +##### Input / Entrée ```json -[ -] + [ + { + "id": 1, + "value": 1 + }, + { + "id": 2, + "value":2 + } + ] ``` -#### Parameters +##### Output / Sortie + +```json + [ + { + "id": "0000000000000000001.00000000000000000000", + "value": { + "id": 1, + "value": 1, + "label": "static value" + } + }, + { + "id": "0000000000000000002.00000000000000000000", + "value": { + "id": 2, + "value": 2, + "label": "static value" + } + } + ] +``` -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use for the sort key (optional, default `id`) +#### Parameters -Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** + (optional, default `id`) +- `method` **(`"natural"` \| `"levenshtein"` \| `"numerical"`)** + + + + + + + (optional, default `natural`) + +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)}** ### value diff --git a/packages/analytics/README.md b/packages/analytics/README.md index 1cf8d07ce..5cb3366c1 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1504,34 +1504,77 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### tune -Take all `Object` and sort them with selected field +Create and replace the id with a unify id that can be used with [sort](#sort) -```json -[{ -}] -``` +Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort) -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 +; Using "tune" with default settings +; Utilisation de "tune" avec les paramettre par defaut [tune] ``` -Output: +##### Input / Entrée ```json -[ -] + [ + { + "id": 1, + "value": 1 + }, + { + "id": 2, + "value":2 + } + ] ``` -#### Parameters +##### Output / Sortie + +```json + [ + { + "id": "0000000000000000001.00000000000000000000", + "value": { + "id": 1, + "value": 1, + "label": "static value" + } + }, + { + "id": "0000000000000000002.00000000000000000000", + "value": { + "id": 2, + "value": 2, + "label": "static value" + } + } + ] +``` -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use for the sort key (optional, default `id`) +#### Parameters -Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** + (optional, default `id`) +- `method` **(`"natural"` \| `"levenshtein"` \| `"numerical"`)** + + + + + + + (optional, default `natural`) + +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)}** ### value diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index 8a3ebf525..6d4d1f894 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -23,33 +23,81 @@ const methods = { const allMethods = Object.keys(methods).join(','); /** - * Take all `Object` and sort them with selected field + * Create and replace the id with a unify id that can be used with [sort](#sort) * - * ```json - * [{ - * }] - * ``` + * Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort) * - * 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 * + * ; Using "tune" with default settings + * ; Utilisation de "tune" avec les paramettre par defaut * [tune] * * ``` * - * Output: + * #### Input / Entrée + * + * ```json + * [ + * { + * "id": 1, + * "value": 1 + * }, + * { + * "id": 2, + * "value": 2 + * } + * ] + * ``` + * + * #### Output / Sortie * * ```json - * [ - * ] + * [ + * { + * "id": "0000000000000000001.00000000000000000000", + * "value": { + * "id": 1, + * "value": 1, + * "label": "static value" + * } + * }, + * { + * "id": "0000000000000000002.00000000000000000000", + * "value": { + * "id": 2, + * "value": 2, + * "label": "static value" + * } + * } + * ] * ``` * * @name tune - * @param {String} [path=id] path to use for the sort key - * @returns {Object} + * @param {String} [path=id] + * + * + * @param {'natural' | 'levenshtein' | 'numerical'} [method=natural] + * + * + * + * + * + * + * + * + * @returns {{ + * id: String, + * value: Object + * }} */ export default function tune(data, feed) { if (this.isLast()) { diff --git a/packages/analytics/test/tune.spec.js b/packages/analytics/test/tune.spec.js new file mode 100644 index 000000000..f2d9ca8e7 --- /dev/null +++ b/packages/analytics/test/tune.spec.js @@ -0,0 +1,42 @@ +import ezs from '@ezs/core/src'; +import from from 'from'; +import assert from 'assert'; +import tune from '../src/tune'; + +ezs.addPath(__dirname); + +const simpleObject = [ + { + id: 1, + value: 1 + }, + { + id: 2, + value: 2 + } +]; + +describe('tune', () => { + it('should normalize simple object', async () => { + ezs.use({ tune }); + + const res = await (new Promise((resolve) => { + const result = []; + from(simpleObject) + .pipe(ezs('tune')) + .on('data', (chunk) => { + assert(typeof chunk === 'object'); + result.push(chunk); + }) + .on('end', () => { + resolve(result); + }); + })); + + assert.equal(res[0].id, '0000000000000000001.00000000000000000000'); + assert.equal(res[1].id, '0000000000000000002.00000000000000000000'); + + assert.equal(res[0].value, simpleObject[0]); + assert.equal(res[1].value, simpleObject[1]); + }); +}); \ No newline at end of file From 7deb2a015abe7a74878733689b123de7a969e77a Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Thu, 14 Dec 2023 09:20:19 +0100 Subject: [PATCH 02/26] test (analytics): add different data set to tune --- packages/analytics/test/tune.spec.js | 139 ++++++++++++++++++++++----- 1 file changed, 113 insertions(+), 26 deletions(-) diff --git a/packages/analytics/test/tune.spec.js b/packages/analytics/test/tune.spec.js index f2d9ca8e7..54aa91866 100644 --- a/packages/analytics/test/tune.spec.js +++ b/packages/analytics/test/tune.spec.js @@ -5,38 +5,125 @@ import tune from '../src/tune'; ezs.addPath(__dirname); -const simpleObject = [ +const simpleData = [ { - id: 1, - value: 1 + 'id': 1, + 'value': 1 }, { - id: 2, - value: 2 + 'id': 2, + 'value': 2 + } +]; + +const arrayData = [ + { + 'id': 1, + 'value': [1, 1] + }, + { + 'id': 2, + 'value': [2, 2] } ]; describe('tune', () => { - it('should normalize simple object', async () => { - ezs.use({ tune }); - - const res = await (new Promise((resolve) => { - const result = []; - from(simpleObject) - .pipe(ezs('tune')) - .on('data', (chunk) => { - assert(typeof chunk === 'object'); - result.push(chunk); - }) - .on('end', () => { - resolve(result); - }); - })); - - assert.equal(res[0].id, '0000000000000000001.00000000000000000000'); - assert.equal(res[1].id, '0000000000000000002.00000000000000000000'); - - assert.equal(res[0].value, simpleObject[0]); - assert.equal(res[1].value, simpleObject[1]); + + describe('normalize, simple object', () => { + + it('should normalize (path = id)', async () => { + ezs.use({ tune }); + + const res = await (new Promise((resolve) => { + const result = []; + from(simpleData) + .pipe(ezs('tune')) + .on('data', (chunk) => { + assert(typeof chunk === 'object'); + result.push(chunk); + }) + .on('end', () => { + resolve(result); + }); + })); + + assert.equal(res[0].id, '0000000000000000001.00000000000000000000'); + assert.equal(res[1].id, '0000000000000000002.00000000000000000000'); + + assert.equal(res[0].value, simpleData[0]); + assert.equal(res[1].value, simpleData[1]); + }); + + it('should normalize (path = value)', async () => { + ezs.use({ tune }); + + const res = await (new Promise((resolve) => { + const result = []; + from(simpleData) + .pipe(ezs('tune', { path: 'value' })) + .on('data', (chunk) => { + assert(typeof chunk === 'object'); + result.push(chunk); + }) + .on('end', () => { + resolve(result); + }); + })); + + assert.equal(res[0].id, '0000000000000000001.00000000000000000000'); + assert.equal(res[1].id, '0000000000000000002.00000000000000000000'); + + assert.equal(res[0].value, simpleData[0]); + assert.equal(res[1].value, simpleData[1]); + }); + }); + + describe('normalize, array object', () => { + + it('should normalize (path = id)', async () => { + ezs.use({ tune }); + + const res = await (new Promise((resolve) => { + const result = []; + from(arrayData) + .pipe(ezs('tune')) + .on('data', (chunk) => { + assert(typeof chunk === 'object'); + result.push(chunk); + }) + .on('end', () => { + resolve(result); + }); + })); + + assert.equal(res[0].id, '0000000000000000001.00000000000000000000'); + assert.equal(res[1].id, '0000000000000000002.00000000000000000000'); + + assert.equal(res[0].value, arrayData[0]); + assert.equal(res[1].value, arrayData[1]); + }); + + it('should normalize (path = value)', async () => { + ezs.use({ tune }); + + const res = await (new Promise((resolve) => { + const result = []; + from(arrayData) + .pipe(ezs('tune', { path: 'value' })) + .on('data', (chunk) => { + assert(typeof chunk === 'object'); + result.push(chunk); + }) + .on('end', () => { + resolve(result); + }); + })); + + assert.equal(res[0].id, '1.1'); + assert.equal(res[1].id, '2.2'); + + assert.equal(res[0].value, arrayData[0]); + assert.equal(res[1].value, arrayData[1]); + }); }); }); \ No newline at end of file From ae6669803ac0900bbf96d0814ced83b990ba915f Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Thu, 14 Dec 2023 09:29:38 +0100 Subject: [PATCH 03/26] test (analytics): fix typo --- packages/analytics/test/tune.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/analytics/test/tune.spec.js b/packages/analytics/test/tune.spec.js index 54aa91866..8bb0d4a4d 100644 --- a/packages/analytics/test/tune.spec.js +++ b/packages/analytics/test/tune.spec.js @@ -119,8 +119,8 @@ describe('tune', () => { }); })); - assert.equal(res[0].id, '1.1'); - assert.equal(res[1].id, '2.2'); + assert.equal(res[0].id, '1,1'); + assert.equal(res[1].id, '2,2'); assert.equal(res[0].value, arrayData[0]); assert.equal(res[1].value, arrayData[1]); From 75f352139205fa02d3b7a968b10bb532057fb998 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 24 Jan 2024 15:09:18 +0100 Subject: [PATCH 04/26] docs (analytics): update tune doc --- packages/analytics/src/tune.js | 102 +++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 37 deletions(-) diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index 6d4d1f894..072c32cf8 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -3,6 +3,11 @@ import clone from 'lodash.clone'; import { levenshteinDistance } from './algorithms'; import core from './core'; +/** + * @private + * @param s {string | number | unknown} + * @returns {string} + */ export const normalize = (s) => { if (typeof s === 'string') { return String(s).normalize('NFD').replace(/[\u0300-\u036f]/g, '').padEnd(40, '~'); @@ -13,7 +18,20 @@ export const normalize = (s) => { return String(s); }; +/** + * @private + * @param x {string | number | unknown} + * @param y {string | number | unknown} + * @returns {*} + */ const levenshtein = (x, y) => levenshteinDistance(normalize(x), normalize(y)); + +/** + * @private + * @param x {number} + * @param y {number} + * @returns {number} + */ const numerical = (x, y) => (x + 1) / (y + 1); const methods = { @@ -22,6 +40,52 @@ const methods = { }; const allMethods = Object.keys(methods).join(','); +/** + * Tune function see documenation at the end. + * This part of the doc is use for jsdoc typing + * @private + * @param data {unknown} + * @param feed + * @param ctx + */ +const tune = (data, feed, ctx) => { + if (ctx.isLast()) { + feed.close(); + return; + } + const path = ctx.getParam('path', 'id'); + const method = ctx.getParam('method', 'natural'); + const fields = Array.isArray(path) ? path : [path]; + const currentValue = fields + .filter((k) => typeof k === 'string') + .map((key) => get(data, key)) + .shift(); + + if (!methods[method] && method !== 'natural') { + throw new Error(`Invalid parameter 'method'. Accepted values are : ${allMethods}`); + } + + if (method === 'natural') { + feed.send(core(normalize(currentValue), data)); + return; + } + + if (!ctx.previousValue) { + ctx.previousValue = currentValue; + ctx.previousDistance = 1; + feed.send(core(1, data)); + return; + } + + const similarity = methods[method](ctx.previousValue, currentValue); + const score = similarity === 0 ? Math.max(ctx.previousValue.length, currentValue.length) : similarity; + const currentDistance = ctx.previousDistance / score; + + ctx.previousValue = clone(currentValue); + ctx.previousDistance = clone(currentDistance); + feed.send(core(currentDistance, data)); +}; + /** * Create and replace the id with a unify id that can be used with [sort](#sort) * @@ -99,43 +163,7 @@ const allMethods = Object.keys(methods).join(','); * value: Object * }} */ -export default function tune(data, feed) { - if (this.isLast()) { - feed.close(); - return; - } - const path = this.getParam('path', 'id'); - const method = this.getParam('method', 'natural'); - const fields = Array.isArray(path) ? path : [path]; - const currentValue = fields - .filter((k) => typeof k === 'string') - .map((key) => get(data, key)) - .shift(); - - if (!methods[method] && method !== 'natural') { - throw new Error(`Invalid parameter 'method'. Accepted values are : ${allMethods}`); - } - - if (method === 'natural') { - feed.send(core(normalize(currentValue), data)); - return; - } - - if (!this.previousValue) { - this.previousValue = currentValue; - this.previousDistance = 1; - feed.send(core(1, data)); - return; - } - - const similarity = methods[method](this.previousValue, currentValue); - const score = similarity === 0 ? Math.max(this.previousValue.length, currentValue.length) : similarity; - const currentDistance = this.previousDistance / score; - - this.previousValue = clone(currentValue); - this.previousDistance = clone(currentDistance); - feed.send(core(currentDistance, data)); -} +export default tune; /* From eb7b3a324ea85466e0aab8802590fab156881883 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 24 Jan 2024 15:10:58 +0100 Subject: [PATCH 05/26] docs (analytics): fix typo --- packages/analytics/src/tune.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index 072c32cf8..a56be4237 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -102,7 +102,7 @@ const tune = (data, feed, ctx) => { * plugin = analytics * * ; Using "tune" with default settings - * ; Utilisation de "tune" avec les paramettre par defaut + * ; Utilisation de "tune" avec les paramètres par défaut * [tune] * * ``` @@ -155,9 +155,9 @@ const tune = (data, feed, ctx) => { * * * - * - * - * + * + * + * * @returns {{ * id: String, * value: Object From 5797e4f0c436131f318f65d9b23aeb530a01c010 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 24 Jan 2024 15:27:07 +0100 Subject: [PATCH 06/26] docs (analytics): generify tests --- packages/analytics/test/tune.spec.js | 160 +++++++++--------------- packages/analytics/test/utils/runEzs.js | 23 ++++ packages/analytics/test/value.spec.js | 47 +++---- 3 files changed, 99 insertions(+), 131 deletions(-) create mode 100644 packages/analytics/test/utils/runEzs.js diff --git a/packages/analytics/test/tune.spec.js b/packages/analytics/test/tune.spec.js index 8bb0d4a4d..de09a0412 100644 --- a/packages/analytics/test/tune.spec.js +++ b/packages/analytics/test/tune.spec.js @@ -1,129 +1,93 @@ -import ezs from '@ezs/core/src'; -import from from 'from'; -import assert from 'assert'; +import ezs from '../../core/src'; import tune from '../src/tune'; +import runEzs from './utils/runEzs'; ezs.addPath(__dirname); -const simpleData = [ - { - 'id': 1, - 'value': 1 - }, - { - 'id': 2, - 'value': 2 - } -]; - -const arrayData = [ - { - 'id': 1, - 'value': [1, 1] - }, - { - 'id': 2, - 'value': [2, 2] - } -]; - describe('tune', () => { - describe('normalize, simple object', () => { + const simpleData = [ + { + 'id': 1, + 'value': 3 + }, + { + 'id': 2, + 'value': 4 + } + ]; it('should normalize (path = id)', async () => { ezs.use({ tune }); - const res = await (new Promise((resolve) => { - const result = []; - from(simpleData) - .pipe(ezs('tune')) - .on('data', (chunk) => { - assert(typeof chunk === 'object'); - result.push(chunk); - }) - .on('end', () => { - resolve(result); - }); - })); - - assert.equal(res[0].id, '0000000000000000001.00000000000000000000'); - assert.equal(res[1].id, '0000000000000000002.00000000000000000000'); - - assert.equal(res[0].value, simpleData[0]); - assert.equal(res[1].value, simpleData[1]); + const result = await runEzs(ezs, simpleData, 'tune'); + + expect(result[0]).not.toBeNull(); + expect(result[1]).not.toBeNull(); + + expect(result[0].id).toStrictEqual('0000000000000000001.00000000000000000000'); + expect(result[1].id).toStrictEqual('0000000000000000002.00000000000000000000'); + + expect(result[0].value).toStrictEqual(simpleData[0]); + expect(result[1].value).toStrictEqual(simpleData[1]); }); it('should normalize (path = value)', async () => { ezs.use({ tune }); - const res = await (new Promise((resolve) => { - const result = []; - from(simpleData) - .pipe(ezs('tune', { path: 'value' })) - .on('data', (chunk) => { - assert(typeof chunk === 'object'); - result.push(chunk); - }) - .on('end', () => { - resolve(result); - }); - })); - - assert.equal(res[0].id, '0000000000000000001.00000000000000000000'); - assert.equal(res[1].id, '0000000000000000002.00000000000000000000'); - - assert.equal(res[0].value, simpleData[0]); - assert.equal(res[1].value, simpleData[1]); + const result = await runEzs(ezs, simpleData, 'tune', { path: 'value' }); + + expect(result[0]).not.toBeNull(); + expect(result[1]).not.toBeNull(); + + expect(result[0].id).toStrictEqual('0000000000000000003.00000000000000000000'); + expect(result[1].id).toStrictEqual('0000000000000000004.00000000000000000000'); + + expect(result[0].value).toStrictEqual(simpleData[0]); + expect(result[1].value).toStrictEqual(simpleData[1]); }); }); describe('normalize, array object', () => { + const arrayData = [ + { + 'id': 1, + 'value': [3, 4] + }, + { + 'id': 2, + 'value': [5, 6] + } + ]; it('should normalize (path = id)', async () => { ezs.use({ tune }); - const res = await (new Promise((resolve) => { - const result = []; - from(arrayData) - .pipe(ezs('tune')) - .on('data', (chunk) => { - assert(typeof chunk === 'object'); - result.push(chunk); - }) - .on('end', () => { - resolve(result); - }); - })); - - assert.equal(res[0].id, '0000000000000000001.00000000000000000000'); - assert.equal(res[1].id, '0000000000000000002.00000000000000000000'); - - assert.equal(res[0].value, arrayData[0]); - assert.equal(res[1].value, arrayData[1]); + const result = await runEzs(ezs, arrayData, 'tune'); + + expect(result[0]).not.toBeNull(); + expect(result[1]).not.toBeNull(); + + expect(result[0].id).toStrictEqual('0000000000000000001.00000000000000000000'); + expect(result[1].id).toStrictEqual('0000000000000000002.00000000000000000000'); + + expect(result[0].value).toStrictEqual(arrayData[0]); + expect(result[1].value).toStrictEqual(arrayData[1]); }); it('should normalize (path = value)', async () => { ezs.use({ tune }); - const res = await (new Promise((resolve) => { - const result = []; - from(arrayData) - .pipe(ezs('tune', { path: 'value' })) - .on('data', (chunk) => { - assert(typeof chunk === 'object'); - result.push(chunk); - }) - .on('end', () => { - resolve(result); - }); - })); - - assert.equal(res[0].id, '1,1'); - assert.equal(res[1].id, '2,2'); - - assert.equal(res[0].value, arrayData[0]); - assert.equal(res[1].value, arrayData[1]); + const result = await runEzs(ezs, arrayData, 'tune'); + + expect(result[0]).not.toBeNull(); + expect(result[1]).not.toBeNull(); + + expect(result[0].id).toStrictEqual('3,4'); + expect(result[1].id).toStrictEqual('5,6'); + + expect(result[0].value).toStrictEqual(arrayData[0]); + expect(result[1].value).toStrictEqual(arrayData[1]); }); }); }); \ No newline at end of file diff --git a/packages/analytics/test/utils/runEzs.js b/packages/analytics/test/utils/runEzs.js new file mode 100644 index 000000000..bb72bd07b --- /dev/null +++ b/packages/analytics/test/utils/runEzs.js @@ -0,0 +1,23 @@ +import from from 'from'; + +/** + * Helper function use to call ezs in each tests + * @param ezsRuntime {(name: string, options: any, environment?: unknown) => NodeJS.WritableStream} + * @param dataSet {Array} + * @param functionName {string} + * @param [options] {any} + * @returns {Promise>} + */ +const runEzs = (ezsRuntime, dataSet, functionName, options) => new Promise((resolve) => { + const result = []; + from(dataSet) + .pipe(ezsRuntime(functionName, options)) + .on('data', (chunk) => { + result.push(chunk); + }) + .on('end', () => { + resolve(result); + }); +}); + +export default runEzs; \ No newline at end of file diff --git a/packages/analytics/test/value.spec.js b/packages/analytics/test/value.spec.js index 75d6bb712..1f38bb957 100644 --- a/packages/analytics/test/value.spec.js +++ b/packages/analytics/test/value.spec.js @@ -1,29 +1,10 @@ -import from from 'from'; import ezs from '../../core/src'; import value from '../src/value'; +import runEzs from './utils/runEzs'; ezs.addPath(__dirname); describe('value', () => { - /** - * Helper function use to call ezs in each tests - * @param ezsRuntime {(name: string, options: any, environment?: unknown) => NodeJS.WritableStream} - * @param dataSet {Array} - * @param path {string | undefined} - * @returns {Promise>} - */ - const runEzs = (ezsRuntime, dataSet, path) => new Promise((resolve) => { - const result = []; - from(dataSet) - .pipe(ezsRuntime('value', { path })) - .on('data', (chunk) => { - result.push(chunk); - }) - .on('end', () => { - resolve(result); - }); - }); - describe('simple data', () => { const simpleData = [ { @@ -38,7 +19,7 @@ describe('value', () => { it('should extract the id (path = id)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, simpleData, 'id'); + const result = await runEzs(ezs, simpleData, 'value', { path: 'id' }); expect(result).toHaveLength(2); @@ -48,7 +29,7 @@ describe('value', () => { it('should extract the value (path = value)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, simpleData, 'value'); + const result = await runEzs(ezs, simpleData, 'value', { path: 'value' }); expect(result).toHaveLength(2); @@ -58,7 +39,7 @@ describe('value', () => { it('should extract the value (path = undefined)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, simpleData); + const result = await runEzs(ezs, simpleData, 'value'); expect(result).toHaveLength(2); @@ -81,7 +62,7 @@ describe('value', () => { it('should extract the id (path = id)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, arrayData, 'id'); + const result = await runEzs(ezs, arrayData, 'value', { path: 'id' }); expect(result).toHaveLength(2); @@ -91,7 +72,7 @@ describe('value', () => { it('should extract the value (path = value)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, arrayData, 'value'); + const result = await runEzs(ezs, arrayData, 'value', { path: 'value' }); expect(result).toHaveLength(2); @@ -118,7 +99,7 @@ describe('value', () => { it('should extract the id (path = id)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, objectData, 'id'); + const result = await runEzs(ezs, objectData, 'value', { path: 'id' }); expect(result).toHaveLength(2); @@ -128,7 +109,7 @@ describe('value', () => { it('should extract the value (path = value)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, objectData, 'value'); + const result = await runEzs(ezs, objectData, 'value', { path: 'value' }); expect(result).toHaveLength(2); @@ -175,7 +156,7 @@ describe('value', () => { it('should extract the id (path = id)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, deepObjectData, 'id'); + const result = await runEzs(ezs, deepObjectData, 'value', { path: 'id' }); expect(result).toHaveLength(2); @@ -185,7 +166,7 @@ describe('value', () => { it('should extract the value (path = value)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, deepObjectData, 'value'); + const result = await runEzs(ezs, deepObjectData, 'value', { path: 'value' }); expect(result).toHaveLength(2); @@ -267,7 +248,7 @@ describe('value', () => { it('should extract the id (path = id)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, animalia100Data, 'id'); + const result = await runEzs(ezs, animalia100Data, 'value', { path: 'id' }); expect(result).toHaveLength(5); @@ -280,7 +261,7 @@ describe('value', () => { it('should extract the value (path = value)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, animalia100Data, 'value'); + const result = await runEzs(ezs, animalia100Data, 'value', { path: 'value' }); expect(result).toHaveLength(5); @@ -329,7 +310,7 @@ describe('value', () => { it('should extract the id (path = id)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, genratedData, 'id'); + const result = await runEzs(ezs, genratedData, 'value', { path: 'id' }); expect(result).toHaveLength(LENGTH); @@ -340,7 +321,7 @@ describe('value', () => { it('should extract the value (path = value)', async () => { ezs.use({ value }); - const result = await runEzs(ezs, genratedData, 'value'); + const result = await runEzs(ezs, genratedData, 'value', { path: 'value' }); expect(result).toHaveLength(LENGTH); From e95c6d339bc67c520e6cefcd4d914b8b76ba1a32 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 24 Jan 2024 15:35:06 +0100 Subject: [PATCH 07/26] docs (analytics): generated doc --- docs/plugin-analytics.md | 10 +++++----- packages/analytics/README.md | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index 5cb3366c1..e2f525f9b 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1519,7 +1519,7 @@ Créer et remplacer l'identifiant par un identifiant unifié qui peut être util plugin = analytics ; Using "tune" with default settings -; Utilisation de "tune" avec les paramettre par defaut +; Utilisation de "tune" avec les paramètres par défaut [tune] ``` @@ -1533,7 +1533,7 @@ plugin = analytics }, { "id": 2, - "value":2 + "value": 2 } ] ``` @@ -1570,9 +1570,9 @@ plugin = analytics
    • levenshtein - Create an identifier based on the Levenshtein algorithm
    • numerical - Create an identifier based on a numeric value
  • méthode utilisée pour créer l'identifiant unifié
-
    • natural - Créer un identifiant normalisé de longueur fixe
-
    • levenshtein - Créer un identifiant basé sur l'algorithme de Levenshtein
-
    • numerical - Créer un identifiant basé sur une valeur numérique
(optional, default `natural`) +
    • natural - Crée un identifiant normalisé de longueur fixe
+
    • levenshtein - Crée un identifiant basé sur l'algorithme de Levenshtein
+
    • numerical - Crée un identifiant basé sur une valeur numérique
(optional, default `natural`) 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)}** diff --git a/packages/analytics/README.md b/packages/analytics/README.md index 5cb3366c1..e2f525f9b 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1519,7 +1519,7 @@ Créer et remplacer l'identifiant par un identifiant unifié qui peut être util plugin = analytics ; Using "tune" with default settings -; Utilisation de "tune" avec les paramettre par defaut +; Utilisation de "tune" avec les paramètres par défaut [tune] ``` @@ -1533,7 +1533,7 @@ plugin = analytics }, { "id": 2, - "value":2 + "value": 2 } ] ``` @@ -1570,9 +1570,9 @@ plugin = analytics
    • levenshtein - Create an identifier based on the Levenshtein algorithm
    • numerical - Create an identifier based on a numeric value
  • méthode utilisée pour créer l'identifiant unifié
-
    • natural - Créer un identifiant normalisé de longueur fixe
-
    • levenshtein - Créer un identifiant basé sur l'algorithme de Levenshtein
-
    • numerical - Créer un identifiant basé sur une valeur numérique
(optional, default `natural`) +
    • natural - Crée un identifiant normalisé de longueur fixe
+
    • levenshtein - Crée un identifiant basé sur l'algorithme de Levenshtein
+
    • numerical - Crée un identifiant basé sur une valeur numérique
(optional, default `natural`) 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)}** From 3084e284b5e9c18f43dd661ef2babb4ef48d2e37 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 24 Jan 2024 15:35:30 +0100 Subject: [PATCH 08/26] test (analytics): fix test --- packages/analytics/{test/utils => test-utils}/runEzs.js | 0 packages/analytics/test/tune.spec.js | 4 ++-- packages/analytics/test/value.spec.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename packages/analytics/{test/utils => test-utils}/runEzs.js (100%) diff --git a/packages/analytics/test/utils/runEzs.js b/packages/analytics/test-utils/runEzs.js similarity index 100% rename from packages/analytics/test/utils/runEzs.js rename to packages/analytics/test-utils/runEzs.js diff --git a/packages/analytics/test/tune.spec.js b/packages/analytics/test/tune.spec.js index de09a0412..8f18d7d6f 100644 --- a/packages/analytics/test/tune.spec.js +++ b/packages/analytics/test/tune.spec.js @@ -1,6 +1,6 @@ import ezs from '../../core/src'; import tune from '../src/tune'; -import runEzs from './utils/runEzs'; +import runEzs from '../test-utils/runEzs'; ezs.addPath(__dirname); @@ -78,7 +78,7 @@ describe('tune', () => { it('should normalize (path = value)', async () => { ezs.use({ tune }); - const result = await runEzs(ezs, arrayData, 'tune'); + const result = await runEzs(ezs, arrayData, 'tune', { path: 'value' }); expect(result[0]).not.toBeNull(); expect(result[1]).not.toBeNull(); diff --git a/packages/analytics/test/value.spec.js b/packages/analytics/test/value.spec.js index 1f38bb957..75eca870e 100644 --- a/packages/analytics/test/value.spec.js +++ b/packages/analytics/test/value.spec.js @@ -1,6 +1,6 @@ import ezs from '../../core/src'; import value from '../src/value'; -import runEzs from './utils/runEzs'; +import runEzs from '../test-utils/runEzs'; ezs.addPath(__dirname); From 41d61e2c676e5727581823d90751542cd2602127 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Thu, 25 Jan 2024 08:11:29 +0100 Subject: [PATCH 09/26] refactor (analytics): update value --- docs/plugin-analytics.md | 109 +++++++++++++++++------ packages/analytics/README.md | 109 +++++++++++++++++------ packages/analytics/src/tune.js | 2 +- packages/analytics/src/value.js | 152 +++++++++++++++++++++++--------- 4 files changed, 274 insertions(+), 98 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index e2f525f9b..d2d6533e8 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1578,48 +1578,103 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### value -Take `Object` object and getting the value field +Create a new object from the value of the given path -```json -[ - { id: 2000, value: 1 }, - { id: 2001, value: 2 }, - { id: 2003, value: 3 }, - { id: 2005, value: 4 }, - { id: 2007, value: 5 }, - { id: 2009, value: 6 }, - { id: 2011, value: 7 }, - { id: 2013, value: 8 }, -] -``` +Créer un nouvel objet à partir de la valeur d'un chemin donnée -Script: +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use value +; Importation du plugin analytique nécessaire pour utiliser value [use] plugin = analytics +; Using "value" with default settings +; Utilisation de "tune" avec les paramètres par défaut [value] -path = id ``` -Output: +##### Input / Entrée + +###### Dataset 1 / Jeu de donnée 1 ```json -[ -2000, -2001, -2003, -2005, -2007, -2009, -2011, -2013 -] + [ + { "id": 2000, "value": 1 }, + { "id": 2001, "value": 2 }, + { "id": 2003, "value": 3 }, + { "id": 2005, "value": 4 }, + { "id": 2007, "value": 5 }, + { "id": 2009, "value": 6 }, + { "id": 2011, "value": 7 }, + { "id": 2013, "value": 8 }, + ] +``` + +###### Dataset 2 / Jeu de donnée 2 + +```json + [ + { + "id": 1, + "value": { + "hello": "world" + } + }, + { + "id": 2, + "value": { + "hello": "ezs" + } + }, + { + "id": 3, + "value": { + "hello": "lodex" + } + }, + ] +``` + +##### Output / Sortie + +###### Dataset 1 / Jeu de donnée 1 + +```json + [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8 + ] +``` + +###### Dataset 2 / Jeu de donnée 2 + +```json + [ + { + "hello": "world" + }, + { + "hello": "ezs" + }, + { + "hello": "lodex" + } + ] ``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the pah of the value field (optional, default `value`) +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to create the new object
+
  • chemin de l'élément utilisé pour créer le nouvel objet
(optional, default `value`) Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** diff --git a/packages/analytics/README.md b/packages/analytics/README.md index e2f525f9b..d2d6533e8 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1578,48 +1578,103 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### value -Take `Object` object and getting the value field +Create a new object from the value of the given path -```json -[ - { id: 2000, value: 1 }, - { id: 2001, value: 2 }, - { id: 2003, value: 3 }, - { id: 2005, value: 4 }, - { id: 2007, value: 5 }, - { id: 2009, value: 6 }, - { id: 2011, value: 7 }, - { id: 2013, value: 8 }, -] -``` +Créer un nouvel objet à partir de la valeur d'un chemin donnée -Script: +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use value +; Importation du plugin analytique nécessaire pour utiliser value [use] plugin = analytics +; Using "value" with default settings +; Utilisation de "tune" avec les paramètres par défaut [value] -path = id ``` -Output: +##### Input / Entrée + +###### Dataset 1 / Jeu de donnée 1 ```json -[ -2000, -2001, -2003, -2005, -2007, -2009, -2011, -2013 -] + [ + { "id": 2000, "value": 1 }, + { "id": 2001, "value": 2 }, + { "id": 2003, "value": 3 }, + { "id": 2005, "value": 4 }, + { "id": 2007, "value": 5 }, + { "id": 2009, "value": 6 }, + { "id": 2011, "value": 7 }, + { "id": 2013, "value": 8 }, + ] +``` + +###### Dataset 2 / Jeu de donnée 2 + +```json + [ + { + "id": 1, + "value": { + "hello": "world" + } + }, + { + "id": 2, + "value": { + "hello": "ezs" + } + }, + { + "id": 3, + "value": { + "hello": "lodex" + } + }, + ] +``` + +##### Output / Sortie + +###### Dataset 1 / Jeu de donnée 1 + +```json + [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8 + ] +``` + +###### Dataset 2 / Jeu de donnée 2 + +```json + [ + { + "hello": "world" + }, + { + "hello": "ezs" + }, + { + "hello": "lodex" + } + ] ``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the pah of the value field (optional, default `value`) +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to create the new object
+
  • chemin de l'élément utilisé pour créer le nouvel objet
(optional, default `value`) Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index a56be4237..d15ecaa0e 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -45,7 +45,7 @@ const allMethods = Object.keys(methods).join(','); * This part of the doc is use for jsdoc typing * @private * @param data {unknown} - * @param feed + * @param feed {Feed} * @param ctx */ const tune = (data, feed, ctx) => { diff --git a/packages/analytics/src/value.js b/packages/analytics/src/value.js index c6c95642c..253afeae3 100644 --- a/packages/analytics/src/value.js +++ b/packages/analytics/src/value.js @@ -1,64 +1,130 @@ import get from 'lodash.get'; /** - * Take `Object` object and getting the value field + * Value function see documenation at the end. + * This part of the doc is use for jsdoc typing + * @private + * @param data {unknown} + * @param feed {Feed} + * @param ctx + */ +const value = (data, feed, ctx) => { + if (ctx.isLast()) { + feed.close(); + return; + } + const path = ctx.getParam('path', 'value'); + const fields = Array.isArray(path) ? path : [path]; + + const val = fields + .filter((k) => typeof k === 'string') + .map((key) => get(data, key)) + .filter((x) => x !== undefined) + .filter((x) => x !== null)[0]; + + feed.send(val); +}; + +/** + * Create a new object from the value of the given path * - * ```json - * [ - * { id: 2000, value: 1 }, - * { id: 2001, value: 2 }, - * { id: 2003, value: 3 }, - * { id: 2005, value: 4 }, - * { id: 2007, value: 5 }, - * { id: 2009, value: 6 }, - * { id: 2011, value: 7 }, - * { id: 2013, value: 8 }, - * ] - * ``` + * Créer un nouvel objet à partir de la valeur d'un chemin donnée * - * Script: + * ### Example / Exemple + * + * #### Script / Scénario * * ```ini + * ; Import analytics plugin required to use value + * ; Importation du plugin analytique nécessaire pour utiliser value * [use] * plugin = analytics * + * ; Using "value" with default settings + * ; Utilisation de "tune" avec les paramètres par défaut * [value] - * path = id * * ``` * - * Output: + * #### Input / Entrée + * + * ##### Dataset 1 / Jeu de donnée 1 + * + * ```json + * [ + * { "id": 2000, "value": 1 }, + * { "id": 2001, "value": 2 }, + * { "id": 2003, "value": 3 }, + * { "id": 2005, "value": 4 }, + * { "id": 2007, "value": 5 }, + * { "id": 2009, "value": 6 }, + * { "id": 2011, "value": 7 }, + * { "id": 2013, "value": 8 }, + * ] + * ``` + * + * ##### Dataset 2 / Jeu de donnée 2 + * + * ```json + * [ + * { + * "id": 1, + * "value": { + * "hello": "world" + * } + * }, + * { + * "id": 2, + * "value": { + * "hello": "ezs" + * } + * }, + * { + * "id": 3, + * "value": { + * "hello": "lodex" + * } + * }, + * ] + * ``` + * + * #### Output / Sortie + * + * ##### Dataset 1 / Jeu de donnée 1 * * ```json - * [ - * 2000, - * 2001, - * 2003, - * 2005, - * 2007, - * 2009, - * 2011, - * 2013 - * ] + * [ + * 1, + * 2, + * 3, + * 4, + * 5, + * 6, + * 7, + * 8 + * ] + * ``` + * + * ##### Dataset 2 / Jeu de donnée 2 + * + * ```json + * [ + * { + * "hello": "world" + * }, + * { + * "hello": "ezs" + * }, + * { + * "hello": "lodex" + * } + * ] * ``` * * @name value - * @param {String} [path=value] the pah of the value field + * @param {String} [path=value] + *
  • path of the element used to create the new object
+ *
  • chemin de l'élément utilisé pour créer le nouvel objet
* @returns {Object} */ -export default function value(data, feed) { - if (this.isLast()) { - feed.close(); - return; - } - const path = this.getParam('path', 'value'); - const fields = Array.isArray(path) ? path : [path]; - - const val = fields - .filter((k) => typeof k === 'string') - .map((key) => get(data, key)) - .filter((x) => x !== undefined) - .filter((x) => x !== null)[0]; - - feed.send(val); -} +export default value; From 4182d475119c8fdd02ab832787f1e40b2e2844a4 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Thu, 25 Jan 2024 08:12:56 +0100 Subject: [PATCH 10/26] docs (analytics): fix typo in json example --- docs/plugin-analytics.md | 4 ++-- packages/analytics/README.md | 4 ++-- packages/analytics/src/value.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index d2d6533e8..bbe1466ea 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1610,7 +1610,7 @@ plugin = analytics { "id": 2007, "value": 5 }, { "id": 2009, "value": 6 }, { "id": 2011, "value": 7 }, - { "id": 2013, "value": 8 }, + { "id": 2013, "value": 8 } ] ``` @@ -1635,7 +1635,7 @@ plugin = analytics "value": { "hello": "lodex" } - }, + } ] ``` diff --git a/packages/analytics/README.md b/packages/analytics/README.md index d2d6533e8..bbe1466ea 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1610,7 +1610,7 @@ plugin = analytics { "id": 2007, "value": 5 }, { "id": 2009, "value": 6 }, { "id": 2011, "value": 7 }, - { "id": 2013, "value": 8 }, + { "id": 2013, "value": 8 } ] ``` @@ -1635,7 +1635,7 @@ plugin = analytics "value": { "hello": "lodex" } - }, + } ] ``` diff --git a/packages/analytics/src/value.js b/packages/analytics/src/value.js index 253afeae3..fabcf5ed0 100644 --- a/packages/analytics/src/value.js +++ b/packages/analytics/src/value.js @@ -59,7 +59,7 @@ const value = (data, feed, ctx) => { * { "id": 2007, "value": 5 }, * { "id": 2009, "value": 6 }, * { "id": 2011, "value": 7 }, - * { "id": 2013, "value": 8 }, + * { "id": 2013, "value": 8 } * ] * ``` * @@ -84,7 +84,7 @@ const value = (data, feed, ctx) => { * "value": { * "hello": "lodex" * } - * }, + * } * ] * ``` * From e5399fef0a1dca7fecc68fb065a2cdbf01b9c416 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Thu, 25 Jan 2024 08:13:41 +0100 Subject: [PATCH 11/26] docs (analytics): fix typo --- packages/analytics/src/tune.js | 2 +- packages/analytics/src/value.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index d15ecaa0e..644ff28c2 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -41,7 +41,7 @@ const methods = { const allMethods = Object.keys(methods).join(','); /** - * Tune function see documenation at the end. + * Tune function see documentation at the end. * This part of the doc is use for jsdoc typing * @private * @param data {unknown} diff --git a/packages/analytics/src/value.js b/packages/analytics/src/value.js index fabcf5ed0..efbdbb5a7 100644 --- a/packages/analytics/src/value.js +++ b/packages/analytics/src/value.js @@ -1,7 +1,7 @@ import get from 'lodash.get'; /** - * Value function see documenation at the end. + * Value function see documentation at the end. * This part of the doc is use for jsdoc typing * @private * @param data {unknown} 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 12/26] 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); From dc721a2743e5702a5cdd182a704db39b6c5f2f0d Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Thu, 25 Jan 2024 15:16:57 +0100 Subject: [PATCH 13/26] test (analytics): add test how send badly structured data note: all test with skip are test who fail because of badly handle data --- packages/analytics/test/summing.spec.js | 56 +++++++++++++++++++++++++ packages/analytics/test/tune.spec.js | 56 +++++++++++++++++++++++++ packages/analytics/test/value.spec.js | 56 +++++++++++++++++++++++++ 3 files changed, 168 insertions(+) diff --git a/packages/analytics/test/summing.spec.js b/packages/analytics/test/summing.spec.js index 7769c1960..c90a1daf4 100644 --- a/packages/analytics/test/summing.spec.js +++ b/packages/analytics/test/summing.spec.js @@ -10,6 +10,62 @@ describe('summing', () => { ezs.use({ summing }); }); + describe('wrong data', () => { + const undefinedData = [ + undefined, + undefined, + undefined, + undefined + ]; + + const nullData = [ + null, + null, + null, + null + ]; + + const undefinedNullData = [ + undefined, + null, + undefined, + null + ]; + + const wrongKeyData = [ + { + 'hello': 'world', + }, + { + 'hello': 'world', + } + ]; + + it('should return no result when input contains undefined', async () => { + const result = await runEzs(ezs, undefinedData, 'summing'); + + expect(result).toHaveLength(0); + }); + + it.skip('should return no result when input contains null', async () => { + const result = await runEzs(ezs, nullData, 'summing'); + + expect(result).toHaveLength(0); + }); + + it.skip('should return no result when input contains undefined and null', async () => { + const result = await runEzs(ezs, undefinedNullData, 'summing'); + + expect(result).toHaveLength(0); + }); + + it('should return no result when input contains wrong key', async () => { + const result = await runEzs(ezs, wrongKeyData, 'summing'); + + expect(result).toHaveLength(0); + }); + }); + describe('summing, simple object', () => { const simpleData = [ { diff --git a/packages/analytics/test/tune.spec.js b/packages/analytics/test/tune.spec.js index e82109071..84ed593b3 100644 --- a/packages/analytics/test/tune.spec.js +++ b/packages/analytics/test/tune.spec.js @@ -10,6 +10,62 @@ describe('tune', () => { ezs.use({ tune }); }); + describe('wrong data', () => { + const undefinedData = [ + undefined, + undefined, + undefined, + undefined + ]; + + const nullData = [ + null, + null, + null, + null + ]; + + const undefinedNullData = [ + undefined, + null, + undefined, + null + ]; + + const wrongKeyData = [ + { + 'hello': 'world', + }, + { + 'hello': 'world', + } + ]; + + it.skip('should return no result when input contains undefined', async () => { + const result = await runEzs(ezs, undefinedData, 'tune'); + + expect(result).toHaveLength(0); + }); + + it.skip('should return no result when input contains null', async () => { + const result = await runEzs(ezs, nullData, 'tune'); + + expect(result).toHaveLength(0); + }); + + it.skip('should return no result when input contains undefined and null', async () => { + const result = await runEzs(ezs, undefinedNullData, 'tune'); + + expect(result).toHaveLength(0); + }); + + it.skip('should return no result when input contains wrong key', async () => { + const result = await runEzs(ezs, wrongKeyData, 'tune'); + + expect(result).toHaveLength(0); + }); + }); + describe('normalize, simple object', () => { const simpleData = [ { diff --git a/packages/analytics/test/value.spec.js b/packages/analytics/test/value.spec.js index 4fcbed8bf..43ca2daea 100644 --- a/packages/analytics/test/value.spec.js +++ b/packages/analytics/test/value.spec.js @@ -10,6 +10,62 @@ describe('value', () => { ezs.use({ value }); }); + describe('wrong data', () => { + const undefinedData = [ + undefined, + undefined, + undefined, + undefined + ]; + + const nullData = [ + null, + null, + null, + null + ]; + + const undefinedNullData = [ + undefined, + null, + undefined, + null + ]; + + const wrongKeyData = [ + { + 'hello': 'world', + }, + { + 'hello': 'world', + } + ]; + + it('should return no result when input contains undefined', async () => { + const result = await runEzs(ezs, undefinedData, 'value'); + + expect(result).toHaveLength(0); + }); + + it.skip('should return no result when input contains null', async () => { + const result = await runEzs(ezs, nullData, 'value'); + + expect(result).toHaveLength(0); + }); + + it.skip('should return no result when input contains undefined and null', async () => { + const result = await runEzs(ezs, undefinedNullData, 'value'); + + expect(result).toHaveLength(0); + }); + + it('should return no result when input contains wrong key', async () => { + const result = await runEzs(ezs, wrongKeyData, 'value'); + + expect(result).toHaveLength(0); + }); + }); + describe('simple data', () => { const simpleData = [ { From 8ddd5398e62a6d3b054eaf35c23edcc090fe1e41 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:34:46 +0100 Subject: [PATCH 14/26] docs (analytics): add sort and statistics doc and fix some typo --- docs/plugin-analytics.md | 266 ++++++++++++++------------- packages/analytics/README.md | 266 ++++++++++++++------------- packages/analytics/src/sort.js | 153 +++++++++------ packages/analytics/src/statistics.js | 263 ++++++++++++++------------ packages/analytics/src/summing.js | 14 +- packages/analytics/src/tune.js | 2 + packages/analytics/src/value.js | 1 + 7 files changed, 531 insertions(+), 434 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index 5ff986f49..e2b2818fe 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1298,162 +1298,175 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### sort -Take all `Object` and sort them with dedicated key +Sort incomming object base on the value of the given path -```json -[{ - { id: 2000, value: 1 }, - { id: 2001, value: 2 }, - { id: 2003, value: 3 }, - { id: 2005, value: 4 }, - { id: 2007, value: 5 }, - { id: 2009, value: 6 }, - { id: 2011, value: 7 }, - { id: 2013, value: 8 }, -}] -``` +Trier les objets entrants sur la base de la valeur du chemin donné -Script: +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use tune +; Importation du plugin analytique nécessaire pour utiliser sort [use] plugin = analytics +; Using "sort" with default settings +; Utilisation de "sort" avec les paramètres par défaut [sort] -path = value -reverse = true +; path = id +; reverse = false ``` -Output: +##### Input / Entrée ```json -[ -{ "id": 2013, "value": 8 }, -{ "id": 2011, "value": 7 }, -{ "id": 2009, "value": 6 }, -{ "id": 2007, "value": 5 }, -{ "id": 2005, "value": 4 }, -{ "id": 2003, "value": 3 }, -{ "id": 2001, "value": 2 }, -{ "id": 2000, "value": 1 } -] + [ + { "id": 2013, "value": 8 }, + { "id": 2011, "value": 7 }, + { "id": 2009, "value": 6 }, + { "id": 2007, "value": 5 }, + { "id": 2005, "value": 4 }, + { "id": 2003, "value": 3 }, + { "id": 2001, "value": 2 }, + { "id": 2000, "value": 1 } + ] ``` -#### Parameters - -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use for id (optional, default `id`) -- `reverse` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** reverser order (optional, default `false`) - -Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** - -### statistics +##### Output / Sortie -Compute some statistics from one or more fields +```json + [ + { "id": 2000, "value": 1 }, + { "id": 2001, "value": 2 }, + { "id": 2003, "value": 3 }, + { "id": 2005, "value": 4 }, + { "id": 2007, "value": 5 }, + { "id": 2009, "value": 6 }, + { "id": 2011, "value": 7 }, + { "id": 2013, "value": 8 }, + ] +``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path of the value field (optional, default `value`) -- `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path of statistics in output object (optional, default `_statistics`) +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to as reference for the sort
+
  • chemin de l'élément utilisé comme reference pour le trie
(optional, default `id`) +- `reverse` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
  • sort in ascending or descending order
+
  • trier par ordre croissant ou décroissant
(optional, default `false`) -#### Examples +Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** -Input +### statistics +Analyse and create statistics from given fields -````javascript -```json -[ - { a: 1, }, - { a: 1, }, - { a: 2, }, - { a: 3, }, - { a: 3, }, - { a: 3, }, -] -``` -```` +Analisse et créer des statistiques a partir des champs donnée -Script +#### Example / Exemple +##### Script / Scénario -````javascript ```ini +; Import analytics plugin required to use statistics +; Importation du plugin analytique nécessaire pour utiliser statistics [use] plugin = analytics +; Using "statistics" with default settings +; Utilisation de "statistics" avec les paramètres par défaut [statistics] -path = a - +; path = value +; target = _statistics ``` -```` -Output +##### Input / Entrée + +```json + [ + { "value": 1 }, + { "value": 1 }, + { "value": 2 }, + { "value": 3 }, + { "value": 3 }, + { "value": 3 } + ] +``` +##### Output / Sortie -````javascript ```json -[{ - "a": 1, - "stats": { - "a": { - "sample": 2, - "frequency": 1, - "percentage": 25, - "sum": 4, - "count": 3, - "min": 1, - "max": 2, - "mean": 1.3333333333333333, - "range": 1, - "midrange": 0.5, - "variance": 0.2222222222222222, - "deviation": 0.4714045207910317, - "population": 2 - } - } -}, -{ - "a": 1, - "stats": { - "a": { - "sample": 2, - "frequency": 1, - "percentage": 25, - "sum": 4, - "count": 3, - "min": 1, - "max": 2, - "mean": 1.3333333333333333, - "range": 1, - "midrange": 0.5, - "variance": 0.2222222222222222, - "deviation": 0.4714045207910317, - "population": 2 - } - } -}, -{ - "a": 2, - "stats": { - "a": { - "sample": 1, - "frequency": 0.5, - "percentage": 50, - "sum": 4, - "count": 3, - "min": 1, - "max": 2, - "mean": 1.3333333333333333, - "range": 1, - "midrange": 0.5, - "variance": 0.2222222222222222, - "deviation": 0.4714045207910317, - "population": 2 + [ + { + "value": 1, + "_statistics": { + "value": { + "sample": 2, + "frequency": 1, + "percentage": 25, + "sum": 4, + "count": 3, + "min": 1, + "max": 2, + "mean": 1.3333333333333333, + "range": 1, + "midrange": 0.5, + "variance": 0.2222222222222222, + "deviation": 0.4714045207910317, + "population": 2 + } + } + }, + { + "value": 1, + "_statistics": { + "value": { + "sample": 2, + "frequency": 1, + "percentage": 25, + "sum": 4, + "count": 3, + "min": 1, + "max": 2, + "mean": 1.3333333333333333, + "range": 1, + "midrange": 0.5, + "variance": 0.2222222222222222, + "deviation": 0.4714045207910317, + "population": 2 + } + } + }, + { + "value": 2, + "_statistics": { + "value": { + "sample": 1, + "frequency": 0.5, + "percentage": 50, + "sum": 4, + "count": 3, + "min": 1, + "max": 2, + "mean": 1.3333333333333333, + "range": 1, + "midrange": 0.5, + "variance": 0.2222222222222222, + "deviation": 0.4714045207910317, + "population": 2 + } + } } - } -}] + ] ``` -```` + +#### Parameters + +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to create the statistics
+
  • chemin de l'élément utilisé pour créer les statistics
(optional, default `value`) +- `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the statistics in the returned object
+
  • chemin des stastistiques dans l'objet retourné
(optional, default `_statistics`) Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** @@ -1468,14 +1481,16 @@ Créer un couple id, value à partir de chemin et applique un somme sur la valeu ##### Script / Scénario ```ini -; Import analytics plugin required to use tune -; Importation du plugin analytique nécessaire pour utiliser tune +; Import analytics plugin required to use summing +; Importation du plugin analytique nécessaire pour utiliser summing [use] plugin = analytics -; Using "tune" with default settings -; Utilisation de "tune" avec les paramètres par défaut -[tune] +; Using "summing" with default settings +; Utilisation de "summing" avec les paramètres par défaut +[summing] +; id = id +; value = value ``` ##### Input / Entrée @@ -1538,6 +1553,8 @@ plugin = analytics ; Using "tune" with default settings ; Utilisation de "tune" avec les paramètres par défaut [tune] +; path = id +; method = natural ``` ##### Input / Entrée @@ -1612,6 +1629,7 @@ plugin = analytics ; Using "value" with default settings ; Utilisation de "tune" avec les paramètres par défaut [value] +; path = value ``` ##### Input / Entrée diff --git a/packages/analytics/README.md b/packages/analytics/README.md index 5ff986f49..e2b2818fe 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1298,162 +1298,175 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### sort -Take all `Object` and sort them with dedicated key +Sort incomming object base on the value of the given path -```json -[{ - { id: 2000, value: 1 }, - { id: 2001, value: 2 }, - { id: 2003, value: 3 }, - { id: 2005, value: 4 }, - { id: 2007, value: 5 }, - { id: 2009, value: 6 }, - { id: 2011, value: 7 }, - { id: 2013, value: 8 }, -}] -``` +Trier les objets entrants sur la base de la valeur du chemin donné -Script: +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use tune +; Importation du plugin analytique nécessaire pour utiliser sort [use] plugin = analytics +; Using "sort" with default settings +; Utilisation de "sort" avec les paramètres par défaut [sort] -path = value -reverse = true +; path = id +; reverse = false ``` -Output: +##### Input / Entrée ```json -[ -{ "id": 2013, "value": 8 }, -{ "id": 2011, "value": 7 }, -{ "id": 2009, "value": 6 }, -{ "id": 2007, "value": 5 }, -{ "id": 2005, "value": 4 }, -{ "id": 2003, "value": 3 }, -{ "id": 2001, "value": 2 }, -{ "id": 2000, "value": 1 } -] + [ + { "id": 2013, "value": 8 }, + { "id": 2011, "value": 7 }, + { "id": 2009, "value": 6 }, + { "id": 2007, "value": 5 }, + { "id": 2005, "value": 4 }, + { "id": 2003, "value": 3 }, + { "id": 2001, "value": 2 }, + { "id": 2000, "value": 1 } + ] ``` -#### Parameters - -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use for id (optional, default `id`) -- `reverse` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** reverser order (optional, default `false`) - -Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** - -### statistics +##### Output / Sortie -Compute some statistics from one or more fields +```json + [ + { "id": 2000, "value": 1 }, + { "id": 2001, "value": 2 }, + { "id": 2003, "value": 3 }, + { "id": 2005, "value": 4 }, + { "id": 2007, "value": 5 }, + { "id": 2009, "value": 6 }, + { "id": 2011, "value": 7 }, + { "id": 2013, "value": 8 }, + ] +``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path of the value field (optional, default `value`) -- `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path of statistics in output object (optional, default `_statistics`) +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to as reference for the sort
+
  • chemin de l'élément utilisé comme reference pour le trie
(optional, default `id`) +- `reverse` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
  • sort in ascending or descending order
+
  • trier par ordre croissant ou décroissant
(optional, default `false`) -#### Examples +Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** -Input +### statistics +Analyse and create statistics from given fields -````javascript -```json -[ - { a: 1, }, - { a: 1, }, - { a: 2, }, - { a: 3, }, - { a: 3, }, - { a: 3, }, -] -``` -```` +Analisse et créer des statistiques a partir des champs donnée -Script +#### Example / Exemple +##### Script / Scénario -````javascript ```ini +; Import analytics plugin required to use statistics +; Importation du plugin analytique nécessaire pour utiliser statistics [use] plugin = analytics +; Using "statistics" with default settings +; Utilisation de "statistics" avec les paramètres par défaut [statistics] -path = a - +; path = value +; target = _statistics ``` -```` -Output +##### Input / Entrée + +```json + [ + { "value": 1 }, + { "value": 1 }, + { "value": 2 }, + { "value": 3 }, + { "value": 3 }, + { "value": 3 } + ] +``` +##### Output / Sortie -````javascript ```json -[{ - "a": 1, - "stats": { - "a": { - "sample": 2, - "frequency": 1, - "percentage": 25, - "sum": 4, - "count": 3, - "min": 1, - "max": 2, - "mean": 1.3333333333333333, - "range": 1, - "midrange": 0.5, - "variance": 0.2222222222222222, - "deviation": 0.4714045207910317, - "population": 2 - } - } -}, -{ - "a": 1, - "stats": { - "a": { - "sample": 2, - "frequency": 1, - "percentage": 25, - "sum": 4, - "count": 3, - "min": 1, - "max": 2, - "mean": 1.3333333333333333, - "range": 1, - "midrange": 0.5, - "variance": 0.2222222222222222, - "deviation": 0.4714045207910317, - "population": 2 - } - } -}, -{ - "a": 2, - "stats": { - "a": { - "sample": 1, - "frequency": 0.5, - "percentage": 50, - "sum": 4, - "count": 3, - "min": 1, - "max": 2, - "mean": 1.3333333333333333, - "range": 1, - "midrange": 0.5, - "variance": 0.2222222222222222, - "deviation": 0.4714045207910317, - "population": 2 + [ + { + "value": 1, + "_statistics": { + "value": { + "sample": 2, + "frequency": 1, + "percentage": 25, + "sum": 4, + "count": 3, + "min": 1, + "max": 2, + "mean": 1.3333333333333333, + "range": 1, + "midrange": 0.5, + "variance": 0.2222222222222222, + "deviation": 0.4714045207910317, + "population": 2 + } + } + }, + { + "value": 1, + "_statistics": { + "value": { + "sample": 2, + "frequency": 1, + "percentage": 25, + "sum": 4, + "count": 3, + "min": 1, + "max": 2, + "mean": 1.3333333333333333, + "range": 1, + "midrange": 0.5, + "variance": 0.2222222222222222, + "deviation": 0.4714045207910317, + "population": 2 + } + } + }, + { + "value": 2, + "_statistics": { + "value": { + "sample": 1, + "frequency": 0.5, + "percentage": 50, + "sum": 4, + "count": 3, + "min": 1, + "max": 2, + "mean": 1.3333333333333333, + "range": 1, + "midrange": 0.5, + "variance": 0.2222222222222222, + "deviation": 0.4714045207910317, + "population": 2 + } + } } - } -}] + ] ``` -```` + +#### Parameters + +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to create the statistics
+
  • chemin de l'élément utilisé pour créer les statistics
(optional, default `value`) +- `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the statistics in the returned object
+
  • chemin des stastistiques dans l'objet retourné
(optional, default `_statistics`) Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** @@ -1468,14 +1481,16 @@ Créer un couple id, value à partir de chemin et applique un somme sur la valeu ##### Script / Scénario ```ini -; Import analytics plugin required to use tune -; Importation du plugin analytique nécessaire pour utiliser tune +; Import analytics plugin required to use summing +; Importation du plugin analytique nécessaire pour utiliser summing [use] plugin = analytics -; Using "tune" with default settings -; Utilisation de "tune" avec les paramètres par défaut -[tune] +; Using "summing" with default settings +; Utilisation de "summing" avec les paramètres par défaut +[summing] +; id = id +; value = value ``` ##### Input / Entrée @@ -1538,6 +1553,8 @@ plugin = analytics ; Using "tune" with default settings ; Utilisation de "tune" avec les paramètres par défaut [tune] +; path = id +; method = natural ``` ##### Input / Entrée @@ -1612,6 +1629,7 @@ plugin = analytics ; Using "value" with default settings ; Utilisation de "tune" avec les paramètres par défaut [value] +; path = value ``` ##### Input / Entrée diff --git a/packages/analytics/src/sort.js b/packages/analytics/src/sort.js index a650fb9ec..2e616e979 100644 --- a/packages/analytics/src/sort.js +++ b/packages/analytics/src/sort.js @@ -10,83 +10,114 @@ const sorting = (arr, reverse = false) => { } return fastsort(arr).desc(); }; + +/** + * Sort 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 sort = async (data, feed, ctx) => { + if (!ctx.store) { + const location = ctx.getParam('location'); + ctx.store = createStore(ctx.ezs, 'sort', location); + ctx.table = []; + } + if (ctx.isLast()) { + const reverse = ctx.getParam('reverse', false); + const sorted = sorting(ctx.table, reverse); + await sorted.reduce( + async (previousPromise, cur) => { + await previousPromise; + return ctx.store.cut(cur) + .then((val) => feed.write(val)); + }, + Promise.resolve(), + ); + await ctx.store.close(); + feed.close(); + } else { + const path = ctx.getParam('path', 'id'); + const fields = Array.isArray(path) ? path : [path]; + const keys = fields + .filter((k) => typeof k === 'string') + .map((key) => get(data, key)); + const key = keys.length > 1 ? keys.join(',') : keys[0]; + const idx = ctx.getIndex() + .toString() + .padStart(20, '0'); + const hash = normalize(key) + .concat('~') + .concat(idx) + .replace(/\s/g, '~'); + ctx.table.push(hash); + await ctx.store.put(hash, data); + feed.end(); + } +}; + /** - * Take all `Object` and sort them with dedicated key + * Sort incomming object base on the value of the given path * - * ```json - * [{ - * { id: 2000, value: 1 }, - * { id: 2001, value: 2 }, - * { id: 2003, value: 3 }, - * { id: 2005, value: 4 }, - * { id: 2007, value: 5 }, - * { id: 2009, value: 6 }, - * { id: 2011, value: 7 }, - * { id: 2013, value: 8 }, - * }] - * ``` + * Trier les objets entrants sur la base de la valeur du chemin donné + * + * ### Example / Exemple * - * Script: + * #### Script / Scénario * * ```ini + * ; Import analytics plugin required to use tune + * ; Importation du plugin analytique nécessaire pour utiliser sort * [use] * plugin = analytics * + * ; Using "sort" with default settings + * ; Utilisation de "sort" avec les paramètres par défaut * [sort] - * path = value - * reverse = true + * ; path = id + * ; reverse = false + * + * ``` + * + * #### Input / Entrée * + * ```json + * [ + * { "id": 2013, "value": 8 }, + * { "id": 2011, "value": 7 }, + * { "id": 2009, "value": 6 }, + * { "id": 2007, "value": 5 }, + * { "id": 2005, "value": 4 }, + * { "id": 2003, "value": 3 }, + * { "id": 2001, "value": 2 }, + * { "id": 2000, "value": 1 } + * ] * ``` * - * Output: + * #### Output / Sortie * * ```json - * [ - * { "id": 2013, "value": 8 }, - * { "id": 2011, "value": 7 }, - * { "id": 2009, "value": 6 }, - * { "id": 2007, "value": 5 }, - * { "id": 2005, "value": 4 }, - * { "id": 2003, "value": 3 }, - * { "id": 2001, "value": 2 }, - * { "id": 2000, "value": 1 } - * ] + * [ + * { "id": 2000, "value": 1 }, + * { "id": 2001, "value": 2 }, + * { "id": 2003, "value": 3 }, + * { "id": 2005, "value": 4 }, + * { "id": 2007, "value": 5 }, + * { "id": 2009, "value": 6 }, + * { "id": 2011, "value": 7 }, + * { "id": 2013, "value": 8 }, + * ] * ``` * * @name sort - * @param {String} [path=id] path to use for id - * @param {boolean} [reverse=false] reverser order + * @param {String} [path=id] + *
  • path of the element used to as reference for the sort
+ *
  • chemin de l'élément utilisé comme reference pour le trie
+ * @param {boolean} [reverse=false] + *
  • sort in ascending or descending order
+ *
  • trier par ordre croissant ou décroissant
* @returns {Object} */ -export default async function sort(data, feed) { - if (!this.store) { - const location = this.getParam('location'); - this.store = createStore(this.ezs, 'sort', location); - this.table = []; - } - if (this.isLast()) { - const reverse = this.getParam('reverse', false); - const sorted = sorting(this.table, reverse); - await sorted.reduce( - async (previousPromise, cur) => { - await previousPromise; - return this.store.cut(cur).then((val) => feed.write(val)); - }, - Promise.resolve(), - ); - await this.store.close(); - feed.close(); - } else { - const path = this.getParam('path', 'id'); - const fields = Array.isArray(path) ? path : [path]; - const keys = fields - .filter((k) => typeof k === 'string') - .map((key) => get(data, key)); - const key = keys.length > 1 ? keys.join(',') : keys[0]; - const idx = this.getIndex().toString().padStart(20, '0'); - const hash = normalize(key).concat('~').concat(idx).replace(/\s/g, '~'); - this.table.push(hash); - await this.store.put(hash, data); - feed.end(); - } -} +export default sort; diff --git a/packages/analytics/src/statistics.js b/packages/analytics/src/statistics.js index 5ea550dcd..241ad2f34 100644 --- a/packages/analytics/src/statistics.js +++ b/packages/analytics/src/statistics.js @@ -29,115 +29,28 @@ const calculating = (values) => { }; /** - * Compute some statistics from one or more fields - * - * @example Input - * ```json - * [ - * { a: 1, }, - * { a: 1, }, - * { a: 2, }, - * { a: 3, }, - * { a: 3, }, - * { a: 3, }, - * ] - * ``` - * - * @example Script - * ```ini - * [use] - * plugin = analytics - * - * [statistics] - * path = a - * - * ``` - * - * @example Output - * ```json - * [{ - * "a": 1, - * "stats": { - * "a": { - * "sample": 2, - * "frequency": 1, - * "percentage": 25, - * "sum": 4, - * "count": 3, - * "min": 1, - * "max": 2, - * "mean": 1.3333333333333333, - * "range": 1, - * "midrange": 0.5, - * "variance": 0.2222222222222222, - * "deviation": 0.4714045207910317, - * "population": 2 - * } - * } - * }, - * { - * "a": 1, - * "stats": { - * "a": { - * "sample": 2, - * "frequency": 1, - * "percentage": 25, - * "sum": 4, - * "count": 3, - * "min": 1, - * "max": 2, - * "mean": 1.3333333333333333, - * "range": 1, - * "midrange": 0.5, - * "variance": 0.2222222222222222, - * "deviation": 0.4714045207910317, - * "population": 2 - * } - * } - * }, - * { - * "a": 2, - * "stats": { - * "a": { - * "sample": 1, - * "frequency": 0.5, - * "percentage": 50, - * "sum": 4, - * "count": 3, - * "min": 1, - * "max": 2, - * "mean": 1.3333333333333333, - * "range": 1, - * "midrange": 0.5, - * "variance": 0.2222222222222222, - * "deviation": 0.4714045207910317, - * "population": 2 - * } - * } - * }] - * ``` - * - * @export - * @name statistics - * @param {String} [path=value] path of the value field - * @param {String} [target=_statistics] path of statistics in output object - * @returns {Object} + * Statistics 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 */ -export default async function statistics(data, feed) { - const path = this.getParam('path', 'value'); - const target = this.getParam('target', '_statistics'); +const statistics = async (data, feed, ctx) => { + const path = ctx.getParam('path', 'value'); + const target = ctx.getParam('target', '_statistics'); const fields = Array.isArray(path) ? path : [path]; const keys = fields.filter((k) => typeof k === 'string'); - if (this.isFirst()) { - const location = this.getParam('location'); - this.store = createStore(this.ezs, 'statistics', location); - this.store.reset(); - this.stack = {}; + if (ctx.isFirst()) { + const location = ctx.getParam('location'); + ctx.store = createStore(ctx.ezs, 'statistics', location); + ctx.store.reset(); + ctx.stack = {}; } - if (this.isLast()) { - const values = keys.filter((key) => this.stack[key]).reduce((obj, key) => { - const result = calculating(this.stack[key].stat); + if (ctx.isLast()) { + const values = keys.filter((key) => ctx.stack[key]).reduce((obj, key) => { + const result = calculating(ctx.stack[key].stat); const range = result.max - result.min; const midrange = range / 2; const variance = result.diff / result.count; @@ -151,16 +64,16 @@ export default async function statistics(data, feed) { midrange, variance, deviation: Math.sqrt(variance), - population: Object.keys(this.stack[key].hash).length, + population: Object.keys(ctx.stack[key].hash).length, }; return obj; }, {}); - const stream = await this.store.empty(); + const stream = await ctx.store.empty(); stream .on('data', ({ value }) => { const localValues = value.hashValues.reduce((obj, item) => { - const sample = this.stack[item.key].hash[item.hashValue]; - const percentage = (100 * this.stack[item.key].vals[item.hashValue]) / values[item.key].sum; + const sample = ctx.stack[item.key].hash[item.hashValue]; + const percentage = (100 * ctx.stack[item.key].vals[item.hashValue]) / values[item.key].sum; const frequency = sample / values[item.key].population; obj[item.key] = { sample, @@ -174,7 +87,7 @@ export default async function statistics(data, feed) { feed.write(value.data); }) .on('end', async () => { - await this.store.close(); + await ctx.store.close(); feed.close(); }); return; @@ -182,11 +95,11 @@ export default async function statistics(data, feed) { const hashValues = keys.map((key) => { const rawValue = get(data, key); if (!rawValue) return; - if (!this.stack[key]) { - this.stack[key] = { stat: [], hash: {}, vals: {} }; + if (!ctx.stack[key]) { + ctx.stack[key] = { stat: [], hash: {}, vals: {} }; } const numValue = Number(rawValue); - this.stack[key].stat.push({ + ctx.stack[key].stat.push({ sum: numValue || 0, min: numValue || 0, max: numValue || 0, @@ -194,14 +107,126 @@ export default async function statistics(data, feed) { diff: 0, }); const hashValue = hashCoerce.hash(rawValue); - if (this.stack[key].hash[hashValue]) { - this.stack[key].hash[hashValue] += 1; + if (ctx.stack[key].hash[hashValue]) { + ctx.stack[key].hash[hashValue] += 1; } else { - this.stack[key].hash[hashValue] = 1; + ctx.stack[key].hash[hashValue] = 1; } - this.stack[key].vals[hashValue] = numValue; + ctx.stack[key].vals[hashValue] = numValue; return { key, hashValue }; }).filter(Boolean); - const uri = 'uid:'.concat(this.getIndex().toString().padStart(10, '0')); - this.store.put(uri, { data, hashValues }).then(() => feed.end()); -} + const uri = 'uid:'.concat(ctx.getIndex().toString().padStart(10, '0')); + ctx.store.put(uri, { data, hashValues }).then(() => feed.end()); +}; + +/** + * Analyse and create statistics from given fields + * + * Analisse et créer des statistiques a partir des champs donnée + * + * ### Example / Exemple + * + * #### Script / Scénario + * ```ini + * ; Import analytics plugin required to use statistics + * ; Importation du plugin analytique nécessaire pour utiliser statistics + * [use] + * plugin = analytics + * + * ; Using "statistics" with default settings + * ; Utilisation de "statistics" avec les paramètres par défaut + * [statistics] + * ; path = value + * ; target = _statistics + * + * ``` + * + * #### Input / Entrée + * ```json + * [ + * { "value": 1 }, + * { "value": 1 }, + * { "value": 2 }, + * { "value": 3 }, + * { "value": 3 }, + * { "value": 3 } + * ] + * ``` + * + * #### Output / Sortie + * + * ```json + * [ + * { + * "value": 1, + * "_statistics": { + * "value": { + * "sample": 2, + * "frequency": 1, + * "percentage": 25, + * "sum": 4, + * "count": 3, + * "min": 1, + * "max": 2, + * "mean": 1.3333333333333333, + * "range": 1, + * "midrange": 0.5, + * "variance": 0.2222222222222222, + * "deviation": 0.4714045207910317, + * "population": 2 + * } + * } + * }, + * { + * "value": 1, + * "_statistics": { + * "value": { + * "sample": 2, + * "frequency": 1, + * "percentage": 25, + * "sum": 4, + * "count": 3, + * "min": 1, + * "max": 2, + * "mean": 1.3333333333333333, + * "range": 1, + * "midrange": 0.5, + * "variance": 0.2222222222222222, + * "deviation": 0.4714045207910317, + * "population": 2 + * } + * } + * }, + * { + * "value": 2, + * "_statistics": { + * "value": { + * "sample": 1, + * "frequency": 0.5, + * "percentage": 50, + * "sum": 4, + * "count": 3, + * "min": 1, + * "max": 2, + * "mean": 1.3333333333333333, + * "range": 1, + * "midrange": 0.5, + * "variance": 0.2222222222222222, + * "deviation": 0.4714045207910317, + * "population": 2 + * } + * } + * } + * ] + * ``` + * + * @name statistics + * @param {String} [path=value] + *
  • path of the element used to create the statistics
+ *
  • chemin de l'élément utilisé pour créer les statistics
+ * @param {String} [target=_statistics] + *
  • path of the statistics in the returned object
+ *
  • chemin des stastistiques dans l'objet retourné
+ * @returns {Object} + */ +export default statistics; \ No newline at end of file diff --git a/packages/analytics/src/summing.js b/packages/analytics/src/summing.js index ba127f22f..dfa0b08c1 100644 --- a/packages/analytics/src/summing.js +++ b/packages/analytics/src/summing.js @@ -37,14 +37,16 @@ const summing = (data, feed, ctx) => { * #### Script / Scénario * * ```ini - * ; Import analytics plugin required to use tune - * ; Importation du plugin analytique nécessaire pour utiliser tune + * ; Import analytics plugin required to use summing + * ; Importation du plugin analytique nécessaire pour utiliser summing * [use] * plugin = analytics * - * ; Using "tune" with default settings - * ; Utilisation de "tune" avec les paramètres par défaut - * [tune] + * ; Using "summing" with default settings + * ; Utilisation de "summing" avec les paramètres par défaut + * [summing] + * ; id = id + * ; value = value * * ``` * @@ -65,7 +67,7 @@ const summing = (data, feed, ctx) => { * ] * ``` * - * #### Output / Sortie + * #### Output / Sortie * * ```json * [ diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index 644ff28c2..b13a2566a 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -104,6 +104,8 @@ const tune = (data, feed, ctx) => { * ; Using "tune" with default settings * ; Utilisation de "tune" avec les paramètres par défaut * [tune] + * ; path = id + * ; method = natural * * ``` * diff --git a/packages/analytics/src/value.js b/packages/analytics/src/value.js index efbdbb5a7..bdf43eeef 100644 --- a/packages/analytics/src/value.js +++ b/packages/analytics/src/value.js @@ -43,6 +43,7 @@ const value = (data, feed, ctx) => { * ; Using "value" with default settings * ; Utilisation de "tune" avec les paramètres par défaut * [value] + * ; path = value * * ``` * From 77bf3467555d5db4791dd254f953229ea7c4e9f4 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Mon, 29 Jan 2024 08:27:01 +0100 Subject: [PATCH 15/26] docs (analytics): add slice doc --- docs/plugin-analytics.md | 91 +++++++++++++------- packages/analytics/README.md | 91 +++++++++++++------- packages/analytics/src/slice.js | 123 ++++++++++++++++++--------- packages/analytics/src/sort.js | 4 +- packages/analytics/src/statistics.js | 4 +- packages/analytics/src/summing.js | 4 +- packages/analytics/src/tune.js | 4 +- packages/analytics/src/value.js | 4 +- 8 files changed, 207 insertions(+), 118 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index e2b2818fe..047833c4e 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1255,52 +1255,77 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### slice -Take `Object` and throw the same object only if it is in the section of the -stream between start and start + size. stream is numbered from 1 +Returns a copy of a section of a stream. -```json -[{ - { id: 2000, value: 1 }, - { id: 2001, value: 2 }, - { id: 2003, value: 3 }, - { id: 2005, value: 4 }, - { id: 2007, value: 5 }, - { id: 2009, value: 6 }, - { id: 2011, value: 7 }, - { id: 2013, value: 8 }, -}] -``` +Renvoie une copie d'une section d'un flux. -Script: +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use slice +; Importation du plugin analytique nécessaire pour utiliser slice [use] plugin = analytics -[drop] +; Using "slice" with default settings +; Utilisation de "slice" avec les paramètres par défaut +[slice] +; start = 1 +; size = 10 ``` -Output: +##### Input / Entrée ```json -[ -{ "id": 2001, "value": 2 }, -{ "id": 2003, "value": 3 }, -] + [ + { "id": 2023, "value": 12 }, + { "id": 2021, "value": 11 }, + { "id": 2019, "value": 10 }, + { "id": 2017, "value": 9 }, + { "id": 2013, "value": 8 }, + { "id": 2011, "value": 7 }, + { "id": 2009, "value": 6 }, + { "id": 2007, "value": 5 }, + { "id": 2005, "value": 4 }, + { "id": 2003, "value": 3 }, + { "id": 2001, "value": 2 }, + { "id": 2000, "value": 1 } + ] +``` + +##### Output / Sortie + +```json + [ + { "id": 2023, "value": 12 }, + { "id": 2021, "value": 11 }, + { "id": 2019, "value": 10 }, + { "id": 2017, "value": 9 }, + { "id": 2013, "value": 8 }, + { "id": 2011, "value": 7 }, + { "id": 2009, "value": 6 }, + { "id": 2007, "value": 5 }, + { "id": 2005, "value": 4 }, + { "id": 2003, "value": 3 } + ] ``` #### Parameters -- `start` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** start of the slice (optional, default `0`) -- `size` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** size of the slice (optional, default `10`) +- `start` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
  • the beginning index of the specified portion of the stream
+
  • l'indice de début de la partie spécifiée du flux
(optional, default `1`) +- `size` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
  • the size of the specified portion of the stream
+
  • la taille de début de la partie spécifiée du flux
(optional, default `10`) Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** ### sort -Sort incomming object base on the value of the given path +Sort incomming object base on the value of the given path. -Trier les objets entrants sur la base de la valeur du chemin donné +Trier les objets entrants sur la base de la valeur du chemin donné. #### Example / Exemple @@ -1360,9 +1385,9 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### statistics -Analyse and create statistics from given fields +Analyse and create statistics from given fields. -Analisse et créer des statistiques a partir des champs donnée +Analisse et créer des statistiques a partir des champs donnée. #### Example / Exemple @@ -1472,9 +1497,9 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### summing -Create an id, value pair from two given path and apply a sum to the value +Create an id, value pair from two given path and apply a sum to the value. -Créer un couple id, value à partir de chemin et applique un somme sur la valeur +Créer un couple id, value à partir de chemin et applique un somme sur la valeur. #### Example / Exemple @@ -1536,9 +1561,9 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### tune -Create and replace the id with a unify id that can be used with [sort](#sort) +Create and replace the id with a unify id that can be used with [sort](#sort). -Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort) +Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort). #### Example / Exemple @@ -1612,9 +1637,9 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### value -Create a new object from the value of the given path +Create a new object from the value of the given path. -Créer un nouvel objet à partir de la valeur d'un chemin donnée +Créer un nouvel objet à partir de la valeur d'un chemin donnée. #### Example / Exemple diff --git a/packages/analytics/README.md b/packages/analytics/README.md index e2b2818fe..047833c4e 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1255,52 +1255,77 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### slice -Take `Object` and throw the same object only if it is in the section of the -stream between start and start + size. stream is numbered from 1 +Returns a copy of a section of a stream. -```json -[{ - { id: 2000, value: 1 }, - { id: 2001, value: 2 }, - { id: 2003, value: 3 }, - { id: 2005, value: 4 }, - { id: 2007, value: 5 }, - { id: 2009, value: 6 }, - { id: 2011, value: 7 }, - { id: 2013, value: 8 }, -}] -``` +Renvoie une copie d'une section d'un flux. -Script: +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use slice +; Importation du plugin analytique nécessaire pour utiliser slice [use] plugin = analytics -[drop] +; Using "slice" with default settings +; Utilisation de "slice" avec les paramètres par défaut +[slice] +; start = 1 +; size = 10 ``` -Output: +##### Input / Entrée ```json -[ -{ "id": 2001, "value": 2 }, -{ "id": 2003, "value": 3 }, -] + [ + { "id": 2023, "value": 12 }, + { "id": 2021, "value": 11 }, + { "id": 2019, "value": 10 }, + { "id": 2017, "value": 9 }, + { "id": 2013, "value": 8 }, + { "id": 2011, "value": 7 }, + { "id": 2009, "value": 6 }, + { "id": 2007, "value": 5 }, + { "id": 2005, "value": 4 }, + { "id": 2003, "value": 3 }, + { "id": 2001, "value": 2 }, + { "id": 2000, "value": 1 } + ] +``` + +##### Output / Sortie + +```json + [ + { "id": 2023, "value": 12 }, + { "id": 2021, "value": 11 }, + { "id": 2019, "value": 10 }, + { "id": 2017, "value": 9 }, + { "id": 2013, "value": 8 }, + { "id": 2011, "value": 7 }, + { "id": 2009, "value": 6 }, + { "id": 2007, "value": 5 }, + { "id": 2005, "value": 4 }, + { "id": 2003, "value": 3 } + ] ``` #### Parameters -- `start` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** start of the slice (optional, default `0`) -- `size` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** size of the slice (optional, default `10`) +- `start` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
  • the beginning index of the specified portion of the stream
+
  • l'indice de début de la partie spécifiée du flux
(optional, default `1`) +- `size` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
  • the size of the specified portion of the stream
+
  • la taille de début de la partie spécifiée du flux
(optional, default `10`) Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** ### sort -Sort incomming object base on the value of the given path +Sort incomming object base on the value of the given path. -Trier les objets entrants sur la base de la valeur du chemin donné +Trier les objets entrants sur la base de la valeur du chemin donné. #### Example / Exemple @@ -1360,9 +1385,9 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### statistics -Analyse and create statistics from given fields +Analyse and create statistics from given fields. -Analisse et créer des statistiques a partir des champs donnée +Analisse et créer des statistiques a partir des champs donnée. #### Example / Exemple @@ -1472,9 +1497,9 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### summing -Create an id, value pair from two given path and apply a sum to the value +Create an id, value pair from two given path and apply a sum to the value. -Créer un couple id, value à partir de chemin et applique un somme sur la valeur +Créer un couple id, value à partir de chemin et applique un somme sur la valeur. #### Example / Exemple @@ -1536,9 +1561,9 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### tune -Create and replace the id with a unify id that can be used with [sort](#sort) +Create and replace the id with a unify id that can be used with [sort](#sort). -Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort) +Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort). #### Example / Exemple @@ -1612,9 +1637,9 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### value -Create a new object from the value of the given path +Create a new object from the value of the given path. -Créer un nouvel objet à partir de la valeur d'un chemin donnée +Créer un nouvel objet à partir de la valeur d'un chemin donnée. #### Example / Exemple diff --git a/packages/analytics/src/slice.js b/packages/analytics/src/slice.js index e3a005bb5..134e5fc80 100644 --- a/packages/analytics/src/slice.js +++ b/packages/analytics/src/slice.js @@ -1,60 +1,99 @@ + /** - * Take `Object` and throw the same object only if it is in the section of the - * stream between start and start + size. stream is numbered from 1 + * Slice 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 slice = (data, feed, ctx) => { + if (ctx.isLast()) { + feed.close(); + return; + } + const start = Number(ctx.getParam('start')) || 1; + const size = Number(ctx.getParam('size')) || 10; + const stop = start + size; + const index = Number(ctx.getIndex()); + + if (index >= stop) { + feed.close(); + } else { + if (index >= start) { + feed.write(data); + } + feed.end(); + } + [].slice(); +}; + +/** + * Returns a copy of a section of a stream. * - * ```json - * [{ - * { id: 2000, value: 1 }, - * { id: 2001, value: 2 }, - * { id: 2003, value: 3 }, - * { id: 2005, value: 4 }, - * { id: 2007, value: 5 }, - * { id: 2009, value: 6 }, - * { id: 2011, value: 7 }, - * { id: 2013, value: 8 }, - * }] - * ``` + * Renvoie une copie d'une section d'un flux. * - * Script: + * ### Example / Exemple + * + * #### Script / Scénario * * ```ini + * ; Import analytics plugin required to use slice + * ; Importation du plugin analytique nécessaire pour utiliser slice * [use] * plugin = analytics * - * [drop] + * ; Using "slice" with default settings + * ; Utilisation de "slice" avec les paramètres par défaut + * [slice] + * ; start = 1 + * ; size = 10 + * + * ``` * + * #### Input / Entrée + * + * ```json + * [ + * { "id": 2023, "value": 12 }, + * { "id": 2021, "value": 11 }, + * { "id": 2019, "value": 10 }, + * { "id": 2017, "value": 9 }, + * { "id": 2013, "value": 8 }, + * { "id": 2011, "value": 7 }, + * { "id": 2009, "value": 6 }, + * { "id": 2007, "value": 5 }, + * { "id": 2005, "value": 4 }, + * { "id": 2003, "value": 3 }, + * { "id": 2001, "value": 2 }, + * { "id": 2000, "value": 1 } + * ] * ``` * - * Output: + * #### Output / Sortie * * ```json - * [ - * { "id": 2001, "value": 2 }, - * { "id": 2003, "value": 3 }, - * ] + * [ + * { "id": 2023, "value": 12 }, + * { "id": 2021, "value": 11 }, + * { "id": 2019, "value": 10 }, + * { "id": 2017, "value": 9 }, + * { "id": 2013, "value": 8 }, + * { "id": 2011, "value": 7 }, + * { "id": 2009, "value": 6 }, + * { "id": 2007, "value": 5 }, + * { "id": 2005, "value": 4 }, + * { "id": 2003, "value": 3 } + * ] * ``` * * @name slice - * @param {Number} [start=0] start of the slice - * @param {Number} [size=10] size of the slice + * @param {Number} [start=1] + *
  • the beginning index of the specified portion of the stream
+ *
  • l'indice de début de la partie spécifiée du flux
+ * @param {Number} [size=10] + *
  • the size of the specified portion of the stream
+ *
  • la taille de début de la partie spécifiée du flux
* @returns {Object} */ -export default function slice(data, feed) { - if (this.isLast()) { - feed.close(); - return; - } - const start = Number(this.getParam('start')) || 1; - const size = Number(this.getParam('size')) || 10; - const stop = start + size; - const index = Number(this.getIndex()); - - if (index >= stop) { - feed.close(); - } else { - if (index >= start) { - feed.write(data); - } - feed.end(); - } -} +export default slice; diff --git a/packages/analytics/src/sort.js b/packages/analytics/src/sort.js index 2e616e979..92686bccf 100644 --- a/packages/analytics/src/sort.js +++ b/packages/analytics/src/sort.js @@ -59,9 +59,9 @@ const sort = async (data, feed, ctx) => { }; /** - * Sort incomming object base on the value of the given path + * Sort incomming object base on the value of the given path. * - * Trier les objets entrants sur la base de la valeur du chemin donné + * Trier les objets entrants sur la base de la valeur du chemin donné. * * ### Example / Exemple * diff --git a/packages/analytics/src/statistics.js b/packages/analytics/src/statistics.js index 241ad2f34..c709d0019 100644 --- a/packages/analytics/src/statistics.js +++ b/packages/analytics/src/statistics.js @@ -120,9 +120,9 @@ const statistics = async (data, feed, ctx) => { }; /** - * Analyse and create statistics from given fields + * Analyse and create statistics from given fields. * - * Analisse et créer des statistiques a partir des champs donnée + * Analisse et créer des statistiques a partir des champs donnée. * * ### Example / Exemple * diff --git a/packages/analytics/src/summing.js b/packages/analytics/src/summing.js index dfa0b08c1..877347623 100644 --- a/packages/analytics/src/summing.js +++ b/packages/analytics/src/summing.js @@ -28,9 +28,9 @@ const summing = (data, feed, ctx) => { }; /** - * Create an id, value pair from two given path and apply a sum to the value + * Create an id, value pair from two given path and apply a sum to the value. * - * Créer un couple id, value à partir de chemin et applique un somme sur la valeur + * Créer un couple id, value à partir de chemin et applique un somme sur la valeur. * * ### Example / Exemple * diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index b13a2566a..fc55f90f2 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -87,9 +87,9 @@ const tune = (data, feed, ctx) => { }; /** - * Create and replace the id with a unify id that can be used with [sort](#sort) + * Create and replace the id with a unify id that can be used with [sort](#sort). * - * Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort) + * Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort). * * ### Example / Exemple * diff --git a/packages/analytics/src/value.js b/packages/analytics/src/value.js index bdf43eeef..7a98ff86f 100644 --- a/packages/analytics/src/value.js +++ b/packages/analytics/src/value.js @@ -26,9 +26,9 @@ const value = (data, feed, ctx) => { }; /** - * Create a new object from the value of the given path + * Create a new object from the value of the given path. * - * Créer un nouvel objet à partir de la valeur d'un chemin donnée + * Créer un nouvel objet à partir de la valeur d'un chemin donnée. * * ### Example / Exemple * From d0f5093ade920c01d49bbba5fdea0e243abae978 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Mon, 29 Jan 2024 08:45:22 +0100 Subject: [PATCH 16/26] docs: add Engine scope jsdoc type --- packages/analytics/src/slice.js | 2 +- packages/analytics/src/sort.js | 6 +++--- packages/analytics/src/statistics.js | 2 +- packages/analytics/src/summing.js | 2 +- packages/analytics/src/tune.js | 2 +- packages/analytics/src/value.js | 2 +- packages/core/src/engine.js | 20 ++++++++++++++++++++ 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/analytics/src/slice.js b/packages/analytics/src/slice.js index 134e5fc80..f1777450d 100644 --- a/packages/analytics/src/slice.js +++ b/packages/analytics/src/slice.js @@ -5,7 +5,7 @@ * @private * @param data {unknown} * @param feed {Feed} - * @param ctx + * @param ctx {import('../../core/src/engine').EngineScope} */ const slice = (data, feed, ctx) => { if (ctx.isLast()) { diff --git a/packages/analytics/src/sort.js b/packages/analytics/src/sort.js index 92686bccf..fa791266f 100644 --- a/packages/analytics/src/sort.js +++ b/packages/analytics/src/sort.js @@ -17,7 +17,7 @@ const sorting = (arr, reverse = false) => { * @private * @param data {unknown} * @param feed {Feed} - * @param ctx + * @param ctx {import('../../core/src/engine').EngineScope} */ const sort = async (data, feed, ctx) => { if (!ctx.store) { @@ -68,7 +68,7 @@ const sort = async (data, feed, ctx) => { * #### Script / Scénario * * ```ini - * ; Import analytics plugin required to use tune + * ; Import analytics plugin required to use sort * ; Importation du plugin analytique nécessaire pour utiliser sort * [use] * plugin = analytics @@ -107,7 +107,7 @@ const sort = async (data, feed, ctx) => { * { "id": 2007, "value": 5 }, * { "id": 2009, "value": 6 }, * { "id": 2011, "value": 7 }, - * { "id": 2013, "value": 8 }, + * { "id": 2013, "value": 8 } * ] * ``` * diff --git a/packages/analytics/src/statistics.js b/packages/analytics/src/statistics.js index c709d0019..fba7ac207 100644 --- a/packages/analytics/src/statistics.js +++ b/packages/analytics/src/statistics.js @@ -34,7 +34,7 @@ const calculating = (values) => { * @private * @param data {unknown} * @param feed {Feed} - * @param ctx + * @param ctx {import('../../core/src/engine').EngineScope} */ const statistics = async (data, feed, ctx) => { const path = ctx.getParam('path', 'value'); diff --git a/packages/analytics/src/summing.js b/packages/analytics/src/summing.js index 877347623..0c88c7155 100644 --- a/packages/analytics/src/summing.js +++ b/packages/analytics/src/summing.js @@ -7,7 +7,7 @@ import core from './core'; * @private * @param data {unknown} * @param feed {Feed} - * @param ctx + * @param ctx {import('../../core/src/engine').EngineScope} */ const summing = (data, feed, ctx) => { if (ctx.isLast()) { diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index fc55f90f2..909be559b 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -46,7 +46,7 @@ const allMethods = Object.keys(methods).join(','); * @private * @param data {unknown} * @param feed {Feed} - * @param ctx + * @param ctx {import('../../core/src/engine').EngineScope} */ const tune = (data, feed, ctx) => { if (ctx.isLast()) { diff --git a/packages/analytics/src/value.js b/packages/analytics/src/value.js index 7a98ff86f..3ba9d023d 100644 --- a/packages/analytics/src/value.js +++ b/packages/analytics/src/value.js @@ -6,7 +6,7 @@ import get from 'lodash.get'; * @private * @param data {unknown} * @param feed {Feed} - * @param ctx + * @param ctx {import('../../core/src/engine').EngineScope} */ const value = (data, feed, ctx) => { if (ctx.isLast()) { diff --git a/packages/core/src/engine.js b/packages/core/src/engine.js index 2b4e6b295..af451f552 100644 --- a/packages/core/src/engine.js +++ b/packages/core/src/engine.js @@ -9,6 +9,23 @@ import Shell from './shell'; import SafeTransform from './SafeTransform'; +/** + * Engine scope object type + * @private + * @typedef {Object} EngineScope + * + * @property {Engine} ezs + * @property {(d: unknown, c: unknown) => void} emit + * @property {() => any} getParams + * @property {() => boolean} isFirst + * @property {() => number} getIndex + * @property {() => boolean} isLast + * @property {() => string} getCumulativeTime + * @property {() => number} getCumulativeTimeMS + * @property {() => number} getCounter + * @property {(name: string, defval?: string | string[], chunk?: unknown) => string | string[] | undefined} getParam + */ + const nanoZero = () => BigInt(0); const nano2sec = (ns) => { @@ -72,6 +89,9 @@ export default class Engine extends SafeTransform { eos(this, decreaseCounter); this.shell = new Shell(ezs, this.environment); this.chunk = {}; + /** + * @type {EngineScope} + */ this.scope = {}; this.scope.getEnv = (name) => (name === undefined ? this.environment : this.environment[name]); this.scope.ezs = this.ezs; From edb6743bccb1b563b220bc2df379bbe20671a7ed Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Mon, 29 Jan 2024 10:32:39 +0100 Subject: [PATCH 17/26] docs (analytics): reformat segment doc --- docs/plugin-analytics.md | 148 ++++++++++++---------- packages/analytics/README.md | 148 ++++++++++++---------- packages/analytics/src/segment.js | 202 +++++++++++++++++------------- 3 files changed, 283 insertions(+), 215 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index 047833c4e..d46f79ebb 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1172,86 +1172,104 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### segment -Take `Object` object getting some fields with json path, and throw segment of -value. Ex: get `[a,b,c]` and throw `[a,b], [b,c]` +Returns an object containing a segmentation of the input. -```json -[{ - { - id: 'doc#1', - value: [ - 1, - 2, - 3, - 4, - ], - }, - { - id: 'doc#2', - value: [ - 4, - 5, - 6, - ], - }, - { - id: 'doc#3', - value: [ - 6, - 7, - ] - }, - { - id: 'doc#4', - value: [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - ] - } -}] -``` +- `[a,b,c]` will be returned as `[a,b], [b,c]` -Script: +Renvoie un objet contenant une segmentation de l'entrée. + +- `[a,b,c]` sera retourné sous la forme `[a,b], [b,c]` + +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use segment +; Importation du plugin analytique nécessaire pour utiliser segment [use] plugin = analytics +; Using "segment" with default settings +; Utilisation de "segment" avec les paramètres par défaut [segment] -path = value +; aggregate = true +; identifier = false +; path = value ``` -Output: +##### Input / Entrée ```json -[ - { id: [ 1, 2 ], value: 1 } - { id: [ 2, 3 ], value: 1 } - { id: [ 3, 4 ], value: 1 } - { id: [ 4, 5 ], value: 1 } - { id: [ 5, 6 ], value: 1 } - { id: [ 6, 7 ], value: 1 } - { id: [ 1, 2 ], value: 1 } - { id: [ 2, 3 ], value: 1 } - { id: [ 3, 4 ], value: 1 } - { id: [ 4, 5 ], value: 1 } - { id: [ 5, 6 ], value: 1 } - { id: [ 6, 7 ], value: 1 } -] + [ + { + "id": "doc#1", + "value": [ + 1, + 2, + 3, + 4 + ] + }, + { + "id": "doc#2", + "value": [ + 4, + 5, + 6 + ] + }, + { + "id": "doc#3", + "value": [ + 6, + 7 + ] + }, + { + "id": "doc#4", + "value": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7 + ] + } + ] +``` + +##### Output / Sortie + +```json + [ + { "id": [ 1, 2 ], "value": 1 }, + { "id": [ 2, 3 ], "value": 1 }, + { "id": [ 3, 4 ], "value": 1 }, + { "id": [ 4, 5 ], "value": 1 }, + { "id": [ 5, 6 ], "value": 1 }, + { "id": [ 6, 7 ], "value": 1 }, + { "id": [ 1, 2 ], "value": 1 }, + { "id": [ 2, 3 ], "value": 1 }, + { "id": [ 3, 4 ], "value": 1 }, + { "id": [ 4, 5 ], "value": 1 }, + { "id": [ 5, 6 ], "value": 1 }, + { "id": [ 6, 7 ], "value": 1 } + ] ``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path (optional, default `value`) -- `aggregate` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** aggregate all values for all paths (or not) (optional, default `true`) -- `identifier` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use to set value result field (if not set or not exists, 1 is use as a default value) (optional, default `false`) +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who need to be segmented
+
  • chemin de l'élément qui doit être segmentés
(optional, default `value`) +- `aggregate` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** aggregate all values for all paths (or not)
  • aggregate all segmented value in one element (work if you have multiple path)
+
  • agréger toutes les valeurs segmentées en un seul élément (fonctionne si vous avez plusieurs chemins)
(optional, default `true`) +- `identifier` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who will be put in value field (if not set, fallback to `1`)
+
  • chemin de l'élément qui sera mis dans le champ valeur (si non défini, fallback à `1`)
(optional, default `false`) -Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** +Returns **{id: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>, value: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}** ### slice @@ -1332,7 +1350,7 @@ Trier les objets entrants sur la base de la valeur du chemin donné. ##### Script / Scénario ```ini -; Import analytics plugin required to use tune +; Import analytics plugin required to use sort ; Importation du plugin analytique nécessaire pour utiliser sort [use] plugin = analytics @@ -1370,7 +1388,7 @@ plugin = analytics { "id": 2007, "value": 5 }, { "id": 2009, "value": 6 }, { "id": 2011, "value": 7 }, - { "id": 2013, "value": 8 }, + { "id": 2013, "value": 8 } ] ``` diff --git a/packages/analytics/README.md b/packages/analytics/README.md index 047833c4e..d46f79ebb 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1172,86 +1172,104 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### segment -Take `Object` object getting some fields with json path, and throw segment of -value. Ex: get `[a,b,c]` and throw `[a,b], [b,c]` +Returns an object containing a segmentation of the input. -```json -[{ - { - id: 'doc#1', - value: [ - 1, - 2, - 3, - 4, - ], - }, - { - id: 'doc#2', - value: [ - 4, - 5, - 6, - ], - }, - { - id: 'doc#3', - value: [ - 6, - 7, - ] - }, - { - id: 'doc#4', - value: [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - ] - } -}] -``` +- `[a,b,c]` will be returned as `[a,b], [b,c]` -Script: +Renvoie un objet contenant une segmentation de l'entrée. + +- `[a,b,c]` sera retourné sous la forme `[a,b], [b,c]` + +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use segment +; Importation du plugin analytique nécessaire pour utiliser segment [use] plugin = analytics +; Using "segment" with default settings +; Utilisation de "segment" avec les paramètres par défaut [segment] -path = value +; aggregate = true +; identifier = false +; path = value ``` -Output: +##### Input / Entrée ```json -[ - { id: [ 1, 2 ], value: 1 } - { id: [ 2, 3 ], value: 1 } - { id: [ 3, 4 ], value: 1 } - { id: [ 4, 5 ], value: 1 } - { id: [ 5, 6 ], value: 1 } - { id: [ 6, 7 ], value: 1 } - { id: [ 1, 2 ], value: 1 } - { id: [ 2, 3 ], value: 1 } - { id: [ 3, 4 ], value: 1 } - { id: [ 4, 5 ], value: 1 } - { id: [ 5, 6 ], value: 1 } - { id: [ 6, 7 ], value: 1 } -] + [ + { + "id": "doc#1", + "value": [ + 1, + 2, + 3, + 4 + ] + }, + { + "id": "doc#2", + "value": [ + 4, + 5, + 6 + ] + }, + { + "id": "doc#3", + "value": [ + 6, + 7 + ] + }, + { + "id": "doc#4", + "value": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7 + ] + } + ] +``` + +##### Output / Sortie + +```json + [ + { "id": [ 1, 2 ], "value": 1 }, + { "id": [ 2, 3 ], "value": 1 }, + { "id": [ 3, 4 ], "value": 1 }, + { "id": [ 4, 5 ], "value": 1 }, + { "id": [ 5, 6 ], "value": 1 }, + { "id": [ 6, 7 ], "value": 1 }, + { "id": [ 1, 2 ], "value": 1 }, + { "id": [ 2, 3 ], "value": 1 }, + { "id": [ 3, 4 ], "value": 1 }, + { "id": [ 4, 5 ], "value": 1 }, + { "id": [ 5, 6 ], "value": 1 }, + { "id": [ 6, 7 ], "value": 1 } + ] ``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path (optional, default `value`) -- `aggregate` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** aggregate all values for all paths (or not) (optional, default `true`) -- `identifier` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use to set value result field (if not set or not exists, 1 is use as a default value) (optional, default `false`) +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who need to be segmented
+
  • chemin de l'élément qui doit être segmentés
(optional, default `value`) +- `aggregate` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** aggregate all values for all paths (or not)
  • aggregate all segmented value in one element (work if you have multiple path)
+
  • agréger toutes les valeurs segmentées en un seul élément (fonctionne si vous avez plusieurs chemins)
(optional, default `true`) +- `identifier` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who will be put in value field (if not set, fallback to `1`)
+
  • chemin de l'élément qui sera mis dans le champ valeur (si non défini, fallback à `1`)
(optional, default `false`) -Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** +Returns **{id: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>, value: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}** ### slice @@ -1332,7 +1350,7 @@ Trier les objets entrants sur la base de la valeur du chemin donné. ##### Script / Scénario ```ini -; Import analytics plugin required to use tune +; Import analytics plugin required to use sort ; Importation du plugin analytique nécessaire pour utiliser sort [use] plugin = analytics @@ -1370,7 +1388,7 @@ plugin = analytics { "id": 2007, "value": 5 }, { "id": 2009, "value": 6 }, { "id": 2011, "value": 7 }, - { "id": 2013, "value": 8 }, + { "id": 2013, "value": 8 } ] ``` diff --git a/packages/analytics/src/segment.js b/packages/analytics/src/segment.js index 851ba9fdd..bab5a2c8c 100644 --- a/packages/analytics/src/segment.js +++ b/packages/analytics/src/segment.js @@ -3,94 +3,21 @@ import flatten from 'lodash.flatten'; import core from './core'; /** - * Take `Object` object getting some fields with json path, and throw segment of - * value. Ex: get `[a,b,c]` and throw `[a,b], [b,c]` - * - * ```json - * [{ - * { - * id: 'doc#1', - * value: [ - * 1, - * 2, - * 3, - * 4, - * ], - * }, - * { - * id: 'doc#2', - * value: [ - * 4, - * 5, - * 6, - * ], - * }, - * { - * id: 'doc#3', - * value: [ - * 6, - * 7, - * ] - * }, - * { - * id: 'doc#4', - * value: [ - * 1, - * 2, - * 3, - * 4, - * 5, - * 6, - * 7, - * ] - * } - * }] - * ``` - * - * Script: - * - * ```ini - * [use] - * plugin = analytics - * - * [segment] - * path = value - * - * ``` - * - * Output: - * - * ```json - * [ - * { id: [ 1, 2 ], value: 1 } - * { id: [ 2, 3 ], value: 1 } - * { id: [ 3, 4 ], value: 1 } - * { id: [ 4, 5 ], value: 1 } - * { id: [ 5, 6 ], value: 1 } - * { id: [ 6, 7 ], value: 1 } - * { id: [ 1, 2 ], value: 1 } - * { id: [ 2, 3 ], value: 1 } - * { id: [ 3, 4 ], value: 1 } - * { id: [ 4, 5 ], value: 1 } - * { id: [ 5, 6 ], value: 1 } - * { id: [ 6, 7 ], value: 1 } - * ] - * ``` - * - * @name segment - * @param {String} [path=value] path - * @param {Boolean} [aggregate=true] aggregate all values for all paths (or not) - * @param {String} [identifier=false] path to use to set value result field (if not set or not exists, 1 is use as a default value) - * @returns {Object} + * Segment 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 {import('../../core/src/engine').EngineScope} */ -export default function segment(data, feed) { - if (this.isLast()) { +const segment = (data, feed, ctx) => { + if (ctx.isLast()) { feed.close(); return; } - const aggr = this.getParam('aggregate', true); - const idt = this.getParam('identifier', false); - const path = this.getParam('path', 'value'); + const aggr = ctx.getParam('aggregate', true); + const idt = ctx.getParam('identifier', false); + const path = ctx.getParam('path', 'value'); const fields = Array.isArray(path) ? path : [path]; const valuesOrig = fields @@ -120,4 +47,109 @@ export default function segment(data, feed) { } feed.end(); -} +}; + +/** + * Returns an object containing a segmentation of the input. + * - `[a,b,c]` will be returned as `[a,b], [b,c]` + * + * Renvoie un objet contenant une segmentation de l'entrée. + * - `[a,b,c]` sera retourné sous la forme `[a,b], [b,c]` + * + * ### Example / Exemple + * + * #### Script / Scénario + * + * ```ini + * ; Import analytics plugin required to use segment + * ; Importation du plugin analytique nécessaire pour utiliser segment + * [use] + * plugin = analytics + * + * ; Using "segment" with default settings + * ; Utilisation de "segment" avec les paramètres par défaut + * [segment] + * ; aggregate = true + * ; identifier = false + * ; path = value + * + * ``` + * + * #### Input / Entrée + * + * ```json + * [ + * { + * "id": "doc#1", + * "value": [ + * 1, + * 2, + * 3, + * 4 + * ] + * }, + * { + * "id": "doc#2", + * "value": [ + * 4, + * 5, + * 6 + * ] + * }, + * { + * "id": "doc#3", + * "value": [ + * 6, + * 7 + * ] + * }, + * { + * "id": "doc#4", + * "value": [ + * 1, + * 2, + * 3, + * 4, + * 5, + * 6, + * 7 + * ] + * } + * ] + * ``` + * + * #### Output / Sortie + * + * ```json + * [ + * { "id": [ 1, 2 ], "value": 1 }, + * { "id": [ 2, 3 ], "value": 1 }, + * { "id": [ 3, 4 ], "value": 1 }, + * { "id": [ 4, 5 ], "value": 1 }, + * { "id": [ 5, 6 ], "value": 1 }, + * { "id": [ 6, 7 ], "value": 1 }, + * { "id": [ 1, 2 ], "value": 1 }, + * { "id": [ 2, 3 ], "value": 1 }, + * { "id": [ 3, 4 ], "value": 1 }, + * { "id": [ 4, 5 ], "value": 1 }, + * { "id": [ 5, 6 ], "value": 1 }, + * { "id": [ 6, 7 ], "value": 1 } + * ] + * ``` + * + * @name segment + * @param {String} [path=value] + *
  • path of the element who need to be segmented
+ *
  • chemin de l'élément qui doit être segmentés
+ * @param {Boolean} [aggregate=true] aggregate all values for all paths (or not) + *
  • aggregate all segmented value in one element (work if you have multiple path)
+ *
  • agréger toutes les valeurs segmentées en un seul élément (fonctionne si vous avez plusieurs chemins)
+ * @param {String} [identifier=false] + *
  • path of the element who will be put in value field (if not set, fallback to `1`)
+ *
  • chemin de l'élément qui sera mis dans le champ valeur (si non défini, fallback à `1`)
+ * @returns {{ + * id: Array, + * value: Object, + * }} + */ +export default segment; From 25a8f6254516db8b76edf9206297c5c9d12568c3 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Mon, 29 Jan 2024 10:33:25 +0100 Subject: [PATCH 18/26] docs (core): add missing private --- packages/core/src/engine.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/engine.js b/packages/core/src/engine.js index af451f552..3b743ef57 100644 --- a/packages/core/src/engine.js +++ b/packages/core/src/engine.js @@ -90,6 +90,7 @@ export default class Engine extends SafeTransform { this.shell = new Shell(ezs, this.environment); this.chunk = {}; /** + * @private * @type {EngineScope} */ this.scope = {}; From a776fe3144c4252955fa15b6163b48ea4326034d Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Tue, 30 Jan 2024 08:25:57 +0100 Subject: [PATCH 19/26] docs (analytics): fix typo --- docs/plugin-analytics.md | 54 ++++++++++++------------- packages/analytics/README.md | 54 ++++++++++++------------- packages/analytics/src/segment.js | 6 +-- packages/analytics/src/slice.js | 6 +-- packages/analytics/src/sort.js | 12 +++--- packages/analytics/src/statistics.js | 10 ++--- packages/analytics/src/summing.js | 10 ++--- packages/analytics/src/tune.js | 8 ++-- packages/analytics/src/value.js | 16 ++++---- packages/analytics/test-utils/runEzs.js | 2 +- packages/analytics/test/summing.spec.js | 20 +-------- packages/analytics/test/tune.spec.js | 20 +++++++-- 12 files changed, 107 insertions(+), 111 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index d46f79ebb..e0e71c452 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1185,8 +1185,8 @@ Renvoie un objet contenant une segmentation de l'entrée. ##### Script / Scénario ```ini -; Import analytics plugin required to use segment -; Importation du plugin analytique nécessaire pour utiliser segment +; Import analytics plugin required to use "segment" +; Importation du plugin analytique nécessaire pour utiliser "segment" [use] plugin = analytics @@ -1282,8 +1282,8 @@ Renvoie une copie d'une section d'un flux. ##### Script / Scénario ```ini -; Import analytics plugin required to use slice -; Importation du plugin analytique nécessaire pour utiliser slice +; Import analytics plugin required to use "slice" +; Importation du plugin analytique nécessaire pour utiliser "slice" [use] plugin = analytics @@ -1341,17 +1341,17 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### sort -Sort incomming object base on the value of the given path. +Sort incomming objects based on the value of the given path. -Trier les objets entrants sur la base de la valeur du chemin donné. +Trie les objets entrants en fonction de la valeur du champ donné par `path`. #### Example / Exemple ##### Script / Scénario ```ini -; Import analytics plugin required to use sort -; Importation du plugin analytique nécessaire pour utiliser sort +; Import analytics plugin required to use "sort" +; Importation du plugin analytique nécessaire pour utiliser "sort" [use] plugin = analytics @@ -1395,7 +1395,7 @@ plugin = analytics #### Parameters - `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to as reference for the sort
-
  • chemin de l'élément utilisé comme reference pour le trie
(optional, default `id`) +
  • chemin de l'élément utilisé comme référence pour le tri
(optional, default `id`) - `reverse` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
  • sort in ascending or descending order
  • trier par ordre croissant ou décroissant
(optional, default `false`) @@ -1405,15 +1405,15 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G Analyse and create statistics from given fields. -Analisse et créer des statistiques a partir des champs donnée. +Analyse et crée des statistiques à partir des champs donnés. #### Example / Exemple ##### Script / Scénario ```ini -; Import analytics plugin required to use statistics -; Importation du plugin analytique nécessaire pour utiliser statistics +; Import analytics plugin required to use "statistics" +; Importation du plugin analytique nécessaire pour utiliser "statistics" [use] plugin = analytics @@ -1507,7 +1507,7 @@ plugin = analytics #### Parameters - `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to create the statistics
-
  • chemin de l'élément utilisé pour créer les statistics
(optional, default `value`) +
  • chemin de l'élément utilisé pour créer les statistiques
(optional, default `value`) - `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the statistics in the returned object
  • chemin des stastistiques dans l'objet retourné
(optional, default `_statistics`) @@ -1515,17 +1515,17 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### summing -Create an id, value pair from two given path and apply a sum to the value. +Create an `id`, `value` pair from two given paths and apply a sum to the value. -Créer un couple id, value à partir de chemin et applique un somme sur la valeur. +Créer un couple id, value à partir de chemins et applique une somme sur `value`. #### Example / Exemple ##### Script / Scénario ```ini -; Import analytics plugin required to use summing -; Importation du plugin analytique nécessaire pour utiliser summing +; Import analytics plugin required to use "summing" +; Importation du plugin analytique nécessaire pour utiliser "summing" [use] plugin = analytics @@ -1579,7 +1579,7 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### tune -Create and replace the id with a unify id that can be used with [sort](#sort). +Create and replace the id with a unified id that can be used with [sort](#sort). Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort). @@ -1588,8 +1588,8 @@ Créer et remplacer l'identifiant par un identifiant unifié qui peut être util ##### Script / Scénario ```ini -; Import analytics plugin required to use tune -; Importation du plugin analytique nécessaire pour utiliser tune +; Import analytics plugin required to use "tune" +; Importation du plugin analytique nécessaire pour utiliser "tune" [use] plugin = analytics @@ -1657,15 +1657,15 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere Create a new object from the value of the given path. -Créer un nouvel objet à partir de la valeur d'un chemin donnée. +Créer un nouvel objet à partir du chemin donné dans `path`. #### Example / Exemple ##### Script / Scénario ```ini -; Import analytics plugin required to use value -; Importation du plugin analytique nécessaire pour utiliser value +; Import analytics plugin required to use "value" +; Importation du plugin analytique nécessaire pour utiliser "value" [use] plugin = analytics @@ -1677,7 +1677,7 @@ plugin = analytics ##### Input / Entrée -###### Dataset 1 / Jeu de donnée 1 +###### Dataset 1 / Jeu de données 1 ```json [ @@ -1692,7 +1692,7 @@ plugin = analytics ] ``` -###### Dataset 2 / Jeu de donnée 2 +###### Dataset 2 / Jeu de données 2 ```json [ @@ -1719,7 +1719,7 @@ plugin = analytics ##### Output / Sortie -###### Dataset 1 / Jeu de donnée 1 +###### Dataset 1 / Jeu de données 1 ```json [ @@ -1734,7 +1734,7 @@ plugin = analytics ] ``` -###### Dataset 2 / Jeu de donnée 2 +###### Dataset 2 / Jeu de données 2 ```json [ diff --git a/packages/analytics/README.md b/packages/analytics/README.md index d46f79ebb..e0e71c452 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1185,8 +1185,8 @@ Renvoie un objet contenant une segmentation de l'entrée. ##### Script / Scénario ```ini -; Import analytics plugin required to use segment -; Importation du plugin analytique nécessaire pour utiliser segment +; Import analytics plugin required to use "segment" +; Importation du plugin analytique nécessaire pour utiliser "segment" [use] plugin = analytics @@ -1282,8 +1282,8 @@ Renvoie une copie d'une section d'un flux. ##### Script / Scénario ```ini -; Import analytics plugin required to use slice -; Importation du plugin analytique nécessaire pour utiliser slice +; Import analytics plugin required to use "slice" +; Importation du plugin analytique nécessaire pour utiliser "slice" [use] plugin = analytics @@ -1341,17 +1341,17 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### sort -Sort incomming object base on the value of the given path. +Sort incomming objects based on the value of the given path. -Trier les objets entrants sur la base de la valeur du chemin donné. +Trie les objets entrants en fonction de la valeur du champ donné par `path`. #### Example / Exemple ##### Script / Scénario ```ini -; Import analytics plugin required to use sort -; Importation du plugin analytique nécessaire pour utiliser sort +; Import analytics plugin required to use "sort" +; Importation du plugin analytique nécessaire pour utiliser "sort" [use] plugin = analytics @@ -1395,7 +1395,7 @@ plugin = analytics #### Parameters - `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to as reference for the sort
-
  • chemin de l'élément utilisé comme reference pour le trie
(optional, default `id`) +
  • chemin de l'élément utilisé comme référence pour le tri
(optional, default `id`) - `reverse` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
  • sort in ascending or descending order
  • trier par ordre croissant ou décroissant
(optional, default `false`) @@ -1405,15 +1405,15 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G Analyse and create statistics from given fields. -Analisse et créer des statistiques a partir des champs donnée. +Analyse et crée des statistiques à partir des champs donnés. #### Example / Exemple ##### Script / Scénario ```ini -; Import analytics plugin required to use statistics -; Importation du plugin analytique nécessaire pour utiliser statistics +; Import analytics plugin required to use "statistics" +; Importation du plugin analytique nécessaire pour utiliser "statistics" [use] plugin = analytics @@ -1507,7 +1507,7 @@ plugin = analytics #### Parameters - `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element used to create the statistics
-
  • chemin de l'élément utilisé pour créer les statistics
(optional, default `value`) +
  • chemin de l'élément utilisé pour créer les statistiques
(optional, default `value`) - `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the statistics in the returned object
  • chemin des stastistiques dans l'objet retourné
(optional, default `_statistics`) @@ -1515,17 +1515,17 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### summing -Create an id, value pair from two given path and apply a sum to the value. +Create an `id`, `value` pair from two given paths and apply a sum to the value. -Créer un couple id, value à partir de chemin et applique un somme sur la valeur. +Créer un couple id, value à partir de chemins et applique une somme sur `value`. #### Example / Exemple ##### Script / Scénario ```ini -; Import analytics plugin required to use summing -; Importation du plugin analytique nécessaire pour utiliser summing +; Import analytics plugin required to use "summing" +; Importation du plugin analytique nécessaire pour utiliser "summing" [use] plugin = analytics @@ -1579,7 +1579,7 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### tune -Create and replace the id with a unify id that can be used with [sort](#sort). +Create and replace the id with a unified id that can be used with [sort](#sort). Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort). @@ -1588,8 +1588,8 @@ Créer et remplacer l'identifiant par un identifiant unifié qui peut être util ##### Script / Scénario ```ini -; Import analytics plugin required to use tune -; Importation du plugin analytique nécessaire pour utiliser tune +; Import analytics plugin required to use "tune" +; Importation du plugin analytique nécessaire pour utiliser "tune" [use] plugin = analytics @@ -1657,15 +1657,15 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere Create a new object from the value of the given path. -Créer un nouvel objet à partir de la valeur d'un chemin donnée. +Créer un nouvel objet à partir du chemin donné dans `path`. #### Example / Exemple ##### Script / Scénario ```ini -; Import analytics plugin required to use value -; Importation du plugin analytique nécessaire pour utiliser value +; Import analytics plugin required to use "value" +; Importation du plugin analytique nécessaire pour utiliser "value" [use] plugin = analytics @@ -1677,7 +1677,7 @@ plugin = analytics ##### Input / Entrée -###### Dataset 1 / Jeu de donnée 1 +###### Dataset 1 / Jeu de données 1 ```json [ @@ -1692,7 +1692,7 @@ plugin = analytics ] ``` -###### Dataset 2 / Jeu de donnée 2 +###### Dataset 2 / Jeu de données 2 ```json [ @@ -1719,7 +1719,7 @@ plugin = analytics ##### Output / Sortie -###### Dataset 1 / Jeu de donnée 1 +###### Dataset 1 / Jeu de données 1 ```json [ @@ -1734,7 +1734,7 @@ plugin = analytics ] ``` -###### Dataset 2 / Jeu de donnée 2 +###### Dataset 2 / Jeu de données 2 ```json [ diff --git a/packages/analytics/src/segment.js b/packages/analytics/src/segment.js index bab5a2c8c..0f7c09965 100644 --- a/packages/analytics/src/segment.js +++ b/packages/analytics/src/segment.js @@ -4,7 +4,7 @@ import core from './core'; /** * Segment function see documentation at the end. - * This part of the doc is use for jsdoc typing + * This part of the doc is used for jsdoc typing * @private * @param data {unknown} * @param feed {Feed} @@ -61,8 +61,8 @@ const segment = (data, feed, ctx) => { * #### Script / Scénario * * ```ini - * ; Import analytics plugin required to use segment - * ; Importation du plugin analytique nécessaire pour utiliser segment + * ; Import analytics plugin required to use "segment" + * ; Importation du plugin analytique nécessaire pour utiliser "segment" * [use] * plugin = analytics * diff --git a/packages/analytics/src/slice.js b/packages/analytics/src/slice.js index f1777450d..db4497be4 100644 --- a/packages/analytics/src/slice.js +++ b/packages/analytics/src/slice.js @@ -1,7 +1,7 @@ /** * Slice function see documentation at the end. - * This part of the doc is use for jsdoc typing + * This part of the doc is used for jsdoc typing * @private * @param data {unknown} * @param feed {Feed} @@ -38,8 +38,8 @@ const slice = (data, feed, ctx) => { * #### Script / Scénario * * ```ini - * ; Import analytics plugin required to use slice - * ; Importation du plugin analytique nécessaire pour utiliser slice + * ; Import analytics plugin required to use "slice" + * ; Importation du plugin analytique nécessaire pour utiliser "slice" * [use] * plugin = analytics * diff --git a/packages/analytics/src/sort.js b/packages/analytics/src/sort.js index fa791266f..b05c71c15 100644 --- a/packages/analytics/src/sort.js +++ b/packages/analytics/src/sort.js @@ -13,7 +13,7 @@ const sorting = (arr, reverse = false) => { /** * Sort function see documentation at the end. - * This part of the doc is use for jsdoc typing + * This part of the doc is used for jsdoc typing * @private * @param data {unknown} * @param feed {Feed} @@ -59,17 +59,17 @@ const sort = async (data, feed, ctx) => { }; /** - * Sort incomming object base on the value of the given path. + * Sort incomming objects based on the value of the given path. * - * Trier les objets entrants sur la base de la valeur du chemin donné. + * Trie les objets entrants en fonction de la valeur du champ donné par `path`. * * ### Example / Exemple * * #### Script / Scénario * * ```ini - * ; Import analytics plugin required to use sort - * ; Importation du plugin analytique nécessaire pour utiliser sort + * ; Import analytics plugin required to use "sort" + * ; Importation du plugin analytique nécessaire pour utiliser "sort" * [use] * plugin = analytics * @@ -114,7 +114,7 @@ const sort = async (data, feed, ctx) => { * @name sort * @param {String} [path=id] *
  • path of the element used to as reference for the sort
- *
  • chemin de l'élément utilisé comme reference pour le trie
+ *
  • chemin de l'élément utilisé comme référence pour le tri
* @param {boolean} [reverse=false] *
  • sort in ascending or descending order
*
  • trier par ordre croissant ou décroissant
diff --git a/packages/analytics/src/statistics.js b/packages/analytics/src/statistics.js index fba7ac207..35a7ec2ab 100644 --- a/packages/analytics/src/statistics.js +++ b/packages/analytics/src/statistics.js @@ -30,7 +30,7 @@ const calculating = (values) => { /** * Statistics function see documentation at the end. - * This part of the doc is use for jsdoc typing + * This part of the doc is used for jsdoc typing * @private * @param data {unknown} * @param feed {Feed} @@ -122,14 +122,14 @@ const statistics = async (data, feed, ctx) => { /** * Analyse and create statistics from given fields. * - * Analisse et créer des statistiques a partir des champs donnée. + * Analyse et crée des statistiques à partir des champs donnés. * * ### Example / Exemple * * #### Script / Scénario * ```ini - * ; Import analytics plugin required to use statistics - * ; Importation du plugin analytique nécessaire pour utiliser statistics + * ; Import analytics plugin required to use "statistics" + * ; Importation du plugin analytique nécessaire pour utiliser "statistics" * [use] * plugin = analytics * @@ -223,7 +223,7 @@ const statistics = async (data, feed, ctx) => { * @name statistics * @param {String} [path=value] *
  • path of the element used to create the statistics
- *
  • chemin de l'élément utilisé pour créer les statistics
+ *
  • chemin de l'élément utilisé pour créer les statistiques
* @param {String} [target=_statistics] *
  • path of the statistics in the returned object
*
  • chemin des stastistiques dans l'objet retourné
diff --git a/packages/analytics/src/summing.js b/packages/analytics/src/summing.js index 0c88c7155..a011101d3 100644 --- a/packages/analytics/src/summing.js +++ b/packages/analytics/src/summing.js @@ -3,7 +3,7 @@ import core from './core'; /** * Summing function see documentation at the end. - * This part of the doc is use for jsdoc typing + * This part of the doc is used for jsdoc typing * @private * @param data {unknown} * @param feed {Feed} @@ -28,17 +28,17 @@ const summing = (data, feed, ctx) => { }; /** - * Create an id, value pair from two given path and apply a sum to the value. + * Create an `id`, `value` pair from two given paths and apply a sum to the value. * - * Créer un couple id, value à partir de chemin et applique un somme sur la valeur. + * Créer un couple id, value à partir de chemins et applique une somme sur `value`. * * ### Example / Exemple * * #### Script / Scénario * * ```ini - * ; Import analytics plugin required to use summing - * ; Importation du plugin analytique nécessaire pour utiliser summing + * ; Import analytics plugin required to use "summing" + * ; Importation du plugin analytique nécessaire pour utiliser "summing" * [use] * plugin = analytics * diff --git a/packages/analytics/src/tune.js b/packages/analytics/src/tune.js index 909be559b..da8035c82 100644 --- a/packages/analytics/src/tune.js +++ b/packages/analytics/src/tune.js @@ -42,7 +42,7 @@ const allMethods = Object.keys(methods).join(','); /** * Tune function see documentation at the end. - * This part of the doc is use for jsdoc typing + * This part of the doc is used for jsdoc typing * @private * @param data {unknown} * @param feed {Feed} @@ -87,7 +87,7 @@ const tune = (data, feed, ctx) => { }; /** - * Create and replace the id with a unify id that can be used with [sort](#sort). + * Create and replace the id with a unified id that can be used with [sort](#sort). * * Créer et remplacer l'identifiant par un identifiant unifié qui peut être utilisé avec [sort](#sort). * @@ -96,8 +96,8 @@ const tune = (data, feed, ctx) => { * #### Script / Scénario * * ```ini - * ; Import analytics plugin required to use tune - * ; Importation du plugin analytique nécessaire pour utiliser tune + * ; Import analytics plugin required to use "tune" + * ; Importation du plugin analytique nécessaire pour utiliser "tune" * [use] * plugin = analytics * diff --git a/packages/analytics/src/value.js b/packages/analytics/src/value.js index 3ba9d023d..16b0e0db6 100644 --- a/packages/analytics/src/value.js +++ b/packages/analytics/src/value.js @@ -2,7 +2,7 @@ import get from 'lodash.get'; /** * Value function see documentation at the end. - * This part of the doc is use for jsdoc typing + * This part of the doc is used for jsdoc typing * @private * @param data {unknown} * @param feed {Feed} @@ -28,15 +28,15 @@ const value = (data, feed, ctx) => { /** * Create a new object from the value of the given path. * - * Créer un nouvel objet à partir de la valeur d'un chemin donnée. + * Créer un nouvel objet à partir du chemin donné dans `path`. * * ### Example / Exemple * * #### Script / Scénario * * ```ini - * ; Import analytics plugin required to use value - * ; Importation du plugin analytique nécessaire pour utiliser value + * ; Import analytics plugin required to use "value" + * ; Importation du plugin analytique nécessaire pour utiliser "value" * [use] * plugin = analytics * @@ -49,7 +49,7 @@ const value = (data, feed, ctx) => { * * #### Input / Entrée * - * ##### Dataset 1 / Jeu de donnée 1 + * ##### Dataset 1 / Jeu de données 1 * * ```json * [ @@ -64,7 +64,7 @@ const value = (data, feed, ctx) => { * ] * ``` * - * ##### Dataset 2 / Jeu de donnée 2 + * ##### Dataset 2 / Jeu de données 2 * * ```json * [ @@ -91,7 +91,7 @@ const value = (data, feed, ctx) => { * * #### Output / Sortie * - * ##### Dataset 1 / Jeu de donnée 1 + * ##### Dataset 1 / Jeu de données 1 * * ```json * [ @@ -106,7 +106,7 @@ const value = (data, feed, ctx) => { * ] * ``` * - * ##### Dataset 2 / Jeu de donnée 2 + * ##### Dataset 2 / Jeu de données 2 * * ```json * [ diff --git a/packages/analytics/test-utils/runEzs.js b/packages/analytics/test-utils/runEzs.js index bb72bd07b..4c55cebb6 100644 --- a/packages/analytics/test-utils/runEzs.js +++ b/packages/analytics/test-utils/runEzs.js @@ -1,7 +1,7 @@ import from from 'from'; /** - * Helper function use to call ezs in each tests + * Helper function used to call ezs in each test * @param ezsRuntime {(name: string, options: any, environment?: unknown) => NodeJS.WritableStream} * @param dataSet {Array} * @param functionName {string} diff --git a/packages/analytics/test/summing.spec.js b/packages/analytics/test/summing.spec.js index c90a1daf4..a6f761773 100644 --- a/packages/analytics/test/summing.spec.js +++ b/packages/analytics/test/summing.spec.js @@ -41,13 +41,13 @@ describe('summing', () => { } ]; - it('should return no result when input contains undefined', async () => { + it('should return no result when input contains only undefined', async () => { const result = await runEzs(ezs, undefinedData, 'summing'); expect(result).toHaveLength(0); }); - it.skip('should return no result when input contains null', async () => { + it.skip('should return no result when input contains only null', async () => { const result = await runEzs(ezs, nullData, 'summing'); expect(result).toHaveLength(0); @@ -86,15 +86,11 @@ describe('summing', () => { 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); }); @@ -109,15 +105,11 @@ describe('summing', () => { 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); }); }); @@ -144,16 +136,12 @@ describe('summing', () => { 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'); }); @@ -169,16 +157,12 @@ describe('summing', () => { 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'); }); diff --git a/packages/analytics/test/tune.spec.js b/packages/analytics/test/tune.spec.js index 84ed593b3..f4788a56e 100644 --- a/packages/analytics/test/tune.spec.js +++ b/packages/analytics/test/tune.spec.js @@ -41,13 +41,13 @@ describe('tune', () => { } ]; - it.skip('should return no result when input contains undefined', async () => { + it.skip('should return no result when input contains only undefined', async () => { const result = await runEzs(ezs, undefinedData, 'tune'); expect(result).toHaveLength(0); }); - it.skip('should return no result when input contains null', async () => { + it.skip('should return no result when input contains only null', async () => { const result = await runEzs(ezs, nullData, 'tune'); expect(result).toHaveLength(0); @@ -59,10 +59,22 @@ describe('tune', () => { expect(result).toHaveLength(0); }); - it.skip('should return no result when input contains wrong key', async () => { + it('should return no result when input contains wrong key', async () => { const result = await runEzs(ezs, wrongKeyData, 'tune'); - expect(result).toHaveLength(0); + expect(result).toHaveLength(2); + + expect(result[0]).not.toBeNull(); + expect(result[0].id).toStrictEqual('undefined'); + expect(result[0].value).toStrictEqual({ + 'hello': 'world', + }); + + expect(result[1]).not.toBeNull(); + expect(result[1].id).toStrictEqual('undefined'); + expect(result[1].value).toStrictEqual({ + 'hello': 'world', + }); }); }); From 164c1190c784319573d400ab610ce82aea2aae50 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Tue, 30 Jan 2024 08:29:41 +0100 Subject: [PATCH 20/26] docs (analytics): typo --- docs/plugin-analytics.md | 4 ++-- packages/analytics/README.md | 4 ++-- packages/analytics/src/summing.js | 2 +- packages/analytics/src/value.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index e0e71c452..a5f20b762 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1517,7 +1517,7 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G Create an `id`, `value` pair from two given paths and apply a sum to the value. -Créer un couple id, value à partir de chemins et applique une somme sur `value`. +Créer un couple `id`, `value` à partir de chemins et applique une somme sur `value`. #### Example / Exemple @@ -1655,7 +1655,7 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### value -Create a new object from the value of the given path. +Create a new object from the value of the given `path`. Créer un nouvel objet à partir du chemin donné dans `path`. diff --git a/packages/analytics/README.md b/packages/analytics/README.md index e0e71c452..a5f20b762 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1517,7 +1517,7 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G Create an `id`, `value` pair from two given paths and apply a sum to the value. -Créer un couple id, value à partir de chemins et applique une somme sur `value`. +Créer un couple `id`, `value` à partir de chemins et applique une somme sur `value`. #### Example / Exemple @@ -1655,7 +1655,7 @@ Returns **{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Refere ### value -Create a new object from the value of the given path. +Create a new object from the value of the given `path`. Créer un nouvel objet à partir du chemin donné dans `path`. diff --git a/packages/analytics/src/summing.js b/packages/analytics/src/summing.js index a011101d3..128adf8d3 100644 --- a/packages/analytics/src/summing.js +++ b/packages/analytics/src/summing.js @@ -30,7 +30,7 @@ const summing = (data, feed, ctx) => { /** * Create an `id`, `value` pair from two given paths and apply a sum to the value. * - * Créer un couple id, value à partir de chemins et applique une somme sur `value`. + * Créer un couple `id`, `value` à partir de chemins et applique une somme sur `value`. * * ### Example / Exemple * diff --git a/packages/analytics/src/value.js b/packages/analytics/src/value.js index 16b0e0db6..ccc8d6662 100644 --- a/packages/analytics/src/value.js +++ b/packages/analytics/src/value.js @@ -26,7 +26,7 @@ const value = (data, feed, ctx) => { }; /** - * Create a new object from the value of the given path. + * Create a new object from the value of the given `path`. * * Créer un nouvel objet à partir du chemin donné dans `path`. * From e0c1920965dbe46ccb081cb2c2cae2163d7d02f6 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Tue, 30 Jan 2024 13:33:19 +0100 Subject: [PATCH 21/26] docs (analytics): add reducing documentation --- docs/plugin-analytics.md | 42 +++++++++----- packages/analytics/README.md | 42 +++++++++----- packages/analytics/src/reducing.js | 91 ++++++++++++++++++------------ 3 files changed, 110 insertions(+), 65 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index a5f20b762..3de4331e2 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1134,33 +1134,45 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### reducing -Take `Object` group value of `{ id, value }` objectpath +Merges the `id`, `value` pairs into a new pair, associating the identifier with the values. -```json -[{ - { id: 'x', value: 2 }, - { id: 't', value: 2 }, - { id: 'x', value: 3 }, - { id: 'x', value: 5 }, -}] -``` +Fusionne les couple `id`, `value`, en un nouveau couple associent l'identifient au valeurs. -Script: +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use "reducing" +; Importation du plugin analytique nécessaire pour utiliser "reducing" [use] plugin = analytics +; Using "reducing" with default settings +; Utilisation de "reducing" avec les paramètres par défaut [reducing] +; id = id +; value = value ``` -Output: +##### Input / Entrée ```json -[ - { id: 'x', value: [2, 3, 5] }, - { id: 't', value: [2] }, -] + [ + { "id": "x", "value": 2 }, + { "id": "t", "value": 2 }, + { "id": "x", "value": 3 }, + { "id": "x", "value": 5 } + ] +``` + +##### Output / Sortie + +```json + [ + { "id": "x", "value": [2, 3, 5] }, + { "id": "t", "value": [2] } + ] ``` #### Parameters diff --git a/packages/analytics/README.md b/packages/analytics/README.md index a5f20b762..3de4331e2 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1134,33 +1134,45 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### reducing -Take `Object` group value of `{ id, value }` objectpath +Merges the `id`, `value` pairs into a new pair, associating the identifier with the values. -```json -[{ - { id: 'x', value: 2 }, - { id: 't', value: 2 }, - { id: 'x', value: 3 }, - { id: 'x', value: 5 }, -}] -``` +Fusionne les couple `id`, `value`, en un nouveau couple associent l'identifient au valeurs. -Script: +#### Example / Exemple + +##### Script / Scénario ```ini +; Import analytics plugin required to use "reducing" +; Importation du plugin analytique nécessaire pour utiliser "reducing" [use] plugin = analytics +; Using "reducing" with default settings +; Utilisation de "reducing" avec les paramètres par défaut [reducing] +; id = id +; value = value ``` -Output: +##### Input / Entrée ```json -[ - { id: 'x', value: [2, 3, 5] }, - { id: 't', value: [2] }, -] + [ + { "id": "x", "value": 2 }, + { "id": "t", "value": 2 }, + { "id": "x", "value": 3 }, + { "id": "x", "value": 5 } + ] +``` + +##### Output / Sortie + +```json + [ + { "id": "x", "value": [2, 3, 5] }, + { "id": "t", "value": [2] } + ] ``` #### Parameters diff --git a/packages/analytics/src/reducing.js b/packages/analytics/src/reducing.js index c41cb50ad..3b5b13d6a 100644 --- a/packages/analytics/src/reducing.js +++ b/packages/analytics/src/reducing.js @@ -2,34 +2,74 @@ import get from 'lodash.get'; import { createStore } from '@ezs/store'; /** - * Take `Object` group value of `{ id, value }` objectpath + * Reducing 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 reducing = async (data, feed, ctx) => { + if (!ctx.store) { + const location = ctx.getParam('location'); + ctx.store = createStore(ctx.ezs, 'reducing', location); + } + if (ctx.isLast()) { + const stream = await ctx.store.empty(); + stream + .on('data', (item) => feed.write(item)) + .on('end', async () => { + await ctx.store.close(); + feed.close(); + }); + } else { + const id = get(data, ctx.getParam('id', 'id')) || ctx.getIndex(); + const value = get(data, ctx.getParam('value', 'value')); + await ctx.store.add(id, value); + feed.end(); + } +}; + +/** + * Merges the `id`, `value` pairs into a new pair, associating the identifier with the values. * - * ```json - * [{ - * { id: 'x', value: 2 }, - * { id: 't', value: 2 }, - * { id: 'x', value: 3 }, - * { id: 'x', value: 5 }, - * }] - * ``` + * Fusionne les couple `id`, `value`, en un nouveau couple associent l'identifient au valeurs. * - * Script: + * ### Example / Exemple + * + * #### Script / Scénario * * ```ini + * ; Import analytics plugin required to use "reducing" + * ; Importation du plugin analytique nécessaire pour utiliser "reducing" * [use] * plugin = analytics * + * ; Using "reducing" with default settings + * ; Utilisation de "reducing" avec les paramètres par défaut * [reducing] + * ; id = id + * ; value = value * * ``` * - * Output: + * #### Input / Entrée + * + * ```json + * [ + * { "id": "x", "value": 2 }, + * { "id": "t", "value": 2 }, + * { "id": "x", "value": 3 }, + * { "id": "x", "value": 5 } + * ] + * ``` + * #### Output / Sortie * * ```json - * [ - * { id: 'x', value: [2, 3, 5] }, - * { id: 't', value: [2] }, - * ] + * [ + * { "id": "x", "value": [2, 3, 5] }, + * { "id": "t", "value": [2] } + * ] * ``` * * @name reducing @@ -37,23 +77,4 @@ import { createStore } from '@ezs/store'; * @param {String} [value=value] path to use for value * @returns {Object} */ -export default async function reducing(data, feed) { - if (!this.store) { - const location = this.getParam('location'); - this.store = createStore(this.ezs, 'reducing', location); - } - if (this.isLast()) { - const stream = await this.store.empty(); - stream - .on('data', (item) => feed.write(item)) - .on('end', async () => { - await this.store.close(); - feed.close(); - }); - } else { - const id = get(data, this.getParam('id', 'id')) || this.getIndex(); - const value = get(data, this.getParam('value', 'value')); - await this.store.add(id, value) - feed.end(); - } -} +export default reducing; From cff6018b0526a742a5df35231618712b9d03070b Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 31 Jan 2024 08:45:32 +0100 Subject: [PATCH 22/26] docs (analytics): reformat pluck doc and fix reducing doc --- docs/plugin-analytics.md | 76 ++++++++++-------- packages/analytics/README.md | 76 ++++++++++-------- packages/analytics/src/pluck.js | 121 ++++++++++++++++++----------- packages/analytics/src/reducing.js | 13 +++- 4 files changed, 174 insertions(+), 112 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index 3de4331e2..6ea00cf97 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1085,52 +1085,64 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### pluck -Take `Object` object getting value of fields (with json `path`) and throws an -object for each value +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 -[ -{ "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 } -] + [ + { "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 } + ] ``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use form group by (optional, default `id`) +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who need to be extrated
+
  • chemin de l'élément qui doit être extrais
(optional, default `id`) -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)}** ### reducing @@ -1177,10 +1189,12 @@ plugin = analytics #### 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 who will be use as the key
+
  • chemin de l'élément qui vas être utilisé comme clé
(optional, default `id`) +- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who will be merge into an array
+
  • chemin de l'élément qui vas être fussioné en un tableau
(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: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)>}** ### segment diff --git a/packages/analytics/README.md b/packages/analytics/README.md index 3de4331e2..6ea00cf97 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1085,52 +1085,64 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### pluck -Take `Object` object getting value of fields (with json `path`) and throws an -object for each value +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 -[ -{ "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 } -] + [ + { "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 } + ] ``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** path to use form group by (optional, default `id`) +- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who need to be extrated
+
  • chemin de l'élément qui doit être extrais
(optional, default `id`) -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)}** ### reducing @@ -1177,10 +1189,12 @@ plugin = analytics #### 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 who will be use as the key
+
  • chemin de l'élément qui vas être utilisé comme clé
(optional, default `id`) +- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who will be merge into an array
+
  • chemin de l'élément qui vas être fussioné en un tableau
(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: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)>}** ### segment diff --git a/packages/analytics/src/pluck.js b/packages/analytics/src/pluck.js index a4807354f..88d487d5c 100644 --- a/packages/analytics/src/pluck.js +++ b/packages/analytics/src/pluck.js @@ -1,68 +1,95 @@ import get from 'lodash.get'; 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] + *
  • path of the element who need to be extrated
+ *
  • chemin de l'élément qui doit être extrais
+ * @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; diff --git a/packages/analytics/src/reducing.js b/packages/analytics/src/reducing.js index 3b5b13d6a..c325c62ea 100644 --- a/packages/analytics/src/reducing.js +++ b/packages/analytics/src/reducing.js @@ -73,8 +73,15 @@ const reducing = async (data, feed, ctx) => { * ``` * * @name reducing - * @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 who will be use as the key
+ *
  • chemin de l'élément qui vas être utilisé comme clé
+ * @param {String} [value=value] + *
  • path of the element who will be merge into an array
+ *
  • chemin de l'élément qui vas être fussioné en un tableau
+ * @returns {{ + * id: String, + * value: Array + * }} */ export default reducing; From eaa00a52794699f338a3332e433a26a2558871f6 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 31 Jan 2024 08:49:01 +0100 Subject: [PATCH 23/26] docs (analytics): fix nitpick typo --- docs/plugin-analytics.md | 2 +- packages/analytics/README.md | 2 +- packages/analytics/src/segment.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index 6ea00cf97..45ed467df 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1295,7 +1295,7 @@ plugin = analytics - `identifier` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who will be put in value field (if not set, fallback to `1`)
  • chemin de l'élément qui sera mis dans le champ valeur (si non défini, fallback à `1`)
(optional, default `false`) -Returns **{id: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>, value: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}** +Returns **{id: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>, value: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}** ### slice diff --git a/packages/analytics/README.md b/packages/analytics/README.md index 6ea00cf97..45ed467df 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1295,7 +1295,7 @@ plugin = analytics - `identifier` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who will be put in value field (if not set, fallback to `1`)
  • chemin de l'élément qui sera mis dans le champ valeur (si non défini, fallback à `1`)
(optional, default `false`) -Returns **{id: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>, value: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}** +Returns **{id: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>, value: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}** ### slice diff --git a/packages/analytics/src/segment.js b/packages/analytics/src/segment.js index 0f7c09965..4024299fe 100644 --- a/packages/analytics/src/segment.js +++ b/packages/analytics/src/segment.js @@ -148,7 +148,7 @@ const segment = (data, feed, ctx) => { *
  • path of the element who will be put in value field (if not set, fallback to `1`)
*
  • chemin de l'élément qui sera mis dans le champ valeur (si non défini, fallback à `1`)
* @returns {{ - * id: Array, + * id: Array, * value: Object, * }} */ From 29470638cde211323c00538032c73dbf775e6f9f Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:22:40 +0100 Subject: [PATCH 24/26] docs (analytics): reformat pair doc --- docs/plugin-analytics.md | 50 ++++++++------- packages/analytics/README.md | 50 ++++++++------- packages/analytics/src/pair.js | 108 ++++++++++++++++++++------------- 3 files changed, 123 insertions(+), 85 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index 45ed467df..9f982a4e8 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -1042,46 +1042,54 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### pair -Take `Object` object getting some fields with json path, and -throw all pair of value from two fields +Create a pair with 'id' containing a pair of the given 'path's and 'value' set to 1. -```json -[ - { departure: ['tokyo', 'nancy'], arrival: 'toul' }, - { departure: ['paris', 'nancy'], arrival: 'toul' }, - { departure: ['london', 'berlin'], arrival: 'toul' }, -] -``` +Créer un couple 'id' contenent un couple des 'path's donnée et 'value' mise à 1. -Script: +##### Script / Scénario ```ini +; Import analytics plugin required to use "pair" +; Importation du plugin analytique nécessaire pour utiliser "pair" [use] plugin = analytics +; Using "pair" with 'departure' and 'arrival' as paths setttings +; Utilisation de "pair" avec 'departure' et 'arrival' comme paramètres de paths [pair] path = departure path = arrival ``` -Output: +##### Input / Entrée ```json -[ - { "id": [ "tokyo", "toul" ], "value": 1 }, -{ "id": [ "nancy", "toul" ], "value": 1 }, -{ "id": [ "paris", "toul" ], "value": 1 }, - { "id": [ "nancy", "toul" ], "value": 1 }, - { "id": [ "london", "toul" ], "value": 1 }, - { "id": [ "berlin", "toul" ], "value": 1 } -] + [ + { "departure": ["tokyo", "nancy"], "arrival": "toul" }, + { "departure": ["paris", "nancy"], "arrival": "toul" }, + { "departure": ["london", "berlin"], "arrival": "toul" } + ] +``` + +##### Output / Sortie + +```json + [ + { "id": ["tokyo", "toul"], "value": 1 }, + { "id": ["nancy", "toul"], "value": 1 }, + { "id": ["paris", "toul"], "value": 1 }, + { "id": ["nancy", "toul"], "value": 1 }, + { "id": ["london", "toul"], "value": 1 }, + { "id": ["berlin", "toul"], "value": 1 } + ] ``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** +- `null` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who will be use to create the pair
+
  • chemin de l'élément qui vas etre utilisé pour créer le couple
-Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** +Returns **{id: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>, value: `1`}** ### pluck diff --git a/packages/analytics/README.md b/packages/analytics/README.md index 45ed467df..9f982a4e8 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -1042,46 +1042,54 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### pair -Take `Object` object getting some fields with json path, and -throw all pair of value from two fields +Create a pair with 'id' containing a pair of the given 'path's and 'value' set to 1. -```json -[ - { departure: ['tokyo', 'nancy'], arrival: 'toul' }, - { departure: ['paris', 'nancy'], arrival: 'toul' }, - { departure: ['london', 'berlin'], arrival: 'toul' }, -] -``` +Créer un couple 'id' contenent un couple des 'path's donnée et 'value' mise à 1. -Script: +##### Script / Scénario ```ini +; Import analytics plugin required to use "pair" +; Importation du plugin analytique nécessaire pour utiliser "pair" [use] plugin = analytics +; Using "pair" with 'departure' and 'arrival' as paths setttings +; Utilisation de "pair" avec 'departure' et 'arrival' comme paramètres de paths [pair] path = departure path = arrival ``` -Output: +##### Input / Entrée ```json -[ - { "id": [ "tokyo", "toul" ], "value": 1 }, -{ "id": [ "nancy", "toul" ], "value": 1 }, -{ "id": [ "paris", "toul" ], "value": 1 }, - { "id": [ "nancy", "toul" ], "value": 1 }, - { "id": [ "london", "toul" ], "value": 1 }, - { "id": [ "berlin", "toul" ], "value": 1 } -] + [ + { "departure": ["tokyo", "nancy"], "arrival": "toul" }, + { "departure": ["paris", "nancy"], "arrival": "toul" }, + { "departure": ["london", "berlin"], "arrival": "toul" } + ] +``` + +##### Output / Sortie + +```json + [ + { "id": ["tokyo", "toul"], "value": 1 }, + { "id": ["nancy", "toul"], "value": 1 }, + { "id": ["paris", "toul"], "value": 1 }, + { "id": ["nancy", "toul"], "value": 1 }, + { "id": ["london", "toul"], "value": 1 }, + { "id": ["berlin", "toul"], "value": 1 } + ] ``` #### Parameters -- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** +- `null` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
  • path of the element who will be use to create the pair
+
  • chemin de l'élément qui vas etre utilisé pour créer le couple
-Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** +Returns **{id: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>, value: `1`}** ### pluck diff --git a/packages/analytics/src/pair.js b/packages/analytics/src/pair.js index 4a0401219..2a5859f99 100644 --- a/packages/analytics/src/pair.js +++ b/packages/analytics/src/pair.js @@ -2,52 +2,19 @@ import get from 'lodash.get'; import core from './core'; /** - * Take `Object` object getting some fields with json path, and - * throw all pair of value from two fields - * - * ```json - * [ - * { departure: ['tokyo', 'nancy'], arrival: 'toul' }, - * { departure: ['paris', 'nancy'], arrival: 'toul' }, - * { departure: ['london', 'berlin'], arrival: 'toul' }, - * ] - * ``` - * - * Script: - * - * ```ini - * [use] - * plugin = analytics - * - * [pair] - * path = departure - * path = arrival - * - * ``` - * - * Output: - * - * ```json - * [ - * { "id": [ "tokyo", "toul" ], "value": 1 }, - * { "id": [ "nancy", "toul" ], "value": 1 }, - * { "id": [ "paris", "toul" ], "value": 1 }, - * { "id": [ "nancy", "toul" ], "value": 1 }, - * { "id": [ "london", "toul" ], "value": 1 }, - * { "id": [ "berlin", "toul" ], "value": 1 } - * ] - * ``` - * - * @name pair - * @param {String} path - * @returns {Object} + * Pair 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} */ -export default function pair(data, feed) { - if (this.isLast()) { +const pair = (data, feed, ctx) => { + if (ctx.isLast()) { feed.close(); return; } - let fields = this.getParam('path', []); + let fields = ctx.getParam('path', []); if (!Array.isArray(fields)) { fields = [fields]; } @@ -68,4 +35,59 @@ export default function pair(data, feed) { }); feed.end(); -} +}; + +/** + * Create a pair with 'id' containing a pair of the given 'path's and 'value' set to 1. + * + * Créer un couple 'id' contenent un couple des 'path's donnée et 'value' mise à 1. + * + * #### Script / Scénario + * + * ```ini + * ; Import analytics plugin required to use "pair" + * ; Importation du plugin analytique nécessaire pour utiliser "pair" + * [use] + * plugin = analytics + * + * ; Using "pair" with 'departure' and 'arrival' as paths setttings + * ; Utilisation de "pair" avec 'departure' et 'arrival' comme paramètres de paths + * [pair] + * path = departure + * path = arrival + * + * ``` + * + * #### Input / Entrée + * + * ```json + * [ + * { "departure": ["tokyo", "nancy"], "arrival": "toul" }, + * { "departure": ["paris", "nancy"], "arrival": "toul" }, + * { "departure": ["london", "berlin"], "arrival": "toul" } + * ] + * ``` + * + * #### Output / Sortie + * + * ```json + * [ + * { "id": ["tokyo", "toul"], "value": 1 }, + * { "id": ["nancy", "toul"], "value": 1 }, + * { "id": ["paris", "toul"], "value": 1 }, + * { "id": ["nancy", "toul"], "value": 1 }, + * { "id": ["london", "toul"], "value": 1 }, + * { "id": ["berlin", "toul"], "value": 1 } + * ] + * ``` + * + * @name pair + * @param {String} + *
  • path of the element who will be use to create the pair
+ *
  • chemin de l'élément qui vas etre utilisé pour créer le couple
+ * @returns {{ + * id: Array, + * value: 1 + * }} + */ +export default pair; From 91d2410ee0be94850c6e3f6373be551d89873890 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 31 Jan 2024 11:04:38 +0100 Subject: [PATCH 25/26] refactor (analytics): remove code added for no reason --- packages/analytics/src/slice.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/analytics/src/slice.js b/packages/analytics/src/slice.js index db4497be4..da387b337 100644 --- a/packages/analytics/src/slice.js +++ b/packages/analytics/src/slice.js @@ -25,7 +25,6 @@ const slice = (data, feed, ctx) => { } feed.end(); } - [].slice(); }; /** From 67ee08d1e39bb521bcba5a465adc0945f86bbb82 Mon Sep 17 00:00:00 2001 From: AlasDiablo <25723276+AlasDiablo@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:55:11 +0100 Subject: [PATCH 26/26] refactor (analytics): reformat output doc --- docs/plugin-analytics.md | 75 +++++++++++++-------- packages/analytics/README.md | 75 +++++++++++++-------- packages/analytics/src/output.js | 110 +++++++++++++++++++++---------- 3 files changed, 169 insertions(+), 91 deletions(-) diff --git a/docs/plugin-analytics.md b/docs/plugin-analytics.md index 9f982a4e8..d741b7571 100644 --- a/docs/plugin-analytics.md +++ b/docs/plugin-analytics.md @@ -996,49 +996,66 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### output -Format the output with data a meta +Create an output string containing all incoming elements in a `data` array. +with given `meta` extracted into an object called `meta`. -#### Parameters - -- `indent` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** indent or not (optional, default `false`) -- `meta` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>?** fields to be considered as metadata - object +Créer une sortie en chain de caratere avec les element entrent mise dans un tableau nommé `data` +eyent les donnée `meta` extrais et mises dans un objet appelé `meta`. -#### Examples +##### Script / Scénario -Input +###### ini +```ini +; Import analytics plugin required to use "output" +; Importation du plugin analytique nécessaire pour utiliser "output" +[use] +plugin = analytics -```javascript -[ - { _id: 1, value: 2, total: 2 }, - { _id: 2, value: 4, total: 2 } -] +; Using "output" with 'indent' as true and 'meta' as total +; Utilisation de "output" avec 'indent' à vrai et total comme paramètres de 'meta' +[output] +indent = true +meta = total ``` -Script - +##### Input / Entrée -```javascript -.pipe(ezs('output', { meta: 'total' })) +```json + [ + { "_id": 1, "value": 2, "total": 2 }, + { "_id": 2, "value": 4, "total": 2 } + ] ``` -Output +##### Output / Sortie +!!! Attention: This is an output function that can only be used at the end of an EZS script. !!! +!!! The output is a string and can't be used with other EZS functions. !!! -```javascript -{ - data: [ - { _id: 1, value: 2 }, - { _id: 2, value: 4 } - ], - meta: { - total: 2 - } -} +!!! Attention : Ceci est une fonction de sortie, Elle peut uniquement etre utilisé à la fin d'un script ezs !!! +!!! Cette sortie est une chaine de carater et ne peut pas etre utilisé avec d'autre fonction ezs !!! + +```json + { + "data": [ + { "_id": 1, "value": 2 }, + { "_id": 2, "value": 4 } + ], + "meta": { + "total": 2 + } + } ``` -Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** +#### Parameters + +- `indent` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
  • indent the output json
+
  • indenté le json de sortie
(optional, default `false`) +- `meta` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?**
  • element from the input to put it in the `meta` object
+
  • élément a extraire de l'entrée et a mettre dans l'objet `meta`
+ +Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** ### pair diff --git a/packages/analytics/README.md b/packages/analytics/README.md index 9f982a4e8..d741b7571 100644 --- a/packages/analytics/README.md +++ b/packages/analytics/README.md @@ -996,49 +996,66 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ### output -Format the output with data a meta +Create an output string containing all incoming elements in a `data` array. +with given `meta` extracted into an object called `meta`. -#### Parameters - -- `indent` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** indent or not (optional, default `false`) -- `meta` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>?** fields to be considered as metadata - object +Créer une sortie en chain de caratere avec les element entrent mise dans un tableau nommé `data` +eyent les donnée `meta` extrais et mises dans un objet appelé `meta`. -#### Examples +##### Script / Scénario -Input +###### ini +```ini +; Import analytics plugin required to use "output" +; Importation du plugin analytique nécessaire pour utiliser "output" +[use] +plugin = analytics -```javascript -[ - { _id: 1, value: 2, total: 2 }, - { _id: 2, value: 4, total: 2 } -] +; Using "output" with 'indent' as true and 'meta' as total +; Utilisation de "output" avec 'indent' à vrai et total comme paramètres de 'meta' +[output] +indent = true +meta = total ``` -Script - +##### Input / Entrée -```javascript -.pipe(ezs('output', { meta: 'total' })) +```json + [ + { "_id": 1, "value": 2, "total": 2 }, + { "_id": 2, "value": 4, "total": 2 } + ] ``` -Output +##### Output / Sortie +!!! Attention: This is an output function that can only be used at the end of an EZS script. !!! +!!! The output is a string and can't be used with other EZS functions. !!! -```javascript -{ - data: [ - { _id: 1, value: 2 }, - { _id: 2, value: 4 } - ], - meta: { - total: 2 - } -} +!!! Attention : Ceci est une fonction de sortie, Elle peut uniquement etre utilisé à la fin d'un script ezs !!! +!!! Cette sortie est une chaine de carater et ne peut pas etre utilisé avec d'autre fonction ezs !!! + +```json + { + "data": [ + { "_id": 1, "value": 2 }, + { "_id": 2, "value": 4 } + ], + "meta": { + "total": 2 + } + } ``` -Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** +#### Parameters + +- `indent` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
  • indent the output json
+
  • indenté le json de sortie
(optional, default `false`) +- `meta` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?**
  • element from the input to put it in the `meta` object
+
  • élément a extraire de l'entrée et a mettre dans l'objet `meta`
+ +Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** ### pair diff --git a/packages/analytics/src/output.js b/packages/analytics/src/output.js index 22ca16591..cdfa59cf2 100644 --- a/packages/analytics/src/output.js +++ b/packages/analytics/src/output.js @@ -1,48 +1,28 @@ import get from 'lodash.get'; import unset from 'lodash.unset'; + /** - * Format the output with data a meta - * - * @example Input - * [ - * { _id: 1, value: 2, total: 2 }, - * { _id: 2, value: 4, total: 2 } - * ] - * - * @example Script - * .pipe(ezs('output', { meta: 'total' })) - * - * @example Output - * { - * data: [ - * { _id: 1, value: 2 }, - * { _id: 2, value: 4 } - * ], - * meta: { - * total: 2 - * } - * } - * - * @name output - * @param {boolean} [indent=false] indent or not - * @param {string[]} [meta] fields to be considered as metadata - * object - * @returns {string} + * Pair 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} */ -function output(data, feed) { - const indent = this.getParam('indent', false); - const extract = this.getParam('meta'); +const output = (data, feed, ctx) => { + const indent = ctx.getParam('indent', false); + const extract = ctx.getParam('meta'); const extracts = Array.isArray(extract) ? extract : [extract]; const keys = extracts.filter((x) => x); const cr = indent ? '\n ' : ''; const json = (d) => JSON.stringify(d, null, indent ? ' ' : null); - if (this.isLast()) { + if (ctx.isLast()) { feed.write(`]}${cr}`); return feed.close(); } - if (this.isFirst() && !this.isLast()) { + if (ctx.isFirst() && !ctx.isLast()) { const values = keys.map((p) => get(data, p)); feed.write(`{${cr}"meta":{${cr}`); if (keys.length > 0) { @@ -64,7 +44,71 @@ function output(data, feed) { keys.forEach((p) => unset(data, p)); feed.write(json(data)); return feed.end(); -} +}; + +/** + * Create an output string containing all incoming elements in a `data` array. + * with given `meta` extracted into an object called `meta`. + * + * Créer une sortie en chain de caratere avec les element entrent mise dans un tableau nommé `data` + * eyent les donnée `meta` extrais et mises dans un objet appelé `meta`. + * + * #### Script / Scénario + * + * ##### ini + * + * ```ini + * ; Import analytics plugin required to use "output" + * ; Importation du plugin analytique nécessaire pour utiliser "output" + * [use] + * plugin = analytics + * + * ; Using "output" with 'indent' as true and 'meta' as total + * ; Utilisation de "output" avec 'indent' à vrai et total comme paramètres de 'meta' + * [output] + * indent = true + * meta = total + * + * ``` + * + * #### Input / Entrée + * + * ```json + * [ + * { "_id": 1, "value": 2, "total": 2 }, + * { "_id": 2, "value": 4, "total": 2 } + * ] + * ``` + * + * #### Output / Sortie + * + * !!! Attention: This is an output function that can only be used at the end of an EZS script. !!! + * !!! The output is a string and can't be used with other EZS functions. !!! + * + * !!! Attention : Ceci est une fonction de sortie, Elle peut uniquement etre utilisé à la fin d'un script ezs !!! + * !!! Cette sortie est une chaine de carater et ne peut pas etre utilisé avec d'autre fonction ezs !!! + * + * ```json + * { + * "data": [ + * { "_id": 1, "value": 2 }, + * { "_id": 2, "value": 4 } + * ], + * "meta": { + * "total": 2 + * } + * } + * ``` + * + * @name output + * @param {Boolean} [indent=false] + *
  • indent the output json
+ *
  • indenté le json de sortie
+ * @param {String} [meta] + *
  • element from the input to put it in the `meta` object
+ *
  • élément a extraire de l'entrée et a mettre dans l'objet `meta`
+ * @returns {String} + */ export default { output, };