1.0.0 • Published 7 years ago

json-partial v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
7 years ago

json-partial

This module will allow to use json "partials" and expand them into another json file. Sometimes you have one or more json snippets and you may want to use them to build a bigger json file. One example is when you are using json to generate templates with handlebars or mustache.

Installation

npm install json-partial

Usage

Suppose you have a JSON snippet that is repeated almost without change in several places of a large JSON file.

{
    "charRemaining": "50",
    "counterText": "characters remaining",
    "fieldName": "customerName",
    "fieldSize": "large",
    "labelPosition": "",
    "labelText": "Customer Name",
    "maxLength": "50",
    "placeHolder": "Enter customer name",
    "value": ""
}

You want to use almost the same values in two places:

{
    "userName": {
        "charRemaining": "50",
        "counterText": "characters remaining",
        "fieldName": "userName",
        "fieldSize": "large",
        "labelPosition": "",
        "labelText": "User Name",
        "maxLength": "50",
        "placeHolder": "enter user name",
        "value": ""
    },
    "password": {
        "charRemaining": "50",
        "counterText": "characters remaining",
        "fieldName": "password",
        "fieldSize": "large",
        "labelPosition": "",
        "labelText": "Password",
        "maxLength": "50",
        "placeHolder": "enter password",
        "value": "",
        "inputType": "password"
    }
}

With json-partial you can rewrite the file like this:

{
    "jsonx": [
        {
            "file": "bar.json",
            "field": "userName",
            "data": {
                "fieldName": "userName",
                "labelText": "User Name",
                "placeHolder": "enter user name"
            }
        },
        {
            "file": "bar.json",
            "field": "password",
            "data": {
                "fieldName": "password",
                "labelText": "Password",
                "placeHolder": "enter password",
                "inputType": "password"
            }
        }
    ]
}

You will have several advantages: 1. The file is cleaner and only the data that changes is required 1. You will need to modify only one instance of the snippet in case of a change or bugs