-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alec Perkins
committed
Sep 5, 2013
1 parent
b9522e9
commit 22e73b4
Showing
5 changed files
with
253 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ logs | |
results | ||
|
||
npm-debug.log | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,85 @@ | ||
params | ||
Params | ||
====== | ||
|
||
A simple front-end parameters manager for specifying global script parameters. | ||
|
||
## Usage | ||
|
||
Include `params.js` in the page and initialize a parameters object. | ||
|
||
```javascript | ||
params = new Params() | ||
``` | ||
|
||
* Set a parameter: | ||
|
||
```javascript | ||
> params.set('eggs', 'green') | ||
``` | ||
|
||
```javascript | ||
> params.set('enabled', true).set('inactive', false).set('name', 'Basil') | ||
``` | ||
|
||
|
||
* Get a parameter: | ||
|
||
```javascript | ||
> params.eggs | ||
'green' | ||
``` | ||
|
||
* Override a parameter: | ||
|
||
```javascript | ||
> params.set('eggs', 'blue') | ||
! Param 'eggs' overridden (`green` with `blue`) | ||
> params.eggs | ||
'blue' | ||
``` | ||
|
||
* Set nested parameters: | ||
|
||
```javascript | ||
> params.set('urls.static', '/static/') | ||
> params.set('urls.media', '/media/') | ||
> params.urls | ||
{ static: '/static/', media: '/media/' } | ||
> params.urls.static | ||
'/static/' | ||
> params.set('a.b.c', true) | ||
> params.a.b.c | ||
true | ||
``` | ||
|
||
* Get all parameters: | ||
|
||
```javascript | ||
> params.getAll() | ||
{ | ||
eggs: 'blue', | ||
enabled: true, | ||
inactive: false, | ||
name: 'Basil', | ||
urls: { | ||
static: '/static/', | ||
media: '/media/' | ||
}, | ||
a: { | ||
b: { | ||
c: true | ||
} | ||
} | ||
} | ||
``` | ||
|
||
* Prevent parameter overriding: | ||
|
||
```javascript | ||
> strict_params = new Params({ strict: true }) | ||
> strict_params.set('only_set_once', true) | ||
> strict_params.set('only_set_once', 'fails') | ||
Error: Param 'only_set_once' was already set with `true` | ||
> strict_params.only_set_once | ||
true | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "params", | ||
"version": "1.0.0", | ||
"description": "", | ||
"dependencies": { | ||
}, | ||
"devDependencies": { | ||
"coffee-script": "1.x.x", | ||
"mocha": "*", | ||
"should": "*", | ||
"uglify-js": "2.x.x" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha --compilers coffee:coffee-script" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/droptype/params.git" | ||
}, | ||
"author": "Alec Perkins <[email protected]>", | ||
"license": { | ||
"type": "Unlicense", | ||
"url": "https://raw.github.com/droptype/params/master/LICENSE" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Alec Perkins", | ||
"email": "[email protected]" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
### | ||
Parameters manager - https://github.com/droptype/params | ||
### | ||
|
||
INTERNAL = ['set', 'getAll', '_strict'] | ||
|
||
class Params | ||
|
||
@VERSION = '1.0.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 | ||
# | ||
# Returns self for chaining. | ||
set: (key, value) => | ||
if key in INTERNAL | ||
throw new Error("Cannot set the '#{ key }' param, silly.") | ||
|
||
key_list = key.split('.') | ||
target_obj = this | ||
prev_key = null | ||
|
||
# Loop over the key list, constructing nested objects as necessary for | ||
# subparameters. | ||
while key_list.length > 0 | ||
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 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 | ||
|
||
else | ||
if not target_obj[key]? | ||
target_obj[key] = {} | ||
target_obj = target_obj[key] | ||
prev_key = key | ||
|
||
return this | ||
|
||
# Public: get all of the parameters. Used to avoid getting the internal | ||
# properties as well. | ||
# | ||
# Returns an object of all the parameters. | ||
getAll: -> | ||
param_obj = {} | ||
for k, v of this | ||
unless k in INTERNAL | ||
param_obj[k] = v | ||
return param_obj | ||
|
||
|
||
if module? | ||
module.exports = Params | ||
else if window? | ||
window.Params = Params | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'should' | ||
|
||
|
||
|
||
describe 'Params', -> | ||
Params = require '../source/params' | ||
it 'should set a parameter', -> | ||
params = new Params() | ||
params.set('key', 1) | ||
params.key.should.equal(1) | ||
|
||
it 'should set a sub parameter', -> | ||
params = new Params() | ||
params.set('key.subkey', 1) | ||
params.key.subkey.should.equal(1) | ||
|
||
it 'should set a sub sub parameter', -> | ||
params = new Params() | ||
params.set('key.subkey.subsubkey', 1) | ||
params.key.subkey.subsubkey.should.equal(1) | ||
|
||
it 'should not set a sub parameter if parameter is not an object', -> | ||
params = new Params() | ||
params.set('key', 1) | ||
(-> params.set('key.foo', 1)).should.throw("Cannot set subparameter 'key.foo'. 'key' is not an object.") | ||
params.key.should.equal(1) | ||
|
||
(-> params.set('key.foo.bar', 1)).should.throw("Cannot set subparameter 'key.foo'. 'key' is not an object.") | ||
params.key.should.equal(1) | ||
|
||
it 'should allow the key to be overridden', -> | ||
params = new Params() | ||
params.set('key', 1) | ||
params.key.should.equal(1) | ||
params.set('key', 2) | ||
params.key.should.equal(2) | ||
|
||
it 'should prevent the key from being overridden in strict mode', -> | ||
params = new Params(strict: true) | ||
params.set('key', 1) | ||
params.key.should.equal(1) | ||
(-> params.set('key', 2)).should.throw("Param 'key' was already set with `1`") | ||
|
||
it 'should not allow internal properties to be set', -> | ||
params = new Params() | ||
(-> params.set('set', 1)).should.throw() | ||
(-> params.set('getAll', 1)).should.throw() | ||
(-> params.set('_strict', 1)).should.throw() | ||
|
||
# params = new Params() | ||
|
||
# console.log params.getAll() | ||
|
||
# params.set('foo', 'a').set('Bar', 1234) | ||
|
||
# console.log params.getAll() | ||
|
||
# params.set('foo', 9) | ||
|
||
# console.log params.getAll() | ||
|
||
|
||
# params.set('asdf.asdf.asdf', 9) | ||
|
||
# console.log params.getAll() | ||
|
||
# params.set('foo.bar', 9) | ||
# console.log params.getAll() | ||
# console.log params.foo |