config-extends
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -14,5 +14,5 @@ { | ||
], | ||
"program": "${workspaceFolder}\\test\\index" | ||
"program": "${workspaceFolder}\\test\\test" | ||
} | ||
] | ||
} |
@@ -47,4 +47,4 @@ const Path = require('path'); | ||
} | ||
static extends(config,...args){ | ||
let _config =Object.assign(config,args); | ||
static extends(source,...args){ | ||
let _config =Object.assign(source,args); | ||
for(let k in _config)_config[k] =ConfigExtends._completeObject(_config,_config[k]); | ||
@@ -61,2 +61,3 @@ for(let k in _config)ConfigExtends._removeFlags(_config[k]); | ||
let base = ConfigExtends._getData(config,baseFullname); | ||
if(base===undefined)throw baseFullname+" not found"; | ||
if(!base['_completed'])base=ConfigExtends._completeObject(config,base); | ||
@@ -69,2 +70,3 @@ obj=ConfigExtends._extend(obj, base); | ||
let base = ConfigExtends._getData(config,baseFullname); | ||
if(base===undefined)throw baseFullname+" not found"; | ||
if(!base['_completed'])base=ConfigExtends._completeObject(config,base); | ||
@@ -71,0 +73,0 @@ obj=ConfigExtends._extend(obj, base); |
{ | ||
"name": "config-extends", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Allow to extend yaml or json files configuration", | ||
"main": "./lib/config-extends", | ||
"scripts": { | ||
"test": "node test/index.js" | ||
"test": "mocha" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
200
README.md
# Config Extends | ||
Allow to extend yaml or json files configuration | ||
Allow to extend json or yaml/json files configuration | ||
## Usage | ||
## Features | ||
- Multiple extension | ||
- Can be loaded from yaml or json files | ||
- Can load from a directory including all its files | ||
- Available from browser and node.js | ||
Install with npm | ||
## Methods | ||
### .extends(source,...args) [synchronously] | ||
This api allows to extend one or more json objects | ||
- Paramas: | ||
- source : object with extensions defined | ||
- args : others sources | ||
### .load(path,format='yaml') [asynchronous] | ||
This api allows to extend configuration from a file or several files from a path | ||
- Paramas: | ||
- path : object of file or folder | ||
- format : file format [yaml|json] | ||
Note: Only node.js | ||
## Installation | ||
``` | ||
@@ -12,18 +30,134 @@ npm i config-extends | ||
```javascript | ||
## Bundled YAML library for node.js | ||
``` | ||
const ConfigExtends = require("config-extends") | ||
... | ||
let source ={ | ||
data : {_extends:'base',d:3,e:4}, | ||
base: {a:1,b:2} | ||
}; | ||
let config = ConfigExtends.extends(source); | ||
console.log(JSON.stringify(config,null,2)); | ||
``` | ||
let config = await configExtends.load(path.join(__dirname,'tetris')); | ||
console.log(JSON.stringify(config)); | ||
## Bundled library for browsers | ||
``` | ||
<script src="config-extends.min.js"></script> | ||
let source ={ | ||
data : {_extends:'base',d:3,e:4}, | ||
base: {a:1,b:2} | ||
}; | ||
let config = ConfigExtends.extends(source); | ||
console.log(JSON.stringify(config,null,2)); | ||
``` | ||
## Examples | ||
### Simple extension | ||
``` | ||
let source ={ | ||
data : {_extends:'base',d:3,e:4}, | ||
base: {a:1,b:2} | ||
}; | ||
let config = ConfigExtends.extends(source); | ||
console.log(JSON.stringify(config,null,2)); | ||
``` | ||
Result: | ||
``` | ||
{ | ||
"data": { | ||
"d": 3, | ||
"e": 4, | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"base": { | ||
"a": 1, | ||
"b": 2 | ||
} | ||
} | ||
``` | ||
### extends in single file | ||
### Chain extension | ||
``` | ||
let source ={ | ||
data: { 1: {_extends:'base',d:3,e:4}, | ||
2: {_extends:'data.1', f:3,g:4}, | ||
}, | ||
base: {a:1,b:2} | ||
} | ||
let config = ConfigExtends.extends(source); | ||
console.log(JSON.stringify(config,null,2)); | ||
``` | ||
Result: | ||
``` | ||
"d": 3, | ||
"e": 4, | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"2": { | ||
"f": 3, | ||
"g": 4, | ||
"d": 3, | ||
"e": 4, | ||
"a": 1, | ||
"b": 2 | ||
} | ||
}, | ||
"base": { | ||
"a": 1, | ||
"b": 2 | ||
} | ||
} | ||
``` | ||
### Multiple extension | ||
``` | ||
let source ={ | ||
data: { 1: {_extends:'base',d:3,e:4}, | ||
2: {_extends:['base','base2'], f:3,g:4}, | ||
}, | ||
base: {a:1,b:2}, | ||
base2: {h:'a',i:'b'} | ||
}; | ||
let config = ConfigExtends.extends(source); | ||
console.log(JSON.stringify(config,null,2)); | ||
``` | ||
Result: | ||
``` | ||
{ | ||
"data": { | ||
"1": { | ||
"d": 3, | ||
"e": 4, | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"2": { | ||
"f": 3, | ||
"g": 4, | ||
"a": 1, | ||
"b": 2, | ||
"h": "a", | ||
"i": "b" | ||
} | ||
}, | ||
"base": { | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"base2": { | ||
"h": "a", | ||
"i": "b" | ||
} | ||
} | ||
``` | ||
### extends from single file | ||
``` | ||
let config = await configExtends.load(path.join(__dirname,'raspberry.yaml')); | ||
console.log(JSON.stringify(config.version)); | ||
console.log(JSON.stringify(config.version,null,2)); | ||
``` | ||
@@ -92,3 +226,3 @@ #### raspberry.yaml content | ||
#### result: | ||
#### Result: | ||
``` | ||
@@ -196,4 +330,50 @@ { | ||
``` | ||
### extends from path | ||
``` | ||
let config = await ConfigExtends.load(path.join(__dirname,'test-1')); | ||
console.log(JSON.stringify(config,null,2)); | ||
``` | ||
#### structure folder | ||
- test-1 | ||
- folder1 | ||
- file1.yaml content => c: 3 | ||
- folder2 | ||
- file1.yaml content => _extends: [file,folder1.file1] | ||
d: 1 | ||
- file2.yaml content => _extends: [folder2.file1] | ||
e: 1 | ||
- file.yaml content => a: 1 | ||
b: "b" | ||
#### Result | ||
``` | ||
{ | ||
"file": { | ||
"a": 1, | ||
"b": "b" | ||
}, | ||
"folder1": { | ||
"file1": { | ||
"c": 3 | ||
} | ||
}, | ||
"folder2": { | ||
"file1": { | ||
"d": 1, | ||
"a": 1, | ||
"b": "b", | ||
"c": 3 | ||
}, | ||
"file2": { | ||
"e": 1, | ||
"d": 1, | ||
"a": 1, | ||
"b": "b", | ||
"c": 3 | ||
} | ||
} | ||
} | ||
``` | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20339
22
243
377