Skip to content

Commit fc75f49

Browse files
committed
Merge pull request #6 from laurilehmijoki/containerOf
Add "containerOf" matcher
2 parents 159b46e + 8b0a596 commit fc75f49

File tree

5 files changed

+125
-8
lines changed

5 files changed

+125
-8
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ You can also filter using `.where()`
1414
And there's negation
1515

1616
age.is().not().greaterThan(18)
17-
17+
1818
You can compare to either constant values or other Observables. Like
1919

2020
// Returns a Property of Booleans
21-
var collision = playerPos.is().equalTo(monsterPos)
22-
21+
var collision = playerPos.is().equalTo(monsterPos)
22+
2323
Complete examples:
2424

2525
Bacon.fromArray([1,2,3]).is().equalTo(2)
26-
26+
2727
=> false, true, false
28-
28+
2929
Bacon.fromArray([1,2,3]).where().equalTo(2)
30-
30+
3131
=> 2
3232

3333
## Additions to Observable API
@@ -58,6 +58,23 @@ Complete examples:
5858

5959
`not()` returns a negated `Matchers` object
6060

61+
`containerOf(x)` supports arrays, strings and object key-values. Examples:
62+
63+
```javascript
64+
stream = Bacon.once([6]).is().containerOf(6) // is true
65+
66+
Bacon.once('hello bacon').is().containerOf('bacon') // is true
67+
68+
Bacon.once({
69+
alien: 'morninglightmountain'
70+
human: 'dudleybose'
71+
}).is().containerOf({ alien: 'morninglightmountain' }) // is true
72+
```
73+
74+
## Browser support
75+
76+
IE 9 and above.
77+
6178
## Download / Install
6279

6380
- Download [javascript file](https://raw.github.com/raimohanska/bacon.matchers/master/bacon.matchers.js)

bacon.matchers.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ init = (Bacon) ->
3737
context["inClosedRange"] = apply3((val, a, b) ->
3838
a <= val <= b
3939
)
40+
context["containerOf"] = apply2((a, b) ->
41+
if a instanceof Array or typeof a == 'string'
42+
a.indexOf(b) >= 0
43+
else if typeof a == 'object'
44+
matchingKeyValuePairs = 0
45+
Object.keys(b).forEach (bKey) -> # Object.keys works in IE9 and above
46+
aHasBKeyAndValue = a.hasOwnProperty(bKey) and a[bKey] == b[bKey]
47+
if aHasBKeyAndValue
48+
matchingKeyValuePairs += 1
49+
aHasAllKeyValuesOfB = matchingKeyValuePairs == Object.keys(b).length
50+
bIsNotEmpty = Object.keys(b).length > 0
51+
aHasAllKeyValuesOfB and bIsNotEmpty
52+
else
53+
false
54+
)
4055
context
4156
Bacon.Observable::is = ->
4257
apply1 = (f) ->

bacon.matchers.js

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

run-tests

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
#!/bin/bash -e
2+
3+
echo "Building ..."
4+
bash build
5+
echo "Running tests ..."
16
node_modules/mocha/bin/mocha --require coffee-script --compilers coffee:coffee-script/register spec/bacon.matchers.spec.coffee

spec/bacon.matchers.spec.coffee

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
assert = require("assert")
2-
Bacon = require("../bacon.matchers")
2+
Bacon = require("../bacon.matchers.js") # Test the result of the build
33

44
assertConstantly = (expectedValue, stream, done) ->
55
stream.onValue (val) -> assert.equal expectedValue, val
@@ -75,4 +75,64 @@ describe 'bacon.matchers', ->
7575
it 'should return false with values not within range', (done) ->
7676
stream = Bacon.fromArray([6,20]).is().inClosedRange(7, 19)
7777
assertConstantly false, stream, done
78-
78+
describe 'containerOf', ->
79+
context 'value is an array', ->
80+
it 'should return true when the array contains the element', (done) ->
81+
stream = Bacon.once([6]).is().containerOf(6)
82+
assertConstantly true, stream, done
83+
it 'should return false when the array does not contain the element', (done) ->
84+
stream = Bacon.once([66]).is().containerOf(6)
85+
assertConstantly false, stream, done
86+
context 'value is a string', ->
87+
it 'should return true when the string contains the sub-string', (done) ->
88+
stream = Bacon.once('hello bacon').is().containerOf('bacon')
89+
assertConstantly true, stream, done
90+
it 'should return false when the string does not contain the sub-string', (done) ->
91+
stream = Bacon.once('hello bacon').is().containerOf('Bacon')
92+
assertConstantly false, stream, done
93+
context 'value is an object', ->
94+
it 'should return true when the object contains the key-value pair', (done) ->
95+
stream = Bacon.once({
96+
alien: 'morninglightmountain'
97+
human: 'dudleybose'
98+
}).is().containerOf({alien: 'morninglightmountain'})
99+
assertConstantly true, stream, done
100+
it 'should return true when comparing equal objects', (done) ->
101+
object =
102+
alien: 'morninglightmountain'
103+
stream = Bacon.once(object).is().containerOf(object)
104+
assertConstantly true, stream, done
105+
it 'should return false when key matches but value does not', (done) ->
106+
stream = Bacon.once(
107+
alien: 'morninglightmountain'
108+
).is().containerOf(alien: 't1000')
109+
assertConstantly false, stream, done
110+
it 'should return false when the object contains a subset of the compared value', (done) ->
111+
stream = Bacon.once(
112+
alien: 'morninglightmountain'
113+
).is().containerOf(
114+
alien: 'morninglightmountain'
115+
human: 'dudleybose'
116+
)
117+
assertConstantly false, stream, done
118+
it 'should return false when comparing a non-empty object to {}', (done) ->
119+
stream = Bacon.once(
120+
alien: 'morninglightmountain'
121+
).is().containerOf( {} )
122+
assertConstantly false, stream, done
123+
it 'should return false when comparing {} to {}', (done) ->
124+
stream = Bacon.once( {} ).is().containerOf( {} )
125+
assertConstantly false, stream, done
126+
context 'value is a boolean', ->
127+
it 'should always return false', (done) ->
128+
stream = Bacon.once(false).is().containerOf(false)
129+
assertConstantly false, stream, done
130+
context 'value is a number', ->
131+
it 'should always return false', (done) ->
132+
stream = Bacon.once(1).is().containerOf(1)
133+
assertConstantly false, stream, done
134+
context 'value is a function', ->
135+
it 'should always return false', (done) ->
136+
fun = ->
137+
stream = Bacon.once(fun).is().containerOf(fun)
138+
assertConstantly false, stream, done

0 commit comments

Comments
 (0)