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

accepting a custom isEqual function #16

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ Includes

## Model API

###Bacon.Model(initValue)
###Bacon.Model(initValue [, options])

Creates a new model, with the given (optional) initial value. The optional `options` argument is an object that
currently accepts the following field:

`isEqual` (optional) : a two-arg function that should return true iff its two arguments are to be considered
equal values, and return false otherwise. This is used for implicitly
[skipping duplicates](https://github.com/baconjs/bacon.js/#property-skipduplicates).
This defaults to `===` comparison.

Creates a new model, with the given (optional) initial value.

###model.set(value)

Expand Down
9 changes: 6 additions & 3 deletions dist/bacon.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
return !a.initial && eq(a.value, b.value);
};
};
Model = Bacon.Model = function(initValue) {
Model = Bacon.Model = function(initValue, options) {
var currentValue, eq, model, modificationBus, myId, myModCount, syncBus, valueWithSource;
if (options == null) {
options = {};
}
myId = idCounter++;
eq = defaultEquals;
eq = options.isEqual || defaultEquals;
myModCount = 0;
modificationBus = new Bacon.Bus();
syncBus = new Bacon.Bus();
Expand Down Expand Up @@ -128,7 +131,7 @@
return lensed;
};
model.syncConverter = id;
if (arguments.length >= 1) {
if (arguments.length === 1 || arguments.length > 1 && typeof arguments[0] !== 'undefined') {
model.set(initValue);
}
return model;
Expand Down
2 changes: 1 addition & 1 deletion dist/bacon.model.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/bacon.model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ init = (Bacon) ->
defaultEquals = (a, b) -> a == b
sameValue = (eq) -> (a, b) -> !a.initial && eq(a.value, b.value)

Model = Bacon.Model = (initValue) ->
Model = Bacon.Model = (initValue, options = {}) ->
myId = idCounter++
eq = defaultEquals
eq = options.isEqual || defaultEquals
myModCount = 0
modificationBus = new Bacon.Bus()
syncBus = new Bacon.Bus()
Expand Down Expand Up @@ -67,7 +67,7 @@ init = (Bacon) ->
valueLens.set(e, lens.get(e.value))))
lensed
model.syncConverter = id
if (arguments.length >= 1)
if (arguments.length == 1 || arguments.length > 1 && typeof arguments[0] != 'undefined')
model.set initValue
model

Expand Down
18 changes: 17 additions & 1 deletion test/bacon.model.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe "Model initial value", ->
it "is sent", ->
cylinders = lib.Model(12)
expect(collect(cylinders)).to.deep.equal([12])
it "handles undefined like any other value", ->
it "handles undefined like any other value if no second argument is given", ->
cylinders = lib.Model(undefined)
expect(collect(cylinders)).to.deep.equal([undefined])
it "can be omitted", ->
Expand Down Expand Up @@ -320,6 +320,22 @@ describe "Model.combine", ->
{ a: "a", b: "b" },
{ a: "a2", b: "b" }])


customEquals = (a, b) -> a.x == b.x && a.y == b.y

describe "Model with custom isEqual function", ->
it "ignores object duplicates from input by custom equality", ->
b = lib.Model(undefined, { isEqual: customEquals })
values = collect(b)
b.addSource(Bacon.fromArray([{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 1, y: 0 }]))
expect(values).to.deep.equal([{ x: 0, y: 0 }, { x: 1, y: 0 }])
it "ignores object duplicates from input with init value by custom equality", ->
b = lib.Model({ x: 0, y: 0 }, { isEqual: customEquals })
values = collect(b)
b.addSource(Bacon.fromArray([{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 1, y: 0 }]))
expect(values).to.deep.equal([{ x: 0, y: 0 }, { x: 1, y: 0 }])


collect = (observable) ->
values = []
observable.onValue (value) ->
Expand Down