diff --git a/SpecRunner.html b/SpecRunner.html
index 4ea86f9..bd214f3 100644
--- a/SpecRunner.html
+++ b/SpecRunner.html
@@ -9,7 +9,7 @@
-
+
diff --git a/UnderscoreKO.nuspec b/UnderscoreKO.nuspec
index a9373bb..a311c57 100644
--- a/UnderscoreKO.nuspec
+++ b/UnderscoreKO.nuspec
@@ -2,7 +2,7 @@
UnderscoreKO
- 1.0.0
+ 1.1.0
Kamran Ayub
Adds all the useful collection and array methods from Underscore.js to Knockout observable arrays. Also includes several convenience methods to manipulate the underlying array.
https://github.com/kamranayub/UnderscoreKO
@@ -15,7 +15,7 @@
-
-
+
+
\ No newline at end of file
diff --git a/spec.js b/spec.js
index 263515b..9f84de0 100644
--- a/spec.js
+++ b/spec.js
@@ -34,7 +34,6 @@ describe("UnderscoreKO", function () {
singleArgFns = [
"include", "contains",
"sortedIndex",
- "indexOf",
"lastIndexOf"
],
arrayArgFns = [
@@ -46,7 +45,8 @@ describe("UnderscoreKO", function () {
"invoke",
"pluck",
"shuffle",
- "without"
+ "without",
+ "chain"
],
mutators = [
"filter_", "select_",
@@ -159,6 +159,20 @@ describe("UnderscoreKO", function () {
expect(vm.arr.without(0, 1, 2)).toEqual([3, 4]);
});
+ it("supports chaining", function () {
+ var result = vm.arr.chain().filter(function (num) {
+ return num > 1;
+ }).without(4).value();
+
+ expect(result).toEqual([2, 3]);
+ });
+
+ it("supports invoking KO arrays", function () {
+ var array = ko.observableArray([5, 6, 7]);
+
+ expect(vm.arr.union(array)).toEqual([0, 1, 2, 3, 4, 5, 6, 7]);
+ });
+
describe("it mutates", function () {
var mutable, changed;
@@ -279,5 +293,13 @@ describe("UnderscoreKO", function () {
[4, "e", 50]
]);
});
+
+ it("mutates when using KO arrays", function () {
+ var array = ko.observableArray([5, 6, 7]);
+
+ mutable.union_(array);
+
+ expect(mutable()).toEqual([0, 1, 2, 3, 4, 5, 6, 7]);
+ });
});
});
\ No newline at end of file
diff --git a/underscore-ko.js b/underscore-ko.js
index 8d2c89f..dcb0473 100644
--- a/underscore-ko.js
+++ b/underscore-ko.js
@@ -1,4 +1,4 @@
-// UnderscoreKO Mashup Library v1.0.0
+// UnderscoreKO Mashup Library v1.1.0
// (c) Kamran Ayub - http://github.com/kamranayub/UnderscoreKO
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
//
@@ -38,30 +38,44 @@
"compact", "compact_",
"flatten", "flatten_",
"without", "without_",
+ "uniq", "unique", "uniq_", "unique_",
+ "lastIndexOf",
+
+ // Misc
+ "chain"
+ ],
+ arrayMethods = [
"union", "union_",
"intersection", "intersection_",
"difference", "difference_",
- "uniq", "unique", "uniq_", "unique_",
- "zip", "zip_",
- "indexOf",
- "lastIndexOf",
+ "zip", "zip_"
];
- _.each(methods, function (fn) {
+ _.each(_.union(methods, arrayMethods), function (fn) {
// Let's be good and safe Javascript citizens!
if (!ko.observableArray.fn[fn]) {
ko.observableArray.fn[fn] = function () {
var args = _.toArray(arguments);
+ // Auto-invoke KO arrays
+ if (_.include(arrayMethods, fn)) {
+ _.each(args, function (arg, i) {
+ // Check if it contains a fn we know we attach
+ // to KO arrays, just in case someone passes in
+ // something other than a KO array fn
+ if (typeof arg === "function" && arg[fn]) {
+ args[i] = arg();
+ }
+ });
+ }
+
// Prepend the raw array value
args.splice(0, 0, this());
// Mutator method?
if (fn.substr(fn.length - 1, 1) === "_") {
- fn = fn.substr(0, fn.length - 1);
-
// Mutate the value (leverage KO to handle change notifications)
- return this(_[fn].apply(this, args));
+ return this(_[fn.substr(0, fn.length - 1)].apply(this, args));
} else {
// Call original _ method
return _[fn].apply(this, args);
diff --git a/underscore-ko.min.js b/underscore-ko.min.js
index 02d8ed2..78063e9 100644
--- a/underscore-ko.min.js
+++ b/underscore-ko.min.js
@@ -1,7 +1,7 @@
-// UnderscoreKO Mashup Library v1.0.0
+// UnderscoreKO Mashup Library v1.1.0
// (c) Kamran Ayub - http://github.com/kamranayub/UnderscoreKO
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
//
// Adds all 45 Underscore.js collection and array methods to
// KO observable arrays.
-(function(a,b,c){var d=["each","forEach","map","collect","reduce","inject","foldl","reduceRight","foldr","find","detect","filter","select","filter_","select_","reject","reject_","all","every","any","some","include","contains","invoke","invoke_","pluck","max","min","sortBy","sortBy_","groupBy","groupBy_","sortedIndex","shuffle","shuffle_","size","first","head","initial","last","rest","tail","rest_","tail_","compact","compact_","flatten","flatten_","without","without_","union","union_","intersection","intersection_","difference","difference_","uniq","unique","uniq_","unique_","zip","zip_","indexOf","lastIndexOf"];b.each(d,function(c){a.observableArray.fn[c]||(a.observableArray.fn[c]=function(){var a=b.toArray(arguments);return a.splice(0,0,this()),c.substr(c.length-1,1)==="_"?(c=c.substr(0,c.length-1),this(b[c].apply(this,a))):b[c].apply(this,a)})})})(ko,_);
\ No newline at end of file
+(function(a,b,c){var d=["each","forEach","map","collect","reduce","inject","foldl","reduceRight","foldr","find","detect","filter","select","filter_","select_","reject","reject_","all","every","any","some","include","contains","invoke","invoke_","pluck","max","min","sortBy","sortBy_","groupBy","groupBy_","sortedIndex","shuffle","shuffle_","size","first","head","initial","last","rest","tail","rest_","tail_","compact","compact_","flatten","flatten_","without","without_","uniq","unique","uniq_","unique_","lastIndexOf","chain"],e=["union","union_","intersection","intersection_","difference","difference_","zip","zip_"];b.each(b.union(d,e),function(c){a.observableArray.fn[c]||(a.observableArray.fn[c]=function(){var a=b.toArray(arguments);return b.include(e,c)&&b.each(a,function(b,d){typeof b=="function"&&b[c]&&(a[d]=b())}),a.splice(0,0,this()),c.substr(c.length-1,1)==="_"?this(b[c.substr(0,c.length-1)].apply(this,a)):b[c].apply(this,a)})})})(ko,_);
\ No newline at end of file