From 56f01f01e8038db5ad20cadfcf42812be48c229e Mon Sep 17 00:00:00 2001 From: dsc Date: Sun, 19 Feb 2012 12:08:51 -0800 Subject: [PATCH] Adds underscore extensions. --- lib/underscore/index.co | 8 +++++ lib/underscore/underscore.array.co | 47 +++++++++++++++++++++++++++ lib/underscore/underscore.object.co | 60 +++++++++++++++++++++++++++++++++++ www/modules.yaml | 15 ++++++--- 4 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 lib/underscore/index.co create mode 100644 lib/underscore/underscore.array.co create mode 100644 lib/underscore/underscore.object.co diff --git a/lib/underscore/index.co b/lib/underscore/index.co new file mode 100644 index 0000000..a2926e7 --- /dev/null +++ b/lib/underscore/index.co @@ -0,0 +1,8 @@ +_ = require 'underscore' +_.str = require 'underscore.string' +_.mixin _.str.exports() +_.mixin require './underscore/underscore.array' +_.mixin require './underscore/underscore.object' + +module.exports = exports = _ + diff --git a/lib/underscore/underscore.array.co b/lib/underscore/underscore.array.co new file mode 100644 index 0000000..4031dcb --- /dev/null +++ b/lib/underscore/underscore.array.co @@ -0,0 +1,47 @@ +_ = require 'underscore' + +I = -> it +defined = (o) -> o? + +_array = do + /** + * Transforms an Array of tuples (two-element Arrays) into an Object, such that for each + * tuple [k, v]: + * result[k] = v if filter(v) + * @param {Array} o A collection. + * @param {Function} [filter=defined] Optional filter function. If omitted, will + * exclude `undefined` and `null` values. + * @return {Object} Transformed result. + */ + generate : (o, filter=defined) -> + _.reduce do + o + (acc, [k, v], idx) -> + if k and filter(v) + acc[k] = v + acc + {} + + /** + * As {@link _.generate}, but first transforms the collection using `fn`. + * @param {Array} o A collection. + * @param {Function} [fn=I] Transformation function. Defaults to the identity transform. + * @param {Function} [filter=defined] Optional filter function. If omitted, will + * exclude `undefined` and `null` values. + * @return {Object} Transformed result. + */ + synthesize : (o, fn=I, filter=defined) -> + _array.generate _.map(o, fn), filter + + + /** + * Symmetric Difference + */ + xor : (a, b) -> + a = _.values a + b = _.values b + return _.union _.difference(a,b), _.difference(b,a) + + + +exports import _array diff --git a/lib/underscore/underscore.object.co b/lib/underscore/underscore.object.co new file mode 100644 index 0000000..621fa14 --- /dev/null +++ b/lib/underscore/underscore.object.co @@ -0,0 +1,60 @@ +_ = require 'underscore' + +_obj = do + /** + * Searches a heirarchical object for a given subkey specified in dotted-property syntax. + * @param {Object} base The object to serve as the root of the property-chain. + * @param {Array|String} chain The property-chain to lookup. + * @retruns {null|Object} If found, the object is of the form `{ key: Qualified key name, obj: Parent object of key, val: Value at obj[key] }`. Otherwise `null`. + */ + getNestedMeta = (obj, chain) -> + chain = chain.split('.') if typeof chain is 'string' + return _.reduce do + chain + (current, key, idx, chain) -> + return null unless current? + + if idx is chain.length-1 + return + obj : current + key : key + val : current[key] + + if key in current + current[key] + else + null + obj + + /** + * Searches a heirarchical object for a given subkey specified in dotted-property syntax. + * @param {Object} obj The object to serve as the root of the property-chain. + * @param {Array|String} chain The property-chain to lookup. + * @param {Any} def Value to return if lookup fails. + * @retruns {null|Object} If found, returns the value, and otherwise `default`. + */ + getNested = (obj, chain, def=null) -> + meta = _obj.getNestedMeta obj, chain + if meta + meta.val + else + def + + /** + * Searches a heirarchical object for a given subkey specified in + * dotted-property syntax, setting it with the provided value if found. + * @param {Object} obj The object to serve as the root of the property-chain. + * @param {Array|String} chain The property-chain to lookup. + * @param {Any} value The value to set. + * @retruns {null|Object} If found, returns the old value, and otherwise `null`. + */ + setNested = (obj, chain, value) -> + meta = _obj.getNestedMeta obj, chain + if meta + meta.obj[meta.key] = value + meta.val + else + null + + +exports import _obj diff --git a/www/modules.yaml b/www/modules.yaml index 37bfbb6..1027fa8 100644 --- a/www/modules.yaml +++ b/www/modules.yaml @@ -20,11 +20,16 @@ all: - u.min - dygraph -# - suffix: .mod.js -# paths: -# - 'js/kraken': -# - graph -# - scaffold +- suffix: .mod.js + paths: + - js: + - kraken: + - underscore: + - underscore.array + - underscore.object + - index + # - scaffold + # - graph # - suffix: .js # paths: -- 1.7.0.4