-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
141 lines (116 loc) · 3.43 KB
/
index.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* global escape */
/*! litejs.com/MIT-LICENSE.txt */
!function() {
"use strict";
module.exports = Template
module.exports.expand = expand
/**
* URI Template
* @see http://tools.ietf.org/html/rfc6570
*/
var RESERVED = /[!'()]/g
// /[[\]:\/!#$&()*+,;='?@]/g
, SEPARATORS = {"": ",", "+": ",", "#": ",", "?": "&"}
, escapeRe = /[$-/?[-^{|}]/g
, expandRe = /\{([#&+./;?]?)((?:[-\w%.]+(\*|:\d+)?(?:,|(?=})))+)\}/g
, parseRe = RegExp(expandRe.source + "|.[^{]*?", "g")
function encodeNormal(val) {
return encodeURIComponent(val).replace(RESERVED, escape)
}
function escapeFn(str) {
return str.replace(escapeRe, "\\$&")
}
function catchDecode(str) {
try {
return decodeURIComponent(str)
} catch(e) {
return str
}
}
function notNull(s) {
return s != null
}
function mapCleanJoin(arr, mapFn, joinStr) {
arr = arr.map(mapFn).filter(notNull)
return arr.length && arr.join(joinStr)
}
function expand(template, data, opts) {
return template.replace(expandRe, function(_, op, vals) {
var sep = SEPARATORS[op] || op
, named = sep == ";" || sep == "&"
, enc = op && sep == "," ? encodeURI : opts && opts.encoder || encodeNormal
, out = mapCleanJoin(vals.split(","), function(_name) {
var mod = _name.split(/[*:]/)
, name = mod[0]
, val = data[name]
if (val == null) return
if (typeof val == "object") {
mod = name != _name
if (Array.isArray(val)) {
val = mapCleanJoin(val, enc, mod ? named ? sep + name + "=" : sep : "," )
} else {
val = mapCleanJoin(Object.keys(val), function(key) {
return enc(key) + (mod ? "=" : ",") + enc(val[key])
}, mod && (named || sep == "/") ? sep : ",")
if (mod) named = 0
}
if (!val) return
} else {
val = enc(mod[1] ? val.slice(0, mod[1]) : val)
}
return (
named ?
name + (val || sep == "&" ? "=" + val : val) :
val
)
}, sep)
return out || out === "" ? (op != "+" ? op + out : out) : ""
}
)}
function Template(template, opts_) {
var self = this
//if (!(self instanceof Template)) return new Template(template)
, opts = Object.assign({
decoder: catchDecode
}, opts_)
, pos = 0
, lengths = {}
, fnStr = ""
, reStr = "^" + template.replace(parseRe, function(_, op, vals) {
if (!vals) return escapeFn(_)
var sep = SEPARATORS[op] || op
, named = sep == ";" || sep == "&"
return vals.split(",").map(function(_name, i) {
var mod = _name.split(/[*:]/)
, name = mod[0]
, re = (lengths[name] || "(") + ".*?)"
pos++
if (mod[1]) {
re = "((?:%..|.){1," + mod[1] + "})"
lengths[name] = "(\\" + pos
}
fnStr += "t=($[" + pos + "]||'').split('" + (mod[1] ? named ? sep + name + "=" : sep : ",") + "').map(d);"
fnStr += "o[\"" + name + "\"]=" + (mod[1] === "" ? "t;" : "t.length>1?t:t[0];")
re = escapeFn(i === 0 ? op === "+" ? "" : op : sep) + (
named ?
escapeFn(name) + "(?:=" + re + ")?" :
//sep == "&" ?
//escapeFn(name + "=") + re :
re
)
return mod[1] === "" ? "(?:" + re + ")?" : re
}).join("")
}) + "$"
, re = RegExp(reStr)
, fn = Function("$,d", "var t,o={};" + fnStr + "return o")
self.template = template
self.match = function(uri) {
var match = re.exec(uri)
return match && fn(match, opts.decoder)
}
self.expand = function(data) {
return expand(template, data, opts)
}
}
// `this` is `exports` in NodeJS and `window` in browser.
}(); // jshint ignore:line