Skip to content

Commit

Permalink
Add ko.isPureComputed function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyu0810 authored and StevenZhaoMS committed Sep 8, 2015
1 parent 3962496 commit 49d698d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spec/dependentObservableBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ describe('Dependent Observable', function() {
expect(ko.isComputed(instance)).toEqual(true);
});

it('Should advertise that instances are not pure computed', function () {
var instance = ko.computed(function () { });
expect(ko.isPureComputed(instance)).toEqual(false);
});

it('Should advertise that instances cannot have values written to them', function () {
var instance = ko.computed(function () { });
expect(ko.isWriteableObservable(instance)).toEqual(false);
Expand Down
5 changes: 5 additions & 0 deletions spec/observableBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ describe('Observable', function() {
expect(ko.isObservable(instance)).toEqual(true);
});

it('Should advertise that instances are not pure computed', function () {
var instance = ko.observable();
expect(ko.isPureComputed(instance)).toEqual(false);
});

it('Should be able to write values to it', function () {
var instance = new ko.observable();
instance(123);
Expand Down
5 changes: 5 additions & 0 deletions spec/pureComputedBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ describe('Pure Computed', function() {
expect(ko.isComputed(computed)).toEqual(true);
});

it('Should advertise that instances are pure computed', function () {
var instance = ko.pureComputed(function () { });
expect(ko.isPureComputed(instance)).toEqual(true);
});

it('Should require an evaluator function as constructor param', function () {
expect(function () { ko.pureComputed(); }).toThrow();
});
Expand Down
6 changes: 6 additions & 0 deletions src/subscribables/dependentObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,15 @@ ko.isComputed = function (instance) {
return ko.hasPrototype(instance, ko.computed);
};

ko.isPureComputed = function (instance) {
return ko.hasPrototype(instance, ko.computed) &&
instance.getVersion === pureComputedOverrides.getVersion;
}

ko.exportSymbol('computed', ko.computed);
ko.exportSymbol('dependentObservable', ko.computed); // export ko.dependentObservable for backwards compatibility (1.x)
ko.exportSymbol('isComputed', ko.isComputed);
ko.exportSymbol('isPureComputed', ko.isPureComputed);
ko.exportSymbol('computed.fn', computedFn);
ko.exportProperty(computedFn, 'peek', computedFn.peek);
ko.exportProperty(computedFn, 'dispose', computedFn.dispose);
Expand Down

0 comments on commit 49d698d

Please sign in to comment.