0.4.2 • Published 6 years ago

mergeon v0.4.2

Weekly downloads
111
License
MIT
Repository
github
Last release
6 years ago

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

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

TypeDefaultRequired
string | objectundefinedyes

context

TypeDefaultRequired
stringprocess.cwd()no

Note: This option is not available to CLI

extendKey

TypeDefaultRequired
string"_extends"no

mergeCustomizer

TypeDefaultRequired
functionundefinedno

This function will be passed as customizer to lodash’s _.mergeWith method.

Note: This option is not available to CLI

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.1

6 years ago

0.2.3

6 years ago

0.2.4

6 years ago

0.2.5

6 years ago

0.2.6

6 years ago

0.2.7

6 years ago

0.2.8

6 years ago

0.2.2

6 years ago

0.2.0

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago