Skip to content

Commit

Permalink
refactor: first draft of a small docs and test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlasDiablo committed Dec 14, 2023
1 parent 0667dff commit 640c8e3
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 35 deletions.
67 changes: 55 additions & 12 deletions docs/plugin-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)** <ul><li>path of the element used to create the unified identifier</li></ul>
<ul><li>chemin de l'élément utilisé pour créer l'identifiant unifié</li></ul> (optional, default `id`)
- `method` **(`"natural"` \| `"levenshtein"` \| `"numerical"`)** <ul><li>method used to create the unified identifier</li></ul>
<ul><ul><li>natural - Create a normalised identifier that is set to a fixed length</li></ul></ul>
<ul><ul><li>levenshtein - Create an identifier based on the Levenshtein algorithm</li></ul></ul>
<ul><ul><li>numerical - Create an identifier based on a numeric value</li></ul></ul>
<ul><li>méthode utilisée pour créer l'identifiant unifié</li></ul>
<ul><ul><li>natural - Créer un identifiant normalisé de longueur fixe</li></ul></ul>
<ul><ul><li>levenshtein - Créer un identifiant basé sur l'algorithme de Levenshtein</li></ul></ul>
<ul><ul><li>numerical - Créer un identifiant basé sur une valeur numérique</li></ul></ul> (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

Expand Down
67 changes: 55 additions & 12 deletions packages/analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)** <ul><li>path of the element used to create the unified identifier</li></ul>
<ul><li>chemin de l'élément utilisé pour créer l'identifiant unifié</li></ul> (optional, default `id`)
- `method` **(`"natural"` \| `"levenshtein"` \| `"numerical"`)** <ul><li>method used to create the unified identifier</li></ul>
<ul><ul><li>natural - Create a normalised identifier that is set to a fixed length</li></ul></ul>
<ul><ul><li>levenshtein - Create an identifier based on the Levenshtein algorithm</li></ul></ul>
<ul><ul><li>numerical - Create an identifier based on a numeric value</li></ul></ul>
<ul><li>méthode utilisée pour créer l'identifiant unifié</li></ul>
<ul><ul><li>natural - Créer un identifiant normalisé de longueur fixe</li></ul></ul>
<ul><ul><li>levenshtein - Créer un identifiant basé sur l'algorithme de Levenshtein</li></ul></ul>
<ul><ul><li>numerical - Créer un identifiant basé sur une valeur numérique</li></ul></ul> (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

Expand Down
70 changes: 59 additions & 11 deletions packages/analytics/src/tune.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
* <ul><li>path of the element used to create the unified identifier</li></ul>
* <ul><li>chemin de l'élément utilisé pour créer l'identifiant unifié</li></ul>
* @param {'natural' | 'levenshtein' | 'numerical'} [method=natural]
* <ul><li>method used to create the unified identifier</li></ul>
* <ul><ul><li>natural - Create a normalised identifier that is set to a fixed length</li></ul></ul>
* <ul><ul><li>levenshtein - Create an identifier based on the Levenshtein algorithm</li></ul></ul>
* <ul><ul><li>numerical - Create an identifier based on a numeric value</li></ul></ul>
* <ul><li>méthode utilisée pour créer l'identifiant unifié</li></ul>
* <ul><ul><li>natural - Créer un identifiant normalisé de longueur fixe</li></ul></ul>
* <ul><ul><li>levenshtein - Créer un identifiant basé sur l'algorithme de Levenshtein</li></ul></ul>
* <ul><ul><li>numerical - Créer un identifiant basé sur une valeur numérique</li></ul></ul>
* @returns {{
* id: String,
* value: Object
* }}
*/
export default function tune(data, feed) {
if (this.isLast()) {
Expand Down
42 changes: 42 additions & 0 deletions packages/analytics/test/tune.spec.js
Original file line number Diff line number Diff line change
@@ -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]);
});
});

0 comments on commit 640c8e3

Please sign in to comment.