Socket
Socket
Sign inDemoInstall

freemarker-to-json2.js

Package Overview
Dependencies
13
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    freemarker-to-json2.js

Get data easier from freemarker templates :zap:


Version published
Maintainers
1
Install size
945 kB
Created

Readme

Source

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

Keywords

FAQs

Last updated on 28 Feb 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc