[Pkg-javascript-commits] [node-knockout-transformations] 01/02: Import Upstream version 2.1.0
Daniel Ring
techwolf-guest at moszumanska.debian.org
Tue Jan 2 04:55:15 UTC 2018
This is an automated email from the git hooks/post-receive script.
techwolf-guest pushed a commit to branch master
in repository node-knockout-transformations.
commit 827f977b4cea34584418439b0caea08766737419
Author: Daniel Ring <dring at wolfishly.me>
Date: Mon Jan 1 20:19:11 2018 -0800
Import Upstream version 2.1.0
---
.gitignore | 3 +
.jscsrc | 65 +++
.jshintrc | 75 +++
LICENSE | 13 +
Makefile | 40 ++
README.md | 301 ++++++++++
dist/knockout-transformations.js | 1064 ++++++++++++++++++++++++++++++++++
dist/knockout-transformations.min.js | 16 +
lib/.jshintrc | 9 +
lib/filter.js | 27 +
lib/index.js | 20 +
lib/indexBy.js | 330 +++++++++++
lib/map.js | 352 +++++++++++
lib/sortBy.js | 340 +++++++++++
package.json | 36 ++
test/.jshintrc | 17 +
test/filter.spec.js | 259 +++++++++
test/indexBy.spec.js | 688 ++++++++++++++++++++++
test/map.spec.js | 642 ++++++++++++++++++++
test/sortBy.spec.js | 442 ++++++++++++++
20 files changed, 4739 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100755
index 0000000..6c6364f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+/node_modules/
+/tmp/
+.DS_Store
diff --git a/.jscsrc b/.jscsrc
new file mode 100644
index 0000000..b33218d
--- /dev/null
+++ b/.jscsrc
@@ -0,0 +1,65 @@
+{
+ "requireSpaceAfterKeywords": [
+ "if",
+ "else",
+ "for",
+ "while",
+ "do",
+ "switch",
+ "case",
+ "return",
+ "try",
+ "catch",
+ "function",
+ "typeof"
+ ],
+ "requireCurlyBraces": [
+ "if",
+ "else",
+ "for",
+ "while",
+ "do",
+ "switch",
+ "catch",
+ "function"
+ ],
+ "disallowSpacesInNamedFunctionExpression": {
+ "beforeOpeningCurlyBrace": true
+ },
+ "requireSpacesInAnonymousFunctionExpression": {
+ "beforeOpeningRoundBrace": true,
+ "beforeOpeningCurlyBrace": true
+ },
+ "requireSpaceBeforeBlockStatements": true,
+ "requireParenthesesAroundIIFE": true,
+ "requireSpacesInConditionalExpression": true,
+ "disallowSpacesInNamedFunctionExpression": {
+ "beforeOpeningRoundBrace": true
+ },
+ "disallowSpacesInFunctionDeclaration": {
+ "beforeOpeningRoundBrace": true
+ },
+ "requireBlocksOnNewline": 1,
+ "disallowEmptyBlocks": true,
+ "disallowSpacesInsideParentheses": true,
+ "requireCommaBeforeLineBreak": true,
+ "disallowSpaceAfterPrefixUnaryOperators": true,
+ "disallowSpaceBeforePostfixUnaryOperators": true,
+ "disallowSpaceBeforeBinaryOperators": [","],
+ "disallowSpacesInCallExpression": true,
+ "requireSpaceBeforeBinaryOperators": true,
+ "requireSpaceAfterBinaryOperators": true,
+ "disallowKeywords": ["with"],
+ "validateIndentation": 4,
+ "disallowMixedSpacesAndTabs": "smart",
+ "disallowTrailingWhitespace": true,
+ "disallowTrailingComma": true,
+ "disallowKeywordsOnNewLine": ["else"],
+ "requireLineFeedAtFileEnd": true,
+ "requireCapitalizedConstructors": true,
+ "disallowNewlineBeforeBlockStatements": true,
+ "requireCamelCaseOrUpperCaseIdentifiers": true,
+ "requireDotNotation": true,
+ "validateParameterSeparator": ", ",
+ "safeContextKeyword": ["that"]
+}
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..d7746c8
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,75 @@
+{
+ // == Enforcing Options ===============================================
+
+ "bitwise" : false, // Prohibit bitwise operators (&, |, ^, etc.).
+ "curly" : false, // Require {} for every new block or scope. (Moved to codestyle check)
+ "eqeqeq" : true, // Require triple equals i.e. `===`.
+ "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`.
+ "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
+ "latedef" : false, // Prohibit variable use before definition.
+ "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`.
+ "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
+ "noempty" : false, // Prohibit use of empty blocks.
+ "nonew" : false, // Prohibit use of constructors for side-effects.
+ "plusplus" : true, // Prohibit use of `++` & `--`.
+ "undef" : true, // Require all non-global variables be declared before they are used.
+ "unused" : "vars", // If variables defined but not used are checked.
+ "strict" : false, // Require `use strict` pragma in every file.
+
+ // == Relaxing Options ================================================
+
+ "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
+ "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
+ "debug" : false, // Allow debugger statements e.g. browser breakpoints.
+ "eqnull" : false, // Tolerate use of `== null`.
+ "esnext" : false, // Allow ES.next specific features such as `const` and `let`.
+ "evil" : false, // Tolerate use of `eval`.
+ "expr" : false, // Tolerate `ExpressionStatement` as Programs.
+ "funcscope" : false, // Tolerate declarations of variables inside of control structures while accessing them later from the outside.
+ "globalstrict" : false, // Allow global "use strict" (also enables 'strict').
+ "iterator" : false, // Allow usage of __iterator__ property.
+ "lastsemic" : false, // Tolerat missing semicolons when the it is omitted for the last statement in a one-line block.
+ "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
+ "laxcomma" : false, // Suppress warnings about comma-first coding style.
+ "loopfunc" : true, // Allow functions to be defined within loops.
+ "multistr" : false, // Tolerate multi-line strings.
+ "proto" : false, // Tolerate __proto__ property. This property is deprecated.
+ "scripturl" : false, // Tolerate script-targeted URLs.
+ "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
+ "sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
+ "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
+ "validthis" : false, // Tolerate strict violations when the code is running in strict mode and you use this in a non-constructor function.
+
+ // == Environments ====================================================
+
+ "browser" : false, // Standard browser globals e.g. `window`, `document`.
+ "couch" : false, // Enable globals exposed by CouchDB.
+ "devel" : false, // Allow development statements e.g. `console.log();`.
+ "dojo" : false, // Enable globals exposed by Dojo Toolkit.
+ "jquery" : false, // Enable globals exposed by jQuery JavaScript library.
+ "mootools" : false, // Enable globals exposed by MooTools JavaScript framework.
+ "node" : false, // Enable globals available when code is running inside of the NodeJS runtime environment.
+ "nonstandard" : false, // Define non-standard but widely adopted globals such as escape and unescape.
+ "phantom" : false, // Enable globals exposed by PhantomJS.
+ "prototypejs" : false, // Enable globals exposed by Prototype JavaScript framework.
+ "rhino" : false, // Enable globals available when your code is running inside of the Rhino runtime environment.
+ "worker" : false, // Enable globals exposed insite of a Worker.
+ "wsh" : false, // Enable globals available when your code is running as a script for the Windows Script Host.
+ "yui" : false, // Enable globals exposed by YUI framework.
+
+ // == Globals =========================================================
+ "globals": {
+ "one": true,
+ "Ext": true,
+ "INCLUDE": false,
+ "GETSTATICURL": false,
+ "GETTEXT": false,
+ "TR": false,
+ "TRPAT": false,
+ "TRHTML": false,
+ "LOCALEID": true,
+ "SUPPORTEDLOCALEIDS": true,
+ "DEFAULTLOCALEID": true,
+ "LOCALECOOKIENAME": true
+ }
+}
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..6c00d41
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+Copyright 2015 One.com
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..c46bffa
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,40 @@
+PATH := ${PATH}:./node_modules/.bin
+
+build: lint test dist/knockout-transformations.min.js
+
+.PHONY: lint
+lint:
+ @jshint lib/*.js test/*.js
+ @jscs lib/*.js test/*.js
+
+.PHONY: test
+test:
+ @mocha
+
+dist/knockout-transformations.js: package.json lib/*
+ @(echo '/*!' &&\
+ cat LICENSE &&\
+ echo '\n*/' &&\
+ cat lib/map.js &&\
+ cat lib/filter.js &&\
+ cat lib/sortBy.js &&\
+ cat lib/indexBy.js) > $@
+
+dist/knockout-transformations.min.js: dist/knockout-transformations.js
+ @(echo '/*!' &&\
+ cat LICENSE &&\
+ echo '\n*/' &&\
+ uglifyjs dist/knockout-transformations.js) > $@
+
+.PHONY: git-dirty-check
+git-dirty-check:
+ifneq ($(shell git describe --always --dirty | grep -- -dirty),)
+ $(error Working tree is dirty, please commit or stash your changes, then try again)
+endif
+
+.PHONY: release-%
+release-%: git-dirty-check lint test dist/knockout-transformations.min.js
+ git add dist/knockout-transformations*.js && git commit -m "Build distribution files"
+ npm version $*
+ @echo $* release ready to be publised to NPM
+ @echo Remember to push tags
diff --git a/README.md b/README.md
new file mode 100755
index 0000000..7c8f39e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,301 @@
+knockout-transformations
+============
+
+Live transform methods for Knockout observable arrays.
+
+This plugin adds observable `map`, `filter`, `indexBy` and `sortBy` features to observable arrays, so you can transform collections in arbitrary ways and have the results automatically update whenever the underlying source data changes.
+
+The project initialy started out as a fork of https://github.com/SteveSanderson/knockout-projections and therefore owes a lot to this project. This project is licensed under Apache 2.0 by Microsoft Corporation and the part of the code derived from this project is constrained by this license. The rest of the code is also licensed under Apache 2.0 by One.com.
+
+Installation
+============
+
+Download a copy of `knockout-transformations.js` from [the `dist` directory](https://github.com/One-com/knockout-transformations/tree/master/dist) and reference it in your web application:
+
+```html
+<!-- First reference KO itself -->
+<script src='knockout-x.y.z.js'></script>
+<!-- Then reference knockout-transformations from dist -->
+<script src='knockout-transformations.js'></script>
+```
+
+Be sure to reference it *after* you reference Knockout itself.
+
+If you are using NPM you can install knockout and knockout-transformations the following way:
+
+ npm install knockout knockout-transformations
+
+Then just reference the distribution files from `node_modules`.
+
+Using require.js you can either point to the index file in `lib` or
+use the individual transformations from located in `lib`.
+
+Usage
+=====
+
+**Mapping**
+
+More info to follow. For now, here's a simple example:
+
+```js
+var sourceItems = ko.observableArray([1, 2, 3, 4, 5]);
+```
+
+There's a plain observable array. Now let's say we want to keep track of the squares of these values:
+
+```js
+var squares = sourceItems.map(function(x) { return x*x; });
+```
+
+Now `squares` is an observable array containing `[1, 4, 9, 16, 25]`. Let's modify the source data:
+
+```js
+sourceItems.push(6);
+// 'squares' has automatically updated and now contains [1, 4, 9, 16, 25, 36]
+```
+
+This works with any transformation of the source data, e.g.:
+
+```js
+sourceItems.reverse();
+// 'squares' now contains [36, 25, 16, 9, 4, 1]
+```
+
+The key point of this library is that these transformations are done *efficiently*. Specifically, your callback
+function that performs the mapping is only called when strictly necessary (usually, that's only for newly-added
+items). When you add new items to the source data, we *don't* need to re-map the existing ones. When you reorder
+the source data, the output order is correspondingly changed *without* remapping anything.
+
+This efficiency might not matter much if you're just squaring numbers, but when you are mapping complex nested
+graphs of custom objects, it can be important to perform each mapping update with the minumum of work.
+
+**Filtering**
+
+As well as `map`, this plugin also provides `filter`:
+
+```js
+var evenSquares = squares.filter(function(x) { return x % 2 === 0; });
+// evenSquares is now an observable containing [36, 16, 4]
+
+sourceItems.push(9);
+// This has no effect on evenSquares, because 9*9=81 is odd
+
+sourceItems.push(10);
+// evenSquares now contains [36, 16, 4, 100]
+```
+
+Again, your `filter` callbacks are only called when strictly necessary. Re-ordering or deleting source items don't
+require any refiltering - the output is simply updated to match. Only newly-added source items must be subjected
+to your `filter` callback.
+
+**Sorting**
+
+As well as `map` and `filter`, this plugin also provides `sortBy`:
+
+```js
+var sortedEvenSquares.sortBy(function (evenSquare, descending) {
+ return descending(evenSquare);
+});
+// sortedEvenSquares now contains [100, 36, 16, 4]
+```
+
+A more involved example:
+
+```js
+function Person(name, yearOfBirth) {
+ this.name = ko.observable(name);
+ this.yearOfBirth = ko.observable(yearOfBirth);
+}
+
+var persons = ko.observableArray([
+ new Person("Marilyn Monroe", 1926),
+ new Person("Abraham Lincoln", 1809),
+ new Person("Mother Teresa", 1910),
+ new Person("John F. Kennedy", 1917),
+ new Person("Martin Luther King", 1929),
+ new Person("Nelson Mandela", 1918),
+ new Person("Winston Churchill", 1874),
+ new Person("Bill Gates", 1955),
+ new Person("Muhammad Ali", 1942),
+ new Person("Mahatma Gandhi", 1869),
+ new Person("Queen Elizabeth II", 1926)
+]);
+
+// Persons sorted by name
+var sortedByName = persons.sortBy(function (person) {
+ return person.name();
+});
+
+// sortedByName now contains
+// [
+// new Person("Abraham Lincoln", 1809),
+// new Person("Bill Gates", 1955),
+// new Person("John F. Kennedy", 1917),
+// new Person("Mahatma Gandhi", 1869),
+// new Person("Marilyn Monroe", 1926),
+// new Person("Martin Luther King", 1929),
+// new Person("Mother Teresa", 1910),
+// new Person("Muhammad Ali", 1942)
+// new Person("Nelson Mandela", 1918),
+// new Person("Queen Elizabeth II", 1926),
+// new Person("Winston Churchill", 1874),
+// ]
+
+// Persons sorted by year of birth descending and then by name
+var sortedByYearOfBirthAndThenName = persons.sortBy(function (person, descending) {
+ return [descending(person.yearOfBirth()), person.name()];
+});
+
+// sortedByYearOfBirthAndThenName now contains
+// [
+// new Person("Abraham Lincoln", 1809),
+// new Person("Mahatma Gandhi", 1869),
+// new Person("Winston Churchill", 1874),
+// new Person("Mother Teresa", 1910),
+// new Person("John F. Kennedy", 1917),
+// new Person("Nelson Mandela", 1918),
+// new Person("Martin Luther King", 1929),
+// new Person("Bill Gates", 1955),
+// new Person("Marilyn Monroe", 1926),
+// new Person("Queen Elizabeth II", 1926),
+// new Person("Muhammad Ali", 1942)
+// ]
+```
+
+The sorted list is only updated when items are added or removed and when properties that are sorted on changes.
+
+**Indexing**
+
+This transformation provides you with live updated index on a key returned
+by the given function. In contrast to the `map`, `filter` and `sortBy`
+this transformation returns an object and is therefore not a candidate for
+chaining.
+
+```js
+var squareIndex = squares.indexBy(function (square) {
+ return square % 2 === 0 ? 'even' : 'odd';
+});
+
+// squareIndex now contains
+// { even: [36, 16, 4], odd: [25, 9, 1] }
+36, 25, 16, 9, 4, 1
+```
+
+A more involved example using the persons defined in the sorting example:
+
+```js
+
+// Persons indexed by year of birth
+var personsIndexedByYearBirth = persons.indexBy(function (person) {
+ return person.yearOfBirth();
+});
+
+// personsIndexedByYearBirth now contains
+// {
+// 1809: [new Person("Abraham Lincoln", 1809)],
+// 1869: [new Person("Mahatma Gandhi", 1869)],
+// 1874: [new Person("Winston Churchill", 1874)],
+// 1910: [new Person("Mother Teresa", 1910)],
+// 1917: [new Person("John F. Kennedy", 1917)],
+// 1918: [new Person("Nelson Mandela", 1918)],
+// 1929: [new Person("Martin Luther King", 1929)],
+// 1955: [new Person("Bill Gates", 1955)],
+// 1926: [new Person("Marilyn Monroe", 1926),
+// new Person("Queen Elizabeth II", 1926)],
+// 1942: [new Person("Muhammad Ali", 1942)]
+// }
+
+// Persons indexed uniquely by name.
+// Notice unique indexes requires items to map to distint keys;
+// otherwise an exception is thrown.
+var personsIndexedByName = persons.uniqueIndexBy(function (person) {
+ return person.name();
+});
+
+// personsIndexedByName now contains
+// {
+// "Abraham Lincoln": new Person("Abraham Lincoln", 1809),
+// "Mahatma Gandhi": new Person("Mahatma Gandhi", 1869),
+// "Winston Churchill": new Person("Winston Churchill", 1874),
+// "Mother Teresa": new Person("Mother Teresa", 1910),
+// "John F. Kennedy": new Person("John F. Kennedy", 1917),
+// "Nelson Mandela": new Person("Nelson Mandela", 1918),
+// "Martin Luther King": new Person("Martin Luther King", 1929),
+// "Bill Gates": new Person("Bill Gates", 1955),
+// "Marilyn Monroe": new Person("Marilyn Monroe", 1926),
+// "Queen Elizabeth II": new Person("Queen Elizabeth II", 1926),
+// "Muhammad Ali": new Person("Muhammad Ali", 1942)
+// }
+```
+
+It is also possible to create an index on multiple keys to following way:
+
+```js
+var texts = ko.observableArray(['foo', 'bar', 'baz', 'qux', 'quux']);
+// Index texts by
+var indexedTexts = texts.indexBy(function (text) {
+ var firstLetter = text[0];
+ var lastLetter = text[text.length - 1];
+ return [firstLetter, lastLetter];
+});
+
+// indexedTexts now contains
+// {
+// f: ['foo'],
+// b: ['bar', 'baz'],
+// q: ['qux', 'quux'],
+// o: ['foo'],
+// r: ['bar'],
+// z: ['baz'],
+// x: ['qux', 'quux']
+// }
+```
+
+**Chaining**
+
+The above code also demonstrates that you can chain together successive `map`, `filter` and `sortBy` transformations.
+
+When the underlying data changes, the effects will ripple out through the chain of computed arrays with the
+minimum necessary invocation of your `map`, `filter` and `sortBy` callbacks.
+
+How to build from source
+========================
+
+First, install [NPM](https://npmjs.org/) if you don't already have it. It comes with Node.js.
+
+Second, install Grunt globally, if you don't already have it:
+
+ npm install -g grunt-cli
+
+Third, use NPM to download all the dependencies for this module:
+
+ cd wherever_you_cloned_this_repo
+ npm install
+
+Now you can build the package (linting and running tests along the way):
+
+ grunt
+
+Or you can just run the linting tool and tests:
+
+ grunt test
+
+Or you can make Grunt watch for changes to the sources/specs and auto-rebuild after each change:
+
+ grunt watch
+
+The browser-ready output files will be dumped at the following locations:
+
+ * `dist/knockout-transformations.js`
+ * `dist/knockout-transformations.min.js`
+
+License - Apache 2.0
+====================
+
+Copyright 2015 One.com
+
+Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
diff --git a/dist/knockout-transformations.js b/dist/knockout-transformations.js
new file mode 100644
index 0000000..043553b
--- /dev/null
+++ b/dist/knockout-transformations.js
@@ -0,0 +1,1064 @@
+/*!
+Copyright 2015 One.com
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout'], factory);
+ } else {
+ factory(root.ko);
+ }
+}(this, function (ko) {
+ ko.transformations = ko.transformations || {
+ fn: {}
+ };
+
+ function StateItem(inputItem, initialStateArrayIndex, initialOutputArrayIndex, mappingOptions, arrayOfState, outputObservableArray) {
+ // Capture state for later use
+ this.inputItem = inputItem;
+ this.stateArrayIndex = initialStateArrayIndex;
+ this.mappingOptions = mappingOptions;
+ this.arrayOfState = arrayOfState;
+ this.outputObservableArray = outputObservableArray;
+ this.outputArray = this.outputObservableArray.peek();
+ this.isIncluded = null; // Means 'not yet determined'
+ this.suppressNotification = false; // TODO: Instead of this technique, consider raising a sparse diff with a "mutated" entry when a single item changes, and not having any other change logic inside StateItem
+
+ // Set up observables
+ this.outputArrayIndex = ko.observable(initialOutputArrayIndex); // When excluded, it's the position the item would go if it became included
+ this.disposeFuncFromMostRecentMapping = null;
+ this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
+ this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
+ this.previousMappedValue = this.mappedValueComputed.peek();
+ }
+
+ StateItem.prototype.dispose = function () {
+ this.mappedValueComputed.dispose();
+ this.disposeResultFromMostRecentEvaluation();
+ };
+
+ StateItem.prototype.disposeResultFromMostRecentEvaluation = function () {
+ if (this.disposeFuncFromMostRecentMapping) {
+ this.disposeFuncFromMostRecentMapping();
+ this.disposeFuncFromMostRecentMapping = null;
+ }
+
+ if (this.mappingOptions.disposeItem) {
+ var mappedItem = this.mappedValueComputed();
+ this.mappingOptions.disposeItem(mappedItem);
+ }
+ };
+
+ StateItem.prototype.mappingEvaluator = function () {
+ if (this.isIncluded !== null) { // i.e., not first run
+ // This is a replace-in-place, so call any dispose callbacks
+ // we have for the earlier value
+ this.disposeResultFromMostRecentEvaluation();
+ }
+
+ var mappedValue;
+ if (this.mappingOptions.mapping) {
+ mappedValue = this.mappingOptions.mapping(this.inputItem, this.outputArrayIndex);
+ } else if (this.mappingOptions.mappingWithDisposeCallback) {
+ var mappedValueWithDisposeCallback = this.mappingOptions.mappingWithDisposeCallback(this.inputItem, this.outputArrayIndex);
+ if (!('mappedValue' in mappedValueWithDisposeCallback)) {
+ throw new Error('Return value from mappingWithDisposeCallback should have a \'mappedItem\' property.');
+ }
+ mappedValue = mappedValueWithDisposeCallback.mappedValue;
+ this.disposeFuncFromMostRecentMapping = mappedValueWithDisposeCallback.dispose;
+ } else {
+ throw new Error('No mapping callback given.');
+ }
+
+ if (this.isIncluded === null) { // first run
+ this.isIncluded = mappedValue !== this.mappingOptions.exclusionMarker;
+ }
+
+ return mappedValue;
+ };
+
+ StateItem.prototype.updateInclusion = function () {
+ var outputArrayIndex = this.outputArrayIndex.peek();
+ var outputArray = this.outputArray;
+ for (var iterationIndex = this.stateArrayIndex; iterationIndex < this.arrayOfState.length; iterationIndex += 1) {
+ var stateItem = this.arrayOfState[iterationIndex];
+
+ stateItem.setOutputArrayIndexSilently(outputArrayIndex);
+ var newValue = stateItem.mappingEvaluator();
+ var newInclusionState = newValue !== this.mappingOptions.exclusionMarker;
+
+ // Inclusion state changes can *only* happen as a result of changing an individual item.
+ // Structural changes to the array can't cause this (because they don't cause any remapping;
+ // they only map newly added items which have no earlier inclusion state to change).
+ if (newInclusionState) {
+ outputArray[outputArrayIndex] = newValue;
+ outputArrayIndex += 1;
+ }
+
+ stateItem.previousMappedValue = newValue;
+ stateItem.isIncluded = newInclusionState;
+ }
+ if (outputArrayIndex < outputArray.length) {
+ outputArray.length = outputArrayIndex;
+ }
+ };
+
+ StateItem.prototype.onMappingResultChanged = function (newValue) {
+ if (newValue !== this.previousMappedValue) {
+ if (!this.suppressNotification) {
+ this.outputObservableArray.valueWillMutate();
+ }
+
+ var newInclusionState = newValue !== this.mappingOptions.exclusionMarker;
+ if (this.isIncluded !== newInclusionState) {
+ this.updateInclusion();
+ } else {
+ if (newInclusionState) {
+ this.outputArray[this.outputArrayIndex.peek()] = newValue;
+ }
+ this.previousMappedValue = newValue;
+ }
+
+ if (!this.suppressNotification) {
+ this.outputObservableArray.valueHasMutated();
+ }
+ }
+ };
+
+ StateItem.prototype.setOutputArrayIndexSilently = function (newIndex) {
+ // We only want to raise one output array notification per input array change,
+ // so during processing, we suppress notifications
+ this.suppressNotification = true;
+ this.outputArrayIndex(newIndex);
+ this.suppressNotification = false;
+ };
+
+ function getDiffEntryPostOperationIndex(diffEntry, editOffset) {
+ // The diff algorithm's "index" value refers to the output array for additions,
+ // but the "input" array for deletions. Get the output array position.
+ if (!diffEntry) { return null; }
+ switch (diffEntry.status) {
+ case 'added':
+ return diffEntry.index;
+ case 'deleted':
+ return diffEntry.index + editOffset;
+ default:
+ throw new Error('Unknown diff status: ' + diffEntry.status);
+ }
+ }
+
+ function insertOutputItem(diffEntry, movedStateItems, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray, outputArray) {
+ // Retain the existing mapped value if this is a move, otherwise perform mapping
+ var isMoved = typeof diffEntry.moved === 'number',
+ stateItem = isMoved ?
+ movedStateItems[diffEntry.moved] :
+ new StateItem(diffEntry.value, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray);
+ arrayOfState.splice(stateArrayIndex, 0, stateItem);
+
+ if (stateItem.isIncluded) {
+ outputArray.splice(outputArrayIndex, 0, stateItem.mappedValueComputed.peek());
+ }
+
+ // Update indexes
+ if (isMoved) {
+ // We don't change the index until *after* updating this item's position in outputObservableArray,
+ // because changing the index may trigger re-mapping, which in turn would cause the new
+ // value to be written to the 'index' position in the output array
+ stateItem.stateArrayIndex = stateArrayIndex;
+ stateItem.setOutputArrayIndexSilently(outputArrayIndex);
+ }
+
+ return stateItem;
+ }
+
+ function deleteOutputItem(diffEntry, arrayOfState, stateArrayIndex, outputArrayIndex, outputArray) {
+ var stateItem = arrayOfState.splice(stateArrayIndex, 1)[0];
+ if (stateItem.isIncluded) {
+ outputArray.splice(outputArrayIndex, 1);
+ }
+ if (typeof diffEntry.moved !== 'number') {
+ // Be careful to dispose only if this item really was deleted and not moved
+ stateItem.dispose();
+ }
+ }
+
+ function updateRetainedOutputItem(stateItem, stateArrayIndex, outputArrayIndex) {
+ // Just have to update its indexes
+ stateItem.stateArrayIndex = stateArrayIndex;
+ stateItem.setOutputArrayIndexSilently(outputArrayIndex);
+
+ // Return the new value for outputArrayIndex
+ return outputArrayIndex + (stateItem.isIncluded ? 1 : 0);
+ }
+
+ function makeLookupOfMovedStateItems(diff, arrayOfState) {
+ // Before we mutate arrayOfComputedMappedValues at all, grab a reference to each moved item
+ var movedStateItems = {};
+ for (var diffIndex = 0; diffIndex < diff.length; diffIndex += 1) {
+ var diffEntry = diff[diffIndex];
+ if (diffEntry.status === 'added' && (typeof diffEntry.moved === 'number')) {
+ movedStateItems[diffEntry.moved] = arrayOfState[diffEntry.moved];
+ }
+ }
+ return movedStateItems;
+ }
+
+ function getFirstModifiedOutputIndex(firstDiffEntry, arrayOfState, outputArray) {
+ // Work out where the first edit will affect the output array
+ // Then we can update outputArrayIndex incrementally while walking the diff list
+ if (!outputArray.length || !arrayOfState[firstDiffEntry.index]) {
+ // The first edit is beyond the end of the output or state array, so we must
+ // just be appending items.
+ return outputArray.length;
+ } else {
+ // The first edit corresponds to an existing state array item, so grab
+ // the first output array index from it.
+ return arrayOfState[firstDiffEntry.index].outputArrayIndex.peek();
+ }
+ }
+
+ function respondToArrayStructuralChanges(inputObservableArray, arrayOfState, outputArray, outputObservableArray, mappingOptions) {
+ return inputObservableArray.subscribe(function (diff) {
+ if (!diff.length) {
+ return;
+ }
+
+ if (arrayOfState.length === 0) {
+ // Only add items
+ var newOutputItems = [];
+ ko.utils.arrayForEach(diff, function (diffEntry, i) {
+ var inputItem = diffEntry.value;
+ var stateItem = new StateItem(inputItem, i, newOutputItems.length, mappingOptions, arrayOfState, outputObservableArray);
+ var mappedValue = stateItem.mappedValueComputed.peek();
+ arrayOfState.push(stateItem);
+
+ if (stateItem.isIncluded) {
+ newOutputItems.push(mappedValue);
+ }
+ });
+
+ outputObservableArray.push.apply(outputObservableArray, newOutputItems);
+ return;
+ }
+
+ outputObservableArray.valueWillMutate();
+
+ var movedStateItems = makeLookupOfMovedStateItems(diff, arrayOfState),
+ diffIndex = 0,
+ diffEntry = diff[0],
+ editOffset = 0, // A running total of (num(items added) - num(items deleted)) not accounting for filtering
+ outputArrayIndex = diffEntry && getFirstModifiedOutputIndex(diffEntry, arrayOfState, outputArray);
+
+ // Now iterate over the state array, at each stage checking whether the current item
+ // is the next one to have been edited. We can skip all the state array items whose
+ // indexes are less than the first edit index (i.e., diff[0].index).
+ for (var stateArrayIndex = diffEntry.index; diffEntry || (stateArrayIndex < arrayOfState.length); stateArrayIndex += 1) {
+ // Does the current diffEntry correspond to this position in the state array?
+ if (getDiffEntryPostOperationIndex(diffEntry, editOffset) === stateArrayIndex) {
+ // Yes - insert or delete the corresponding state and output items
+ switch (diffEntry.status) {
+ case 'added':
+ // Add to output, and update indexes
+ var stateItem = insertOutputItem(diffEntry, movedStateItems, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray, outputArray);
+ if (stateItem.isIncluded) {
+ outputArrayIndex += 1;
+ }
+ editOffset += 1;
+ break;
+ case 'deleted':
+ // Just erase from the output, and update indexes
+ deleteOutputItem(diffEntry, arrayOfState, stateArrayIndex, outputArrayIndex, outputArray);
+ editOffset -= 1;
+ stateArrayIndex -= 1; // To compensate for the "for" loop incrementing it
+ break;
+ default:
+ throw new Error('Unknown diff status: ' + diffEntry.status);
+ }
+
+ // We're done with this diff entry. Move on to the next one.
+ diffIndex += 1;
+ diffEntry = diff[diffIndex];
+ } else if (stateArrayIndex < arrayOfState.length) {
+ // No - the current item was retained. Just update its index.
+ outputArrayIndex = updateRetainedOutputItem(arrayOfState[stateArrayIndex], stateArrayIndex, outputArrayIndex);
+ }
+ }
+
+ outputObservableArray.valueHasMutated();
+ }, null, 'arrayChange');
+ }
+
+ ko.observableArray.fn.map = ko.transformations.fn.map = function map(mappingOptions) {
+ var that = this,
+ arrayOfState = [],
+ outputArray = [],
+ outputObservableArray = ko.observableArray(outputArray),
+ originalInputArrayContents = that.peek();
+
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof mappingOptions === 'function') {
+ mappingOptions = { mapping: mappingOptions };
+ }
+
+ if (!mappingOptions.exclusionMarker) {
+ mappingOptions.exclusionMarker = {};
+ }
+
+ // Validate the options
+ if (mappingOptions.mappingWithDisposeCallback) {
+ if (mappingOptions.mapping || mappingOptions.disposeItem) {
+ throw new Error('\'mappingWithDisposeCallback\' cannot be used in conjunction with \'mapping\' or \'disposeItem\'.');
+ }
+ } else if (!mappingOptions.mapping) {
+ throw new Error('Specify either \'mapping\' or \'mappingWithDisposeCallback\'.');
+ }
+
+ // Initial state: map each of the inputs
+ for (var i = 0; i < originalInputArrayContents.length; i += 1) {
+ var inputItem = originalInputArrayContents[i],
+ stateItem = new StateItem(inputItem, i, outputArray.length, mappingOptions, arrayOfState, outputObservableArray),
+ mappedValue = stateItem.mappedValueComputed.peek();
+ arrayOfState.push(stateItem);
+
+ if (stateItem.isIncluded) {
+ outputArray.push(mappedValue);
+ }
+ }
+
+ // If the input array changes structurally (items added or removed), update the outputs
+ var inputArraySubscription = respondToArrayStructuralChanges(that, arrayOfState, outputArray, outputObservableArray, mappingOptions);
+
+ var outputComputed = ko.computed(outputObservableArray);
+ if ('throttle' in mappingOptions) {
+ outputComputed = outputComputed.extend({ throttle: mappingOptions.throttle });
+ }
+ // Return value is a readonly computed which can track its own changes to permit chaining.
+ // When disposed, it cleans up everything it created.
+ var returnValue = outputComputed.extend({ trackArrayChanges: true }),
+ originalDispose = returnValue.dispose;
+ returnValue.dispose = function () {
+ inputArraySubscription.dispose();
+ ko.utils.arrayForEach(arrayOfState, function (stateItem) {
+ stateItem.dispose();
+ });
+ originalDispose.call(this, arguments);
+ };
+
+ // Make transformations chainable
+ ko.utils.extend(returnValue, ko.transformations.fn);
+
+ return returnValue;
+ };
+
+ return ko.transformations.fn.map;
+}));
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'), require('./map'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout', './map'], factory);
+ } else {
+ var ko = root.ko;
+ factory(ko, ko.transformations.fn.map);
+ }
+}(this, function (ko, map) {
+ ko.observable.fn.filter = ko.transformations.fn.filter = function filter(mappingOptions) {
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof mappingOptions === 'function') {
+ mappingOptions = { mapping: mappingOptions };
+ }
+ var predicate = mappingOptions.mapping;
+
+ var exclusionMarker = {};
+ mappingOptions.mapping = function (item) {
+ return predicate(item) ? item : exclusionMarker;
+ };
+ mappingOptions.exclusionMarker = exclusionMarker;
+
+ return map.call(this, mappingOptions);
+ };
+ return ko.transformations.fn.filter;
+}));
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout'], factory);
+ } else {
+ factory(root.ko);
+ }
+}(this, function (ko) {
+ ko.transformations = ko.transformations || {
+ fn: {}
+ };
+
+ function sortingKeysEquals(aSortKeys, bSortKeys) {
+ var Descending = SortByTransformation.Descending;
+ if (!Array.isArray(aSortKeys)) {
+ aSortKeys = [aSortKeys];
+ bSortKeys = [bSortKeys];
+ }
+
+ var aSortKey, bSortKey;
+
+ for (var i = 0; i < aSortKeys.length; i += 1) {
+ aSortKey = aSortKeys[i];
+ bSortKey = bSortKeys[i];
+ if (aSortKey instanceof Descending) {
+ if (bSortKey instanceof Descending) {
+ aSortKey = aSortKey.value;
+ bSortKey = bSortKey.value;
+ } else {
+ return false;
+ }
+ }
+
+ if (aSortKey !== bSortKey) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ function compareSortingKeys(aSortKeys, bSortKeys, comparator) {
+ var Descending = SortByTransformation.Descending;
+ if (!Array.isArray(aSortKeys)) {
+ aSortKeys = [aSortKeys];
+ bSortKeys = [bSortKeys];
+ }
+
+ var aSortKey, bSortKey, comparison;
+
+ for (var i = 0; i < aSortKeys.length; i += 1) {
+ aSortKey = aSortKeys[i];
+ bSortKey = bSortKeys[i];
+ if (aSortKey instanceof Descending) {
+ comparison = comparator(bSortKey.value, aSortKey.value);
+ } else {
+ comparison = comparator(aSortKey, bSortKey);
+ }
+ if (comparison !== 0) {
+ return comparison;
+ }
+ }
+ return 0;
+ }
+
+ // Sorting
+ function mappingToComparefn(mapping, comparator) {
+ var Descending = SortByTransformation.Descending;
+ return function (a, b) {
+ var aSortKeys = mapping(a, Descending.create);
+ var bSortKeys = mapping(b, Descending.create);
+ return compareSortingKeys(aSortKeys, bSortKeys, comparator);
+ };
+ }
+
+ function binarySearch(items, item, comparefn) {
+ var left = -1,
+ right = items.length,
+ mid;
+
+ while (right - left > 1) {
+ mid = (left + right) >>> 1;
+ var c = comparefn(items[mid], item);
+ if (c < 0) {
+ left = mid;
+ } else {
+ right = mid;
+ if (!c) {
+ break;
+ }
+ }
+ }
+ return (right === items.length || comparefn(items[right], item)) ? -right - 1 : right;
+ }
+
+ function findInsertionIndex(items, newItem, comparefn) {
+ var left = -1,
+ right = items.length,
+ mid;
+ while (right - left > 1) {
+ mid = (left + right) >>> 1;
+ if (comparefn(items[mid], newItem) < 0) {
+ left = mid;
+ } else {
+ right = mid;
+ }
+ }
+ return right;
+ }
+
+ function binaryIndexOf(items, item, comparefn) {
+ var index = binarySearch(items, item, comparefn);
+ if (index < 0 || items.length <= index || comparefn(items[index], item) !== 0) {
+ return -1;
+ } else {
+ var startIndex = index;
+ // find the first index of an item that looks like the item
+ while (index - 1 >= 0 && comparefn(items[index - 1], item) === 0) {
+ index -= 1;
+ }
+
+ // find the index of the item
+ while (index <= startIndex) {
+ if (items[index] === item) {
+ return index;
+ }
+ index += 1;
+ }
+
+ while (index < items.length) {
+ if (comparefn(items[index], item) !== 0) {
+ return -1;
+ }
+ if (items[index] === item) {
+ return index;
+ }
+
+ index += 1;
+ }
+
+ return -1;
+ }
+ }
+
+ function SortedStateItem(transformation, inputItem) {
+ this.transformation = transformation;
+ this.inputItem = inputItem;
+
+ this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
+ this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
+ this.previousMappedValue = this.mappedValueComputed.peek();
+ }
+
+ SortedStateItem.prototype.dispose = function () {
+ var mappedItem = this.mappedValueComputed();
+ this.mappedValueComputed.dispose();
+ if (this.transformation.options.disposeItem) {
+ this.transformation.options.disposeItem(mappedItem);
+ }
+ };
+
+ SortedStateItem.prototype.mappingEvaluator = function () {
+ return this.transformation.mapping(this.inputItem, SortByTransformation.Descending.create);
+ };
+
+ SortedStateItem.prototype.onMappingResultChanged = function (newValue) {
+ if (!sortingKeysEquals(newValue, this.previousMappedValue)) {
+ var transformation = this.transformation;
+ var outputObservable = transformation.outputObservable;
+ var outputArray = outputObservable.peek();
+ var stateItems = transformation.stateItems;
+ var oldIndex = binaryIndexOf(stateItems, this, mappingToComparefn(function (stateItem) {
+ return stateItem.previousMappedValue;
+ }, transformation.comparator));
+
+ if (stateItems[oldIndex] === this) {
+ outputObservable.valueWillMutate();
+ // It seems the sort order of the underlying array is still usable
+ outputArray.splice(oldIndex, 1);
+ stateItems.splice(oldIndex, 1);
+
+ var index = findInsertionIndex(outputArray, this.inputItem, transformation.comparefn);
+ outputArray.splice(index, 0, this.inputItem);
+ stateItems.splice(index, 0, this);
+
+ this.previousMappedValue = newValue;
+ outputObservable.valueHasMutated();
+ } else {
+ ko.utils.arrayForEach(stateItems, function (stateItem) {
+ stateItem.previousMappedValue = stateItem.mappingEvaluator();
+ });
+
+ // The underlying array needs to be recalculated from scratch
+ stateItems.sort(mappingToComparefn(function (stateItem) {
+ return stateItem.previousMappedValue;
+ }, transformation.comparator));
+
+ outputArray = [];
+ ko.utils.arrayForEach(stateItems, function (stateItem) {
+ outputArray.push(stateItem.inputItem);
+ });
+ outputObservable(outputArray);
+ }
+ }
+ };
+
+ function SortByTransformation(inputObservableArray, options) {
+ var that = this;
+ this.options = options;
+
+ this.mapping = options.mapping;
+ if (options.comparator) {
+ this.comparator = options.comparator;
+ } else {
+ this.comparator = function (a, b) {
+ if (a > b) {
+ return 1;
+ } else if (b > a) {
+ return -1;
+ } else {
+ return 0;
+ }
+ };
+ }
+ this.comparefn = mappingToComparefn(this.mapping, this.comparator);
+
+ this.stateItems = ko.utils.arrayMap(inputObservableArray.peek(), function (inputItem) {
+ return new SortedStateItem(that, inputItem);
+ });
+ this.stateItems.sort(function (a, b) {
+ return compareSortingKeys(a.mappedValueComputed(), b.mappedValueComputed(), that.comparator);
+ });
+
+ this.outputObservable = ko.observable(ko.utils.arrayMap(this.stateItems, function (stateItem) {
+ return stateItem.inputItem;
+ }));
+
+ // If the input array changes structurally (items added or removed), update the outputs
+ var inputArraySubscription = inputObservableArray.subscribe(this.onStructuralChange, this, 'arrayChange');
+
+ var outputComputed = ko.computed(this.outputObservable);
+ if ('throttle' in options) {
+ outputComputed = outputComputed.extend({ throttle: options.throttle });
+ }
+
+ // Return value is a readonly computed which can track its own changes to permit chaining.
+ // When disposed, it cleans up everything it created.
+ this.output = outputComputed.extend({ trackArrayChanges: true });
+ var originalDispose = this.output.dispose;
+ this.output.dispose = function () {
+ inputArraySubscription.dispose();
+ ko.utils.arrayForEach(that.stateItems, function (stateItem) {
+ stateItem.dispose();
+ });
+ originalDispose.call(this, arguments);
+ };
+
+ ko.utils.extend(this.output, ko.transformations.fn);
+ }
+
+ SortByTransformation.Descending = function Descending(value) {
+ this.value = value;
+ };
+
+ SortByTransformation.Descending.create = function (value) {
+ return new SortByTransformation.Descending(value);
+ };
+
+ SortByTransformation.prototype.onStructuralChange = function (diff) {
+ if (!diff.length) {
+ return;
+ }
+
+ this.outputObservable.valueWillMutate();
+
+ var that = this;
+ var addQueue = [];
+ var deleteQueue = [];
+ ko.utils.arrayForEach(diff, function (diffEntry) {
+ if (typeof diffEntry.moved !== 'number') {
+ switch (diffEntry.status) {
+ case 'added':
+ addQueue.push(diffEntry);
+ break;
+ case 'deleted':
+ deleteQueue.push(diffEntry);
+ break;
+ }
+ }
+ });
+
+ var outputArray = this.outputObservable.peek();
+ ko.utils.arrayForEach(deleteQueue, function (diffEntry) {
+ var index = binaryIndexOf(outputArray, diffEntry.value, that.comparefn);
+ if (index !== -1) {
+ outputArray.splice(index, 1);
+ that.stateItems[index].dispose();
+ that.stateItems.splice(index, 1);
+ }
+ });
+
+ if (deleteQueue.length === 0 && this.stateItems.length === 0) {
+ // Adding to an empty array
+ this.stateItems = ko.utils.arrayMap(addQueue, function (diffEntry) {
+ return new SortedStateItem(that, diffEntry.value);
+ });
+
+ this.stateItems.sort(function (a, b) {
+ return compareSortingKeys(a.mappedValueComputed(), b.mappedValueComputed(), that.comparator);
+ });
+
+ ko.utils.arrayForEach(this.stateItems, function (stateItem) {
+ outputArray.push(stateItem.inputItem);
+ });
+ } else {
+ ko.utils.arrayForEach(addQueue, function (diffEntry) {
+ var index = findInsertionIndex(outputArray, diffEntry.value, that.comparefn);
+ var stateItem = new SortedStateItem(that, diffEntry.value);
+ outputArray.splice(index, 0, stateItem.inputItem);
+ that.stateItems.splice(index, 0, stateItem);
+ });
+ }
+
+ this.outputObservable.valueHasMutated();
+ };
+
+ ko.observableArray.fn.sortBy = ko.transformations.fn.sortBy = function sortBy(options) {
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof options === 'function') {
+ options = { mapping: options };
+ }
+
+ var transformation = new SortByTransformation(this, options);
+
+ return transformation.output;
+ };
+
+ return ko.transformations.fn.sortBy;
+}));
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout'], factory);
+ } else {
+ factory(root.ko);
+ }
+}(this, function (ko) {
+ ko.transformations = ko.transformations || {
+ fn: {}
+ };
+
+ function IndexByTransformation(inputObservableArray, options) {
+ var that = this;
+ this.options = options;
+ this.outputObservable = ko.observable({});
+ this.stateItems = {};
+
+ this.mapping = function (item) {
+ return [].concat(options.mapping(item));
+ };
+
+ var inputArray = inputObservableArray.peek();
+ for (var i = 0; i < inputArray.length; i += 1) {
+ this.addToIndex(inputArray[i], i);
+ }
+
+ // If the input array changes structurally (items added or removed), update the outputs
+ var inputArraySubscription = inputObservableArray.subscribe(this.onStructuralChange, this, 'arrayChange');
+
+ var outputComputed = ko.computed(this.outputObservable);
+ if ('throttle' in options) {
+ outputComputed = outputComputed.extend({ throttle: options.throttle });
+ }
+
+ // Return value is a readonly, when disposed, it cleans up everything it created.
+ this.output = outputComputed;
+ var originalDispose = this.output.dispose;
+ this.output.dispose = function () {
+ inputArraySubscription.dispose();
+ for (var prop in that.stateItems) {
+ if (that.stateItems.hasOwnProperty(prop)) {
+ that.stateItems[prop].dispose();
+ }
+ }
+ originalDispose.call(this, arguments);
+ };
+
+ ko.utils.extend(this.output, ko.transformations.fn);
+ }
+
+ IndexByTransformation.prototype.arraysEqual = function (a, b) {
+ if (a === b) {
+ return true;
+ }
+
+ if (typeof a === 'undefined' || typeof b === 'undefined') {
+ return false;
+ }
+
+ if (a.length !== b.length) {
+ return false;
+ }
+
+ for (var i = 0; i < a.length; i += 1) {
+ if ((ko.observable.fn.equalityComparer &&
+ ko.isObservable(a[i]) &&
+ !ko.observable.fn.equalityComparer(a[i], b[i])) ||
+ a[i] !== b[i]) {
+ return false;
+ }
+ }
+ return true;
+ };
+
+
+ IndexByTransformation.prototype.appendToEntry = function (obj, key, item) {
+ var entry = obj[key];
+ if (!entry) {
+ entry = obj[key] = [];
+ }
+ entry.push(item);
+ };
+
+ IndexByTransformation.prototype.removeFromEntry = function (obj, key, item) {
+ var entry = obj[key];
+ if (entry) {
+ var index = entry.indexOf(item);
+ if (index !== -1) {
+ if (entry.length === 1) {
+ delete obj[key];
+ } else {
+ entry.splice(index, 1);
+ }
+ }
+ }
+ };
+
+ IndexByTransformation.prototype.insertByKeyAndItem = function (indexMapping, key, item) {
+ this.appendToEntry(indexMapping, key, item);
+ };
+
+ IndexByTransformation.prototype.removeByKeyAndItem = function (indexMapping, key, item) {
+ this.removeFromEntry(indexMapping, key, item);
+ };
+
+ IndexByTransformation.prototype.addStateItemToIndex = function (stateItem) {
+ var key = this.mapping(stateItem.inputItem)[0];
+ this.appendToEntry(this.stateItems, key, stateItem);
+ };
+
+ IndexByTransformation.prototype.findStateItem = function (inputItem) {
+ var key = this.mapping(inputItem)[0];
+ var entry = this.stateItems[key];
+ if (!entry) {
+ return null;
+ }
+
+ var result = ko.utils.arrayFilter(entry, function (stateItem) {
+ return stateItem.inputItem === inputItem;
+ });
+ return result[0] || null;
+ };
+
+ IndexByTransformation.prototype.removeStateItem = function (stateItem) {
+ var key = stateItem.mappedValueComputed()[0];
+ this.removeFromEntry(this.stateItems, key, stateItem);
+ stateItem.dispose();
+ };
+
+ IndexByTransformation.prototype.addToIndex = function (inputItem) {
+ var that = this;
+ var keys = this.mapping(inputItem);
+ var output = this.outputObservable.peek();
+ ko.utils.arrayForEach(keys, function (key) {
+ that.insertByKeyAndItem(output, key, inputItem);
+ });
+ var stateItem = new IndexedStateItem(this, inputItem);
+ this.addStateItemToIndex(stateItem);
+ };
+
+ IndexByTransformation.prototype.removeItem = function (inputItem) {
+ var that = this;
+ var stateItem = this.findStateItem(inputItem);
+ if (stateItem) {
+ var keys = stateItem.mappedValueComputed();
+ var output = this.outputObservable.peek();
+ ko.utils.arrayForEach(keys, function (key) {
+ that.removeByKeyAndItem(output, key, inputItem);
+ });
+ this.removeStateItem(stateItem);
+ }
+ };
+
+ IndexByTransformation.prototype.onStructuralChange = function (diff) {
+ var that = this;
+ if (!diff.length) {
+ return;
+ }
+
+ var addQueue = [];
+ var deleteQueue = [];
+ ko.utils.arrayForEach(diff, function (diffEntry) {
+ if (typeof diffEntry.moved !== 'number') {
+ switch (diffEntry.status) {
+ case 'added':
+ addQueue.push(diffEntry);
+ break;
+ case 'deleted':
+ deleteQueue.push(diffEntry);
+ break;
+ }
+ }
+ });
+
+ ko.utils.arrayForEach(deleteQueue, function (diffEntry) {
+ that.removeItem(diffEntry.value, diffEntry.index);
+ });
+
+ ko.utils.arrayForEach(addQueue, function (diffEntry) {
+ that.addToIndex(diffEntry.value, diffEntry.index);
+ });
+
+ this.outputObservable.valueHasMutated();
+ };
+
+ function IndexedStateItem(transformation, inputItem) {
+ this.transformation = transformation;
+ this.inputItem = inputItem;
+ this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
+ this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
+ this.previousMappedValue = this.mappedValueComputed.peek();
+ }
+
+ IndexedStateItem.prototype.dispose = function () {
+ var mappedItem = this.mappedValueComputed();
+ this.mappedValueComputed.dispose();
+
+ if (this.transformation.options.disposeItem) {
+ this.transformation.options.disposeItem(mappedItem);
+ }
+ };
+
+ IndexedStateItem.prototype.mappingEvaluator = function () {
+ return this.transformation.mapping(this.inputItem);
+ };
+
+ function toArray(value) {
+ return Array.isArray(value) ? value : [value];
+ }
+
+ IndexedStateItem.prototype.onMappingResultChanged = function (newValue) {
+ var transformation = this.transformation;
+ if (!transformation.arraysEqual(this.newValue, this.previousMappedValue)) {
+ var outputObservable = transformation.outputObservable;
+ var output = outputObservable.peek();
+ outputObservable.valueWillMutate();
+
+ var that = this;
+ ko.utils.arrayForEach(toArray(this.previousMappedValue), function (key) {
+ transformation.removeByKeyAndItem(output, key, that.inputItem);
+ transformation.removeByKeyAndItem(transformation.stateItems, key, that);
+ });
+
+
+ ko.utils.arrayForEach(toArray(newValue), function (key) {
+ transformation.insertByKeyAndItem(output, key, that.inputItem);
+ });
+
+ transformation.addStateItemToIndex(this);
+ this.previousMappedValue = newValue;
+ outputObservable.valueHasMutated();
+ }
+ };
+
+ function UniqueIndexByTransformation(inputObservableArray, options) {
+ IndexByTransformation.call(this, inputObservableArray, options);
+ }
+
+ ko.utils.extend(UniqueIndexByTransformation.prototype, IndexByTransformation.prototype);
+
+ UniqueIndexByTransformation.prototype.insertByKeyAndItem = function (indexMapping, key, item) {
+ if (key in indexMapping) {
+ throw new Error('Unique indexes requires items must map to different keys; duplicate key: ' + key);
+ }
+
+ indexMapping[key] = item;
+ };
+
+ UniqueIndexByTransformation.prototype.removeByKeyAndItem = function (indexMapping, key) {
+ delete indexMapping[key];
+ };
+
+ UniqueIndexByTransformation.prototype.addStateItemToIndex = function (stateItem) {
+ var key = stateItem.mappedValueComputed()[0];
+ this.stateItems[key] = stateItem;
+ };
+
+ UniqueIndexByTransformation.prototype.findStateItem = function (inputItem) {
+ var key = this.mapping(inputItem)[0];
+ return this.stateItems[key] || null;
+ };
+
+ UniqueIndexByTransformation.prototype.removeStateItem = function (stateItem) {
+ var key = stateItem.mappedValueComputed()[0];
+ if (this.stateItems[key] === stateItem) {
+ delete this.stateItems[key];
+ }
+ stateItem.dispose();
+ };
+
+ UniqueIndexByTransformation.prototype.addToIndex = function (inputItem) {
+ var that = this;
+ var keys = this.mapping(inputItem);
+ var output = this.outputObservable.peek();
+ ko.utils.arrayForEach(keys, function (key) {
+ that.insertByKeyAndItem(output, key, inputItem);
+ });
+ var stateItem = new UniqueIndexedStateItem(this, inputItem);
+ this.addStateItemToIndex(stateItem);
+ };
+
+ UniqueIndexByTransformation.prototype.removeItem = function (inputItem) {
+ var that = this;
+ var stateItem = this.findStateItem(inputItem);
+ if (stateItem) {
+ var keys = stateItem.mappedValueComputed();
+ var output = this.outputObservable.peek();
+ ko.utils.arrayForEach(keys, function (key) {
+ that.removeByKeyAndItem(output, key, inputItem);
+ });
+ this.removeStateItem(stateItem);
+ }
+ };
+
+ function UniqueIndexedStateItem(transformation, inputItem) {
+ IndexedStateItem.call(this, transformation, inputItem);
+ }
+
+ ko.utils.extend(UniqueIndexedStateItem.prototype, IndexedStateItem.prototype);
+
+
+ ko.observableArray.fn.indexBy = ko.transformations.fn.indexBy = function indexBy(options) {
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof options === 'function') {
+ options = { mapping: options, unique: false };
+ }
+
+ var transformation = options.unique ?
+ new UniqueIndexByTransformation(this, options) :
+ new IndexByTransformation(this, options);
+
+ return transformation.output;
+ };
+
+ ko.observableArray.fn.uniqueIndexBy = ko.transformations.fn.uniqueIndexBy = function uniqueIndexBy(options) {
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof options === 'function') {
+ options = { mapping: options };
+ }
+ options.unique = true;
+
+ var transformation = new UniqueIndexByTransformation(this, options);
+
+ return transformation.output;
+ };
+
+ return ko.transformations.fn.indexBy;
+}));
diff --git a/dist/knockout-transformations.min.js b/dist/knockout-transformations.min.js
new file mode 100644
index 0000000..639eedc
--- /dev/null
+++ b/dist/knockout-transformations.min.js
@@ -0,0 +1,16 @@
+/*!
+Copyright 2015 One.com
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+(function(root,factory){if(typeof exports==="object"){module.exports=factory(require("knockout"))}else if(typeof define==="function"&&define.amd){define(["knockout"],factory)}else{factory(root.ko)}})(this,function(ko){ko.transformations=ko.transformations||{fn:{}};function StateItem(inputItem,initialStateArrayIndex,initialOutputArrayIndex,mappingOptions,arrayOfState,outputObservableArray){this.inputItem=inputItem;this.stateArrayIndex=initialStateArrayIndex;this.mappingOptions=mappingOpti [...]
\ No newline at end of file
diff --git a/lib/.jshintrc b/lib/.jshintrc
new file mode 100644
index 0000000..c111a3c
--- /dev/null
+++ b/lib/.jshintrc
@@ -0,0 +1,9 @@
+{
+ "extends": "../.jshintrc",
+ "browser": true,
+ "globals": {
+ "module": false,
+ "require": false,
+ "define": false
+ }
+}
diff --git a/lib/filter.js b/lib/filter.js
new file mode 100644
index 0000000..ad00a61
--- /dev/null
+++ b/lib/filter.js
@@ -0,0 +1,27 @@
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'), require('./map'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout', './map'], factory);
+ } else {
+ var ko = root.ko;
+ factory(ko, ko.transformations.fn.map);
+ }
+}(this, function (ko, map) {
+ ko.observable.fn.filter = ko.transformations.fn.filter = function filter(mappingOptions) {
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof mappingOptions === 'function') {
+ mappingOptions = { mapping: mappingOptions };
+ }
+ var predicate = mappingOptions.mapping;
+
+ var exclusionMarker = {};
+ mappingOptions.mapping = function (item) {
+ return predicate(item) ? item : exclusionMarker;
+ };
+ mappingOptions.exclusionMarker = exclusionMarker;
+
+ return map.call(this, mappingOptions);
+ };
+ return ko.transformations.fn.filter;
+}));
diff --git a/lib/index.js b/lib/index.js
new file mode 100644
index 0000000..c645c23
--- /dev/null
+++ b/lib/index.js
@@ -0,0 +1,20 @@
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'),
+ require('./map'), require('./filter'),
+ require('./sortBy'), require('./indexBy'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout', './map', './filter', './sortBy', './indexBy'], factory);
+ } else {
+ var ko = root.ko;
+ factory(
+ ko,
+ ko.transformations.fn.map,
+ ko.transformations.fn.filter,
+ ko.transformations.fn.sortBy,
+ ko.transformations.fn.indexBy
+ );
+ }
+}(this, function (ko, map, filter, sortBy, indexBy) {
+ return ko.transformations.fn;
+}));
diff --git a/lib/indexBy.js b/lib/indexBy.js
new file mode 100644
index 0000000..0cc0c1c
--- /dev/null
+++ b/lib/indexBy.js
@@ -0,0 +1,330 @@
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout'], factory);
+ } else {
+ factory(root.ko);
+ }
+}(this, function (ko) {
+ ko.transformations = ko.transformations || {
+ fn: {}
+ };
+
+ function IndexByTransformation(inputObservableArray, options) {
+ var that = this;
+ this.options = options;
+ this.outputObservable = ko.observable({});
+ this.stateItems = {};
+
+ this.mapping = function (item) {
+ return [].concat(options.mapping(item));
+ };
+
+ var inputArray = inputObservableArray.peek();
+ for (var i = 0; i < inputArray.length; i += 1) {
+ this.addToIndex(inputArray[i], i);
+ }
+
+ // If the input array changes structurally (items added or removed), update the outputs
+ var inputArraySubscription = inputObservableArray.subscribe(this.onStructuralChange, this, 'arrayChange');
+
+ var outputComputed = ko.computed(this.outputObservable);
+ if ('throttle' in options) {
+ outputComputed = outputComputed.extend({ throttle: options.throttle });
+ }
+
+ // Return value is a readonly, when disposed, it cleans up everything it created.
+ this.output = outputComputed;
+ var originalDispose = this.output.dispose;
+ this.output.dispose = function () {
+ inputArraySubscription.dispose();
+ for (var prop in that.stateItems) {
+ if (that.stateItems.hasOwnProperty(prop)) {
+ that.stateItems[prop].dispose();
+ }
+ }
+ originalDispose.call(this, arguments);
+ };
+
+ ko.utils.extend(this.output, ko.transformations.fn);
+ }
+
+ IndexByTransformation.prototype.arraysEqual = function (a, b) {
+ if (a === b) {
+ return true;
+ }
+
+ if (typeof a === 'undefined' || typeof b === 'undefined') {
+ return false;
+ }
+
+ if (a.length !== b.length) {
+ return false;
+ }
+
+ for (var i = 0; i < a.length; i += 1) {
+ if ((ko.observable.fn.equalityComparer &&
+ ko.isObservable(a[i]) &&
+ !ko.observable.fn.equalityComparer(a[i], b[i])) ||
+ a[i] !== b[i]) {
+ return false;
+ }
+ }
+ return true;
+ };
+
+
+ IndexByTransformation.prototype.appendToEntry = function (obj, key, item) {
+ var entry = obj[key];
+ if (!entry) {
+ entry = obj[key] = [];
+ }
+ entry.push(item);
+ };
+
+ IndexByTransformation.prototype.removeFromEntry = function (obj, key, item) {
+ var entry = obj[key];
+ if (entry) {
+ var index = entry.indexOf(item);
+ if (index !== -1) {
+ if (entry.length === 1) {
+ delete obj[key];
+ } else {
+ entry.splice(index, 1);
+ }
+ }
+ }
+ };
+
+ IndexByTransformation.prototype.insertByKeyAndItem = function (indexMapping, key, item) {
+ this.appendToEntry(indexMapping, key, item);
+ };
+
+ IndexByTransformation.prototype.removeByKeyAndItem = function (indexMapping, key, item) {
+ this.removeFromEntry(indexMapping, key, item);
+ };
+
+ IndexByTransformation.prototype.addStateItemToIndex = function (stateItem) {
+ var key = this.mapping(stateItem.inputItem)[0];
+ this.appendToEntry(this.stateItems, key, stateItem);
+ };
+
+ IndexByTransformation.prototype.findStateItem = function (inputItem) {
+ var key = this.mapping(inputItem)[0];
+ var entry = this.stateItems[key];
+ if (!entry) {
+ return null;
+ }
+
+ var result = ko.utils.arrayFilter(entry, function (stateItem) {
+ return stateItem.inputItem === inputItem;
+ });
+ return result[0] || null;
+ };
+
+ IndexByTransformation.prototype.removeStateItem = function (stateItem) {
+ var key = stateItem.mappedValueComputed()[0];
+ this.removeFromEntry(this.stateItems, key, stateItem);
+ stateItem.dispose();
+ };
+
+ IndexByTransformation.prototype.addToIndex = function (inputItem) {
+ var that = this;
+ var keys = this.mapping(inputItem);
+ var output = this.outputObservable.peek();
+ ko.utils.arrayForEach(keys, function (key) {
+ that.insertByKeyAndItem(output, key, inputItem);
+ });
+ var stateItem = new IndexedStateItem(this, inputItem);
+ this.addStateItemToIndex(stateItem);
+ };
+
+ IndexByTransformation.prototype.removeItem = function (inputItem) {
+ var that = this;
+ var stateItem = this.findStateItem(inputItem);
+ if (stateItem) {
+ var keys = stateItem.mappedValueComputed();
+ var output = this.outputObservable.peek();
+ ko.utils.arrayForEach(keys, function (key) {
+ that.removeByKeyAndItem(output, key, inputItem);
+ });
+ this.removeStateItem(stateItem);
+ }
+ };
+
+ IndexByTransformation.prototype.onStructuralChange = function (diff) {
+ var that = this;
+ if (!diff.length) {
+ return;
+ }
+
+ var addQueue = [];
+ var deleteQueue = [];
+ ko.utils.arrayForEach(diff, function (diffEntry) {
+ if (typeof diffEntry.moved !== 'number') {
+ switch (diffEntry.status) {
+ case 'added':
+ addQueue.push(diffEntry);
+ break;
+ case 'deleted':
+ deleteQueue.push(diffEntry);
+ break;
+ }
+ }
+ });
+
+ ko.utils.arrayForEach(deleteQueue, function (diffEntry) {
+ that.removeItem(diffEntry.value, diffEntry.index);
+ });
+
+ ko.utils.arrayForEach(addQueue, function (diffEntry) {
+ that.addToIndex(diffEntry.value, diffEntry.index);
+ });
+
+ this.outputObservable.valueHasMutated();
+ };
+
+ function IndexedStateItem(transformation, inputItem) {
+ this.transformation = transformation;
+ this.inputItem = inputItem;
+ this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
+ this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
+ this.previousMappedValue = this.mappedValueComputed.peek();
+ }
+
+ IndexedStateItem.prototype.dispose = function () {
+ var mappedItem = this.mappedValueComputed();
+ this.mappedValueComputed.dispose();
+
+ if (this.transformation.options.disposeItem) {
+ this.transformation.options.disposeItem(mappedItem);
+ }
+ };
+
+ IndexedStateItem.prototype.mappingEvaluator = function () {
+ return this.transformation.mapping(this.inputItem);
+ };
+
+ function toArray(value) {
+ return Array.isArray(value) ? value : [value];
+ }
+
+ IndexedStateItem.prototype.onMappingResultChanged = function (newValue) {
+ var transformation = this.transformation;
+ if (!transformation.arraysEqual(this.newValue, this.previousMappedValue)) {
+ var outputObservable = transformation.outputObservable;
+ var output = outputObservable.peek();
+ outputObservable.valueWillMutate();
+
+ var that = this;
+ ko.utils.arrayForEach(toArray(this.previousMappedValue), function (key) {
+ transformation.removeByKeyAndItem(output, key, that.inputItem);
+ transformation.removeByKeyAndItem(transformation.stateItems, key, that);
+ });
+
+
+ ko.utils.arrayForEach(toArray(newValue), function (key) {
+ transformation.insertByKeyAndItem(output, key, that.inputItem);
+ });
+
+ transformation.addStateItemToIndex(this);
+ this.previousMappedValue = newValue;
+ outputObservable.valueHasMutated();
+ }
+ };
+
+ function UniqueIndexByTransformation(inputObservableArray, options) {
+ IndexByTransformation.call(this, inputObservableArray, options);
+ }
+
+ ko.utils.extend(UniqueIndexByTransformation.prototype, IndexByTransformation.prototype);
+
+ UniqueIndexByTransformation.prototype.insertByKeyAndItem = function (indexMapping, key, item) {
+ if (key in indexMapping) {
+ throw new Error('Unique indexes requires items must map to different keys; duplicate key: ' + key);
+ }
+
+ indexMapping[key] = item;
+ };
+
+ UniqueIndexByTransformation.prototype.removeByKeyAndItem = function (indexMapping, key) {
+ delete indexMapping[key];
+ };
+
+ UniqueIndexByTransformation.prototype.addStateItemToIndex = function (stateItem) {
+ var key = stateItem.mappedValueComputed()[0];
+ this.stateItems[key] = stateItem;
+ };
+
+ UniqueIndexByTransformation.prototype.findStateItem = function (inputItem) {
+ var key = this.mapping(inputItem)[0];
+ return this.stateItems[key] || null;
+ };
+
+ UniqueIndexByTransformation.prototype.removeStateItem = function (stateItem) {
+ var key = stateItem.mappedValueComputed()[0];
+ if (this.stateItems[key] === stateItem) {
+ delete this.stateItems[key];
+ }
+ stateItem.dispose();
+ };
+
+ UniqueIndexByTransformation.prototype.addToIndex = function (inputItem) {
+ var that = this;
+ var keys = this.mapping(inputItem);
+ var output = this.outputObservable.peek();
+ ko.utils.arrayForEach(keys, function (key) {
+ that.insertByKeyAndItem(output, key, inputItem);
+ });
+ var stateItem = new UniqueIndexedStateItem(this, inputItem);
+ this.addStateItemToIndex(stateItem);
+ };
+
+ UniqueIndexByTransformation.prototype.removeItem = function (inputItem) {
+ var that = this;
+ var stateItem = this.findStateItem(inputItem);
+ if (stateItem) {
+ var keys = stateItem.mappedValueComputed();
+ var output = this.outputObservable.peek();
+ ko.utils.arrayForEach(keys, function (key) {
+ that.removeByKeyAndItem(output, key, inputItem);
+ });
+ this.removeStateItem(stateItem);
+ }
+ };
+
+ function UniqueIndexedStateItem(transformation, inputItem) {
+ IndexedStateItem.call(this, transformation, inputItem);
+ }
+
+ ko.utils.extend(UniqueIndexedStateItem.prototype, IndexedStateItem.prototype);
+
+
+ ko.observableArray.fn.indexBy = ko.transformations.fn.indexBy = function indexBy(options) {
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof options === 'function') {
+ options = { mapping: options, unique: false };
+ }
+
+ var transformation = options.unique ?
+ new UniqueIndexByTransformation(this, options) :
+ new IndexByTransformation(this, options);
+
+ return transformation.output;
+ };
+
+ ko.observableArray.fn.uniqueIndexBy = ko.transformations.fn.uniqueIndexBy = function uniqueIndexBy(options) {
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof options === 'function') {
+ options = { mapping: options };
+ }
+ options.unique = true;
+
+ var transformation = new UniqueIndexByTransformation(this, options);
+
+ return transformation.output;
+ };
+
+ return ko.transformations.fn.indexBy;
+}));
diff --git a/lib/map.js b/lib/map.js
new file mode 100644
index 0000000..c4c678e
--- /dev/null
+++ b/lib/map.js
@@ -0,0 +1,352 @@
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout'], factory);
+ } else {
+ factory(root.ko);
+ }
+}(this, function (ko) {
+ ko.transformations = ko.transformations || {
+ fn: {}
+ };
+
+ function StateItem(inputItem, initialStateArrayIndex, initialOutputArrayIndex, mappingOptions, arrayOfState, outputObservableArray) {
+ // Capture state for later use
+ this.inputItem = inputItem;
+ this.stateArrayIndex = initialStateArrayIndex;
+ this.mappingOptions = mappingOptions;
+ this.arrayOfState = arrayOfState;
+ this.outputObservableArray = outputObservableArray;
+ this.outputArray = this.outputObservableArray.peek();
+ this.isIncluded = null; // Means 'not yet determined'
+ this.suppressNotification = false; // TODO: Instead of this technique, consider raising a sparse diff with a "mutated" entry when a single item changes, and not having any other change logic inside StateItem
+
+ // Set up observables
+ this.outputArrayIndex = ko.observable(initialOutputArrayIndex); // When excluded, it's the position the item would go if it became included
+ this.disposeFuncFromMostRecentMapping = null;
+ this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
+ this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
+ this.previousMappedValue = this.mappedValueComputed.peek();
+ }
+
+ StateItem.prototype.dispose = function () {
+ this.mappedValueComputed.dispose();
+ this.disposeResultFromMostRecentEvaluation();
+ };
+
+ StateItem.prototype.disposeResultFromMostRecentEvaluation = function () {
+ if (this.disposeFuncFromMostRecentMapping) {
+ this.disposeFuncFromMostRecentMapping();
+ this.disposeFuncFromMostRecentMapping = null;
+ }
+
+ if (this.mappingOptions.disposeItem) {
+ var mappedItem = this.mappedValueComputed();
+ this.mappingOptions.disposeItem(mappedItem);
+ }
+ };
+
+ StateItem.prototype.mappingEvaluator = function () {
+ if (this.isIncluded !== null) { // i.e., not first run
+ // This is a replace-in-place, so call any dispose callbacks
+ // we have for the earlier value
+ this.disposeResultFromMostRecentEvaluation();
+ }
+
+ var mappedValue;
+ if (this.mappingOptions.mapping) {
+ mappedValue = this.mappingOptions.mapping(this.inputItem, this.outputArrayIndex);
+ } else if (this.mappingOptions.mappingWithDisposeCallback) {
+ var mappedValueWithDisposeCallback = this.mappingOptions.mappingWithDisposeCallback(this.inputItem, this.outputArrayIndex);
+ if (!('mappedValue' in mappedValueWithDisposeCallback)) {
+ throw new Error('Return value from mappingWithDisposeCallback should have a \'mappedItem\' property.');
+ }
+ mappedValue = mappedValueWithDisposeCallback.mappedValue;
+ this.disposeFuncFromMostRecentMapping = mappedValueWithDisposeCallback.dispose;
+ } else {
+ throw new Error('No mapping callback given.');
+ }
+
+ if (this.isIncluded === null) { // first run
+ this.isIncluded = mappedValue !== this.mappingOptions.exclusionMarker;
+ }
+
+ return mappedValue;
+ };
+
+ StateItem.prototype.updateInclusion = function () {
+ var outputArrayIndex = this.outputArrayIndex.peek();
+ var outputArray = this.outputArray;
+ for (var iterationIndex = this.stateArrayIndex; iterationIndex < this.arrayOfState.length; iterationIndex += 1) {
+ var stateItem = this.arrayOfState[iterationIndex];
+
+ stateItem.setOutputArrayIndexSilently(outputArrayIndex);
+ var newValue = stateItem.mappingEvaluator();
+ var newInclusionState = newValue !== this.mappingOptions.exclusionMarker;
+
+ // Inclusion state changes can *only* happen as a result of changing an individual item.
+ // Structural changes to the array can't cause this (because they don't cause any remapping;
+ // they only map newly added items which have no earlier inclusion state to change).
+ if (newInclusionState) {
+ outputArray[outputArrayIndex] = newValue;
+ outputArrayIndex += 1;
+ }
+
+ stateItem.previousMappedValue = newValue;
+ stateItem.isIncluded = newInclusionState;
+ }
+ if (outputArrayIndex < outputArray.length) {
+ outputArray.length = outputArrayIndex;
+ }
+ };
+
+ StateItem.prototype.onMappingResultChanged = function (newValue) {
+ if (newValue !== this.previousMappedValue) {
+ if (!this.suppressNotification) {
+ this.outputObservableArray.valueWillMutate();
+ }
+
+ var newInclusionState = newValue !== this.mappingOptions.exclusionMarker;
+ if (this.isIncluded !== newInclusionState) {
+ this.updateInclusion();
+ } else {
+ if (newInclusionState) {
+ this.outputArray[this.outputArrayIndex.peek()] = newValue;
+ }
+ this.previousMappedValue = newValue;
+ }
+
+ if (!this.suppressNotification) {
+ this.outputObservableArray.valueHasMutated();
+ }
+ }
+ };
+
+ StateItem.prototype.setOutputArrayIndexSilently = function (newIndex) {
+ // We only want to raise one output array notification per input array change,
+ // so during processing, we suppress notifications
+ this.suppressNotification = true;
+ this.outputArrayIndex(newIndex);
+ this.suppressNotification = false;
+ };
+
+ function getDiffEntryPostOperationIndex(diffEntry, editOffset) {
+ // The diff algorithm's "index" value refers to the output array for additions,
+ // but the "input" array for deletions. Get the output array position.
+ if (!diffEntry) { return null; }
+ switch (diffEntry.status) {
+ case 'added':
+ return diffEntry.index;
+ case 'deleted':
+ return diffEntry.index + editOffset;
+ default:
+ throw new Error('Unknown diff status: ' + diffEntry.status);
+ }
+ }
+
+ function insertOutputItem(diffEntry, movedStateItems, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray, outputArray) {
+ // Retain the existing mapped value if this is a move, otherwise perform mapping
+ var isMoved = typeof diffEntry.moved === 'number',
+ stateItem = isMoved ?
+ movedStateItems[diffEntry.moved] :
+ new StateItem(diffEntry.value, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray);
+ arrayOfState.splice(stateArrayIndex, 0, stateItem);
+
+ if (stateItem.isIncluded) {
+ outputArray.splice(outputArrayIndex, 0, stateItem.mappedValueComputed.peek());
+ }
+
+ // Update indexes
+ if (isMoved) {
+ // We don't change the index until *after* updating this item's position in outputObservableArray,
+ // because changing the index may trigger re-mapping, which in turn would cause the new
+ // value to be written to the 'index' position in the output array
+ stateItem.stateArrayIndex = stateArrayIndex;
+ stateItem.setOutputArrayIndexSilently(outputArrayIndex);
+ }
+
+ return stateItem;
+ }
+
+ function deleteOutputItem(diffEntry, arrayOfState, stateArrayIndex, outputArrayIndex, outputArray) {
+ var stateItem = arrayOfState.splice(stateArrayIndex, 1)[0];
+ if (stateItem.isIncluded) {
+ outputArray.splice(outputArrayIndex, 1);
+ }
+ if (typeof diffEntry.moved !== 'number') {
+ // Be careful to dispose only if this item really was deleted and not moved
+ stateItem.dispose();
+ }
+ }
+
+ function updateRetainedOutputItem(stateItem, stateArrayIndex, outputArrayIndex) {
+ // Just have to update its indexes
+ stateItem.stateArrayIndex = stateArrayIndex;
+ stateItem.setOutputArrayIndexSilently(outputArrayIndex);
+
+ // Return the new value for outputArrayIndex
+ return outputArrayIndex + (stateItem.isIncluded ? 1 : 0);
+ }
+
+ function makeLookupOfMovedStateItems(diff, arrayOfState) {
+ // Before we mutate arrayOfComputedMappedValues at all, grab a reference to each moved item
+ var movedStateItems = {};
+ for (var diffIndex = 0; diffIndex < diff.length; diffIndex += 1) {
+ var diffEntry = diff[diffIndex];
+ if (diffEntry.status === 'added' && (typeof diffEntry.moved === 'number')) {
+ movedStateItems[diffEntry.moved] = arrayOfState[diffEntry.moved];
+ }
+ }
+ return movedStateItems;
+ }
+
+ function getFirstModifiedOutputIndex(firstDiffEntry, arrayOfState, outputArray) {
+ // Work out where the first edit will affect the output array
+ // Then we can update outputArrayIndex incrementally while walking the diff list
+ if (!outputArray.length || !arrayOfState[firstDiffEntry.index]) {
+ // The first edit is beyond the end of the output or state array, so we must
+ // just be appending items.
+ return outputArray.length;
+ } else {
+ // The first edit corresponds to an existing state array item, so grab
+ // the first output array index from it.
+ return arrayOfState[firstDiffEntry.index].outputArrayIndex.peek();
+ }
+ }
+
+ function respondToArrayStructuralChanges(inputObservableArray, arrayOfState, outputArray, outputObservableArray, mappingOptions) {
+ return inputObservableArray.subscribe(function (diff) {
+ if (!diff.length) {
+ return;
+ }
+
+ if (arrayOfState.length === 0) {
+ // Only add items
+ var newOutputItems = [];
+ ko.utils.arrayForEach(diff, function (diffEntry, i) {
+ var inputItem = diffEntry.value;
+ var stateItem = new StateItem(inputItem, i, newOutputItems.length, mappingOptions, arrayOfState, outputObservableArray);
+ var mappedValue = stateItem.mappedValueComputed.peek();
+ arrayOfState.push(stateItem);
+
+ if (stateItem.isIncluded) {
+ newOutputItems.push(mappedValue);
+ }
+ });
+
+ outputObservableArray.push.apply(outputObservableArray, newOutputItems);
+ return;
+ }
+
+ outputObservableArray.valueWillMutate();
+
+ var movedStateItems = makeLookupOfMovedStateItems(diff, arrayOfState),
+ diffIndex = 0,
+ diffEntry = diff[0],
+ editOffset = 0, // A running total of (num(items added) - num(items deleted)) not accounting for filtering
+ outputArrayIndex = diffEntry && getFirstModifiedOutputIndex(diffEntry, arrayOfState, outputArray);
+
+ // Now iterate over the state array, at each stage checking whether the current item
+ // is the next one to have been edited. We can skip all the state array items whose
+ // indexes are less than the first edit index (i.e., diff[0].index).
+ for (var stateArrayIndex = diffEntry.index; diffEntry || (stateArrayIndex < arrayOfState.length); stateArrayIndex += 1) {
+ // Does the current diffEntry correspond to this position in the state array?
+ if (getDiffEntryPostOperationIndex(diffEntry, editOffset) === stateArrayIndex) {
+ // Yes - insert or delete the corresponding state and output items
+ switch (diffEntry.status) {
+ case 'added':
+ // Add to output, and update indexes
+ var stateItem = insertOutputItem(diffEntry, movedStateItems, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray, outputArray);
+ if (stateItem.isIncluded) {
+ outputArrayIndex += 1;
+ }
+ editOffset += 1;
+ break;
+ case 'deleted':
+ // Just erase from the output, and update indexes
+ deleteOutputItem(diffEntry, arrayOfState, stateArrayIndex, outputArrayIndex, outputArray);
+ editOffset -= 1;
+ stateArrayIndex -= 1; // To compensate for the "for" loop incrementing it
+ break;
+ default:
+ throw new Error('Unknown diff status: ' + diffEntry.status);
+ }
+
+ // We're done with this diff entry. Move on to the next one.
+ diffIndex += 1;
+ diffEntry = diff[diffIndex];
+ } else if (stateArrayIndex < arrayOfState.length) {
+ // No - the current item was retained. Just update its index.
+ outputArrayIndex = updateRetainedOutputItem(arrayOfState[stateArrayIndex], stateArrayIndex, outputArrayIndex);
+ }
+ }
+
+ outputObservableArray.valueHasMutated();
+ }, null, 'arrayChange');
+ }
+
+ ko.observableArray.fn.map = ko.transformations.fn.map = function map(mappingOptions) {
+ var that = this,
+ arrayOfState = [],
+ outputArray = [],
+ outputObservableArray = ko.observableArray(outputArray),
+ originalInputArrayContents = that.peek();
+
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof mappingOptions === 'function') {
+ mappingOptions = { mapping: mappingOptions };
+ }
+
+ if (!mappingOptions.exclusionMarker) {
+ mappingOptions.exclusionMarker = {};
+ }
+
+ // Validate the options
+ if (mappingOptions.mappingWithDisposeCallback) {
+ if (mappingOptions.mapping || mappingOptions.disposeItem) {
+ throw new Error('\'mappingWithDisposeCallback\' cannot be used in conjunction with \'mapping\' or \'disposeItem\'.');
+ }
+ } else if (!mappingOptions.mapping) {
+ throw new Error('Specify either \'mapping\' or \'mappingWithDisposeCallback\'.');
+ }
+
+ // Initial state: map each of the inputs
+ for (var i = 0; i < originalInputArrayContents.length; i += 1) {
+ var inputItem = originalInputArrayContents[i],
+ stateItem = new StateItem(inputItem, i, outputArray.length, mappingOptions, arrayOfState, outputObservableArray),
+ mappedValue = stateItem.mappedValueComputed.peek();
+ arrayOfState.push(stateItem);
+
+ if (stateItem.isIncluded) {
+ outputArray.push(mappedValue);
+ }
+ }
+
+ // If the input array changes structurally (items added or removed), update the outputs
+ var inputArraySubscription = respondToArrayStructuralChanges(that, arrayOfState, outputArray, outputObservableArray, mappingOptions);
+
+ var outputComputed = ko.computed(outputObservableArray);
+ if ('throttle' in mappingOptions) {
+ outputComputed = outputComputed.extend({ throttle: mappingOptions.throttle });
+ }
+ // Return value is a readonly computed which can track its own changes to permit chaining.
+ // When disposed, it cleans up everything it created.
+ var returnValue = outputComputed.extend({ trackArrayChanges: true }),
+ originalDispose = returnValue.dispose;
+ returnValue.dispose = function () {
+ inputArraySubscription.dispose();
+ ko.utils.arrayForEach(arrayOfState, function (stateItem) {
+ stateItem.dispose();
+ });
+ originalDispose.call(this, arguments);
+ };
+
+ // Make transformations chainable
+ ko.utils.extend(returnValue, ko.transformations.fn);
+
+ return returnValue;
+ };
+
+ return ko.transformations.fn.map;
+}));
diff --git a/lib/sortBy.js b/lib/sortBy.js
new file mode 100644
index 0000000..102c836
--- /dev/null
+++ b/lib/sortBy.js
@@ -0,0 +1,340 @@
+(function (root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory(require('knockout'));
+ } else if (typeof define === 'function' && define.amd) {
+ define(['knockout'], factory);
+ } else {
+ factory(root.ko);
+ }
+}(this, function (ko) {
+ ko.transformations = ko.transformations || {
+ fn: {}
+ };
+
+ function sortingKeysEquals(aSortKeys, bSortKeys) {
+ var Descending = SortByTransformation.Descending;
+ if (!Array.isArray(aSortKeys)) {
+ aSortKeys = [aSortKeys];
+ bSortKeys = [bSortKeys];
+ }
+
+ var aSortKey, bSortKey;
+
+ for (var i = 0; i < aSortKeys.length; i += 1) {
+ aSortKey = aSortKeys[i];
+ bSortKey = bSortKeys[i];
+ if (aSortKey instanceof Descending) {
+ if (bSortKey instanceof Descending) {
+ aSortKey = aSortKey.value;
+ bSortKey = bSortKey.value;
+ } else {
+ return false;
+ }
+ }
+
+ if (aSortKey !== bSortKey) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ function compareSortingKeys(aSortKeys, bSortKeys, comparator) {
+ var Descending = SortByTransformation.Descending;
+ if (!Array.isArray(aSortKeys)) {
+ aSortKeys = [aSortKeys];
+ bSortKeys = [bSortKeys];
+ }
+
+ var aSortKey, bSortKey, comparison;
+
+ for (var i = 0; i < aSortKeys.length; i += 1) {
+ aSortKey = aSortKeys[i];
+ bSortKey = bSortKeys[i];
+ if (aSortKey instanceof Descending) {
+ comparison = comparator(bSortKey.value, aSortKey.value);
+ } else {
+ comparison = comparator(aSortKey, bSortKey);
+ }
+ if (comparison !== 0) {
+ return comparison;
+ }
+ }
+ return 0;
+ }
+
+ // Sorting
+ function mappingToComparefn(mapping, comparator) {
+ var Descending = SortByTransformation.Descending;
+ return function (a, b) {
+ var aSortKeys = mapping(a, Descending.create);
+ var bSortKeys = mapping(b, Descending.create);
+ return compareSortingKeys(aSortKeys, bSortKeys, comparator);
+ };
+ }
+
+ function binarySearch(items, item, comparefn) {
+ var left = -1,
+ right = items.length,
+ mid;
+
+ while (right - left > 1) {
+ mid = (left + right) >>> 1;
+ var c = comparefn(items[mid], item);
+ if (c < 0) {
+ left = mid;
+ } else {
+ right = mid;
+ if (!c) {
+ break;
+ }
+ }
+ }
+ return (right === items.length || comparefn(items[right], item)) ? -right - 1 : right;
+ }
+
+ function findInsertionIndex(items, newItem, comparefn) {
+ var left = -1,
+ right = items.length,
+ mid;
+ while (right - left > 1) {
+ mid = (left + right) >>> 1;
+ if (comparefn(items[mid], newItem) < 0) {
+ left = mid;
+ } else {
+ right = mid;
+ }
+ }
+ return right;
+ }
+
+ function binaryIndexOf(items, item, comparefn) {
+ var index = binarySearch(items, item, comparefn);
+ if (index < 0 || items.length <= index || comparefn(items[index], item) !== 0) {
+ return -1;
+ } else {
+ var startIndex = index;
+ // find the first index of an item that looks like the item
+ while (index - 1 >= 0 && comparefn(items[index - 1], item) === 0) {
+ index -= 1;
+ }
+
+ // find the index of the item
+ while (index <= startIndex) {
+ if (items[index] === item) {
+ return index;
+ }
+ index += 1;
+ }
+
+ while (index < items.length) {
+ if (comparefn(items[index], item) !== 0) {
+ return -1;
+ }
+ if (items[index] === item) {
+ return index;
+ }
+
+ index += 1;
+ }
+
+ return -1;
+ }
+ }
+
+ function SortedStateItem(transformation, inputItem) {
+ this.transformation = transformation;
+ this.inputItem = inputItem;
+
+ this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
+ this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
+ this.previousMappedValue = this.mappedValueComputed.peek();
+ }
+
+ SortedStateItem.prototype.dispose = function () {
+ var mappedItem = this.mappedValueComputed();
+ this.mappedValueComputed.dispose();
+ if (this.transformation.options.disposeItem) {
+ this.transformation.options.disposeItem(mappedItem);
+ }
+ };
+
+ SortedStateItem.prototype.mappingEvaluator = function () {
+ return this.transformation.mapping(this.inputItem, SortByTransformation.Descending.create);
+ };
+
+ SortedStateItem.prototype.onMappingResultChanged = function (newValue) {
+ if (!sortingKeysEquals(newValue, this.previousMappedValue)) {
+ var transformation = this.transformation;
+ var outputObservable = transformation.outputObservable;
+ var outputArray = outputObservable.peek();
+ var stateItems = transformation.stateItems;
+ var oldIndex = binaryIndexOf(stateItems, this, mappingToComparefn(function (stateItem) {
+ return stateItem.previousMappedValue;
+ }, transformation.comparator));
+
+ if (stateItems[oldIndex] === this) {
+ outputObservable.valueWillMutate();
+ // It seems the sort order of the underlying array is still usable
+ outputArray.splice(oldIndex, 1);
+ stateItems.splice(oldIndex, 1);
+
+ var index = findInsertionIndex(outputArray, this.inputItem, transformation.comparefn);
+ outputArray.splice(index, 0, this.inputItem);
+ stateItems.splice(index, 0, this);
+
+ this.previousMappedValue = newValue;
+ outputObservable.valueHasMutated();
+ } else {
+ ko.utils.arrayForEach(stateItems, function (stateItem) {
+ stateItem.previousMappedValue = stateItem.mappingEvaluator();
+ });
+
+ // The underlying array needs to be recalculated from scratch
+ stateItems.sort(mappingToComparefn(function (stateItem) {
+ return stateItem.previousMappedValue;
+ }, transformation.comparator));
+
+ outputArray = [];
+ ko.utils.arrayForEach(stateItems, function (stateItem) {
+ outputArray.push(stateItem.inputItem);
+ });
+ outputObservable(outputArray);
+ }
+ }
+ };
+
+ function SortByTransformation(inputObservableArray, options) {
+ var that = this;
+ this.options = options;
+
+ this.mapping = options.mapping;
+ if (options.comparator) {
+ this.comparator = options.comparator;
+ } else {
+ this.comparator = function (a, b) {
+ if (a > b) {
+ return 1;
+ } else if (b > a) {
+ return -1;
+ } else {
+ return 0;
+ }
+ };
+ }
+ this.comparefn = mappingToComparefn(this.mapping, this.comparator);
+
+ this.stateItems = ko.utils.arrayMap(inputObservableArray.peek(), function (inputItem) {
+ return new SortedStateItem(that, inputItem);
+ });
+ this.stateItems.sort(function (a, b) {
+ return compareSortingKeys(a.mappedValueComputed(), b.mappedValueComputed(), that.comparator);
+ });
+
+ this.outputObservable = ko.observable(ko.utils.arrayMap(this.stateItems, function (stateItem) {
+ return stateItem.inputItem;
+ }));
+
+ // If the input array changes structurally (items added or removed), update the outputs
+ var inputArraySubscription = inputObservableArray.subscribe(this.onStructuralChange, this, 'arrayChange');
+
+ var outputComputed = ko.computed(this.outputObservable);
+ if ('throttle' in options) {
+ outputComputed = outputComputed.extend({ throttle: options.throttle });
+ }
+
+ // Return value is a readonly computed which can track its own changes to permit chaining.
+ // When disposed, it cleans up everything it created.
+ this.output = outputComputed.extend({ trackArrayChanges: true });
+ var originalDispose = this.output.dispose;
+ this.output.dispose = function () {
+ inputArraySubscription.dispose();
+ ko.utils.arrayForEach(that.stateItems, function (stateItem) {
+ stateItem.dispose();
+ });
+ originalDispose.call(this, arguments);
+ };
+
+ ko.utils.extend(this.output, ko.transformations.fn);
+ }
+
+ SortByTransformation.Descending = function Descending(value) {
+ this.value = value;
+ };
+
+ SortByTransformation.Descending.create = function (value) {
+ return new SortByTransformation.Descending(value);
+ };
+
+ SortByTransformation.prototype.onStructuralChange = function (diff) {
+ if (!diff.length) {
+ return;
+ }
+
+ this.outputObservable.valueWillMutate();
+
+ var that = this;
+ var addQueue = [];
+ var deleteQueue = [];
+ ko.utils.arrayForEach(diff, function (diffEntry) {
+ if (typeof diffEntry.moved !== 'number') {
+ switch (diffEntry.status) {
+ case 'added':
+ addQueue.push(diffEntry);
+ break;
+ case 'deleted':
+ deleteQueue.push(diffEntry);
+ break;
+ }
+ }
+ });
+
+ var outputArray = this.outputObservable.peek();
+ ko.utils.arrayForEach(deleteQueue, function (diffEntry) {
+ var index = binaryIndexOf(outputArray, diffEntry.value, that.comparefn);
+ if (index !== -1) {
+ outputArray.splice(index, 1);
+ that.stateItems[index].dispose();
+ that.stateItems.splice(index, 1);
+ }
+ });
+
+ if (deleteQueue.length === 0 && this.stateItems.length === 0) {
+ // Adding to an empty array
+ this.stateItems = ko.utils.arrayMap(addQueue, function (diffEntry) {
+ return new SortedStateItem(that, diffEntry.value);
+ });
+
+ this.stateItems.sort(function (a, b) {
+ return compareSortingKeys(a.mappedValueComputed(), b.mappedValueComputed(), that.comparator);
+ });
+
+ ko.utils.arrayForEach(this.stateItems, function (stateItem) {
+ outputArray.push(stateItem.inputItem);
+ });
+ } else {
+ ko.utils.arrayForEach(addQueue, function (diffEntry) {
+ var index = findInsertionIndex(outputArray, diffEntry.value, that.comparefn);
+ var stateItem = new SortedStateItem(that, diffEntry.value);
+ outputArray.splice(index, 0, stateItem.inputItem);
+ that.stateItems.splice(index, 0, stateItem);
+ });
+ }
+
+ this.outputObservable.valueHasMutated();
+ };
+
+ ko.observableArray.fn.sortBy = ko.transformations.fn.sortBy = function sortBy(options) {
+ // Shorthand syntax - just pass a function instead of an options object
+ if (typeof options === 'function') {
+ options = { mapping: options };
+ }
+
+ var transformation = new SortByTransformation(this, options);
+
+ return transformation.output;
+ };
+
+ return ko.transformations.fn.sortBy;
+}));
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..ce3cd4d
--- /dev/null
+++ b/package.json
@@ -0,0 +1,36 @@
+{
+ "name": "knockout-transformations",
+ "version": "2.1.0",
+ "description": "Live transform methods for Knockout observable arrays",
+ "main": "lib/index.js",
+ "directories": {
+ "test": "test"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/One-com/knockout-transformations"
+ },
+ "peerDependencies": {
+ "knockout": "^3.1.0"
+ },
+ "devDependencies": {
+ "jscs": "1.10.0",
+ "jshint": "2.6.0",
+ "knockout": "3.1.0",
+ "mocha": "2.1.0",
+ "sinon": "1.12.2",
+ "uglify-js": "2.4.16",
+ "unexpected": "5.8.1",
+ "unexpected-knockout": "5.2.0",
+ "unexpected-sinon": "5.0.0"
+ },
+ "licenses": [
+ {
+ "type": "Apache 2.0",
+ "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
+ }
+ ]
+}
diff --git a/test/.jshintrc b/test/.jshintrc
new file mode 100644
index 0000000..2998f1b
--- /dev/null
+++ b/test/.jshintrc
@@ -0,0 +1,17 @@
+{
+ "extends": "../.jshintrc",
+ "browser": true,
+ "node": true,
+
+ "globals": {
+ "require": false,
+ "define": false,
+ "describe": false,
+ "it": false,
+ "before": false,
+ "beforeEach": false,
+ "afterEach": false,
+ "after": false,
+ "defineTest": false
+ }
+}
diff --git a/test/filter.spec.js b/test/filter.spec.js
new file mode 100644
index 0000000..4e910e2
--- /dev/null
+++ b/test/filter.spec.js
@@ -0,0 +1,259 @@
+var ko = require('knockout');
+var expect = require('unexpected').clone()
+ .installPlugin(require('unexpected-sinon'));
+var sinon = require('sinon');
+
+require('../lib/filter.js');
+
+describe("Filter", function () {
+ var clock;
+
+ beforeEach(function () {
+ clock = sinon.useFakeTimers();
+ });
+
+ afterEach(function () {
+ clock.restore();
+ });
+
+ var makeSampleData = function () {
+ var sampleData = {
+ everest: { height: ko.observable(8848) },
+ aconcagua: { height: ko.observable(6961) },
+ mckinley: { height: ko.observable(6194) },
+ kilimanjaro: { height: ko.observable(5895) },
+ elbrus: { height: ko.observable(5642) },
+ vinson: { height: ko.observable(4892) },
+ puncakjaya: { height: ko.observable(4884) }
+ };
+ sampleData.all = [sampleData.everest, sampleData.aconcagua, sampleData.mckinley, sampleData.kilimanjaro, sampleData.elbrus, sampleData.vinson, sampleData.puncakjaya];
+ return sampleData;
+ };
+
+ it("returns a readonly computed observable array", function () {
+ var sampleData = makeSampleData(),
+ sourceArray = ko.observableArray(sampleData.all),
+ filteredArray = sourceArray.filter(function (item) { return true; });
+
+ expect(ko.isObservable(filteredArray), 'to be', true);
+ expect(ko.isComputed(filteredArray), 'to be', true);
+ expect(function () { filteredArray([1, 2, 3]); }, 'to throw',
+ "Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.");
+ });
+
+ it("filters the input array using the predicate", function () {
+ var sampleData = makeSampleData(),
+ sourceArray = ko.observableArray(sampleData.all),
+ filteredArray = sourceArray.filter(function (item) { return item.height() > 6000; });
+
+ // Check we have the original instances
+ expect(filteredArray().length, 'to be', 3);
+ expect(filteredArray()[0], 'to be', sampleData.everest);
+ expect(filteredArray()[1], 'to be', sampleData.aconcagua);
+ expect(filteredArray()[2], 'to be', sampleData.mckinley);
+ });
+
+ it("responds to changes in the input data, but retains the same output array instance", function () {
+ var sampleData = makeSampleData(),
+ sourceArray = ko.observableArray(sampleData.all),
+ filteredArray = sourceArray.filter(function (item) { return item.height() > 6000; }),
+ originalFilteredArrayInstance = filteredArray();
+ expect(filteredArray(), 'to equal', [sampleData.everest, sampleData.aconcagua, sampleData.mckinley]);
+
+ // Make a change to include an item
+ sampleData.vinson.height(10000);
+ expect(filteredArray(), 'to equal', [sampleData.everest, sampleData.aconcagua, sampleData.mckinley, sampleData.vinson]);
+ expect(filteredArray(), 'to be', originalFilteredArrayInstance);
+
+ // Exclude some other
+ sampleData.everest.height(23);
+ expect(filteredArray(), 'to equal', [sampleData.aconcagua, sampleData.mckinley, sampleData.vinson]);
+ });
+
+ it("is possible to chain filters", function () {
+ var sampleData = makeSampleData(),
+ sourceArray = ko.observableArray(sampleData.all),
+ tallOnes = sourceArray.filter(function (item) { return item.height() > 5000; }),
+ heightsOfTallOnes = tallOnes.map(function (item) { return item.height(); }),
+ evenHeightsOfTallOnes = heightsOfTallOnes.filter(function (height) { return height % 2 === 0; });
+
+ expect(tallOnes(), 'to equal', [sampleData.everest, sampleData.aconcagua, sampleData.mckinley, sampleData.kilimanjaro, sampleData.elbrus]);
+ expect(heightsOfTallOnes(), 'to equal', [8848, 6961, 6194, 5895, 5642]);
+ expect(evenHeightsOfTallOnes(), 'to equal', [8848, 6194, 5642]);
+
+ // See that changes ripple through - make a new item appear
+ sampleData.puncakjaya.height(10000);
+ expect(tallOnes(), 'to equal', [sampleData.everest, sampleData.aconcagua, sampleData.mckinley, sampleData.kilimanjaro, sampleData.elbrus, sampleData.puncakjaya]);
+ expect(heightsOfTallOnes(), 'to equal', [8848, 6961, 6194, 5895, 5642, 10000]);
+ expect(evenHeightsOfTallOnes(), 'to equal', [8848, 6194, 5642, 10000]);
+
+ // Make one disappear
+ sampleData.everest.height(2);
+ expect(tallOnes(), 'to equal', [sampleData.aconcagua, sampleData.mckinley, sampleData.kilimanjaro, sampleData.elbrus, sampleData.puncakjaya]);
+ expect(heightsOfTallOnes(), 'to equal', [6961, 6194, 5895, 5642, 10000]);
+ expect(evenHeightsOfTallOnes(), 'to equal', [6194, 5642, 10000]);
+ });
+
+ it("only runs the filter predicate for items affected by a change", function () {
+ var sampleData = makeSampleData(),
+ sourceArray = ko.observableArray(sampleData.all),
+ filterCallsCount = 0,
+ veryTallOnes = sourceArray.filter(function (item) {
+ filterCallsCount += 1;
+ return item.height() > 6000;
+ });
+
+ expect(veryTallOnes(), 'to equal', [sampleData.everest, sampleData.aconcagua, sampleData.mckinley]);
+ expect(filterCallsCount, 'to be', 7); // All were filtered
+
+ // Add one item
+ var newItem = { height: ko.observable(10000) };
+ sourceArray.push(newItem);
+ expect(veryTallOnes(), 'to equal', [sampleData.everest, sampleData.aconcagua, sampleData.mckinley, newItem]);
+ expect(filterCallsCount, 'to be', 8); // Only new item was filtered
+
+ // Remove one item
+ sourceArray.remove(sampleData.aconcagua);
+ expect(veryTallOnes(), 'to equal', [sampleData.everest, sampleData.mckinley, newItem]);
+ expect(filterCallsCount, 'to be', 8); // No additional filter calls were required
+ });
+
+ it("only issues notifications when some inclusion status has actually changed", function () {
+ var sampleData = makeSampleData(),
+ sourceArray = ko.observableArray(sampleData.all),
+ outputNotifications = 0,
+ veryTallOnes = sourceArray.filter(function (item) {
+ return item.height() > 6000;
+ });
+ veryTallOnes.subscribe(function () {
+ outputNotifications += 1;
+ });
+ expect(veryTallOnes(), 'to equal', [sampleData.everest, sampleData.aconcagua, sampleData.mckinley]);
+
+ // Mutate one to make it become excluded
+ sampleData.everest.height(10);
+ expect(outputNotifications, 'to be', 1);
+ expect(veryTallOnes(), 'to equal', [sampleData.aconcagua, sampleData.mckinley]);
+
+ // Mutate one to make it become included
+ sampleData.puncakjaya.height(10000);
+ expect(outputNotifications, 'to be', 2);
+ expect(veryTallOnes(), 'to equal', [sampleData.aconcagua, sampleData.mckinley, sampleData.puncakjaya]);
+
+ // Mutate an included one in such a way that it remains included
+ sampleData.mckinley.height(12345);
+ expect(outputNotifications, 'to be', 2); // No new notifications
+
+ // Mutate an excluded one in such a way that it remains excluded
+ sampleData.everest.height(123);
+ expect(outputNotifications, 'to be', 2); // No new notifications
+ });
+
+ it('supports a throttle option', function () {
+ var underlyingArray = ko.observableArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+
+ var modulus = ko.observable(1);
+ var filteredArray = underlyingArray.filter({
+ mapping: function (x) {
+ return x % modulus() === 0;
+ },
+ throttle: 200
+ });
+ var spy = sinon.spy();
+ filteredArray.subscribe(spy);
+
+ expect(filteredArray(), 'to equal', underlyingArray());
+ modulus(3);
+ expect(spy, 'was not called');
+
+ clock.tick(201);
+
+ expect(spy, 'was called once');
+
+ expect(filteredArray(), 'to equal', [0, 3, 6, 9]);
+ });
+
+ describe('group inclussion example', function () {
+ var persons, groups, person;
+
+ function Person(id) {
+ var that = this;
+ this.id = id;
+ this.groups = groups.filter(function (group) {
+ return group.contains(that);
+ });
+ }
+
+ function Group(ids) {
+ this.ids = ko.observableArray(ids);
+ this.members = this.ids.map(function (id) {
+ return persons().filter(function (person) {
+ return person.id === id;
+ })[0];
+ });
+ }
+
+ Group.prototype.contains = function (member) {
+ return this.ids.indexOf(member.id) !== -1;
+ };
+
+ Group.prototype.add = function (member) {
+ if (!this.contains(member)) {
+ this.ids.push(member.id);
+ }
+ };
+
+ Group.prototype.remove = function (member) {
+ if (this.contains(member)) {
+ this.ids.remove(member.id);
+ }
+ };
+
+ beforeEach(function () {
+ persons = ko.observableArray([]);
+ groups = ko.observableArray([]);
+ for (var id = 0; id < 10; id += 1) {
+ persons.push(new Person(id));
+ }
+ groups([
+ new Group([0, 1, 2]),
+ new Group([2, 3, 4, 5, 6]),
+ new Group([5, 6, 7, 8, 9])
+ ]);
+ person = persons()[5];
+ });
+
+ it('group membership is initialized correctly', function () {
+ expect(person.groups(), 'to equal', groups.slice(1));
+ expect(groups()[0].members(), 'not to contain', person);
+ expect(groups()[1].members(), 'to contain', person);
+ expect(groups()[2].members(), 'to contain', person);
+ });
+
+ it('group membership is updated correctly when the person is added to an existing group', function () {
+ groups()[0].add(person);
+ expect(person.groups(), 'to equal', groups());
+ expect(groups()[0].members(), 'to contain', person);
+ expect(groups()[1].members(), 'to contain', person);
+ expect(groups()[2].members(), 'to contain', person);
+ });
+
+ it('group membership is updated correctly when the person is removed from a group', function () {
+ groups()[1].remove(person);
+ expect(person.groups(), 'to equal', groups.slice(2));
+ expect(groups()[0].members(), 'not to contain', person);
+ expect(groups()[1].members(), 'not to contain', person);
+ expect(groups()[2].members(), 'to contain', person);
+ });
+
+ it('group membership is updated correctly when the person is added to a new group', function () {
+ groups.push(new Group());
+ groups()[groups().length - 1].add(person);
+ expect(person.groups(), 'to equal', groups.slice(1));
+ expect(groups()[0].members(), 'not to contain', person);
+ expect(groups()[1].members(), 'to contain', person);
+ expect(groups()[2].members(), 'to contain', person);
+ expect(groups()[3].members(), 'to contain', person);
+ });
+ });
+});
diff --git a/test/indexBy.spec.js b/test/indexBy.spec.js
new file mode 100644
index 0000000..1569525
--- /dev/null
+++ b/test/indexBy.spec.js
@@ -0,0 +1,688 @@
+var ko = require('knockout');
+var expect = require('unexpected').clone()
+ .installPlugin(require('unexpected-sinon'))
+ .installPlugin(require('unexpected-knockout'));
+var sinon = require('sinon');
+
+require('../lib/indexBy.js');
+
+function Person(name, yearOfBirth) {
+ this.name = ko.observable(name);
+ this.yearOfBirth = ko.observable(yearOfBirth);
+}
+
+expect.addType({
+ name: 'Person',
+ base: 'object',
+ identify: function (value) {
+ return value instanceof Person;
+ },
+ inspect: function (value, depth, output) {
+ output.text(value.yearOfBirth() + ' ' + value.name());
+ }
+});
+
+
+expect.addAssertion('to contain items for key', function (expect, subject, key, itemNames) {
+ var actualNames = subject()[key].map(function (item) {
+ return item && item.name;
+ }).sort();
+ var expectedNames = [].concat(itemNames).sort();
+ this.errorMode = 'nested';
+ expect(actualNames, 'to equal', expectedNames);
+});
+
+describe("IndexBy", function () {
+ var clock;
+
+ beforeEach(function () {
+ clock = sinon.useFakeTimers();
+ });
+
+ afterEach(function () {
+ clock.restore();
+ });
+
+ describe('on an empty array', function () {
+ var sampleData, sourceArray, indexedData;
+ beforeEach(function () {
+ sampleData = [];
+ sourceArray = ko.observableArray(sampleData);
+ indexedData = sourceArray.indexBy(function (item) {
+ return item;
+ });
+ });
+
+ it('results in an empty map', function () {
+ expect(indexedData(), 'to equal', {});
+ });
+ });
+
+ describe('on simple data', function () {
+ var sampleData, sourceArray, indexedData;
+
+ beforeEach(function () {
+ sampleData = ['Beta', 'Beta', 'Gamma', 'Alpha'];
+ sourceArray = ko.observableArray(sampleData);
+ indexedData = sourceArray.indexBy(function (item) { return item; })();
+ });
+
+ it("indexes the array according to the given function, returning a computed map", function () {
+ expect(indexedData, 'to equal', {
+ 'Alpha': ['Alpha'],
+ 'Beta': ['Beta', 'Beta'],
+ 'Gamma': ['Gamma']
+ });
+ });
+
+ it('updates the index on unshift', function () {
+ sourceArray.unshift('Foo', 'Bar');
+ expect(indexedData, 'to equal', {
+ 'Alpha': ['Alpha'],
+ 'Beta': ['Beta', 'Beta'],
+ 'Gamma': ['Gamma'],
+ 'Foo': ['Foo'],
+ 'Bar': ['Bar']
+ });
+ });
+
+ it('updates the index on push', function () {
+ sourceArray.push('Foo', 'Bar', 'Beta');
+ expect(indexedData, 'to equal', {
+ 'Alpha': ['Alpha'],
+ 'Beta': ['Beta', 'Beta', 'Beta'],
+ 'Gamma': ['Gamma'],
+ 'Foo': ['Foo'],
+ 'Bar': ['Bar']
+ });
+ });
+
+ it('updates the index on splice', function () {
+ sourceArray.splice(3, 0, 'Foo', 'Bar');
+ expect(indexedData, 'to equal', {
+ 'Alpha': ['Alpha'],
+ 'Beta': ['Beta', 'Beta'],
+ 'Gamma': ['Gamma'],
+ 'Foo': ['Foo'],
+ 'Bar': ['Bar']
+ });
+ });
+
+ it('updates the index on replacing with splice', function () {
+ sourceArray.splice(1, 2, 'Foo', 'Bar');
+ expect(indexedData, 'to equal', {
+ 'Alpha': ['Alpha'],
+ 'Beta': ['Beta'],
+ 'Foo': ['Foo'],
+ 'Bar': ['Bar']
+ });
+ });
+
+ it('updates the index on pop', function () {
+ sourceArray.pop();
+ expect(indexedData, 'to equal', {
+ 'Beta': ['Beta', 'Beta'],
+ 'Gamma': ['Gamma']
+ });
+ });
+
+ it('updates the index on shift', function () {
+ sourceArray.shift();
+ expect(indexedData, 'to equal', {
+ 'Alpha': ['Alpha'],
+ 'Beta': ['Beta'],
+ 'Gamma': ['Gamma']
+ });
+ });
+
+ it('does not change the index when the data is reversed', function () {
+ sourceArray.reverse();
+ expect(indexedData, 'to equal', {
+ 'Alpha': ['Alpha'],
+ 'Beta': ['Beta', 'Beta'],
+ 'Gamma': ['Gamma']
+ });
+ });
+
+ it('does not change the index when the data is sort', function () {
+ sourceArray.sort();
+ expect(indexedData, 'to equal', {
+ 'Alpha': ['Alpha'],
+ 'Beta': ['Beta', 'Beta'],
+ 'Gamma': ['Gamma']
+ });
+ });
+ });
+
+ describe('on complex data', function () {
+ var sampleData, sourceArray, indexedData, expectedIndex;
+ var queenVictoria, johnMKeynes;
+
+ beforeEach(function () {
+ queenVictoria = new Person("Queen Victoria", 1819);
+ johnMKeynes = new Person("John M Keynes", 1883);
+
+ sampleData = [
+ new Person("Marilyn Monroe", 1926),
+ new Person("Abraham Lincoln", 1809),
+ new Person("Mother Teresa", 1910),
+ new Person("John F. Kennedy", 1917),
+ new Person("Martin Luther King", 1929),
+ new Person("Nelson Mandela", 1918),
+ new Person("Winston Churchill", 1874),
+ new Person("Bill Gates", 1955),
+ new Person("Muhammad Ali", 1942),
+ new Person("Mahatma Gandhi", 1869),
+ new Person("Margaret Thatcher", 1925),
+ new Person("Charles de Gaulle", 1890),
+ new Person("Christopher Columbus", 1451),
+ new Person("George Orwell", 1903),
+ new Person("Charles Darwin", 1809),
+ new Person("Elvis Presley", 1935),
+ new Person("Albert Einstein", 1879),
+ new Person("Paul McCartney", 1942),
+ new Person("Plato", 423),
+ new Person("Plato", 427),
+ new Person("Queen Elizabeth II", 1926)
+ ];
+
+ expectedIndex = {};
+ ko.utils.arrayForEach(sampleData, function (person) {
+ expectedIndex[person.name()] = expectedIndex[person.name()] || [];
+ expectedIndex[person.name()].push(person);
+ });
+
+ sourceArray = ko.observableArray(sampleData);
+ indexedData = sourceArray.indexBy(function (person) {
+ return person.name();
+ })();
+ });
+
+ it("indexes the array according to the given function, returning a computed map", function () {
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on unshift', function () {
+ sourceArray.unshift(queenVictoria, johnMKeynes);
+
+ expectedIndex[queenVictoria.name()] = [queenVictoria];
+ expectedIndex[johnMKeynes.name()] = [johnMKeynes];
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on push', function () {
+ sourceArray.push(queenVictoria, johnMKeynes);
+
+ expectedIndex[queenVictoria.name()] = [queenVictoria];
+ expectedIndex[johnMKeynes.name()] = [johnMKeynes];
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on splice', function () {
+ sourceArray.splice(3, 0, queenVictoria, johnMKeynes);
+
+ expectedIndex[queenVictoria.name()] = [queenVictoria];
+ expectedIndex[johnMKeynes.name()] = [johnMKeynes];
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on replacing with splice', function () {
+ delete expectedIndex[sampleData[3].name()];
+ delete expectedIndex[sampleData[4].name()];
+
+ sourceArray.splice(3, 2, queenVictoria, johnMKeynes);
+
+ expectedIndex[queenVictoria.name()] = [queenVictoria];
+ expectedIndex[johnMKeynes.name()] = [johnMKeynes];
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on pop', function () {
+ delete expectedIndex[sampleData[sampleData.length - 1].name()];
+ sourceArray.pop();
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on shift', function () {
+ delete expectedIndex[sampleData[0].name()];
+ sourceArray.shift();
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('does not change the index when the data is reversed', function () {
+ sourceArray.reverse();
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('does not change the index when the data is sort', function () {
+ sourceArray.sort();
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index when a sort key changes', function () {
+ var nameBeforeChange = sampleData[3].name.peek();
+ expectedIndex['Jesus Christ'] = [sampleData[3]];
+ delete expectedIndex[nameBeforeChange];
+
+ sampleData[3].name('Jesus Christ');
+
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ describe('on multiple keys', function () {
+ it("indexes the array according to the given function, returning a computed map", function () {
+ indexedData = sourceArray.indexBy(function (person) {
+ return [person.name(), person.yearOfBirth()];
+ })();
+
+ expectedIndex = {};
+ ko.utils.arrayForEach(sampleData, function (person) {
+ expectedIndex[person.name()] = expectedIndex[person.name()] || [];
+ expectedIndex[person.name()].push(person);
+
+ expectedIndex[person.yearOfBirth()] = expectedIndex[person.yearOfBirth()] || [];
+ expectedIndex[person.yearOfBirth()].push(person);
+ });
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ describe('handling a many-to-many relation', function () {
+ var sampleData, sourceArray, indexedData;
+ beforeEach(function () {
+ sampleData = [
+ {
+ name: 'Alpha',
+ connections: ko.observableArray([ 1, 2, 3 ])
+ },
+ {
+ name: 'Beta',
+ connections: ko.observableArray([ 1, 3 ])
+ },
+ {
+ name: 'Gamma',
+ connections: ko.observableArray([ 2, 4 ])
+ },
+ {
+ name: 'Delta',
+ connections: ko.observableArray([ 1, 3, 4 ])
+ }
+ ];
+ sourceArray = ko.observableArray(sampleData);
+ indexedData = sourceArray.indexBy(function (item) {
+ return item.connections();
+ });
+ });
+
+ it('updates the array when an item is added to the source array', function () {
+ sourceArray.push({
+ name: 'Epsilon',
+ connections: ko.observableArray([ 1, 4 ])
+ });
+ expect(indexedData, 'to contain items for key', 4, [
+ 'Gamma', 'Delta', 'Epsilon'
+ ]);
+ });
+
+ it('updates the array when an item is deleted from the source array', function () {
+ sourceArray.remove(sampleData[3]);
+ expect(indexedData, 'to contain items for key', 4, [
+ 'Gamma'
+ ]);
+ });
+
+ it('updates the array when a key is added to an item in the source array', function () {
+ sampleData[0].connections.push(4);
+ expect(indexedData, 'to contain items for key', 4, [
+ 'Alpha', 'Gamma', 'Delta'
+ ]);
+ });
+
+ it('updates the array when a key is removed from an item in the source array', function () {
+ sampleData[3].connections.remove(4);
+ expect(indexedData, 'to contain items for key', 4, [
+ 'Gamma'
+ ]);
+ });
+ });
+ });
+ });
+});
+
+describe("UniqueIndexBy", function () {
+ var clock;
+
+ beforeEach(function () {
+ clock = sinon.useFakeTimers();
+ });
+
+ afterEach(function () {
+ clock.restore();
+ });
+
+ describe('on an empty array', function () {
+ var sampleData, sourceArray, indexedData;
+ beforeEach(function () {
+ sampleData = [];
+ sourceArray = ko.observableArray(sampleData);
+ indexedData = sourceArray.indexBy({
+ unique: true,
+ mapping: function (item) {
+ return item;
+ }
+ })();
+ });
+
+ it('results in an empty map', function () {
+ expect(indexedData, 'to equal', {});
+ });
+ });
+
+ describe('on simple data', function () {
+ var sampleData, sourceArray, indexedData;
+
+ beforeEach(function () {
+ sampleData = ['Beta', 'Gamma', 'Alpha'];
+ sourceArray = ko.observableArray(sampleData);
+ indexedData = sourceArray.uniqueIndexBy(function (item) {
+ return item;
+ })();
+ });
+
+ it("indexes the array according to the given function, returning a computed map", function () {
+ expect(indexedData, 'to equal', {
+ 'Alpha': 'Alpha',
+ 'Beta': 'Beta',
+ 'Gamma': 'Gamma'
+ });
+ });
+
+ it('updates the index on unshift', function () {
+ sourceArray.unshift('Foo', 'Bar');
+ expect(indexedData, 'to equal', {
+ 'Alpha': 'Alpha',
+ 'Beta': 'Beta',
+ 'Gamma': 'Gamma',
+ 'Foo': 'Foo',
+ 'Bar': 'Bar'
+ });
+ });
+
+ it('updates the index on push', function () {
+ sourceArray.push('Foo', 'Bar');
+ expect(indexedData, 'to equal', {
+ 'Alpha': 'Alpha',
+ 'Beta': 'Beta',
+ 'Gamma': 'Gamma',
+ 'Foo': 'Foo',
+ 'Bar': 'Bar'
+ });
+ });
+
+ it('updates the index on splice', function () {
+ sourceArray.splice(3, 0, 'Foo', 'Bar');
+ expect(indexedData, 'to equal', {
+ 'Alpha': 'Alpha',
+ 'Beta': 'Beta',
+ 'Gamma': 'Gamma',
+ 'Foo': 'Foo',
+ 'Bar': 'Bar'
+ });
+ });
+
+ it('updates the index on replacing with splice', function () {
+ sourceArray.splice(1, 2, 'Foo', 'Bar');
+ expect(indexedData, 'to equal', {
+ 'Beta': 'Beta',
+ 'Foo': 'Foo',
+ 'Bar': 'Bar'
+ });
+ });
+
+ it('updates the index on pop', function () {
+ sourceArray.pop();
+ expect(indexedData, 'to equal', {
+ 'Beta': 'Beta',
+ 'Gamma': 'Gamma'
+ });
+ });
+
+ it('updates the index on shift', function () {
+ sourceArray.shift();
+ expect(indexedData, 'to equal', {
+ 'Alpha': 'Alpha',
+ 'Gamma': 'Gamma'
+ });
+ });
+
+ it('does not change the index when the data is reversed', function () {
+ sourceArray.reverse();
+ expect(indexedData, 'to equal', {
+ 'Alpha': 'Alpha',
+ 'Beta': 'Beta',
+ 'Gamma': 'Gamma'
+ });
+ });
+
+ it('does not change the index when the data is sort', function () {
+ sourceArray.sort();
+ expect(indexedData, 'to equal', {
+ 'Alpha': 'Alpha',
+ 'Beta': 'Beta',
+ 'Gamma': 'Gamma'
+ });
+ });
+
+ it('throw an error if multiple items maps to the same key', function () {
+ expect(function () {
+ sourceArray.push('Beta');
+ }, 'to throw', 'Unique indexes requires items must map to different keys; duplicate key: Beta');
+ });
+
+ it.skip('handles reindexing on another field', function () {
+ sampleData = ko.utils.arrayMap(sampleData, function (text, i) {
+ return { first: text, next: sampleData[(i + 1) % sampleData.length] };
+ });
+ var indexField = ko.observable('first');
+ sourceArray = ko.observableArray(sampleData);
+ indexedData = sourceArray.uniqueIndexBy(function (item) {
+ return item[indexField()];
+ });
+
+ expect(indexedData(), 'to equal', {
+ 'Alpha': { first: 'Alpha', next: 'Beta' },
+ 'Beta': { first: 'Beta', next: 'Gamma' },
+ 'Gamma': { first: 'Gamma', next: 'Alpha' }
+ });
+
+ indexField('next');
+
+ expect(indexedData(), 'to equal', {
+ 'Beta': { first: 'Alpha', next: 'Beta' },
+ 'Gamma': { first: 'Beta', next: 'Gamma' },
+ 'Alpha': { first: 'Gamma', next: 'Alpha' }
+ });
+ });
+ });
+
+ describe('on complex data', function () {
+ var sampleData, sourceArray, indexedData, expectedIndex;
+ var queenVictoria, johnMKeynes;
+
+ beforeEach(function () {
+ queenVictoria = new Person("Queen Victoria", 1819);
+ johnMKeynes = new Person("John M Keynes", 1883);
+
+ sampleData = [
+ new Person("Marilyn Monroe", 1926),
+ new Person("Abraham Lincoln", 1809),
+ new Person("Mother Teresa", 1910),
+ new Person("John F. Kennedy", 1917),
+ new Person("Martin Luther King", 1929),
+ new Person("Nelson Mandela", 1918),
+ new Person("Winston Churchill", 1874),
+ new Person("Bill Gates", 1955),
+ new Person("Muhammad Ali", 1942),
+ new Person("Mahatma Gandhi", 1869),
+ new Person("Margaret Thatcher", 1925),
+ new Person("Charles de Gaulle", 1890),
+ new Person("Christopher Columbus", 1451),
+ new Person("George Orwell", 1903),
+ new Person("Elvis Presley", 1935),
+ new Person("Albert Einstein", 1879),
+ new Person("Plato", 423)
+ ];
+
+ expectedIndex = {};
+ ko.utils.arrayForEach(sampleData, function (person) {
+ expectedIndex[person.name()] = person;
+ });
+
+ sourceArray = ko.observableArray(sampleData);
+ indexedData = sourceArray.uniqueIndexBy(function (person) {
+ return person.name();
+ })();
+ });
+
+ it("indexes the array according to the given function, returning a computed map", function () {
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on unshift', function () {
+ sourceArray.unshift(queenVictoria, johnMKeynes);
+
+ expectedIndex[queenVictoria.name()] = queenVictoria;
+ expectedIndex[johnMKeynes.name()] = johnMKeynes;
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on push', function () {
+ sourceArray.push(queenVictoria, johnMKeynes);
+
+ expectedIndex[queenVictoria.name()] = queenVictoria;
+ expectedIndex[johnMKeynes.name()] = johnMKeynes;
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on splice', function () {
+ sourceArray.splice(3, 0, queenVictoria, johnMKeynes);
+
+ expectedIndex[queenVictoria.name()] = queenVictoria;
+ expectedIndex[johnMKeynes.name()] = johnMKeynes;
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on replacing with splice', function () {
+ delete expectedIndex[sampleData[3].name()];
+ delete expectedIndex[sampleData[4].name()];
+
+ sourceArray.splice(3, 2, queenVictoria, johnMKeynes);
+
+ expectedIndex[queenVictoria.name()] = queenVictoria;
+ expectedIndex[johnMKeynes.name()] = johnMKeynes;
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on pop', function () {
+ delete expectedIndex[sampleData[sampleData.length - 1].name()];
+ sourceArray.pop();
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index on shift', function () {
+ delete expectedIndex[sampleData[0].name()];
+ sourceArray.shift();
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('does not change the index when the data is reversed', function () {
+ sourceArray.reverse();
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('does not change the index when the data is sort', function () {
+ sourceArray.sort();
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('updates the index when a sort key changes', function () {
+ var nameBeforeChange = sampleData[3].name.peek();
+ expectedIndex['Jesus Christ'] = sampleData[3];
+ delete expectedIndex[nameBeforeChange];
+
+ sampleData[3].name('Jesus Christ');
+
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+
+ it('throw an error if multiple items maps to the same key', function () {
+ expect(function () {
+ sourceArray.push(sampleData[0]);
+ }, 'to throw', 'Unique indexes requires items must map to different keys; duplicate key: Marilyn Monroe');
+ });
+
+ describe('on multiple keys', function () {
+ beforeEach(function () {
+ indexedData = sourceArray.uniqueIndexBy(function (person) {
+ return [person.name(), person.yearOfBirth()];
+ })();
+
+ expectedIndex = {};
+ ko.utils.arrayForEach(sampleData, function (person) {
+ expectedIndex[person.name()] = person;
+ expectedIndex[person.yearOfBirth()] = person;
+ });
+ });
+
+ it("indexes the array according to the given function, returning a computed map", function () {
+ expect(indexedData, 'to equal', expectedIndex);
+ });
+ });
+ });
+
+ it('supports a throttle option', function () {
+ var underlyingArray = ko.observableArray([5, 6, 2, 5, 8, 3, 1, 2, 7, 7, 1, 0, 9, 3, 6, 4]);
+
+ var prefix = ko.observable('number: ');
+ var sortedArray = underlyingArray.indexBy({
+ mapping: function (x) {
+ return prefix() + x;
+ },
+ throttle: 200
+ });
+ var spy = sinon.spy();
+ sortedArray.subscribe(spy);
+
+ expect(sortedArray(), 'to equal', {
+ 'number: 5': [5, 5],
+ 'number: 6': [6, 6],
+ 'number: 2': [2, 2],
+ 'number: 8': [8],
+ 'number: 3': [3, 3],
+ 'number: 1': [1, 1],
+ 'number: 7': [7, 7],
+ 'number: 0': [0],
+ 'number: 9': [9],
+ 'number: 4': [4]
+ });
+
+ prefix('id: ');
+ expect(spy, 'was not called');
+
+ clock.tick(201);
+
+ expect(spy, 'was called once');
+
+ expect(sortedArray(), 'to equal', {
+ 'id: 5': [5, 5],
+ 'id: 6': [6, 6],
+ 'id: 2': [2, 2],
+ 'id: 8': [8],
+ 'id: 3': [3, 3],
+ 'id: 1': [1, 1],
+ 'id: 7': [7, 7],
+ 'id: 0': [0],
+ 'id: 9': [9],
+ 'id: 4': [4]
+ });
+ });
+});
diff --git a/test/map.spec.js b/test/map.spec.js
new file mode 100644
index 0000000..1965dd1
--- /dev/null
+++ b/test/map.spec.js
@@ -0,0 +1,642 @@
+var ko = require('knockout');
+var expect = require('unexpected').clone()
+ .installPlugin(require('unexpected-sinon'));
+var sinon = require('sinon');
+
+require('../lib/map.js');
+
+describe("Map", function () {
+ var clock;
+
+ beforeEach(function () {
+ clock = sinon.useFakeTimers();
+ });
+
+ afterEach(function () {
+ clock.restore();
+ });
+
+ var sampleData = ['Alpha', 'Beta', 'Gamma'];
+
+ it("returns a readonly computed observable array", function () {
+ var sourceArray = ko.observableArray(sampleData.slice(0)),
+ mappedArray = sourceArray.map(function (item) { return item.length; });
+
+ expect(ko.isObservable(mappedArray), 'to be', true);
+ expect(ko.isComputed(mappedArray), 'to be', true);
+ expect(function () { mappedArray([1, 2, 3]); }, 'to throw',
+ "Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.");
+ });
+
+ it("maps each entry in the array, returning a new observable array", function () {
+ var sourceArray = ko.observableArray(sampleData.slice(0)),
+ mappedArray = sourceArray.map(function (item) { return item.length; });
+ expect(mappedArray(), 'to equal', [5, 4, 5]);
+ });
+
+ it("supports an alternative 'options' object syntax", function () {
+ var sourceArray = ko.observableArray(sampleData.slice(0)),
+ mappedArray = sourceArray.map({
+ mapping: function (item) { return item.length; }
+ });
+ expect(mappedArray(), 'to equal', [5, 4, 5]);
+ });
+
+ it("issues notifications when the underlying data changes, updating the mapped result", function () {
+ var sourceArray = ko.observableArray(sampleData.slice(0)),
+ mappedArray = sourceArray.map(function (item) { return item.length; }),
+ log = [];
+ mappedArray.subscribe(function (values) { log.push(values); });
+
+ // Initial state is set without any notification
+ expect(mappedArray(), 'to equal', [5, 4, 5]);
+ expect(log.length, 'to be', 0);
+
+ // Try adding an item
+ sourceArray.push('Another');
+ expect(log.length, 'to be', 1);
+ expect(log[0], 'to equal', [5, 4, 5, 7]);
+
+ // Try removing an item
+ sourceArray.splice(1, 1);
+ expect(log.length, 'to be', 2);
+ expect(log[1], 'to equal', [5, 5, 7]);
+
+ // Try mutating in place
+ sourceArray()[1] = 'Modified';
+ sourceArray.valueHasMutated();
+ expect(log.length, 'to be', 3);
+ expect(log[2], 'to equal', [5, 8, 7]);
+ });
+
+ it("does not issue notifications for in-place edits if the mapping function returns the same object instance", function () {
+ // With mapping, you should typically return new object instances each time from your mapping function.
+ // If you preserve object instances we assume it's not a change you want to issue notification about.
+ // When the mapping result is a primitive, this isn't controversial - there really isn't a change to notify.
+ // When the mapping result is a nonprimitive it's less clear what semantics you intend, and arguably you
+ // might have mutated that object's internal state and expect to notify about it. But that's such a strange
+ // thing to be doing, with no clear use case, I think it's OK to suppress it. This could be changed
+ // or made into an option in the future.
+
+ var sourceArray = ko.observableArray([{ value: ko.observable('Alpha') }, { value: ko.observable('Beta') }]),
+ mappedItem = { theItem: true },
+ mappedArray = sourceArray.map(function (item) { return mappedItem; }),
+ log = [];
+ mappedArray.subscribe(function (values) { log.push(ko.toJSON(values)); });
+
+ // Initial state
+ expect(mappedArray(), 'to equal', [mappedItem, mappedItem]);
+ expect(ko.toJSON(mappedArray), 'to be', '[{"theItem":true},{"theItem":true}]');
+ expect(log.length, 'to be', 0);
+
+ // Since the mapping returns the same object instance, we don't treat it as a change to notify about
+ sourceArray()[0].value('Alphalonger');
+ expect(log.length, 'to be', 0);
+ expect(ko.toJSON(mappedArray), 'to be', '[{"theItem":true},{"theItem":true}]');
+ });
+
+ it("is possible to chain mappings", function () {
+ var sourceArray = ko.observableArray(sampleData.slice(0)),
+ mappedArray1 = sourceArray.map(function (item) { return item + item.toUpperCase(); }),
+ mappedArray2 = mappedArray1.map(function (item) { return item.length; }),
+ log1 = [],
+ log2 = [];
+ mappedArray1.subscribe(function (values) { log1.push(values); });
+ mappedArray2.subscribe(function (values) { log2.push(values); });
+
+ // Initial state is set without any notification
+ expect(mappedArray1(), 'to equal', ['AlphaALPHA', 'BetaBETA', 'GammaGAMMA']);
+ expect(mappedArray2(), 'to equal', [10, 8, 10]);
+ expect(log1.length, 'to be', 0);
+ expect(log2.length, 'to be', 0);
+
+ // Try adding an item
+ sourceArray.push('Another');
+ expect(log1.length, 'to be', 1);
+ expect(log2.length, 'to be', 1);
+ expect(log1[0], 'to equal', ['AlphaALPHA', 'BetaBETA', 'GammaGAMMA', 'AnotherANOTHER']);
+ expect(log2[0], 'to equal', [10, 8, 10, 14]);
+ });
+
+ it("only calls the mapping function for each newly added item", function () {
+ var sourceArray = ko.observableArray(sampleData.slice(0)),
+ mapCallsCount = 0,
+ mappedArray = sourceArray.map(function (item) {
+ mapCallsCount += 1;
+ return item.length;
+ }),
+ originalMappedArrayInstance = mappedArray(),
+ log = [];
+ mappedArray.subscribe(function (values) { log.push(values); });
+
+ // Initially the mapping is run for each item
+ expect(mappedArray(), 'to equal', [5, 4, 5]);
+ expect(mapCallsCount, 'to be', 3);
+
+ // On add, only the new item is mapped, and the output is the same array instance
+ sourceArray.push('Another');
+ expect(mappedArray(), 'to be', originalMappedArrayInstance);
+ expect(mappedArray(), 'to equal', [5, 4, 5, 7]);
+ expect(mapCallsCount, 'to be', 4);
+
+ // Try multiple adds at once
+ sourceArray.splice(2, 0, 'X', 'YZ');
+ expect(mappedArray(), 'to equal', [5, 4, 1, 2, 5, 7]);
+ expect(mapCallsCount, 'to be', 6);
+
+ // On delete, doesn't need to map anything
+ sourceArray.splice(2, 3);
+ expect(mappedArray(), 'to equal', [5, 4, 7]);
+ expect(mapCallsCount, 'to be', 6);
+
+ // On move, doesn't need to map anything
+ sourceArray(['Another', 'Beta']); // Delete 'Alpha', plus swap 'Another' and 'Beta'
+ expect(mappedArray(), 'to equal', [7, 4]);
+ expect(mapCallsCount, 'to be', 6);
+ });
+
+ it("responds to observable changes on individual items", function () {
+ var prefix = ko.observable('');
+
+ // Set up an array mapping in which individual items are observable
+ var sourceArray = ko.observableArray([
+ { name: 'Bert', age: ko.observable(123) },
+ { name: 'Mollie', age: ko.observable(246) }
+ ]),
+ mapCallsCount = 0,
+ mappedArray = sourceArray.map(function (item) {
+ mapCallsCount += 1;
+ return prefix() + item.name + ' is age ' + item.age();
+ }),
+ log = [];
+ mappedArray.subscribe(function (values) { log.push(values); });
+ expect(log.length, 'to be', 0);
+ expect(mapCallsCount, 'to be', 2);
+ expect(mappedArray(), 'to equal', ['Bert is age 123', 'Mollie is age 246']);
+
+ // Mutate one of the original items; see it affect the output
+ sourceArray()[0].age(555);
+ expect(log.length, 'to be', 1);
+ expect(mapCallsCount, 'to be', 3);
+ expect(mappedArray(), 'to equal', ['Bert is age 555', 'Mollie is age 246']);
+
+ // Add a new item
+ var megatron = { name: 'Megatron', age: ko.observable(6) };
+ sourceArray.push(megatron);
+ expect(log.length, 'to be', 2);
+ expect(mapCallsCount, 'to be', 4);
+ expect(mappedArray(), 'to equal', ['Bert is age 555', 'Mollie is age 246', 'Megatron is age 6']);
+
+ // Mutate it; see it affect the output
+ megatron.age(7);
+ expect(log.length, 'to be', 3);
+ expect(mapCallsCount, 'to be', 5);
+ expect(mappedArray(), 'to equal', ['Bert is age 555', 'Mollie is age 246', 'Megatron is age 7']);
+
+ // Mutate all items at once
+ prefix('person: ');
+ expect(log.length, 'to be', 6);
+ expect(mapCallsCount, 'to be', 8);
+ expect(mappedArray(), 'to equal', ['person: Bert is age 555', 'person: Mollie is age 246', 'person: Megatron is age 7']);
+ });
+
+ it("supplies an observable index value that can be read in mappings", function () {
+ var sourceArray = ko.observableArray(['Alpha', 'Beta']),
+ mapCallsCount = 0,
+ mappedArray = sourceArray.map(function (item, index) {
+ mapCallsCount += 1;
+ return "Item " + index() + " is " + item;
+ });
+ expect(mappedArray(), 'to equal', ['Item 0 is Alpha', 'Item 1 is Beta']);
+ expect(mapCallsCount, 'to be', 2);
+
+ // Check the index is given to newly-added items
+ sourceArray.push('Gamma');
+ expect(mappedArray(), 'to equal', ['Item 0 is Alpha', 'Item 1 is Beta', 'Item 2 is Gamma']);
+ expect(mapCallsCount, 'to be', 3); // One new item
+
+ // Check that indexes are updated when item positions are affected by deletions before them
+ sourceArray.remove('Beta');
+ expect(mappedArray(), 'to equal', ['Item 0 is Alpha', 'Item 1 is Gamma']);
+ expect(mapCallsCount, 'to be', 4); // Re-mapped Gamma because index changed
+
+ // Check that indexes are updated when item positions are affected by insertions before them
+ sourceArray.splice(0, 0, 'First');
+ expect(mappedArray(), 'to equal', ['Item 0 is First', 'Item 1 is Alpha', 'Item 2 is Gamma']);
+ expect(mapCallsCount, 'to be', 7); // Mapped First (new) and remapped both others because indexes changed
+
+ // Check that indexes are updated following a move
+ sourceArray(['First', 'Gamma', 'Alpha']);
+ expect(mappedArray(), 'to equal', ['Item 0 is First', 'Item 1 is Gamma', 'Item 2 is Alpha']);
+ expect(mapCallsCount, 'to be', 9); // Remapped Alpha and Gamma because their indexes changed
+ });
+
+ it("excludes any mapped values that match the private exclusion marker", function () {
+ // The private exclusion marker is only for use by the 'filter' function.
+ // It's only required to work in cases where the mapping does *not* reference index at all
+ // (and therefore items can't be included/excluded based on index, which wouldn't be meaningful)
+
+ var alpha = { name: 'Alpha', age: ko.observable(100) },
+ beta = { name: 'Beta', age: ko.observable(101) },
+ gamma = { name: 'Gamma', age: ko.observable(102) },
+ delta = { name: 'Delta', age: ko.observable(103) },
+ epsilon = { name: 'Epsilon', age: ko.observable(104) },
+ sourceArray = ko.observableArray([alpha, beta, gamma]),
+ mapCallsCount = 0,
+ exclusionMarker = {},
+ mappedArray = sourceArray.map({
+ mapping: function (item, index) {
+ // Include only items with even age
+ mapCallsCount += 1;
+ return item.age() % 2 === 0 ? (index() + ': ' + item.name + ' is age ' + item.age()) : exclusionMarker;
+ },
+ exclusionMarker: exclusionMarker
+ });
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Gamma is age 102']);
+ expect(mapCallsCount, 'to be', 3); // All items mapped
+
+ // Indexes still correctly reflect the filtered result after an item mutates
+ gamma.age(200);
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Gamma is age 200']);
+ expect(mapCallsCount, 'to be', 4); // Remapped Gamma only
+
+ // Filtering and indexes are preserved when items are retained
+ sourceArray.valueHasMutated();
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Gamma is age 200']);
+ expect(mapCallsCount, 'to be', 4); // Nothing needed to be remapped
+
+ // The filter applies to newly-added items too
+ sourceArray.splice(3, 0, delta, epsilon);
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Gamma is age 200', '2: Epsilon is age 104']);
+ expect(mapCallsCount, 'to be', 6); // Mapped the two new items
+
+ // Mutating an excluded item doesn't affect the output (assuming it stays excluded)
+ beta.age(201);
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Gamma is age 200', '2: Epsilon is age 104']);
+ expect(mapCallsCount, 'to be', 7); // Remapped Beta only
+
+ // The filter updates in response to item changes
+ beta.age(300);
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Beta is age 300', '2: Gamma is age 200', '3: Epsilon is age 104']);
+
+ beta.age(301);
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Gamma is age 200', '2: Epsilon is age 104']);
+
+ // The filter is respected after moves. Previous order is [alpha, beta, gamma, delta, epsilon]
+ sourceArray([alpha, beta, epsilon, gamma, delta]);
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Epsilon is age 104', '2: Gamma is age 200']);
+
+ // Note that in the above case, Delta isn't remapped at all, because last time its evaluator ran,
+ // it returned the exclusion marker without even reading index(), so it has no dependency on index.
+ // However, we can still bring it back and cause it to start responding to index changes:
+ delta.age(500);
+ expect(mappedArray(), 'to equal', ['0: Alpha is age 100', '1: Epsilon is age 104', '2: Gamma is age 200', '3: Delta is age 500']);
+
+ // Try an arbitrary more complex combination of moves too
+ sourceArray([gamma, beta, alpha, delta, epsilon]);
+ expect(mappedArray(), 'to equal', ['0: Gamma is age 200', '1: Alpha is age 100', '2: Delta is age 500', '3: Epsilon is age 104']);
+
+ // Try deleting an item that was already filtered out
+ sourceArray.splice(1, 1);
+ expect(mappedArray(), 'to equal', ['0: Gamma is age 200', '1: Alpha is age 100', '2: Delta is age 500', '3: Epsilon is age 104']);
+ });
+
+ it("disposes subscriptions when items are removed, and when the whole thing is disposed", function () {
+ // Set up an array mapping in which individual items are observable
+ var bert = { name: 'Bert', age: ko.observable(123) },
+ mollie = { name: 'Mollie', age: ko.observable(246) },
+ sourceArray = ko.observableArray([bert, mollie]),
+ mappedArray = sourceArray.map(function (item) {
+ return item.name + ' is age ' + item.age();
+ });
+ expect(mappedArray(), 'to equal', ['Bert is age 123', 'Mollie is age 246']);
+ expect(bert.age.getSubscriptionsCount(), 'to be', 1);
+ expect(mollie.age.getSubscriptionsCount(), 'to be', 1);
+
+ // One internal 'change' subscription needed to implement array change tracking,
+ // plus one 'arrayChange' subscription needed for the mapping, so two in total.
+ expect(sourceArray.getSubscriptionsCount(), 'to be', 2);
+
+ // Removing an item from the array disposes any mapping subscription held for that item
+ sourceArray.remove(bert);
+ expect(sourceArray.getSubscriptionsCount(), 'to be', 2);
+ expect(bert.age.getSubscriptionsCount(), 'to be', 0);
+ expect(mollie.age.getSubscriptionsCount(), 'to be', 1);
+
+ // Disposing the entire mapped array disposes everything
+ mappedArray.dispose();
+ expect(bert.age.getSubscriptionsCount(), 'to be', 0);
+ expect(mollie.age.getSubscriptionsCount(), 'to be', 0);
+
+ // KO core's internal 'change' subscription for trackArrayChanges is not disposed (but will be
+ // GCed when the array itself is). A possible future optimization for KO core would be to
+ // remove/re-add trackArrayChanges based on whether num(trackArrayChange subscriptions) is zero.
+ expect(sourceArray.getSubscriptionsCount(), 'to be', 1);
+ });
+
+ it("is possible to nest mappings", function () {
+ var sourceArray = ko.observableArray([
+ { id: 1, items: ko.observableArray(['Alpha', 'Beta', 'Gamma']) },
+ { id: 2, items: ko.observableArray(['Delta']) },
+ { id: 3, items: ko.observableArray([]) }
+ ]),
+ outerMappingsCallCount = 0,
+ innerMappingsCallCount = 0,
+ mappedArray = sourceArray.map(function (data) {
+ outerMappingsCallCount += 1;
+ return {
+ id2: data.id,
+ things: data.items.map(function (item) {
+ innerMappingsCallCount += 1;
+ return { name: item };
+ })
+ };
+ });
+
+ expect(ko.toJS(mappedArray()), 'to equal', [
+ { id2: 1, things: [{ name: 'Alpha' }, { name: 'Beta' }, { name: 'Gamma' }] },
+ { id2: 2, things: [{ name: 'Delta' }] },
+ { id2: 3, things: [] }
+ ]);
+ expect(outerMappingsCallCount, 'to be', 3);
+ expect(innerMappingsCallCount, 'to be', 4);
+
+ // Can mutate an inner item without causing re-mapping of outer items
+ sourceArray()[1].items.push('Epsilon');
+ expect(ko.toJS(mappedArray()), 'to equal', [
+ { id2: 1, things: [{ name: 'Alpha' }, { name: 'Beta' }, { name: 'Gamma' }] },
+ { id2: 2, things: [{ name: 'Delta' }, { name: 'Epsilon' }] },
+ { id2: 3, things: [] }
+ ]);
+ expect(outerMappingsCallCount, 'to be', 3);
+ expect(innerMappingsCallCount, 'to be', 5);
+
+ // Can mutate an outer item and only cause re-mapping of its children
+ sourceArray.splice(1, 1, { id: 'new', items: ko.observableArray(['NewChild1', 'NewChild2']) });
+ expect(ko.toJS(mappedArray()), 'to equal', [
+ { id2: 1, things: [{ name: 'Alpha' }, { name: 'Beta' }, { name: 'Gamma' }] },
+ { id2: 'new', things: [{ name: 'NewChild1' }, { name: 'NewChild2' }] },
+ { id2: 3, things: [] }
+ ]);
+ expect(outerMappingsCallCount, 'to be', 4);
+ expect(innerMappingsCallCount, 'to be', 7);
+ });
+
+ it("is possible to provide a 'disposeItem' option to clear up the mapped object", function () {
+ var modelItem = {
+ name: ko.observable('Annie')
+ },
+ underlyingArray = ko.observableArray(),
+ disposedItems = [],
+ mappedArray = underlyingArray.map({
+ mapping: function (item) {
+ return {
+ nameUpper: ko.computed(function () {
+ return item.name().toUpperCase();
+ })
+ };
+ },
+ disposeItem: function (mappedItem) {
+ disposedItems.push(mappedItem.nameUpper());
+ mappedItem.nameUpper.dispose();
+ }
+ });
+
+ // See that each mapped item causes a subscription on the underlying observable
+ expect(modelItem.name.getSubscriptionsCount(), 'to be', 0);
+ underlyingArray.push(modelItem);
+ underlyingArray.push(modelItem);
+ expect(modelItem.name.getSubscriptionsCount(), 'to be', 2);
+
+ // See that removing items causes subscriptions to be disposed
+ underlyingArray.pop();
+ expect(modelItem.name.getSubscriptionsCount(), 'to be', 1);
+ expect(disposedItems, 'to equal', ['ANNIE']);
+
+ // See that mutating the observable doesn't affect its subscription count
+ modelItem.name('Clarabel');
+ expect(ko.toJS(mappedArray), 'to equal', [{ nameUpper: 'CLARABEL' }]);
+ expect(modelItem.name.getSubscriptionsCount(), 'to be', 1);
+
+ // See that disposing the whole mapped array also triggers the disposeItem callbacks
+ mappedArray.dispose();
+ expect(modelItem.name.getSubscriptionsCount(), 'to be', 0);
+ expect(disposedItems, 'to equal', ['ANNIE', 'CLARABEL']);
+ });
+
+ it("calls 'disposeItem' when items are being replaced in-place", function () {
+ var modelItem1 = { name: ko.observable('Annie') },
+ modelItem2 = { name: ko.observable('Clarabel') },
+ underlyingArray = ko.observableArray(),
+ disposedItemsIndices = [],
+ mappedItemNextIndex = 0,
+ mappedArray = underlyingArray.map({
+ mapping: function (item) {
+ // Notice there is no extra layer of observability here
+ // (no ko.computed), so when 'name' changes, this entire
+ // mapped entry has to get replaced.
+ var newIndex = mappedItemNextIndex;
+ mappedItemNextIndex += 1;
+ return {
+ nameUpper: item.name().toUpperCase(),
+ mappedItemIndex: newIndex
+ };
+ },
+ disposeItem: function (mappedItem) {
+ disposedItemsIndices.push(mappedItem.mappedItemIndex);
+ }
+ });
+
+ underlyingArray.push(modelItem1);
+ underlyingArray.push(modelItem2);
+ expect(mappedArray(), 'to equal', [
+ { nameUpper: 'ANNIE', mappedItemIndex: 0 },
+ { nameUpper: 'CLARABEL', mappedItemIndex: 1 }
+ ]);
+ expect(disposedItemsIndices, 'to equal', []);
+
+ // See that replacing in-place causes disposeItem to fire
+ modelItem2.name('ClarabelMutated');
+ expect(mappedArray(), 'to equal', [
+ { nameUpper: 'ANNIE', mappedItemIndex: 0 },
+ { nameUpper: 'CLARABELMUTATED', mappedItemIndex: 2 }
+ ]);
+ expect(disposedItemsIndices, 'to equal', [1]);
+
+ // See that reordering does not trigger any disposeItem calls
+ underlyingArray.reverse();
+ expect(mappedArray(), 'to equal', [
+ { nameUpper: 'CLARABELMUTATED', mappedItemIndex: 2 },
+ { nameUpper: 'ANNIE', mappedItemIndex: 0 }
+ ]);
+ expect(disposedItemsIndices, 'to equal', [1]);
+
+ // See that actual removal does trigger a disposeItem call
+ underlyingArray.shift();
+ expect(mappedArray(), 'to equal', [
+ { nameUpper: 'ANNIE', mappedItemIndex: 0 }
+ ]);
+ expect(disposedItemsIndices, 'to equal', [1, 2]);
+ });
+
+ it("is possible to provide a 'mappingWithDisposeCallback' option to combine both 'mapping' and 'disposeItem' in one", function () {
+ // If you 'mapping' callback wants to create some per-item resource that needs disposal,
+ // but that item is not the mapping result itself, then you would struggle to implement
+ // the disposal because 'disposeItem' only gives you the mapping result, and not any
+ // other context that helps you find the other per-item resource you created.
+ // To solve this, 'mappingWithDisposeCallback' is an alternative to 'mapping'. Your return
+ // value should be an object of the form { mappedValue: ..., dispose: function () { ... } },
+ // and then the 'dispose' callback will be invoked when the mappedValue should be disposed.
+
+ var underlyingArray = ko.observableArray([
+ { name: ko.observable('alpha') },
+ { name: ko.observable('beta') },
+ { name: ko.observable('gamma') }
+ ]),
+ alphaObject = underlyingArray()[0],
+ perItemResources = {},
+ mappedArray = underlyingArray.map({
+ mappingWithDisposeCallback: function (value, index) {
+ var name = value.name();
+ perItemResources[name] = { disposed: false };
+
+ return {
+ mappedValue: name.toUpperCase(),
+ dispose: function () { perItemResources[name].disposed = true; }
+ };
+ }
+ });
+
+ expect(mappedArray(), 'to equal', ['ALPHA', 'BETA', 'GAMMA']);
+ expect(perItemResources, 'to equal', {
+ alpha: { disposed: false },
+ beta: { disposed: false },
+ gamma: { disposed: false }
+ });
+
+ // See that removal triggers the per-item dispose callback
+ underlyingArray.splice(1, 1);
+ expect(mappedArray(), 'to equal', ['ALPHA', 'GAMMA']);
+ expect(perItemResources, 'to equal', {
+ alpha: { disposed: false },
+ beta: { disposed: true },
+ gamma: { disposed: false }
+ });
+
+ // See that reordering does not trigger the per-item dispose callback
+ underlyingArray.reverse();
+ expect(mappedArray(), 'to equal', ['GAMMA', 'ALPHA']);
+ expect(perItemResources, 'to equal', {
+ alpha: { disposed: false },
+ beta: { disposed: true },
+ gamma: { disposed: false }
+ });
+
+ // See that replace-in-place does trigger the per-item dispose callback
+ alphaObject.name('replaced');
+ expect(mappedArray(), 'to equal', ['GAMMA', 'REPLACED']);
+ expect(perItemResources, 'to equal', {
+ alpha: { disposed: true },
+ beta: { disposed: true },
+ gamma: { disposed: false },
+ replaced: { disposed: false }
+ });
+ });
+
+ it("will attempt to disposed mapped items on every evaluation, even if the evaluator returns the same object instances each time", function () {
+ // It would be unusual to have a mapping evaluator that returns the same object instances each time
+ // it is called, but if you do that, we will still tell you to dispose the item before every evaluation
+ var underlyingArray = ko.observableArray([1, 2, 3]),
+ constantMappedValue = { theMappedValue: true },
+ disposeCount = 0,
+ mappedArray = underlyingArray.map({
+ mappingWithDisposeCallback: function (item) {
+ return {
+ mappedValue: constantMappedValue,
+ dispose: function () {
+ disposeCount += 1;
+ }
+ };
+ }
+ });
+
+ expect(mappedArray(), 'to equal', [constantMappedValue, constantMappedValue, constantMappedValue]);
+ expect(disposeCount, 'to equal', 0);
+
+ // Adding items doesn't trigger disposal
+ underlyingArray.push(4);
+ expect(mappedArray(), 'to equal', [constantMappedValue, constantMappedValue, constantMappedValue, constantMappedValue]);
+ expect(disposeCount, 'to equal', 0);
+
+ // Removing items does
+ underlyingArray.splice(1, 2);
+ expect(mappedArray(), 'to equal', [constantMappedValue, constantMappedValue]);
+ expect(disposeCount, 'to equal', 2);
+
+ // Reordering items does not
+ underlyingArray.reverse();
+ expect(mappedArray(), 'to equal', [constantMappedValue, constantMappedValue]);
+ expect(disposeCount, 'to equal', 2);
+
+ // Replacing items does
+ underlyingArray([5, 6]);
+ expect(mappedArray(), 'to equal', [constantMappedValue, constantMappedValue]);
+ expect(disposeCount, 'to equal', 4);
+
+ // Disposing the entire mapped array does
+ mappedArray.dispose();
+ expect(mappedArray(), 'to equal', [constantMappedValue, constantMappedValue]);
+ expect(disposeCount, 'to equal', 6);
+ });
+
+ it('supports a throttle option', function () {
+ var underlyingArray = ko.observableArray([1, 2, 3]);
+
+ var factor = ko.observable(1);
+ var mappedArray = underlyingArray.map({
+ mapping: function (x) {
+ return x * factor();
+ },
+ throttle: 200
+ });
+ var spy = sinon.spy();
+ mappedArray.subscribe(spy);
+
+ expect(mappedArray(), 'to equal', underlyingArray());
+
+ factor(5);
+ expect(spy, 'was not called');
+
+ clock.tick(201);
+
+ expect(spy, 'was called once');
+
+ expect(mappedArray(), 'to equal', [5, 10, 15]);
+ });
+
+ it("is mandatory to specify 'mapping' or 'mappingWithDisposeCallback'", function () {
+ var underlyingArray = ko.observableArray([1, 2, 3]);
+
+ expect(function () {
+ underlyingArray.map({ /* empty options */ });
+ }, 'to throw', "Specify either 'mapping' or 'mappingWithDisposeCallback'.");
+ });
+
+ it("is not allowed to specify 'mappingWithDisposeCallback' in conjunction with 'mapping' or 'disposeItem'", function () {
+ var underlyingArray = ko.observableArray([1, 2, 3]);
+
+ expect(function () {
+ underlyingArray.map({
+ mapping: function () { },
+ mappingWithDisposeCallback: function () { }
+ });
+ }, 'to throw', "'mappingWithDisposeCallback' cannot be used in conjunction with 'mapping' or 'disposeItem'.");
+
+ expect(function () {
+ underlyingArray.map({
+ disposeItem: function () { },
+ mappingWithDisposeCallback: function () { }
+ });
+ }, 'to throw', "'mappingWithDisposeCallback' cannot be used in conjunction with 'mapping' or 'disposeItem'.");
+ });
+});
diff --git a/test/sortBy.spec.js b/test/sortBy.spec.js
new file mode 100644
index 0000000..9889d2f
--- /dev/null
+++ b/test/sortBy.spec.js
@@ -0,0 +1,442 @@
+var ko = require('knockout');
+
+var expect = require('unexpected').clone()
+ .installPlugin(require('unexpected-sinon'));
+var sinon = require('sinon');
+
+require('../lib/sortBy.js');
+
+function Person(name, yearOfBirth) {
+ var that = this;
+ this.name = ko.observable(name);
+ this.yearOfBirth = ko.observable(yearOfBirth);
+
+ this.isBefore1900 = ko.computed(function () {
+ return that.yearOfBirth() < 1900;
+ });
+}
+
+expect.addType({
+ name: 'Person',
+ base: 'object',
+ identify: function (value) {
+ return value instanceof Person;
+ },
+ inspect: function (value, depth, output) {
+ output.text(value.yearOfBirth() + ' ' + value.name());
+ }
+});
+
+describe("SortBy", function () {
+ var clock;
+
+ beforeEach(function () {
+ clock = sinon.useFakeTimers();
+ });
+
+ afterEach(function () {
+ clock.restore();
+ });
+
+ function sorted(arr, comparefn) {
+ return [].concat(arr).sort(comparefn);
+ }
+
+ describe('on an empty array', function () {
+ var sampleData, sourceArray, sortedArray;
+ beforeEach(function () {
+ sampleData = [];
+ sourceArray = ko.observableArray(sampleData);
+ sortedArray = sourceArray.sortBy(function (item) {
+ return item;
+ });
+ });
+
+ it('results in an empty array', function () {
+ expect(sortedArray(), 'to equal', []);
+ });
+ });
+
+ describe('on simple data', function () {
+ var sampleData, sourceArray, sortedArray;
+
+ beforeEach(function () {
+ sampleData = ['Beta', 'Beta', 'Gamma', 'Alpha'];
+ sourceArray = ko.observableArray(sampleData);
+ sortedArray = sourceArray.sortBy(function (item) { return item; });
+ });
+
+ it("sort the array according to the given function, returning a new observable array", function () {
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it('maintains the sort order when data is unshifted to the source array', function () {
+ sourceArray.unshift('Foo', 'Bar');
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it('maintains the sort order when data is pushed to the source array', function () {
+ sourceArray.push('Foo', 'Bar');
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it('maintains the sort order when data is spliced into the source array', function () {
+ sourceArray.splice(3, 0, 'Foo', 'Bar');
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it('maintains the sort order when data is replaced in the source array', function () {
+ sourceArray.splice(3, 2, 'Foo', 'Bar');
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it('maintains the sort order when items are popped from the source array', function () {
+ sourceArray.pop();
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it('maintains the sort order when items are shifted from the source array', function () {
+ sourceArray.shift();
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it('maintains the sort order when the source array is reversed', function () {
+ sourceArray.reverse();
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it('maintains the sort order when the source array is sorted', function () {
+ sourceArray.sort();
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it("returns a readonly computed observable array", function () {
+ expect(ko.isObservable(sortedArray), 'to be', true);
+ expect(ko.isComputed(sortedArray), 'to be', true);
+ expect(function () { sortedArray([1, 2, 3]); }, 'to throw',
+ "Cannot write a value to a ko.computed unless you specify a 'write' option." +
+ " If you wish to read the current value, don't pass any parameters.");
+ });
+
+ it("supports an alternative 'options' object syntax", function () {
+ var sortedArray = sourceArray.sortBy({
+ mapping: function (item) { return item; }
+ });
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it("issues notifications when the underlying data changes, updating the mapped result", function () {
+ var log = [];
+ sortedArray.subscribe(function (values) { log.push(values); });
+
+ // Initial state is set without any notification
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ expect(log.length, 'to be', 0);
+
+ // Try adding an item
+ sourceArray.push('Another');
+ expect(log.length, 'to be', 1);
+ expect(log[0], 'to equal', sorted(sampleData));
+
+ // Try removing an item
+ sourceArray.splice(1, 1);
+ expect(log.length, 'to be', 2);
+ expect(log[1], 'to equal', sorted(sampleData));
+
+ // Try mutating in place
+ sourceArray()[1] = 'Modified';
+ sourceArray.valueHasMutated();
+ expect(log.length, 'to be', 3);
+ expect(log[2], 'to equal', sorted(sampleData));
+ });
+
+ it('ignores when items are moved in the underlying data', function () {
+ var log = [];
+ sortedArray.subscribe(function (values) { log.push(values); });
+
+ // Moving items in the underlying array
+ Array.prototype.push.apply(sampleData, sampleData.splice(1, 3));
+ sourceArray.valueHasMutated();
+ expect(log.length, 'to be', 0);
+ expect(sortedArray(), 'to equal', sorted(sampleData));
+ });
+
+ it("is possible to chain mappings", function () {
+ function getLength(item) {
+ return item.length;
+ }
+
+ var mappedArray = sortedArray.map(getLength);
+
+ expect(mappedArray(), 'to equal', sorted(sampleData).map(getLength));
+ });
+
+ });
+
+ describe('on complex data', function () {
+ function comparefn(a, b) {
+ if (a.yearOfBirth() > b.yearOfBirth()) {
+ return -1;
+ } else if (a.yearOfBirth() < b.yearOfBirth()) {
+ return 1;
+ }
+
+ if (a.name() < b.name()) {
+ return -1;
+ } else if (a.name() > b.name()) {
+ return 1;
+ }
+
+ return 0;
+ }
+
+ var sampleData, sourceArray, sortedArray;
+
+ beforeEach(function () {
+ sampleData = [
+ new Person("Marilyn Monroe", 1926),
+ new Person("Abraham Lincoln", 1809),
+ new Person("Mother Teresa", 1910),
+ new Person("John F. Kennedy", 1917),
+ new Person("Martin Luther King", 1929),
+ new Person("Nelson Mandela", 1918),
+ new Person("Winston Churchill", 1874),
+ new Person("Bill Gates", 1955),
+ new Person("Muhammad Ali", 1942),
+ new Person("Mahatma Gandhi", 1869),
+ new Person("Margaret Thatcher", 1925),
+ new Person("Charles de Gaulle", 1890),
+ new Person("Christopher Columbus", 1451),
+ new Person("George Orwell", 1903),
+ new Person("Charles Darwin", 1809),
+ new Person("Elvis Presley", 1935),
+ new Person("Albert Einstein", 1879),
+ new Person("Paul McCartney", 1942),
+ new Person("Plato", 423),
+ new Person("Queen Elizabeth II", 1926)
+ ];
+
+ sourceArray = ko.observableArray(sampleData);
+ sortedArray = sourceArray.sortBy(function (person, descending) {
+ return [descending(person.yearOfBirth()), person.name()];
+ });
+ });
+
+ it("sort the array according to the given function, returning a new observable array", function () {
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when data is unshifted to the source array', function () {
+ sourceArray.unshift(new Person("Queen Victoria", 1819), new Person("John M Keynes", 1883));
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when data is pushed to the source array', function () {
+ sourceArray.push(new Person("Queen Victoria", 1819), new Person("John M Keynes", 1883));
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when data is spliced into the source array', function () {
+ sourceArray.splice(3, 0, new Person("Queen Victoria", 1819), new Person("John M Keynes", 1883));
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when data is replaced in the source array', function () {
+ sourceArray.splice(3, 2, new Person("Queen Victoria", 1819), new Person("John M Keynes", 1883));
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when items are popped from the source array', function () {
+ sourceArray.pop();
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when items are shifted from the source array', function () {
+ sourceArray.shift();
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when the source array is reversed', function () {
+ sourceArray.reverse();
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when the source array is sorted', function () {
+ sourceArray.sort();
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ it('maintains the sort order when the data that is sorted on changes', function () {
+ sampleData[3].name('Jesus Christ');
+ sampleData[3].yearOfBirth(0);
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ expect(sortedArray()[sortedArray().length - 1].name(), 'to equal', 'Jesus Christ');
+ });
+
+ it('maintains the sort order when the sort direction flips', function () {
+ sampleData.forEach(function (item) {
+ item.yearOfBirth(0 - item.yearOfBirth());
+ });
+
+ expect(sortedArray(), 'to equal', sorted(sampleData, comparefn));
+ });
+
+ describe('when the sort direction can change', function () {
+ var variableSortedArray, variablecomparefn, sortDirection;
+
+ beforeEach(function () {
+ sortDirection = ko.observable(-1);
+
+ variablecomparefn = function (a, b) {
+ var sortDir = sortDirection();
+
+ if (a.yearOfBirth() > b.yearOfBirth()) {
+ return 1 * sortDir;
+ } else if (a.yearOfBirth() < b.yearOfBirth()) {
+ return -1 * sortDir;
+ }
+
+ if (a.name() < b.name()) {
+ return -1;
+ } else if (a.name() > b.name()) {
+ return 1;
+ }
+
+ return 0;
+ };
+
+ variableSortedArray = sourceArray.sortBy(function (person, descending) {
+ if (sortDirection() === 1) {
+ return [person.yearOfBirth(), person.name()];
+ } else {
+ return [descending(person.yearOfBirth()), person.name()];
+ }
+ });
+ });
+
+ it('initially has the right sort order', function () {
+ expect(variableSortedArray(), 'to equal', sorted(sampleData, variablecomparefn));
+ });
+
+ it('maintains sort order when changing to ascending sort direction', function () {
+ sortDirection(1);
+
+ expect(variableSortedArray(), 'to equal', sorted(sampleData, variablecomparefn));
+ });
+ });
+
+ describe('when chained on top of a "map" and "filter" transformations', function () {
+ var mappedArray,
+ mappedArrayIndex,
+ filteredArrayLeft, filteredArrayRight,
+ sortedArrayLeft, sortedArrayRight;
+
+ beforeEach(function () {
+ function PersonView(person) {
+ this.person = person;
+ }
+
+ mappedArray = sourceArray.map(function (person) {
+ return new PersonView(person);
+ });
+
+ mappedArrayIndex = mappedArray.uniqueIndexBy(function (view) {
+ return view.person.yearOfBirth() + ' ' + view.person.name();
+ });
+
+ filteredArrayLeft = mappedArray.filter(function (view) {
+ return view.person.isBefore1900();
+ });
+
+ filteredArrayRight = mappedArray.filter(function (view) {
+ return !view.person.isBefore1900();
+ });
+
+ sortedArrayLeft = filteredArrayLeft.sortBy(function (view) {
+ return [view.person.yearOfBirth(), view.person.name()];
+ }).extend({rateLimit: 10});
+
+ sortedArrayRight = filteredArrayRight.sortBy(function (view) {
+ return [view.person.yearOfBirth(), view.person.name()];
+ }).extend({rateLimit: 10});
+ });
+
+ it('has the same amount of items in the filtered and sorted arrays', function () {
+ expect(filteredArrayLeft().length, 'to equal', sortedArrayLeft().length);
+
+ expect(filteredArrayRight().length, 'to equal', sortedArrayRight().length);
+ });
+
+ describe('when pushing a new item that first is in the left then in the right filtered array', function () {
+ var person, personView;
+ beforeEach(function () {
+ person = new Person("Queen Victoria", 1819);
+ sourceArray.push(person);
+
+ person.yearOfBirth(1919);
+
+ personView = mappedArrayIndex()[person.yearOfBirth() + ' ' + person.name()];
+ });
+
+ it('has the new item in the right sorted array only', function () {
+ expect(sortedArrayLeft().indexOf(personView), 'to equal', -1);
+ expect(sortedArrayRight().indexOf(personView), 'to equal', 4);
+ });
+
+ it('has the same amount of items in the filtered and sorted arrays', function () {
+ expect(filteredArrayLeft().length, 'to equal', sortedArrayLeft().length);
+
+ expect(filteredArrayRight().length, 'to equal', sortedArrayRight().length);
+ });
+ });
+ });
+ });
+
+ it('supports a throttle option', function () {
+ var underlyingArray = ko.observableArray([ 0, 6, 5, 1, 3, 7, 2, 8, 9, 4 ]);
+
+ var descending = ko.observable(false);
+ var sortedArray = underlyingArray.sortBy({
+ mapping: function (x, decending) {
+ return descending() ? decending(x) : x;
+ },
+ throttle: 200
+ });
+ var spy = sinon.spy();
+ sortedArray.subscribe(spy);
+
+ expect(sortedArray(), 'to equal', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+ descending(true);
+ expect(spy, 'was not called');
+
+ clock.tick(201);
+
+ expect(spy, 'was called once');
+
+ expect(sortedArray(), 'to equal', [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]);
+ });
+
+ describe('with a custom comparator', function () {
+ var comparator;
+ beforeEach(function () {
+ comparator = function (a, b) {
+ // Considers numbers larger than letters
+ var aNum = parseInt(a, 10);
+ var bNum = parseInt(b, 10);
+ if (!isNaN(aNum) && isNaN(bNum)) {
+ return 1;
+ } else if (isNaN(aNum) && !isNaN(bNum)) {
+ return -1;
+ } else {
+ return a > b ? 1 : a < b ? -1 : 0;
+ }
+ };
+ });
+
+ it('sorts according to the comparator', function () {
+ var sourceArray = ko.observableArray([ 0, 6, 'c', 5, 1, 3, 7, 'b', 2, 8, 9, 'a', 4 ]);
+ var sortedArray = sourceArray.sortBy({ mapping: function (item) { return item; }, comparator: comparator });
+ expect(sortedArray(), 'to equal', ['a', 'b', 'c', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+ });
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-knockout-transformations.git
More information about the Pkg-javascript-commits
mailing list