Skip to content

Commit

Permalink
[Closes #1, #2, Fixes #3] Add chain(), remove indexOf(), auto-invoke …
Browse files Browse the repository at this point in the history
…KO arrays, and fix a bug with mutators renaming methodName_ fns to methodName fns..
  • Loading branch information
kamranayub committed Mar 14, 2012
1 parent 647edba commit 8be302c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion SpecRunner.html
Expand Up @@ -9,7 +9,7 @@
<script type="text/javascript" src="lib/jasmine-1.1.0/jasmine-html.js"></script>
<script type="text/javascript" src="lib/underscore-1.3.1.min.js"></script>
<script type="text/javascript" src="lib/knockout-2.0.0.min.js"></script>
<script type="text/javascript" src="underscore-ko.min.js"></script>
<script type="text/javascript" src="underscore-ko.js"></script>

<script type="text/javascript" src="spec.js"></script>

Expand Down
6 changes: 3 additions & 3 deletions UnderscoreKO.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>UnderscoreKO</id>
<version>1.0.0</version>
<version>1.1.0</version>
<authors>Kamran Ayub</authors>
<description>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.</description>
<projectUrl>https://github.com/kamranayub/UnderscoreKO</projectUrl>
Expand All @@ -15,7 +15,7 @@
</dependencies>
</metadata>
<files>
<file src="underscore-ko.js" target="content/Scripts/underscore-ko-1.0.0.js" />
<file src="underscore-ko.min.js" target="content/Scripts/underscore-ko-1.0.0.min.js" />
<file src="underscore-ko.js" target="content/Scripts/underscore-ko-1.1.0.js" />
<file src="underscore-ko.min.js" target="content/Scripts/underscore-ko-1.1.0.min.js" />
</files>
</package>
26 changes: 24 additions & 2 deletions spec.js
Expand Up @@ -34,7 +34,6 @@ describe("UnderscoreKO", function () {
singleArgFns = [
"include", "contains",
"sortedIndex",
"indexOf",
"lastIndexOf"
],
arrayArgFns = [
Expand All @@ -46,7 +45,8 @@ describe("UnderscoreKO", function () {
"invoke",
"pluck",
"shuffle",
"without"
"without",
"chain"
],
mutators = [
"filter_", "select_",
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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]);
});
});
});
32 changes: 23 additions & 9 deletions 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)
//
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions underscore-ko.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8be302c

Please sign in to comment.