json-composer

1.0.1 • Public • Published

json-composer

small utility to help to compose json files with other json files.

Travis branch Coverage Status bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies licence Conventional Commits Commitizen friendly

Usage

You need to provide a resolver (example with json from fs) :

const Composer = require('json-composer');
 
// ********** Define a resolver *************
 
const path = require('path');
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
 
async function readJSON(jsonPath) {
    try {
        const content = await readFile(jsonPath, 'utf8');
        return JSON.parse(content);
    } catch (e) {
        console.error('Error while parsing : ', e); // eslint-disable-line no-console
    }
}
 
async function resolver(cwd, filePath) {
    const finalPath = path.resolve(cwd, filePath);
 
    // don't forget to forward your resolver
    return Composer.extend(await readJSON(finalPath), finalPath, resolver);
}
 
// ********** end resolver *************
 
try {
    resolver('/', './composed.json')
        .then(r => console.log('extended', r))
        .catch(e => console.error('error', e));
} catch (e) {
    console.error('error', e);
}

Then imagine files as this :

./bru.json

{
    "bru": true
}

./bro.json

{
    "bro": 1234
}

./bar.json

{
    "zoo": "bidoo",
    "bloupi": {
        ">>": ["./bru.json", "./bro.json"]
    }
}

./composed.json

{
    ">>foo": "./bar.json",
    "hello": "world"
}

Then using your resolver above :

try {
    resolver(__dirname, './composed.json')
        .then(r => console.log('extended :', r))
        .catch(e => console.error('error', e));
} catch (e) {
    console.error('error', e);
}

Will print :

> extended : { hello: 'world', foo: { zoo: 'bidoo', bloupi: { bru: true, bro: 1234 } } }

Licence

The MIT License

Copyright 2018 (c) Gilles Coomans

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i json-composer

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

32.5 kB

Total Files

12

Last publish

Collaborators

  • nomocas