Skip to content

Commit

Permalink
refactor (analytics): reformat output doc
Browse files Browse the repository at this point in the history
  • Loading branch information
AlasDiablo committed Jan 31, 2024
1 parent 91d2410 commit 67ee08d
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 91 deletions.
75 changes: 46 additions & 29 deletions docs/plugin-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)** <ul><li>indent the output json</li></ul>
<ul><li>indenté le json de sortie</li></ul> (optional, default `false`)
- `meta` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** <ul><li>element from the input to put it in the `meta` object</li></ul>
<ul><li>élément a extraire de l'entrée et a mettre dans l'objet `meta`</li></ul>

Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**

### pair

Expand Down
75 changes: 46 additions & 29 deletions packages/analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)&lt;[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)** <ul><li>indent the output json</li></ul>
<ul><li>indenté le json de sortie</li></ul> (optional, default `false`)
- `meta` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** <ul><li>element from the input to put it in the `meta` object</li></ul>
<ul><li>élément a extraire de l'entrée et a mettre dans l'objet `meta`</li></ul>

Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**

### pair

Expand Down
110 changes: 77 additions & 33 deletions packages/analytics/src/output.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,28 @@
import get from 'lodash.get';
import unset from 'lodash.unset';

/**
* Format the output with data a meta
*
* @example <caption>Input</caption>
* [
* { _id: 1, value: 2, total: 2 },
* { _id: 2, value: 4, total: 2 }
* ]
*
* @example <caption>Script</caption>
* .pipe(ezs('output', { meta: 'total' }))
*
* @example <caption>Output</caption>
* {
* 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) {
Expand All @@ -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]
* <ul><li>indent the output json</li></ul>
* <ul><li>indenté le json de sortie</li></ul>
* @param {String} [meta]
* <ul><li>element from the input to put it in the `meta` object</li></ul>
* <ul><li>élément a extraire de l'entrée et a mettre dans l'objet `meta`</li></ul>
* @returns {String}
*/
export default {
output,
};

0 comments on commit 67ee08d

Please sign in to comment.