1 |
|
2 | {Template, Expression} = require 'uri-template/lib/classes'
|
3 |
|
4 | queryStringOps = ['?', '&']
|
5 |
|
6 | Template::match = (input) ->
|
7 | if @prefix
|
8 | return false unless m = input.match '^' + @prefix
|
9 | input = input.substring m[0].length
|
10 | vars = {}
|
11 | for expr in @expressions
|
12 | inQS = expr.op.first in queryStringOps
|
13 | remaining = expr.match input, vars
|
14 | if remaining is null
|
15 | return false
|
16 | if remaining is false and not inQS
|
17 | return false
|
18 | input = remaining
|
19 | if input and not inQS
|
20 | return false
|
21 | return vars
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | Expression::match = (input, vars) ->
|
31 | len = 0
|
32 | inQS = @op.first in queryStringOps
|
33 | if not inQS
|
34 | [input, qs] = input.split '?'
|
35 | qs = if qs then '?'+qs else ''
|
36 | else
|
37 | qs = ''
|
38 |
|
39 | if @op.first
|
40 | return false unless input.substring(0,1) is @op.first
|
41 | input = input.substring 1
|
42 | len++
|
43 |
|
44 | if @suffix
|
45 | return false unless m = input.match @suffix
|
46 | len += @suffix.length
|
47 | matchable = input.substring 0, m.index
|
48 | else
|
49 | matchable = input
|
50 |
|
51 | len += matchable.length
|
52 | i = 0
|
53 | named = {}
|
54 | ordered = []
|
55 | for part in matchable.split @op.sep
|
56 | if part.match(/\//) and @op.allow isnt 'U+R'
|
57 | return null
|
58 | [n, v] = part.split '='
|
59 | if not v?
|
60 | if inQS
|
61 | named[n] = true
|
62 | else
|
63 | ordered.push unescape n
|
64 | else
|
65 | named[n] = unescape v
|
66 |
|
67 | for p in @params
|
68 | if (v = named[p.name])?
|
69 |
|
70 | else
|
71 | if p.explode
|
72 | if ordered.length then v = ordered; ordered = null
|
73 | else v = named; named = null
|
74 | else
|
75 | v = ordered.shift()
|
76 | return false unless v or inQS
|
77 | vars[p.name] = v or []
|
78 | remaining = input.substring len
|
79 | remaining + qs
|