https://github.com/schapka/mergeon
Loading extendable JSON structures
https://github.com/schapka/mergeon
cli data extend include inheritance json merge partials
Last synced: 8 months ago
JSON representation
Loading extendable JSON structures
- Host: GitHub
- URL: https://github.com/schapka/mergeon
- Owner: schapka
- Created: 2017-12-17T06:31:32.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-23T14:23:31.000Z (about 8 years ago)
- Last Synced: 2025-04-20T00:39:00.558Z (10 months ago)
- Topics: cli, data, extend, include, inheritance, json, merge, partials
- Language: JavaScript
- Homepage:
- Size: 84 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/mergeon)
[](https://travis-ci.org/schapka/mergeon)
[](https://ci.appveyor.com/project/schapka/mergeon/branch/master)
# Mergeon
**Loading extendable JSON structures:**
* Load data from different JSON files
* Use wildcards/globs
* Override inherited values
* Merge data
* Customize merging
## Contents
* [Installation](#installation)
* [Examples](#examples)
* [Simple use case](#simple-use-case)
* [Target path](#target-path)
* [Wildcards](#wildcards)
* [Usage](#usage)
* [JavaScript API](#javascript-api)
* [CLI](#cli)
* [Options](#options)
* [entry](#entry)
* [context](#context)
* [extendKey](#extendkey)
* [mergeCustomizer](#mergecustomizer)
## Installation
**npm**
```text
npm i -S mergeon
```
**yarn**
```text
yarn add mergeon
```
## Examples
### Simple use case
Load data from different JSON file and override values:
**`entry.json`**
```json
{
"_extends": "./default-data.json",
"title": "New title",
"image": {
"alt": "New alt value"
}
}
```
**`default-data.json`**
```json
{
"title": "Default title",
"image": {
"src": "path/to/default/image.jpg",
"alt": "Default alt value"
}
}
```
**Result**
```json
{
"title": "New title",
"image": {
"src": "path/to/default/image.jpg",
"alt": "New alt value"
}
}
```
### Target path
Define a target path by using special syntax (`:`):
**`entry.json`**
```json
{
"_extends:target.path": "./default-data.json"
}
```
**`default-data.json`**
```json
{
"title": "Default title"
}
```
**Result**
```json
{
"target": {
"path": {
"title": "Default title"
}
}
}
```
### Wildcards
Load multiple files by adding wildcards:
**`entry.json`**
```json
{
"_extends:buttons": "./buttons/*.json"
}
```
**`buttons/primary.json`**
```json
{
"type": "primary"
}
```
**`buttons/secondary.json`**
```json
{
"type": "secondary"
}
```
**Result**
```json
{
"buttons": {
"primary": {
"type": "primary"
},
"secondary": {
"type": "secondary"
}
}
}
```
### Additional examples
See [test directory](https://github.com/schapka/mergeon/tree/master/test) for additional examples, including **wildcards**, **globstars**, **customized merging** and many more.
## Usage
### JavaScript API
```js
import mergeon from 'mergeon';
mergeon
.load({
entry: 'data/entry.json',
})
.then(result => {
const jsonString = JSON.stringify(result.data, null, 2);
/* ... */
})
.catch(error => {
/* ... */
});
```
### CLI
```text
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`](https://lodash.com/docs/4.17.4#mergeWith) method.
> **Note:** This option is not available to CLI