Skip to content

Commit

Permalink
Add push functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alec Perkins committed Sep 8, 2013
1 parent 13f16e0 commit 40eb95b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 12 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ params = new Params()
```javascript
> params.set('enabled', true).set('inactive', false).set('name', 'Basil')
```


* Get a parameter:

Expand Down Expand Up @@ -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
Expand All @@ -69,7 +87,12 @@ params = new Params()
b: {
c: true
}
}
},
config: {
editor: ['title', 'cover']
},
options: ['editable', 'fancy', 'pants']
}
```

Expand All @@ -82,4 +105,5 @@ params = new Params()
Error: Param 'only_set_once' was already set with `true`
> strict_params.only_set_once
true
```

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "params",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"dependencies": {
},
Expand Down
41 changes: 32 additions & 9 deletions source/params.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ 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


# 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.")

Expand All @@ -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]?
Expand All @@ -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.
#
Expand Down
26 changes: 26 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 40eb95b

Please sign in to comment.