summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/models/__tests__/config.js30
-rw-r--r--lib/models/config.js12
-rw-r--r--lib/utils/reducedObject.js28
3 files changed, 69 insertions, 1 deletions
diff --git a/lib/models/__tests__/config.js b/lib/models/__tests__/config.js
index ff04ffd..abad754 100644
--- a/lib/models/__tests__/config.js
+++ b/lib/models/__tests__/config.js
@@ -55,6 +55,36 @@ describe('Config', function() {
expect(world).toBe(2);
});
});
+
+ describe('toReducedVersion', function() {
+ it('must only return diffs for simple values', function() {
+ var _config = Config.createWithValues({
+ gitbook: '3.0.0'
+ });
+
+ var reducedVersion = _config.toReducedVersion();
+
+ expect(reducedVersion.toJS()).toEqual({
+ gitbook: '3.0.0'
+ });
+ });
+
+ it('must only return diffs for deep values', function() {
+ var _config = Config.createWithValues({
+ structure: {
+ readme: 'intro.md'
+ }
+ });
+
+ var reducedVersion = _config.toReducedVersion();
+
+ expect(reducedVersion.toJS()).toEqual({
+ structure: {
+ readme: 'intro.md'
+ }
+ });
+ });
+ });
});
diff --git a/lib/models/config.js b/lib/models/config.js
index 00d2b47..6de52f9 100644
--- a/lib/models/config.js
+++ b/lib/models/config.js
@@ -4,6 +4,7 @@ var Immutable = require('immutable');
var File = require('./file');
var PluginDependency = require('./pluginDependency');
var configDefault = require('../constants/configDefault');
+var reducedObject = require('../utils/reducedObject');
var Config = Immutable.Record({
file: File(),
@@ -19,11 +20,20 @@ Config.prototype.getValues = function() {
};
/**
+ * Return minimum version of configuration,
+ * Basically it returns the current config minus the default one
+ * @return {Map}
+ */
+Config.prototype.toReducedVersion = function() {
+ return reducedObject(configDefault, this.getValues());
+};
+
+/**
* Render config as text
* @return {Promise<String>}
*/
Config.prototype.toText = function() {
- return JSON.stringify(this.getValues().toJS(), null, 4);
+ return JSON.stringify(this.toReducedVersion().toJS(), null, 4);
};
/**
diff --git a/lib/utils/reducedObject.js b/lib/utils/reducedObject.js
new file mode 100644
index 0000000..fa5d32c
--- /dev/null
+++ b/lib/utils/reducedObject.js
@@ -0,0 +1,28 @@
+var Immutable = require('immutable');
+
+/**
+ * Reduce the difference between a map and its default version
+ * @param {Map} defaultVersion
+ * @param {Map} currentVersion
+ */
+function reducedObject(defaultVersion, currentVersion) {
+ return currentVersion.reduce(function(result, value, key) {
+ var defaultValue = defaultVersion.get(key);
+
+ if (Immutable.Map.isMap(value)) {
+ var diffs = reducedObject(defaultValue, value);
+
+ if (diffs.size > 0) {
+ return result.set(key, diffs);
+ }
+ }
+
+ if (Immutable.is(defaultValue, value)) {
+ return result;
+ }
+
+ return result.set(key, value);
+ }, Immutable.Map());
+}
+
+module.exports = reducedObject;