0.0.0 • Published 5 years ago

com.devaddins.rdar v0.0.0

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago
 ____      ____               ____    
|  _ \    |  _ \     __ _    |  _ \   
| |_) |   | | | |   / _` |   | |_) |  
|  _ < _  | |_| |  | (_| |_  |  _ < _ 
|_| \_(_) |____(_)  \__,_(_) |_| \_(_)
                                      

Recursive Dereference and Replace

Recursively dereference elements of an object and replace associated palceholders in a string with the values of those elements.

Placeholders

There are two supported types of placeholders.

Array Placeholders

Array placeholders will repeat for every item in the associated array. For example given this object...

let new_orders = {
    "orders": [
        { "name": "Customer One", "arrival-date": "1/1/2019" },
        { "name": "Customer Too", "arrival-date": "3/3/2019" },
        { "name": "Customer Also", "arrival-date": "9/9/2019" }
    ]
};

... and given this template ...

{{orders}}
Dear {{name}}: 
Your order has shipped and will arrive on {{arrival-date}}

{[/orders]}

... the output will look like this ...

Dear Customer One:
Your order has shipped and will arrive on 1/1/2019

Dear Customer Too:
Your order has shipped and will arrive on 3/3/2019

Dear Customer Also:
Your order has shipped and will arrive on 9/9/2019

Element Placeholders

Element placeholders can reach down the object tree and they can also be functions. For example given this object...

let greet = {
    eol: '\n',
    planets: {
        rand: {
            planet: function() {
                let plns = ['Mercury', 'Venus', 'Earth', 'Mars', 
                'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'];
                return plns[Math.floor(Math.random()*plns.length)];
            }
        } 
    }, 
    greetings: [
        {greeting: 'Good Morning'}, 
        {greeting: 'Good Afternoon'}, 
        {greeting: 'Good Night'}
    ]
}

... and given this template ...

let template = '{[greetings]}{{greeting}} {{planets.rand.planet}}{{eol}}{[/greetings]}';

... the output will look something like this ...

Good Morning Jupiter!
Good Afternoon Jupiter!
Good Night Jupiter!