Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
Updating tests and root/Rx refs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpodwysocki committed Mar 29, 2013
1 parent 1c96846 commit 19471ad
Show file tree
Hide file tree
Showing 45 changed files with 2,826 additions and 2,778 deletions.
116 changes: 84 additions & 32 deletions rx.aggregates.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
} else {
root.Rx = factory(root, {}, root.Rx);
}
}(this, function (global, exp, root, undefined) {
}(this, function (global, exp, Rx, undefined) {

// References
var Observable = root.Observable,
var Observable = Rx.Observable,
observableProto = Observable.prototype,
CompositeDisposable = root.CompositeDisposable,
AnonymousObservable = root.Internals.AnonymousObservable;
CompositeDisposable = Rx.CompositeDisposable,
AnonymousObservable = Rx.Internals.AnonymousObservable;

// Defaults
var argumentOutOfRange = 'Argument out of range';
Expand Down Expand Up @@ -82,16 +82,14 @@
return x[0];
}

// Aggregation methods

/**
* Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified seed value is used as the initial accumulator value.
* For aggregation behavior with incremental intermediate results, see Observable.scan.
*
* @example
* 1 - res = source.aggregate(function (acc, x) { return acc + x; });
* 2 - res = source.aggregate(0, function (acc, x) { return acc + x; });
* @memberOf Observable#
* @param {Mixed} [seed] The initial accumulator value.
* @param {Function} accumulator An accumulator function to be invoked on each element.
* @returns {Observable} An observable sequence containing a single element with the final accumulator value.
Expand All @@ -108,6 +106,18 @@
return hasSeed ? this.scan(seed, accumulator).startWith(seed).finalValue() : this.scan(accumulator).finalValue();
};

/**
* Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified seed value is used as the initial accumulator value.
* For aggregation behavior with incremental intermediate results, see Observable.scan.
*
* @example
* 1 - res = source.reduce(function (acc, x) { return acc + x; });
* 2 - res = source.reduce(function (acc, x) { return acc + x; }, 0);
* @memberOf Observable#
* @param {Mixed} [seed] The initial accumulator value.
* @param {Function} accumulator An accumulator function to be invoked on each element.
* @returns {Observable} An observable sequence containing a single element with the final accumulator value.
*/
observableProto.reduce = function () {
var seed, hasSeed, accumulator = arguments[0];
if (arguments.length === 2) {
Expand All @@ -122,14 +132,14 @@
*
* 1 - source.any();
* 2 - source.any(function (x) { return x > 3; });
*
* @memberOf Observable#
* @param {Function} [predicate] A function to test each element for a condition.
* @returns {Observable} An observable sequence containing a single element determining whether any elements in the source sequence pass the test in the specified predicate if given, else if any items are in the sequence.
*/
observableProto.any = observableProto.some = function (predicate) {
observableProto.any = function (predicate, thisArg) {
var source = this;
return predicate
? source.where(predicate).any()
? source.where(predicate, thisArg).any()
: new AnonymousObservable(function (observer) {
return source.subscribe(function () {
observer.onNext(true);
Expand All @@ -140,10 +150,12 @@
});
});
};
observableProto.some = observableProto.any;

/**
* Determines whether an observable sequence is empty.
*
*
* @memberOf Observable#
* @returns {Observable} An observable sequence containing a single element determining whether the source sequence is empty.
*/
observableProto.isEmpty = function () {
Expand All @@ -154,24 +166,26 @@
* Determines whether all elements of an observable sequence satisfy a condition.
*
* 1 - res = source.all(function (value) { return value.length > 3; });
*
* @memberOf Observable#
* @param {Function} [predicate] A function to test each element for a condition.
* @param {Any} [thisArg] Object to use as this when executing callback.
* @returns {Observable} An observable sequence containing a single element determining whether all elements in the source sequence pass the test in the specified predicate.
*/
observableProto.all = observableProto.every = function (predicate) {
observableProto.all = function (predicate, thisArg) {
return this.where(function (v) {
return !predicate(v);
}).any().select(function (b) {
}, thisArg).any().select(function (b) {
return !b;
});
};
observableProto.every = observableProto.all;

/**
* Determines whether an observable sequence contains a specified element with an optional equality comparer.
*
* 1 - res = source.contains(42);
* 2 - res = source.contains({ value: 42 }, function (x, y) { return x.value === y.value; });
*
* @memberOf Observable#
* @param value The value to locate in the source sequence.</param>
* @param {Function} [comparer] An equality comparer to compare elements.
* @returns {Observable} An observable sequence containing a single element determining whether the source sequence contains an element that has the specified value.
Expand All @@ -188,7 +202,7 @@
*
* 1 - res = source.count();
* 2 - res = source.count(function (x) { return x > 3; });
*
* @memberOf Observable#
* @param {Function} [predicate]A function to test each element for a condition.
* @returns {Observable} An observable sequence containing a single element with a number that represents how many elements in the input sequence satisfy the condition in the predicate function if provided, else the count of items in the sequence.
*/
Expand All @@ -205,7 +219,7 @@
*
* 1 - res = source.sum();
* 2 - res = source.sum(function (x) { return x.value; });
*
* @memberOf Observable#
* @param {Function} [selector]A transform function to apply to each element.
* @returns {Observable} An observable sequence containing a single element with the sum of the values in the source sequence.
*/
Expand All @@ -222,7 +236,7 @@
*
* 1 - source.minBy(function (x) { return x.value; });
* 2 - source.minBy(function (x) { return x.value; }, function (x, y) { return x - y; });
*
* @memberOf Observable#
* @param {Function} keySelector Key selector function.</param>
* @param {Function} [comparer] Comparer used to compare key values.</param>
* @returns {Observable} An observable sequence containing a list of zero or more elements that have a minimum key value.
Expand All @@ -239,7 +253,7 @@
*
* 1 - source.min();
* 2 - source.min(function (x, y) { return x.value - y.value; });
*
* @memberOf Observable#
* @param {Function} [comparer] Comparer used to compare elements.
* @returns {Observable} An observable sequence containing a single element with the minimum element in the source sequence.
*/
Expand All @@ -255,7 +269,7 @@
* @example
* 1 - source.maxBy(function (x) { return x.value; });
* 2 - source.maxBy(function (x) { return x.value; }, function (x, y) { return x - y;; });
*
* @memberOf Observable#
* @param {Function} keySelector Key selector function.
* @param {Function} [comparer] Comparer used to compare key values.
* @returns {Observable} An observable sequence containing a list of zero or more elements that have a maximum key value.
Expand All @@ -271,7 +285,7 @@
* @example
* 1 - source.max();
* 2 - source.max(function (x, y) { return x.value - y.value; });
*
* @memberOf Observable#
* @param {Function} [comparer] Comparer used to compare elements.
* @returns {Observable} An observable sequence containing a single element with the maximum element in the source sequence.
*/
Expand All @@ -287,7 +301,7 @@
* @example
* 1 - res = source.average();
* 2 - res = source.average(function (x) { return x.value; });
*
* @memberOf Observable#
* @param {Function} [selector] A transform function to apply to each element.
* @returns {Observable} An observable sequence containing a single element with the average of the sequence of values.
*/
Expand Down Expand Up @@ -339,7 +353,7 @@
* 2 - res = source.sequenceEqual([{ value: 42 }], function (x, y) { return x.value === y.value; });
* 3 - res = source.sequenceEqual(Rx.Observable.returnValue(42));
* 4 - res = source.sequenceEqual(Rx.Observable.returnValue({ value: 42 }), function (x, y) { return x.value === y.value; });
*
* @memberOf Observable#
* @param {Observable} second Second observable sequence or array to compare.
* @param {Function} [comparer] Comparer used to compare elements of both sequences.
* @returns {Observable} An observable sequence that contains a single element which indicates whether both sequences are of equal length and their corresponding elements are equal according to the specified equality comparer.
Expand Down Expand Up @@ -448,7 +462,7 @@
*
* @example
* source.elementAt(5);
*
* @memberOf Observable#
* @param {Number} index The zero-based index of the element to retrieve.
* @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence.
*/
Expand All @@ -462,7 +476,7 @@
* @example
* source.elementAtOrDefault(5);
* source.elementAtOrDefault(5, 0);
*
* @memberOf Observable#
* @param {Number} index The zero-based index of the element to retrieve.
* @param [defaultValue] The default value if the index is outside the bounds of the source sequence.
* @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence, or a default value if the index is outside the bounds of the source sequence.
Expand Down Expand Up @@ -498,7 +512,7 @@
* @example
* 1 - res = source.single();
* 2 - res = source.single(function (x) { return x === 42; });
*
* @memberOf Observable#
* @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
* @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.
*/
Expand All @@ -517,7 +531,7 @@
* 2 - res = source.singleOrDefault(function (x) { return x === 42; });
* 3 - res = source.singleOrDefault(function (x) { return x === 42; }, 0);
* 4 - res = source.singleOrDefault(null, 0);
*
* @memberOf Observable#
* @param {Function} predicate A predicate function to evaluate for elements in the source sequence.
* @param [defaultValue] The default value if the index is outside the bounds of the source sequence.
* @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
Expand Down Expand Up @@ -551,7 +565,7 @@
* @example
* 1 - res = source.first();
* 2 - res = source.first(function (x) { return x > 3; });
*
* @memberOf Observable#
* @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
* @returns {Observable} Sequence containing the first element in the observable sequence that satisfies the condition in the predicate if provided, else the first item in the sequence.
*/
Expand All @@ -570,7 +584,7 @@
* 2 - res = source.firstOrDefault(function (x) { return x > 3; });
* 3 - res = source.firstOrDefault(function (x) { return x > 3; }, 0);
* 4 - res = source.firstOrDefault(null, 0);
*
* @memberOf Observable#
* @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
* @param [defaultValue] The default value if no such element exists. If not specified, defaults to null.
* @returns {Observable} Sequence containing the first element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
Expand Down Expand Up @@ -605,7 +619,7 @@
* @example
* 1 - res = source.last();
* 2 - res = source.last(function (x) { return x > 3; });
*
* @memberOf Observable#
* @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
* @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate.
*/
Expand All @@ -624,7 +638,7 @@
* 2 - res = source.lastOrDefault(function (x) { return x > 3; });
* 3 - res = source.lastOrDefault(function (x) { return x > 3; }, 0);
* 4 - res = source.lastOrDefault(null, 0);
*
* @memberOf Observable#
* @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
* @param [defaultValue] The default value if no such element exists. If not specified, defaults to null.
* @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
Expand All @@ -636,5 +650,43 @@
return lastOrDefaultAsync(this, true, defaultValue);
};

return root;
function findValue (source, predicate, thisArg, yieldIndex) {
return new AnonymousObservable(function (observer) {
return source.subscribe(function (x) {
if (predicate.call(thisArg, x, i, source)) {
observer.onNext(yieldIndex ? i : x);
observer.onCompleted();
} else {
i++;
}
}, observer.onError.bind(observer), function () {
observer.onNext(yieldIndex ? -1 : undefined);
observer.onCompleted();
});
});
}

/**
* Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Observable sequence.
*
* @memberOf Observable#
* @param {Function} predicate The predicate that defines the conditions of the element to search for.
* @returns {Observable} An Observable sequence with the first element that matches the conditions defined by the specified predicate, if found; otherwise, undefined.
*/
observableProto.find = function (predicate) {
return findValue(this, predicate, arguments[1], false);
};

/**
* Searches for an element that matches the conditions defined by the specified predicate, and returns
* an Observable sequence with the zero-based index of the first occurrence within the entire Observable sequence.
*
* @memberOf Observable#
* @param {Function} predicate The predicate that defines the conditions of the element to search for.
* @returns {Observable} An Observable sequence with the zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.
*/
observableProto.findIndex = function (predicate) {
return findValue(this, predicate, arguments[1], true);
};
return Rx;
}));
Loading

0 comments on commit 19471ad

Please sign in to comment.