forked from PawelDecowski/jquery-creditcardvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange.coffee
27 lines (21 loc) · 758 Bytes
/
range.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Range
constructor: (@trie) ->
if @trie.constructor != Trie
throw Error 'Range constructor requires a Trie parameter'
@rangeWithString: (ranges) ->
if typeof ranges != 'string'
throw Error 'rangeWithString requires a string parameter'
ranges = ranges.replace(/ /g, '')
ranges = ranges.split ','
trie = new Trie
for range in ranges
if r = range.match /^(\d+)-(\d+)$/
for n in [r[1]..r[2]]
trie.push n
else if range.match /^\d+$/
trie.push range
else
throw Error "Invalid range '#{r}'"
new Range trie
match: (number) ->
return @trie.find(number)