Adds underscore extensions.
authordsc <david.schoonover@gmail.com>
Sun, 19 Feb 2012 20:08:51 +0000 (12:08 -0800)
committerdsc <david.schoonover@gmail.com>
Sun, 19 Feb 2012 20:08:51 +0000 (12:08 -0800)
lib/underscore/index.co [new file with mode: 0644]
lib/underscore/underscore.array.co [new file with mode: 0644]
lib/underscore/underscore.object.co [new file with mode: 0644]
www/modules.yaml

diff --git a/lib/underscore/index.co b/lib/underscore/index.co
new file mode 100644 (file)
index 0000000..a2926e7
--- /dev/null
@@ -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 (file)
index 0000000..4031dcb
--- /dev/null
@@ -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 (file)
index 0000000..621fa14
--- /dev/null
@@ -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
index 37bfbb6..1027fa8 100644 (file)
@@ -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: