diff options
-rw-r--r-- | lib/constants/pluginHooks.js | 8 | ||||
-rw-r--r-- | lib/constants/pluginResources.js | 4 | ||||
-rw-r--r-- | lib/models/__tests__/config.js | 63 | ||||
-rw-r--r-- | lib/models/config.js | 53 | ||||
-rw-r--r-- | package.json | 2 |
5 files changed, 125 insertions, 5 deletions
diff --git a/lib/constants/pluginHooks.js b/lib/constants/pluginHooks.js new file mode 100644 index 0000000..2d5dcaa --- /dev/null +++ b/lib/constants/pluginHooks.js @@ -0,0 +1,8 @@ +module.exports = [ + 'init', + 'finish', + 'finish:before', + 'config', + 'page', + 'page:before' +]; diff --git a/lib/constants/pluginResources.js b/lib/constants/pluginResources.js new file mode 100644 index 0000000..de5eb18 --- /dev/null +++ b/lib/constants/pluginResources.js @@ -0,0 +1,4 @@ +module.exports = [ + 'js', + 'css' +]; diff --git a/lib/models/__tests__/config.js b/lib/models/__tests__/config.js new file mode 100644 index 0000000..8445cef --- /dev/null +++ b/lib/models/__tests__/config.js @@ -0,0 +1,63 @@ +jest.autoMockOff(); + +var Immutable = require('immutable'); + +describe('Config', function() { + var Config = require('../config'); + + var config = Config.createWithValues({ + hello: { + world: 1, + test: 'Hello', + isFalse: false + } + }); + + describe('getValue', function() { + it('must return value as immutable', function() { + var value = config.getValue('hello'); + expect(Immutable.Map.isMap(value)).toBeTruthy(); + }); + + it('must return deep value', function() { + var value = config.getValue('hello.world'); + expect(value).toBe(1); + }); + + it('must return default value if non existant', function() { + var value = config.getValue('hello.nonExistant', 'defaultValue'); + expect(value).toBe('defaultValue'); + }); + + it('must not return default value for falsy values', function() { + var value = config.getValue('hello.isFalse', 'defaultValue'); + expect(value).toBe(false); + }); + }); + + describe('setValue', function() { + it('must set value as immutable', function() { + var testConfig = config.setValue('hello', { + 'cool': 1 + }); + var value = testConfig.getValue('hello'); + + expect(Immutable.Map.isMap(value)).toBeTruthy(); + expect(value.size).toBe(1); + expect(value.has('cool')).toBeTruthy(); + }); + + it('must set deep value', function() { + var testConfig = config.setValue('hello.world', 2); + var hello = testConfig.getValue('hello'); + var world = testConfig.getValue('hello.world'); + + expect(Immutable.Map.isMap(hello)).toBeTruthy(); + expect(hello.size).toBe(3); + + expect(world).toBe(2); + }); + }); +}); + + diff --git a/lib/models/config.js b/lib/models/config.js index fd4201d..e32ccdc 100644 --- a/lib/models/config.js +++ b/lib/models/config.js @@ -6,7 +6,7 @@ var File = require('./file'); var Config = Immutable.Record({ file: File(), values: Immutable.Map() -}); +}, 'Config'); Config.prototype.getPath = function() { return this.get('path'); @@ -24,13 +24,35 @@ Config.prototype.getValues = function() { */ Config.prototype.getValue = function(keyPath, def) { var values = this.getValues(); - if (is.string(keyPath)) keyPath = keyPath.split('.'); + keyPath = Config.keyToKeyPath(keyPath); + + if (!values.hasIn(keyPath)) { + return def; + } + + return values.getIn(keyPath); +}; + +/** + Update a configuration value + + @param {String} key + @param {Mixed} value + @return {Mixed} +*/ +Config.prototype.setValue = function(keyPath, value) { + keyPath = Config.keyToKeyPath(keyPath); + + value = Immutable.fromJS(value); - return values.getIn(keyPath) || def; + var values = this.getValues(); + values = values.setIn(keyPath, value); + + return this.set('values', values); }; /** - Create a new config, throw error if invalid + Create a new config for a file @param {File} file @param {Object} values @@ -43,5 +65,28 @@ Config.create = function(file, values) { }); }; +/** + Create a new config + + @param {Object} values + @returns {Config} +*/ +Config.createWithValues = function(values) { + return new Config({ + values: Immutable.fromJS(values) + }); +}; + + +/** + Convert a keyPath to an array of keys + + @param {String|Array} + @return {Array} +*/ +Config.keyToKeyPath = function(keyPath) { + if (is.string(keyPath)) keyPath = keyPath.split('.'); + return keyPath; +} module.exports = Config; diff --git a/package.json b/package.json index 70bb58a..5f35410 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "should": "8.3.0" }, "scripts": { - "test": "node_modules/.bin/mocha --reporter spec --bail --timeout 15000 ./test/all.js", + "test": "node_modules/.bin/jest --bail", "lint": "eslint ." }, "repository": { |