Permalink
Browse files

0.2.5 _.deep

  • Loading branch information...
1 parent 1302ece commit 60b929a0a9660e92cdf9c47d54fc5d46f1e3490e @DavidSouther committed Jun 19, 2012
Showing with 77 additions and 26 deletions.
  1. +37 −4 dist/superscore.js
  2. +2 −2 dist/superscore.min.js
  3. +36 −18 docs/core.html
  4. +1 −1 package.json
  5. +1 −1 src/core.js
View
@@ -1,8 +1,8 @@
-// superscore - v0.2.4.1 - 2012-06-18
+// superscore - v0.2.5 - 2012-06-19
// https://github.com/DavidSouther/superscore
// Copyright (c) 2012 David Souther; Licensed MIT
-// superscore core.js 0.2.4
+// superscore core.js 0.2.5
// (c) 2012 David Souther
// superscore is freely distributable under the MIT license.
// For all details and documentation:
@@ -13,7 +13,7 @@
// Missing from Underscore.
_.mixin({
- // ## indexBy*(list, func)*
+ // ### indexBy*(list, func)*
// The default underscore indexOf uses a literal value; we often want to use an comparator. This function returns the index of the first element in the list that the comparator returns truthy when evaluating, or -1 if no elements match.
indexBy: function(list, func) {
list = list || []; func = func || function(){return false;};
@@ -23,7 +23,7 @@ _.mixin({
return -1;
},
- // ## noop
+ // ### noop
noop: function(){},
// ### symmetricDifference*(set1, set2[, ...setN])*
@@ -36,6 +36,39 @@ _.mixin({
_.difference(second, first)
);
});
+ },
+
+ // ### deep*(object, path[, value[, overwrite]])*
+ // Follow a path deep into an object, creating intermediate objects or arrays as necessary.
+ // If value is specified, sets the value of that key if unset. If overwrite is true, sets
+ // even if the value is already set. Returns the root object on set, or the current value
+ // on get.
+ deep: function(object, path, value, overwrite){
+ overwrite = overwrite || false;
+ value = value || null;
+
+ // Break the path, if it's not already an array.
+ path = _.isString(path) ? path.split('.') : _.isArray(path) ? path : [];
+ // Get the next step
+ var part = path.shift();
+ // Different behavior depending on if we're at the last step
+ if(path.length) {
+ // More children, so make sure there's a container at the next level
+ if(!object[part]){
+ object[part] = !_.isNaN(+path[0]) ? [] : {};
+ }
+ // Recurse, returning either the object or the old value
+ var next = _.deep(object[part], path, value, overwrite);
+ return value ? object : next;
+ } else {
+ // If no value, return the part.
+ if(!value){
+ return object[part];
+ } else {
+ object[part] = overwrite ? value : (object[part] || value);
+ return object;
+ }
+ }
}
});
Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 60b929a

Please sign in to comment.