freemarker-to-json2.js

0.3.0 • Public • Published

freemarker-to-json2.js

This module take a yaml file or a json file and convert it to a freemarker file that output the data as valid json format.

Example:

Convert this

name: paul
id: 1
favorite: 
  type: food
    value: ramen
  type: drink
    value: milk-tea

or

{
  "name": "paul",
  "id": 1,
  "favorite": [{
    "type": "food",
    "value": "ramen"
  }, {
    "type": "drink",
    "value": "milk-tea"
  }]
}

to

{
  "name": ${get(name)},
  
  "id": ${get(id)},
  
  "favorite": 
  <@arrayFrame favorite; item>{
    "type": ${get(item, 'type')},
 
    "value": ${get(item, 'value')}
  }</@arrayFrame>
}

which the get, arrayFrame is just a convenient way to transform ftl data to valid json.

<#-- A simple helper to convert ftl data to valid json value -->
<#function value input="">
  <#if input?is_number>
    <#return input?c>
 
  <#elseif input?is_boolean>
    <#return input?string>
 
  <#elseif input?is_string>
    <#return '"' + input?js_string + '"'>
 
  <#elseif input?is_date>
    <#return '"' + input?string["yyyy/MM/dd HH:mm:ss"] + '"'>
 
  </#if>
</#function>
 
<#-- A lodash.get alike helper -->
<#function get object="" path="" default='""'>
    <#if object?is_hash && path != "">
        <#local childs = path?split(".")>
        <#list childs as child>
            <#if object[child]??>
                <#local object = object[child]>
            <#else>
                <#return default>
            </#if>
        </#list>
    </#if>
 
    <#return value(object)>
</#function>
 
<#-- A simple helper to wrap freemarker `#list` with json array -->
<#macro arrayFrame items=[]>
    <#compress>
    [
        <#list items as item>
            <#nested item><#if item_has_next>,</#if>
        </#list>
    ]
  </#compress>
</#macro>

Usage

const transform = require('freemarker-to-json2')
 
transform('input.yaml', 'output.ftl')
  .then(result => console.log(result)) // same as output.ftl

Test

npm test

Package Sidebar

Install

npm i freemarker-to-json2.js

Weekly Downloads

3

Version

0.3.0

License

MIT

Last publish

Collaborators

  • thammin