mergeon

Loading extendable JSON structures

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
mergeon
500.4.26 years ago6 years agoMinified + gzip package size for mergeon in KB

Readme

npm Build Status Build status
Mergeon
Loading extendable JSON structures:
  • Load data from different JSON files
  • Use wildcards/globs
  • Override inherited values
  • Merge data
  • Customize merging

Contents

Simple use case Target path Wildcards JavaScript API CLI entry context extendKey mergeCustomizer

Installation

npm
npm i -S mergeon

yarn
yarn add mergeon

Examples

Simple use case

Load data from different JSON file and override values:
entry.json
{
  "_extends": "./default-data.json",
  "title": "New title",
  "image": {
    "alt": "New alt value"
  }
}

default-data.json
{
  "title": "Default title",
  "image": {
    "src": "path/to/default/image.jpg",
    "alt": "Default alt value"
  }
}

Result
{
  "title": "New title",
  "image": {
    "src": "path/to/default/image.jpg",
    "alt": "New alt value"
  }
}

Target path

Define a target path by using special syntax (<extendKey>:<targetPath>):
entry.json
{
  "_extends:target.path": "./default-data.json"
}

default-data.json
{
  "title": "Default title"
}

Result
{
  "target": {
    "path": {
      "title": "Default title"
    }
  }
}

Wildcards

Load multiple files by adding wildcards:
entry.json
{
  "_extends:buttons": "./buttons/*.json"
}

buttons/primary.json
{
  "type": "primary"
}

buttons/secondary.json
{
  "type": "secondary"
}

Result
{
  "buttons": {
    "primary": {
      "type": "primary"
    },
    "secondary": {
      "type": "secondary"
    }
  }
}

Additional examples

See test directory for additional examples, including wildcards, globstars, customized merging and many more.

Usage

JavaScript API

import mergeon from 'mergeon';

mergeon
  .load({
    entry: 'data/entry.json',
  })
  .then(result => {
    const jsonString = JSON.stringify(result.data, null, 2);
    /* ... */
  })
  .catch(error => {
    /* ... */
  });

CLI

mergeon data/entry.json > output.json

Options

entry

| Type | Default | Required | | ------------------------ | ----------- | -------- | | string | object | undefined | yes |

context

| Type | Default | Required | | -------- | --------------- | -------- | | string | process.cwd() | no |
Note: This option is not available to CLI

extendKey

| Type | Default | Required | | -------- | ------------ | -------- | | string | "_extends" | no |

mergeCustomizer

| Type | Default | Required | | ---------- | ----------- | -------- | | function | undefined | no |
This function will be passed as customizer to lodash’s _.mergeWith method.
Note: This option is not available to CLI