@ray-js/library 1.2.0-beta.0 → 1.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +1 -1
- package/lib/regexp/NamedRegexp.js +26 -43
- package/lib/regexp/index.js +1 -1
- package/lib/url/format.js +4 -17
- package/lib/url/index.js +8 -8
- package/lib/url/interface.js +1 -1
- package/lib/url/params.js +15 -18
- package/lib/url/parse.js +37 -55
- package/lib/url/queryStringify.js +13 -21
- package/lib/url/searchParse.js +13 -31
- package/package.json +3 -3
package/lib/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import url from './url';
|
|
2
|
-
export { url };
|
|
2
|
+
export { url };
|
|
@@ -1,49 +1,32 @@
|
|
|
1
|
-
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
2
|
-
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
3
|
-
import "core-js/modules/es.regexp.exec.js";
|
|
4
|
-
import "core-js/modules/es.array.reduce.js";
|
|
5
|
-
import "core-js/modules/es.object.to-string.js";
|
|
6
|
-
|
|
7
1
|
/**
|
|
8
2
|
* 实现 Named Groups RegExp 的正则百表达式
|
|
9
3
|
* ECMA 2018 的实现
|
|
10
4
|
* 通过 new RegExp 运算符,无法被 babel 转义
|
|
11
5
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
groups = options.groups;
|
|
24
|
-
this.regex = regex;
|
|
25
|
-
this.groups = groups;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
_createClass(NamedRegexp, [{
|
|
29
|
-
key: "exec",
|
|
30
|
-
value: function exec(value) {
|
|
31
|
-
var _this = this;
|
|
32
|
-
|
|
33
|
-
var matches = this.regex.exec(value);
|
|
34
|
-
if (!matches) return matches;
|
|
35
|
-
var groups = matches.reduce(function (result, match, index) {
|
|
36
|
-
if (index > 0) // This subtraction is required because we count
|
|
37
|
-
// match indexes from 1, because 0 is the entire matched string
|
|
38
|
-
result[_this.groups[index - 1]] = match;
|
|
39
|
-
return result;
|
|
40
|
-
}, {});
|
|
41
|
-
matches.groups = groups;
|
|
42
|
-
return matches;
|
|
6
|
+
export default class NamedRegexp {
|
|
7
|
+
/**
|
|
8
|
+
* 创建 Named RegExp
|
|
9
|
+
* @param {object} options
|
|
10
|
+
* @param {RegExp} options.regex 正则
|
|
11
|
+
* @param {Array<String>} options.groups 分组
|
|
12
|
+
*/
|
|
13
|
+
constructor(options) {
|
|
14
|
+
const { regex, groups } = options;
|
|
15
|
+
this.regex = regex;
|
|
16
|
+
this.groups = groups;
|
|
43
17
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
18
|
+
exec(value) {
|
|
19
|
+
const matches = this.regex.exec(value);
|
|
20
|
+
if (!matches)
|
|
21
|
+
return matches;
|
|
22
|
+
const groups = matches.reduce((result, match, index) => {
|
|
23
|
+
if (index > 0)
|
|
24
|
+
// This subtraction is required because we count
|
|
25
|
+
// match indexes from 1, because 0 is the entire matched string
|
|
26
|
+
result[this.groups[index - 1]] = match;
|
|
27
|
+
return result;
|
|
28
|
+
}, {});
|
|
29
|
+
matches.groups = groups;
|
|
30
|
+
return matches;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/lib/regexp/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default as NamedRegexp } from './NamedRegexp';
|
|
1
|
+
export { default as NamedRegexp } from './NamedRegexp';
|
package/lib/url/format.js
CHANGED
|
@@ -1,20 +1,7 @@
|
|
|
1
|
-
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
-
import "core-js/modules/es.regexp.exec.js";
|
|
3
|
-
import "core-js/modules/es.string.search.js";
|
|
4
|
-
import "core-js/modules/es.array.concat.js";
|
|
5
1
|
import queryStringify from './queryStringify';
|
|
6
2
|
import searchParse from './searchParse';
|
|
7
3
|
export default function format(attrs) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
_attrs$origin = attrs.origin,
|
|
13
|
-
origin = _attrs$origin === void 0 ? '' : _attrs$origin,
|
|
14
|
-
_attrs$query = attrs.query,
|
|
15
|
-
query = _attrs$query === void 0 ? {} : _attrs$query,
|
|
16
|
-
_attrs$search = attrs.search,
|
|
17
|
-
search = _attrs$search === void 0 ? '' : _attrs$search;
|
|
18
|
-
var searchQuery = searchParse(search);
|
|
19
|
-
return "".concat(origin).concat(pathname).concat(queryStringify(_objectSpread(_objectSpread({}, query), searchQuery))).concat(hash);
|
|
20
|
-
}
|
|
4
|
+
const { hash = '', pathname = '', origin = '', query = {}, search = '' } = attrs;
|
|
5
|
+
const searchQuery = searchParse(search);
|
|
6
|
+
return `${origin}${pathname}${queryStringify(Object.assign(Object.assign({}, query), searchQuery))}${hash}`;
|
|
7
|
+
}
|
package/lib/url/index.js
CHANGED
|
@@ -3,13 +3,13 @@ import params from './params';
|
|
|
3
3
|
import parse from './parse';
|
|
4
4
|
import queryStringify from './queryStringify';
|
|
5
5
|
import searchParse from './searchParse';
|
|
6
|
-
|
|
6
|
+
const queryParse = searchParse;
|
|
7
7
|
export { parse, format, queryStringify, params, searchParse, queryParse };
|
|
8
8
|
export default {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
9
|
+
parse,
|
|
10
|
+
format,
|
|
11
|
+
queryStringify,
|
|
12
|
+
queryParse,
|
|
13
|
+
params,
|
|
14
|
+
searchParse,
|
|
15
|
+
};
|
package/lib/url/interface.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
package/lib/url/params.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
5
12
|
import merge from 'merge';
|
|
6
13
|
import parse from './parse';
|
|
7
14
|
import format from './format';
|
|
@@ -11,17 +18,7 @@ import format from './format';
|
|
|
11
18
|
* @param args - 附加到 query 上的参数
|
|
12
19
|
* @returns
|
|
13
20
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
rest = _objectWithoutProperties(_parse, _excluded);
|
|
19
|
-
|
|
20
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
21
|
-
args[_key - 1] = arguments[_key];
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return format(_objectSpread({
|
|
25
|
-
query: merge.apply(void 0, [query].concat(args))
|
|
26
|
-
}, rest));
|
|
27
|
-
}
|
|
21
|
+
export default function params(url, ...args) {
|
|
22
|
+
const _a = parse(url), { query } = _a, rest = __rest(_a, ["query"]);
|
|
23
|
+
return format(Object.assign({ query: merge(query, ...args) }, rest));
|
|
24
|
+
}
|
package/lib/url/parse.js
CHANGED
|
@@ -1,61 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
10
12
|
import { NamedRegexp } from '../regexp';
|
|
11
13
|
import searchParse from './searchParse';
|
|
12
14
|
import format from './format';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
const URL_REGEX = new NamedRegexp({
|
|
16
|
+
regex: /(((?:http|https):)?(?:\/\/)?([^./]([^:/?#]*)(?::(\d+))?))?([^?#]+)?(\?[^#]*)?(#.*)?/,
|
|
17
|
+
groups: ['origin', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash'],
|
|
16
18
|
});
|
|
17
19
|
export default function parse(href) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
groups
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
/* istanbul ignore next */
|
|
21
|
+
const { groups = {} } = URL_REGEX.exec(href) || { groups: {} };
|
|
22
|
+
Object.keys(groups).forEach((key) => {
|
|
23
|
+
if (!groups[key]) {
|
|
24
|
+
groups[key] = '';
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
const { pathname } = groups, restGroups = __rest(groups, ["pathname"]);
|
|
28
|
+
const res = Object.assign({ query: {}, href, pathname: pathname ? pathname : '/' }, restGroups);
|
|
29
|
+
if (groups.search) {
|
|
30
|
+
// 小程序不能传递hash,用query.____h_a_s_h____传递
|
|
31
|
+
// 回显恢复____h_a_s_h____ ====> hash
|
|
32
|
+
// 从query中接出hash值
|
|
33
|
+
const _a = searchParse(groups.search), { ____h_a_s_h____ } = _a, query = __rest(_a, ["____h_a_s_h____"]);
|
|
34
|
+
res.query = query;
|
|
35
|
+
if (____h_a_s_h____) {
|
|
36
|
+
// 剔除 ____h_a_s_h____
|
|
37
|
+
res.hash = res.hash || `#${____h_a_s_h____}`;
|
|
38
|
+
res.search = format({ query });
|
|
39
|
+
res.href = format(res);
|
|
40
|
+
}
|
|
28
41
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var pathname = groups.pathname,
|
|
32
|
-
restGroups = _objectWithoutProperties(groups, _excluded);
|
|
33
|
-
|
|
34
|
-
var res = _objectSpread({
|
|
35
|
-
query: {},
|
|
36
|
-
href: href,
|
|
37
|
-
pathname: pathname ? pathname : '/'
|
|
38
|
-
}, restGroups);
|
|
39
|
-
|
|
40
|
-
if (groups.search) {
|
|
41
|
-
// 小程序不能传递hash,用query.____h_a_s_h____传递
|
|
42
|
-
// 回显恢复____h_a_s_h____ ====> hash
|
|
43
|
-
// 从query中接出hash值
|
|
44
|
-
var _searchParse = searchParse(groups.search),
|
|
45
|
-
____h_a_s_h____ = _searchParse.____h_a_s_h____,
|
|
46
|
-
query = _objectWithoutProperties(_searchParse, _excluded2);
|
|
47
|
-
|
|
48
|
-
res.query = query;
|
|
49
|
-
|
|
50
|
-
if (____h_a_s_h____) {
|
|
51
|
-
// 剔除 ____h_a_s_h____
|
|
52
|
-
res.hash = res.hash || "#".concat(____h_a_s_h____);
|
|
53
|
-
res.search = format({
|
|
54
|
-
query: query
|
|
55
|
-
});
|
|
56
|
-
res.href = format(res);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return res;
|
|
61
|
-
}
|
|
42
|
+
return res;
|
|
43
|
+
}
|
|
@@ -1,21 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
keys.forEach(function (key) {
|
|
17
|
-
var value = query[key];
|
|
18
|
-
search.push([key, value].join('='));
|
|
19
|
-
});
|
|
20
|
-
return "".concat(prefix ? '?' : '').concat(search.join('&'));
|
|
21
|
-
}
|
|
1
|
+
export default function queryStringify(query, options = {}) {
|
|
2
|
+
const { prefix = true } = options;
|
|
3
|
+
const search = [];
|
|
4
|
+
const keys = Object.keys(query);
|
|
5
|
+
if (keys.length === 0) {
|
|
6
|
+
return '';
|
|
7
|
+
}
|
|
8
|
+
keys.forEach((key) => {
|
|
9
|
+
const value = query[key];
|
|
10
|
+
search.push([key, value].join('='));
|
|
11
|
+
});
|
|
12
|
+
return `${prefix ? '?' : ''}${search.join('&')}`;
|
|
13
|
+
}
|
package/lib/url/searchParse.js
CHANGED
|
@@ -1,41 +1,23 @@
|
|
|
1
|
-
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
-
import _toArray from "@babel/runtime/helpers/esm/toArray";
|
|
3
|
-
import "core-js/modules/es.array.slice.js";
|
|
4
|
-
import "core-js/modules/es.regexp.exec.js";
|
|
5
|
-
import "core-js/modules/es.string.replace.js";
|
|
6
|
-
import "core-js/modules/es.object.to-string.js";
|
|
7
|
-
import "core-js/modules/web.dom-collections.for-each.js";
|
|
8
|
-
|
|
9
1
|
/**
|
|
10
2
|
* 分割url search key value,返回 [key, value]
|
|
11
3
|
*/
|
|
12
4
|
function splitSearchKeyValue(str) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
valueArr = _str$split2.slice(1);
|
|
17
|
-
|
|
18
|
-
var value = valueArr.length ? valueArr.join('=') : '';
|
|
19
|
-
return [key, value];
|
|
5
|
+
const [key, ...valueArr] = str.split('=');
|
|
6
|
+
const value = valueArr.length ? valueArr.join('=') : '';
|
|
7
|
+
return [key, value];
|
|
20
8
|
}
|
|
21
9
|
/**
|
|
22
10
|
* 解析 url.search 返回对象类型
|
|
23
11
|
* @param search URL search 信息
|
|
24
12
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
13
|
export default function searchParse(search) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
query[key] = value;
|
|
39
|
-
});
|
|
40
|
-
return query;
|
|
41
|
-
}
|
|
14
|
+
const query = {};
|
|
15
|
+
if (!search)
|
|
16
|
+
return query;
|
|
17
|
+
search = search.replace(/^\?/, ''); // 移除首字符 问号
|
|
18
|
+
search.split('&').forEach((i) => {
|
|
19
|
+
const [key, value] = splitSearchKeyValue(i);
|
|
20
|
+
query[key] = value;
|
|
21
|
+
});
|
|
22
|
+
return query;
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/library",
|
|
3
|
-
"version": "1.2.0-beta.
|
|
3
|
+
"version": "1.2.0-beta.1",
|
|
4
4
|
"description": "Ray library for browser",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ray"
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"merge": "^2.1.1"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@ray-js/cli": "^1.2.0-beta.
|
|
26
|
+
"@ray-js/cli": "^1.2.0-beta.1"
|
|
27
27
|
},
|
|
28
28
|
"maintainers": [
|
|
29
29
|
{
|
|
@@ -31,6 +31,6 @@
|
|
|
31
31
|
"email": "tuyafe@tuya.com"
|
|
32
32
|
}
|
|
33
33
|
],
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "2b34058549219b57afaa6b6f80c1b896e1602d53",
|
|
35
35
|
"repository": {}
|
|
36
36
|
}
|