diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d66ae0d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,20 @@ +# Params Changelog + + + +## 1.1.0 + +### Features + +* Add push method + + + +## 1.0.0 + +Initial Params. + +### Features + +* Set a parameter +* Prevent overriding of parameters in strict mode diff --git a/README.md b/README.md index b66c444..f0baa49 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,6 @@ params = new Params() ```javascript > params.set('enabled', true).set('inactive', false).set('name', 'Basil') ``` - * Get a parameter: @@ -52,6 +51,25 @@ params = new Params() true ``` +* Push a parameter: + + ```javascript + > params.push('options', 'editable') + > params.push('options', 'fancy') + > params.push('options', 'pants') + > params.options + ['editable', 'fancy', 'pants'] + ``` + +* Push a parameter: + + ```javascript + > params.push('config.editor', 'title') + > params.push('config.editor', 'cover') + > params.config.editor + ['title', 'cover'] + ``` + * Get all parameters: ```javascript @@ -69,7 +87,12 @@ params = new Params() b: { c: true } - } + }, + config: { + editor: ['title', 'cover'] + }, + options: ['editable', 'fancy', 'pants'] + } ``` @@ -82,4 +105,5 @@ params = new Params() Error: Param 'only_set_once' was already set with `true` > strict_params.only_set_once true + ``` diff --git a/package.json b/package.json index 96a22a8..742a8a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "params", - "version": "1.0.0", + "version": "1.1.0", "description": "", "dependencies": { }, diff --git a/source/params.coffee b/source/params.coffee index 1313d3b..ed7590e 100644 --- a/source/params.coffee +++ b/source/params.coffee @@ -6,7 +6,7 @@ INTERNAL = ['set', 'getAll', '_strict'] class Params - @VERSION = '1.0.0' + @VERSION = '1.1.0' constructor: (opts={}) -> @_strict = if opts.strict? then opts.strict else false @@ -14,11 +14,14 @@ class Params # Public: set a parameter. # - # key - a String key to set. MAY use dot notation to specify sub parameters. - # value - an arbitrary value to set + # key - a String key to set. MAY use dot notation to specify sub parameters. + # value - an arbitrary value to set + # kwargs - an object of optional arguments + # push - a Boolean indicating the key is should be set to an Array # # Returns self for chaining. - set: (key, value) => + set: (key, value, kwargs={}) => + if key in INTERNAL throw new Error("Cannot set the '#{ key }' param, silly.") @@ -32,14 +35,23 @@ class Params key = key_list.shift() if typeof target_obj isnt 'object' - throw new Error("Cannot set subparameter '#{ prev_key }.#{ key }'. '#{ prev_key }' is not an object.") + if kwargs.push and not target_obj.push? + throw new Error("Cannot push subparameter '#{ prev_key }.#{ key }'. '#{ prev_key }' is not an Array.") + else + throw new Error("Cannot set subparameter '#{ prev_key }.#{ key }'. '#{ prev_key }' is not an object.") if key_list.length is 0 if target_obj[key]? - if @_strict - throw new Error("Param '#{ key }' was already set with `#{ target_obj[key] }`") - console?.warn?("Param '#{ key }' overridden (`#{ target_obj[key] }` with `#{ value }`)") - target_obj[key] = value + if not kwargs.push + if @_strict + throw new Error("Param '#{ key }' was already set with `#{ target_obj[key] }`") + console?.warn?("Param '#{ key }' overridden (`#{ target_obj[key] }` with `#{ value }`)") + if kwargs.push + unless target_obj[key]? + target_obj[key] = [] + target_obj[key].push(value) + else + target_obj[key] = value else if not target_obj[key]? @@ -49,6 +61,17 @@ class Params return this + + # Public: push a parameter. If they key is not already set, it will be created. + # + # key - a String key to set. MAY use dot notation to specify sub parameters. + # value - an arbitrary value to set + # + # Returns self for chaining. + push: (key, value) => + return @set(key, value, push: true) + + # Public: get all of the parameters. Used to avoid getting the internal # properties as well. # diff --git a/test/test.coffee b/test/test.coffee index 245b31b..7e6b080 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -47,6 +47,32 @@ describe 'Params', -> (-> params.set('getAll', 1)).should.throw() (-> params.set('_strict', 1)).should.throw() + it 'should push a parameter', -> + params = new Params() + params.push('key', 1) + params.key.should.eql([1]) + params.push('key', 2) + params.key.should.eql([1, 2]) + + it 'should push a subparameter', -> + params = new Params() + params.push('key.subkey', 1) + params.key.subkey.should.eql([1]) + params.push('key.subkey', 2) + params.key.subkey.should.eql([1, 2]) + + it 'should not push a sub parameter if parameter is not an Array', -> + params = new Params() + params.set('key', 1) + (-> params.push('key.foo', 1)).should.throw("Cannot push subparameter 'key.foo'. 'key' is not an Array.") + params.key.should.equal(1) + + params = new Params() + params.set('key.foo', 1) + (-> params.push('key.foo.bar', 1)).should.throw("Cannot push subparameter 'foo.bar'. 'foo' is not an Array.") + params.key.foo.should.equal(1) + + # params = new Params() # console.log params.getAll()