-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
320 lines (264 loc) · 7.15 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'use strict';
var querystring = require('blear.utils.querystring');
var array = require('blear.utils.array');
var object = require('blear.utils.object');
var typeis = require('blear.utils.typeis');
var access = require('blear.utils.access');
var path = require('blear.utils.path');
var rePathname = /[?#].*$/;
var reSearch = /\?.*$/;
var reHash = /#.*$/;
var reBase = /^(?:([^\/]+:)?\/\/)?([^\/:]+)(:\d+)?/;
var reEndSlash = /\/$/;
var reLastSlash = /\/(:\w+\?\/)/;
var reSep = /\//g;
var reColon = /:(\w+\b)/g;
var reStar = /\*/g;
var reIgnoreMatch = /^[.?#]/;
// @see http://www.topscan.com/pingtai/
var QR_CODE_URL = 'http://qr.topscan.com/api.php?';
var qrcodeDefaults = {
// 背景色
bg: 'ffffff',
// 前景色
fg: '000000',
// 渐变
gc: '000000',
// 定位点外框颜色
pt: '000000',
// 定位点内容颜色
ipt: '000000',
// 纠错等级:h/q/m/l
el: 'm',
// 内容尺寸
w: 300,
// 边框尺寸
m: 0,
// logo
logo: '',
// 文本
text: ''
};
/**
* url 解析
* @param url {String} url 地址
* @returns {{hash: String, host: String, hostname: String, href: String, pathname: String, port: String, protocol:String, querystring: String, query: Object, search: string, base: String}}
*/
var parse = exports.parse = function parse(url) {
var href = url;
var base = '';
var protocol = '';
var hostname = '';
var port = '';
if (!reIgnoreMatch.test(url)) {
var matches = url.match(reBase);
if (matches) {
base = matches[0];
protocol = matches[1] || '';
hostname = matches[2];
matches[3] = matches[3] || '';
port = matches[3].slice(1) || '';
}
url = url.replace(reBase, '');
}
var hash = '';
var search = '';
url = url.replace(reHash, function (source) {
hash = source;
return '';
});
url = url.replace(reSearch, function (source) {
search = source;
return '';
});
var pathname = url.replace(rePathname, '');
var qss = search.slice(1);
return {
href: href,
base: base,
protocol: protocol,
host: hostname + (port ? ':' : '') + port,
hostname: hostname,
port: port,
pathname: pathname,
search: search,
hash: hash,
hashstring: hash,
querystring: qss,
query: querystring.parse(qss)
};
};
/**
* url 字符化
* @param obj {Object} url 信息
* @returns {string}
*/
var stringify = exports.stringify = function stringify(obj) {
obj.protocol = obj.protocol || '';
obj.host = obj.host || '';
obj.pathname = obj.pathname || '';
obj.search = obj.search || '';
obj.hash = obj.hash || '';
if (obj.query) {
obj.search = querystring.stringify(obj.query);
obj.search = obj.search ? '?' + obj.search : '';
}
return obj.protocol + (obj.protocol ? '//' : '' ) + obj.host + obj.pathname + obj.search + obj.hash;
};
var matchPathDefaults = exports.matchPathDefaults = {
/**
* 是否忽略大小写,默认 false
* @type Boolean
*/
ignoreCase: false,
/**
* 是否严格模式,默认 false,即默认忽略末尾“/”
* @type Boolean
*/
strict: false
};
/**
* 匹配 URL path 部分
* @param {String} url hash 字符串
* @param {String} rule 路由规则
* @param {Object} [options] 参数配置
* @param {Object} [options.ignoreCase=false] 是否忽略大小写,默认 false
* @param {Object} [options.strict=true] 是否忽略末尾斜杠,默认 true
* @returns {*}
*
* @example
* 语法:
* `/name/:name/page/:page?/`
* 匹配:
* /name/cloudcome/page/123/
* /name/cloudcome/page/123
* /name/cloudcome/page/
* /name/cloudcome/page
*
* hashbang.matches('/id/abc123/', '/id/:id/');
* // =>
* // {
* // id: "abc123"
* // }
*
* hashbang.matches('/name/abc123/', '/id/:id/');
* // => null
*/
exports.matchPath = function (url, rule, options) {
// /id/:id/ => /id/abc123/ √
options = object.assign({}, matchPathDefaults, options);
var routeRuleOrigin = rule;
var reg;
var ret = null;
var pathname = parse(url).pathname;
if (!options.strict) {
rule += reEndSlash.test(rule) ? '?' : '/?';
rule = rule.replace(reLastSlash, '/?$1');
}
rule = rule
.replace(reColon, (options.strict ? '?' : '') + '([^/]+)')
.replace(reSep, '\\/')
.replace(reStar, '.*');
try {
reg = new RegExp('^' + rule + '$', options.ignoreCase ? 'i' : '');
} catch (err) {
/* istanbul ignore next */
return ret;
}
var keys = routeRuleOrigin.match(reColon) || [];
var matches = pathname.match(reg);
if (!matches) {
return ret;
}
keys.unshift('');
ret = {};
array.each(keys, function (index, key) {
if (index) {
ret[key.slice(1)] = matches[index] || '';
}
});
return ret;
};
/**
* 分配 query
* @param url {String} url
* @param key {Object|String} 参数名、参数键值对、字符串
* @param [val] {String} 参数值
* @returns {*}
*/
exports.assignQuery = function (url, key, val) {
var args = access.args(arguments);
var query = {};
// assignQuery(url, 'a', '1');
if (args.length === 3) {
query[key] = val;
} else {
// assignQuery(url, 'a=1&b=2');
if (typeis.String(key)) {
query = querystring.parse(key);
}
// assignQuery(url, {a:1,b:2});
else {
query = key;
}
}
var urlParsed = parse(url);
object.assign(urlParsed.query, query);
return stringify(urlParsed);
};
var RESOLVE = 'resolve';
var JOIN = 'join';
/**
* 处理路径
* @param from {String} 起始路径
* @param to {String} 目标路径
* @param method {String} 方法
* @returns {String}
*/
var add = function (from, to, method) {
var fromRet = parse(from);
var toRet = parse(to);
if (toRet.host || toRet.protocol) {
return to;
}
toRet.protocol = fromRet.protocol;
toRet.host = fromRet.host;
if (method === RESOLVE) {
fromRet.pathname = path.dirname(fromRet.pathname);
}
toRet.pathname = path[method](fromRet.pathname, toRet.pathname);
return stringify(toRet);
};
var buildExports = function (method) {
/**
* 合并、解决路径
* @param from {String} 起始路径
* @param to {String} 目标路径
* @returns {String}
*/
return function (from, to/*arguments*/) {
var args = access.args(arguments);
var current = 1;
var end = args.length;
var ret = args[0];
while (current < end) {
ret = add(ret, args[current], method);
current++;
}
return ret;
};
};
exports.resolve = buildExports(RESOLVE);
exports.join = buildExports(JOIN);
/**
* 二维码地址
* @param options {Object|String} 配置
* @returns {string}
*/
exports.qrcode = function (options) {
if (typeis.String(options)) {
options = {text: options};
}
options = object.assign({}, qrcodeDefaults, options);
return QR_CODE_URL + querystring.stringify(options);
};