Permalink
Browse files

support objects now

remove console.log

remove console.log
  • Loading branch information...
1 parent a2aa24e commit 655fccec90f2cecccaf629908b1499034f8ebdc3 @jiyinyiyong committed Aug 4, 2013
Showing with 39 additions and 14 deletions.
  1. +1 −0 README.md
  2. +1 −1 package.json
  3. +12 −5 pattern.coffee
  4. +17 −7 pattern.js
  5. +8 −1 test.Spec.coffee
View
@@ -23,6 +23,7 @@ match 'string or number here',
5, (data) -> print 'it is five'
null, -> print 'matches null not undefined'
undefined, (data) -> print "#{data} has no matching pattern"
+ key: -> 'object as a shortcut'
```
or more crazy:
View
@@ -1,6 +1,6 @@
{
"name": "coffee-pattern",
- "version": "0.0.4",
+ "version": "0.0.5",
"description": "coffee-pattern: write pattern matching in CoffeeScript syntax",
"main": "pattern.js",
"scripts": {
View
@@ -5,11 +5,18 @@ get_type = (varable) ->
divide_list = (stack, long_list) ->
if long_list.length > 0
- solution =
- pattern: long_list[0]
- result: long_list[1]
- stack.push solution
- divide_list stack, long_list[2..]
+ if (get_type long_list[0]) is 'object'
+ keys = Object.keys long_list[0]
+ keys.forEach (key) ->
+ stack.push
+ pattern: key
+ result: long_list[0][key]
+ divide_list stack, long_list[keys.length..]
+ else
+ stack.push
+ pattern: long_list[0]
+ result: long_list[1]
+ divide_list stack, long_list[2..]
else stack
exports.match = (data, choices...) ->
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -83,4 +83,11 @@ describe 'test patterns', ->
1, -> count += 1
undefined, -> count += 1
undefined, -> count += 1
- expect(count).toBe(1)
+ expect(count).toBe(1)
+
+ it 'supports objects as shortcuts', ->
+ ret = match 'name',
+ '': -> 'empty string'
+ name: -> 1
+ undefined, -> 2
+ expect(ret).toBe(1)

0 comments on commit 655fcce

Please sign in to comment.