From 22e73b4c4714c187a348e689f25197c8dffcbd16 Mon Sep 17 00:00:00 2001 From: Alec Perkins Date: Thu, 5 Sep 2013 14:08:41 -0400 Subject: [PATCH] 1.0.0 --- .gitignore | 1 + README.md | 85 +++++++++++++++++++++++++++++++++++++++++++- package.json | 31 ++++++++++++++++ source/params.coffee | 68 +++++++++++++++++++++++++++++++++++ test/test.coffee | 69 +++++++++++++++++++++++++++++++++++ 5 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 package.json create mode 100644 source/params.coffee create mode 100644 test/test.coffee diff --git a/.gitignore b/.gitignore index f356293..4d063b5 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ logs results npm-debug.log +node_modules \ No newline at end of file diff --git a/README.md b/README.md index 66a4a19..359f87b 100644 --- a/README.md +++ b/README.md @@ -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 + diff --git a/package.json b/package.json new file mode 100644 index 0000000..96a22a8 --- /dev/null +++ b/package.json @@ -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 ", + "license": { + "type": "Unlicense", + "url": "https://raw.github.com/droptype/params/master/LICENSE" + }, + "contributors": [ + { + "name": "Alec Perkins", + "email": "alec@droptype.com" + } + ] +} \ No newline at end of file diff --git a/source/params.coffee b/source/params.coffee new file mode 100644 index 0000000..1313d3b --- /dev/null +++ b/source/params.coffee @@ -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 + diff --git a/test/test.coffee b/test/test.coffee new file mode 100644 index 0000000..245b31b --- /dev/null +++ b/test/test.coffee @@ -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 \ No newline at end of file