README
Agnese -
Save thouthands of lines in mappings.
Table of Contents
Features
- Convert source data to objects, arrays, strings or simple data based on mapping configuration.
- Read mapping configuration from JSON file.
- Conditional fields/items powered by Quara*.
- Array iteration.
- Possibility of define a default value for a field.
- Assign values by their paths in source data.
- Force types and casting.
- Use of custom functions to preprocess values before assign them.
- Values from conditional path (switch).
- YAML optional support.
*Quara is a simple JavaScript interpreted language that will be available soon as a separate module.
Installation
Agnese is available as an NPM package:
npm install agnese
If you prefer download it from Github Packages (take a look at the documentation):
npm install @wandeber/agnese
Getting started
We will use the next source data in the examples throughout this document:
const sourceData = {
name: "Gohan",
surname: "Son",
isAlive: true,
isDeath: false,
alias: [
"Great Saiyaman",
"The Golden Fighter",
"The Golden Warrior",
"The Chosen One",
"Monkey boy"
],
characteristics: {
race: "Human/Saiyan",
gender: "Male",
age: "10",
height: 176.5,
weight: 61
},
transformations: [
{
name: "Super Saiyan",
power: "Uff...",
level: 1
},
{
name: "Super Saiyan II",
power: "Ask Cell... (De locos...)",
level: 2
}
]
};
Simple mapping example:
One of the most awesome things about this module is you can keep all your mapping info in a separate JSON file.
map-info.json
{
"type": "Object",
"fields": [
{
"name": "lastname",
"if": {
"quara": "surname == \"Son\""
},
"value": {
"fromPath": "surname"
}
}
]
}
JavaScript code to map from JSON file
let mapper = new Agnese();
mapper.setMapInfo("map-info.json");
let target = mapper.map(sourceData);
The variable target
will contain the next object:
{
lastname: "Son"
}
Since the code is almost completely common to any case of use, sometimes you will only find the map info or settings in future examples of this document.
While I complete this documentation, I recommend you take a look at the examples used in the unit tests.
Easy CSV generation
If you think on CSV as an object with one unique level, you can easily map any data to CSV or other similar structure.