Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix matching for null etc.

  • Loading branch information...
commit 908e18067e8f373548c246b6dea6711797f25052 1 parent 37a9067
@mental authored
Showing with 23 additions and 9 deletions.
  1. +13 −0 spec/MatchSpec.coffee
  2. +10 −9 src/Match.coffee
View
13 spec/MatchSpec.coffee
@@ -7,6 +7,16 @@ describe "WebActors.match", ->
expect(WebActors.match("foobar", "barfoo")).toBeFalsy()
expect(WebActors.match("foobar", "foobar")).toBeTruthy()
+ it "should match null only with null", ->
+ expect(WebActors.match(null, null)).toBeTruthy()
+ expect(WebActors.match(null, {})).toBeFalsy()
+ expect(WebActors.match(null, "3")).toBeFalsy()
+ expect(WebActors.match(null, undefined)).toBeFalsy()
+
+ it "should not match strings and numbers", ->
+ expect(WebActors.match("3", 3)).toBeFalsy()
+ expect(WebActors.match(3, "3")).toBeFalsy()
+
it "should exactly match booleans", ->
for b in [true, false]
expect(WebActors.match(b, not b)).toBeFalsy()
@@ -29,3 +39,6 @@ describe "WebActors.match", ->
it "should support wildcards", ->
expect(WebActors.match(WebActors.ANY, 42)).toBeTruthy()
expect(WebActors.match(WebActors.ANY, "testing")).toBeTruthy()
+ expect(WebActors.match(WebActors.ANY, true)).toBeTruthy()
+ expect(WebActors.match(WebActors.ANY, false)).toBeTruthy()
+ expect(WebActors.match(WebActors.ANY, undefined)).toBeTruthy()
View
19 src/Match.coffee
@@ -19,27 +19,28 @@ is_array = (value) ->
else
false
-match = (pattern, value, captured) ->
+match = (pattern, value) ->
if typeof(pattern) is "object"
if pattern instanceof Array
- return null unless is_array(value)
+ return false unless is_array(value)
pattern_length = pattern.length
- return null unless value.length is pattern_length
+ return false unless value.length is pattern_length
for i in [0...pattern_length]
- matched = match(pattern[i], value[i], captured)
+ matched = match(pattern[i], value[i])
return false unless matched
+ if pattern is null
+ value is null
else
return null if typeof(value) is not "object"
for name of pattern
- matched = match(pattern[name], value[name], captured)
+ matched = match(pattern[name], value[name])
return false unless matched
else if pattern is ANY
# match anything
- return true
+ true
else
- return pattern is value
+ pattern is value
-WebActors.match = (pattern, value) ->
- match pattern, value, []
+WebActors.match = match
WebActors.ANY = ANY
Please sign in to comment.
Something went wrong with that request. Please try again.