Skip to content

Commit

Permalink
Merge pull request #12 from biotope/fix-deep-merge-issue
Browse files Browse the repository at this point in the history
Fixes problem with deep merging
  • Loading branch information
smohadjer authored Mar 9, 2018
2 parents 706c6a4 + 601954d commit 5f80a12
Show file tree
Hide file tree
Showing 5 changed files with 2,658 additions and 18 deletions.
15 changes: 2 additions & 13 deletions configurationLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import {deepSet} from './deepSet';
import {deepGet} from './deepGet';
import 'whatwg-fetch';
import merge from 'deepmerge';

interface ConfigurationLoaderOptions {
data: object;
Expand All @@ -27,19 +28,7 @@ export const configurationLoader = (options: ConfigurationLoaderOptions): Promis

return fetch(options.json).then((response) => {
return response.json().then((jsonData) => {
let mergedData = {};

if (options.overwriteInlineConfigs) {
mergedData = {
...getData(options),
...jsonData
};
} else {
mergedData = {
...jsonData,
...getData(options)
};
}
let mergedData: object = (options.overwriteInlineConfigs) ? merge(getData(options), jsonData) : merge(jsonData, getData(options));

return createConfiguration(mergedData);
});
Expand Down
8 changes: 5 additions & 3 deletions demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script>
var app = {};
app.configuration = {};
app.configuration = { data: {} };
app.configuration.lang = 'en';
app.configuration.data.page = "test";
</script>
</head>
<body>
Expand All @@ -21,8 +22,9 @@
data: app.configuration,
overwriteInlineConfigs: false
}).then(function(configuration) {
// It's now safe to run scripts depending on configuration parameters
console.log(configuration.get('lang'), configuration.get('data'));
console.log(configuration.get('lang')); //depending on whether overwriteInlineConfigs flag is true or false this will return "de" or "en"

console.log(configuration.get('data')); //this would always return properties "id" and "page", value of "page" property depends on overwriteInlineConfigs flag.
});
</script>
</body>
Expand Down
3 changes: 2 additions & 1 deletion demo/demo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"data": {
"id": "lorem"
"id": "lorem",
"page": "demo"
},
"lang": "de"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@biotope/configuration",
"version": "2.0.1",
"version": "2.0.2",
"description": "configurationLoader loads configurations from a json file and then merges them with existing configurations",
"main": "configurationLoader.min.js",
"scripts": {
Expand All @@ -26,6 +26,7 @@
"@types/mocha": "^2.2.48",
"awesome-typescript-loader": "^3.4.1",
"chai": "^4.1.2",
"deepmerge": "^2.1.0",
"mocha": "^5.0.1",
"ts-node": "^4.1.0",
"typescript": "^2.7.2",
Expand Down
Loading

0 comments on commit 5f80a12

Please sign in to comment.