Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2190 Implement a util.wrap function #2191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions spec/utilsBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,55 @@ describe('unwrapObservable', function() {
});
});

describe('wrap', function() {
it('Should return an observable containing the non-observable parameter passed', function() {
var someObject = { abc: 123 },
primitiveValue = 123,
someFunction = function() { return primitiveValue; },
wrappedPrimitiveValue,
wrappedObject,
wrappedFunction,
wrappedNull,
wrappedUndefined;

wrappedObject = ko.utils.wrap(someObject);
expect(ko.isObservable(wrappedObject)).toBe(true);
expect(wrappedObject()).toBe(someObject);

wrappedPrimitiveValue = ko.utils.wrap(primitiveValue);
expect(ko.isObservable(wrappedPrimitiveValue)).toBe(true);
expect(wrappedPrimitiveValue()).toBe(123);

wrappedFunction = ko.utils.wrap(someFunction);
expect(ko.isObservable(wrappedFunction)).toBe(true);
expect(wrappedFunction()).toBe(someFunction);

wrappedNull = ko.utils.wrap(null);
expect(ko.isObservable(wrappedNull)).toBe(true);
expect(wrappedNull()).toBe(null);

wrappedUndefined = ko.utils.wrap(undefined);
expect(ko.isObservable(wrappedUndefined)).toBe(true);
expect(wrappedUndefined()).toBe(undefined);
});

it('Should return the same supplied observable', function() {
var someObject = { abc: 123 },
observablePrimitiveValue = ko.observable(123),
observableObjectValue = ko.observable(someObject),
observableNullValue = ko.observable(null),
observableUndefinedValue = ko.observable(undefined),
computedValue = ko.computed(function() { return observablePrimitiveValue() + 1; });

expect(ko.utils.wrap(observablePrimitiveValue)).toBe(observablePrimitiveValue);
expect(ko.utils.wrap(observableObjectValue)).toBe(observableObjectValue);
expect(ko.utils.wrap(observableNullValue)).toBe(observableNullValue);
expect(ko.utils.wrap(observableUndefinedValue)).toBe(observableUndefinedValue);
expect(ko.utils.wrap(computedValue)).toBe(computedValue);

});
});

describe('arrayForEach', function () {
it('Should go call the callback for each element of the array, in order', function () {
var callback = jasmine.createSpy('callback');
Expand Down
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ ko.utils = (function () {
return ko.isObservable(value) ? value() : value;
},

wrap: function (value) {
return ko.isObservable(value) ? value : ko.observable(value);
},

peekObservable: function (value) {
return ko.isObservable(value) ? value.peek() : value;
},
Expand Down Expand Up @@ -609,6 +613,7 @@ ko.exportSymbol('utils.unwrapObservable', ko.utils.unwrapObservable);
ko.exportSymbol('utils.objectForEach', ko.utils.objectForEach);
ko.exportSymbol('utils.addOrRemoveItem', ko.utils.addOrRemoveItem);
ko.exportSymbol('utils.setTextContent', ko.utils.setTextContent);
ko.exportSymbol('utils.wrap', ko.utils.wrap);
ko.exportSymbol('unwrap', ko.utils.unwrapObservable); // Convenient shorthand, because this is used so commonly

if (!Function.prototype['bind']) {
Expand Down