ALMOsT is an AgiLe MOdel Transformation framework for JavaScript
npm install almostJSON
{
"elements": [
...
],
"relations": [
...
]
}
`
The only requirements set by ALMOsT are:
- A valid __Model__ is a JSON object
- A valid __Model__ has a field called elements, which is an __Array__.
Anything item of this array will be considered as an __Element__ in the model.
- A valid __Model__ has a field called relations, which is an __Array__.
Anything item of this array will be considered as a __Relationship__ between elements in the model.
No other requirements are mandated in order to start using ALMOsT.
$3
Rules are the building block of a transformation.
`JavaScript
createRule(
/ Activation Function /
function (model) { / Condition / },
/ Action Function /
function (model) { / Action / }
)
`
They are composed of two functions:
- The __Activation Function__ which decides if the rules is going to be enabled on the specific input.
- The __Action Function__ which, given the current input, generates a partial output.
There are 3 types of __Rules__:
- __Model Rules__ which are invokes once on the entire model
`JavaScript
createRule(
function (model) { / Condition / },
function (model) { / Action / }
)
`
- __Element Rules__ which are invokes once on each element in the model (i.e. the items of the elements array)
`JavaScript
createRule(
function (element, model) { / Condition / },
function (element, model) { / Action / }
)
`
- __Relation Rules__ which are invokes once on each relationship in the model (i.e. the items of the relations array)
`JavaScript
createRule(
function (relation, model) { / Condition / },
function (relation, model) { / Action / }
)
`
N.B. there is no guarantees on the execution order of transformation rules.
$3
All the partial results, generated by the transformation rules, are merged togheter by means of a reducer.
ALMOsT Provides 3 default reduction policies:
- __Model To Model__ m2m, in this case the rules output is going to be merged as if it was a partial model (i.e. it contains two fields named elements and relations which content is going to be concatenated)
- __Model To Text__ m2t, in this case the rules output is going to be merged as if it was a partial "file system".
Each partial result is an Object in which each field is an Object itself describing a __folder__ or a __file__:
- __Files__ are Objects having 2 fields:
- name a String which contains the name of the file
- content a String which contains the name of the file
Only one rules should generate a specific file
- __Folders__ are Objects having 3 fields (:
- name a String which contains the name of the folder
- isFolder always set to true
- children an Array containing Strings, each one referencing a the key of another filed in the to "file system like" Object
Multiple rules can generate the same folder, but only one rule should set name and isFolder, the elements of children will be concatenated.
- __Model To ALMOsT__ m2a the same as m2m, but Elements and Relations should follow the extra requirements defined in A bit or restrictions for extra power.
__almost-core__
$3
`JavaScript
var createTransformer = require('almost').createTransformer,
createRule = require('almost').createRule;
var transform = createTransformer({
model: [
createRule(
function (model) { / Condition / },
function (model) { / Action / }
)
],
element: [
createRule(
function (element, model) { / Condition / },
function (element, model) { / Action / }
)
],
relation: [
createRule(
function (relation, model) { / Condition / },
function (relation, model) { / Action / }
)
]
}, 'm2m');
var inputModel = {
elements: [
...
],
relations: [
...
],
}
var output = transform(input);
`
$3
While there are minimal restrictions imposed on the Model, in particular the structure of an __Element__ or a __Relationship__, ALMOsT has a suggested structure to follow.
See the documentation of __almost-extend__.
Following this structure not just enables the use of __almost-extend__, but also enables the use of the m2a reducer, which enables the developer to generate the same __Element__ in multiple rules.
Elements generated in different rules are matched by id and their attributes, and matadata, are merged using the mergeOrSingle policty defined in __almost-core__.
Under this policy Array attributes are concatenated, while Object` attributes are reducersively merged with the same policy.