alamid-junction
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -67,7 +67,7 @@ "use strict"; | ||
if (obj.hasOwnProperty(key)) { | ||
setter(this, key, obj[key]); | ||
setter(this, key, this.setter(key, obj[key])); | ||
} | ||
} | ||
} else { | ||
setter(this, key, value); | ||
setter(this, key, this.setter(key, value)); | ||
} | ||
@@ -79,2 +79,16 @@ | ||
/** | ||
* Gets called for every value that changes (including changes by signals). Override | ||
* this method if you want to apply some normalizations on the new value. | ||
* | ||
* The returned value will be the new value, so make sure to always return something. | ||
* | ||
* @param {String} key | ||
* @param {*} value | ||
* @returns {*} | ||
*/ | ||
Junction.prototype.setter = function (key, value) { | ||
return value; | ||
}; | ||
/** | ||
* Retrieve one or all values. | ||
@@ -132,3 +146,2 @@ * | ||
delete this._values[key]; | ||
signal = this._signals[key]; | ||
@@ -138,2 +151,3 @@ if (signal) { | ||
} | ||
delete this._values[key]; | ||
@@ -158,5 +172,8 @@ return this; | ||
signal = new this.Signal(); | ||
signal.setter = function setter(value) { | ||
return self.setter(key, value); | ||
}; | ||
signal(this._values[key]); | ||
signal.subscribe(function onSignalChange(newValue) { | ||
self._values[key] = newValue; | ||
self._values[key] = self.setter(key, newValue); | ||
}); | ||
@@ -163,0 +180,0 @@ |
{ | ||
"name": "alamid-junction", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "Provides convenient methods for setting and retrieving multiple signals", | ||
@@ -5,0 +5,0 @@ "main": "./lib/Junction.js", |
@@ -9,2 +9,3 @@ "use strict"; | ||
chai.Assertion.includeStack = true; | ||
chai.use(require("sinon-chai")); | ||
@@ -204,2 +205,39 @@ | ||
describe(".setter", function () { | ||
beforeEach(function () { | ||
junction.setter = sinon.stub().returns("it works"); | ||
}); | ||
it("should be called when set(key, value) is called and take the returned value as new value", function () { | ||
junction.setter = sinon.stub().returns("it works"); | ||
junction.set("hello", "pirate"); | ||
expect(junction.setter).to.have.been.calledOnce; | ||
expect(junction.setter).to.have.been.calledWith("hello", "pirate"); | ||
expect(junction.get("hello")).to.equal("it works"); | ||
}); | ||
it("should be called for every key-value-pair and take the returned value as new value", function () { | ||
junction.set({ | ||
greeting: "Ahoy!", | ||
age: 34, | ||
attributes: {} | ||
}); | ||
expect(junction.setter).to.have.been.calledThrice; | ||
expect(junction.setter.firstCall).to.have.been.calledWith("greeting", "Ahoy!"); | ||
expect(junction.setter.secondCall).to.have.been.calledWith("age", 34); | ||
expect(junction.setter.thirdCall).to.have.been.calledWith("attributes", {}); | ||
expect(junction.get()).to.eql({ | ||
greeting: "it works", | ||
age: "it works", | ||
attributes: "it works" | ||
}); | ||
}); | ||
}); | ||
describe(".reset()", function () { | ||
@@ -247,2 +285,12 @@ | ||
it("should not work different if a signal has previously been retrieved", function () { | ||
junction.signal("greeting"); | ||
junction.remove("greeting"); | ||
expect(junction.get()).to.eql({ | ||
age: 34 | ||
}); | ||
}); | ||
it("should set the key's signal to undefined", function () { | ||
@@ -300,2 +348,13 @@ var greeting = junction.signal("greeting"); | ||
it("should apply a setter to the signal which calls the Junction's setter", function () { | ||
var greeting; | ||
junction.setter = sinon.spy(); | ||
greeting = junction.signal("greeting"); | ||
greeting.setter("hello"); | ||
expect(junction.setter).to.have.been.calledWith("greeting", "hello"); | ||
}); | ||
it("should update the junction's property when the signal changes", function () { | ||
@@ -302,0 +361,0 @@ var greeting = junction.signal("greeting"); |
21840
505