@ray-js/shared 1.4.1 → 1.4.3
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/regexp/NamedRegexp.d.ts +24 -0
- package/lib/regexp/NamedRegexp.js +35 -0
- package/lib/regexp/index.d.ts +1 -0
- package/lib/regexp/index.js +8 -0
- package/lib/url/format.d.ts +2 -0
- package/lib/url/format.js +13 -0
- package/lib/url/index.d.ts +16 -0
- package/lib/url/index.js +26 -0
- package/lib/url/interface.d.ts +12 -0
- package/lib/url/interface.js +2 -0
- package/lib/url/params.d.ts +1 -0
- package/lib/url/params.js +14 -0
- package/lib/url/parse.d.ts +2 -0
- package/lib/url/parse.js +39 -0
- package/lib/url/queryStringify.d.ts +3 -0
- package/lib/url/queryStringify.js +16 -0
- package/lib/url/searchParse.d.ts +5 -0
- package/lib/url/searchParse.js +26 -0
- package/package.json +3 -3
- package/LICENSE.md +0 -9
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
interface ExecArray extends RegExpExecArray {
|
|
2
|
+
groups?: Record<string, string> | undefined;
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* 实现 Named Groups RegExp 的正则百表达式
|
|
6
|
+
* ECMA 2018 的实现
|
|
7
|
+
* 通过 new RegExp 运算符,无法被 babel 转义
|
|
8
|
+
*/
|
|
9
|
+
export default class NamedRegexp {
|
|
10
|
+
regex: RegExp;
|
|
11
|
+
groups: string[];
|
|
12
|
+
/**
|
|
13
|
+
* 创建 Named RegExp
|
|
14
|
+
* @param {object} options
|
|
15
|
+
* @param {RegExp} options.regex 正则
|
|
16
|
+
* @param {Array<String>} options.groups 分组
|
|
17
|
+
*/
|
|
18
|
+
constructor(options: {
|
|
19
|
+
regex: RegExp;
|
|
20
|
+
groups: string[];
|
|
21
|
+
});
|
|
22
|
+
exec(value: string): ExecArray | null;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* 实现 Named Groups RegExp 的正则百表达式
|
|
5
|
+
* ECMA 2018 的实现
|
|
6
|
+
* 通过 new RegExp 运算符,无法被 babel 转义
|
|
7
|
+
*/
|
|
8
|
+
class NamedRegexp {
|
|
9
|
+
/**
|
|
10
|
+
* 创建 Named RegExp
|
|
11
|
+
* @param {object} options
|
|
12
|
+
* @param {RegExp} options.regex 正则
|
|
13
|
+
* @param {Array<String>} options.groups 分组
|
|
14
|
+
*/
|
|
15
|
+
constructor(options) {
|
|
16
|
+
const { regex, groups } = options;
|
|
17
|
+
this.regex = regex;
|
|
18
|
+
this.groups = groups;
|
|
19
|
+
}
|
|
20
|
+
exec(value) {
|
|
21
|
+
const matches = this.regex.exec(value);
|
|
22
|
+
if (!matches)
|
|
23
|
+
return matches;
|
|
24
|
+
const groups = matches.reduce((result, match, index) => {
|
|
25
|
+
if (index > 0)
|
|
26
|
+
// This subtraction is required because we count
|
|
27
|
+
// match indexes from 1, because 0 is the entire matched string
|
|
28
|
+
result[this.groups[index - 1]] = match;
|
|
29
|
+
return result;
|
|
30
|
+
}, {});
|
|
31
|
+
matches.groups = groups;
|
|
32
|
+
return matches;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.default = NamedRegexp;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as NamedRegexp } from './NamedRegexp';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.NamedRegexp = void 0;
|
|
7
|
+
var NamedRegexp_1 = require("./NamedRegexp");
|
|
8
|
+
Object.defineProperty(exports, "NamedRegexp", { enumerable: true, get: function () { return __importDefault(NamedRegexp_1).default; } });
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const queryStringify_1 = __importDefault(require("./queryStringify"));
|
|
7
|
+
const searchParse_1 = __importDefault(require("./searchParse"));
|
|
8
|
+
function format(attrs) {
|
|
9
|
+
const { hash = '', pathname = '', origin = '', query, search = '' } = attrs;
|
|
10
|
+
const searchQuery = (0, searchParse_1.default)(search);
|
|
11
|
+
return `${origin}${pathname}${(0, queryStringify_1.default)(Object.assign(Object.assign({}, query), searchQuery))}${hash}`;
|
|
12
|
+
}
|
|
13
|
+
exports.default = format;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import format from './format';
|
|
2
|
+
import params from './params';
|
|
3
|
+
import parse from './parse';
|
|
4
|
+
import queryStringify from './queryStringify';
|
|
5
|
+
import searchParse from './searchParse';
|
|
6
|
+
declare const queryParse: typeof searchParse;
|
|
7
|
+
export { parse, format, queryStringify, params, searchParse, queryParse };
|
|
8
|
+
declare const _default: {
|
|
9
|
+
parse: typeof parse;
|
|
10
|
+
format: typeof format;
|
|
11
|
+
queryStringify: typeof queryStringify;
|
|
12
|
+
queryParse: typeof searchParse;
|
|
13
|
+
params: typeof params;
|
|
14
|
+
searchParse: typeof searchParse;
|
|
15
|
+
};
|
|
16
|
+
export default _default;
|
package/lib/url/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.queryParse = exports.searchParse = exports.params = exports.queryStringify = exports.format = exports.parse = void 0;
|
|
7
|
+
const format_1 = __importDefault(require("./format"));
|
|
8
|
+
exports.format = format_1.default;
|
|
9
|
+
const params_1 = __importDefault(require("./params"));
|
|
10
|
+
exports.params = params_1.default;
|
|
11
|
+
const parse_1 = __importDefault(require("./parse"));
|
|
12
|
+
exports.parse = parse_1.default;
|
|
13
|
+
const queryStringify_1 = __importDefault(require("./queryStringify"));
|
|
14
|
+
exports.queryStringify = queryStringify_1.default;
|
|
15
|
+
const searchParse_1 = __importDefault(require("./searchParse"));
|
|
16
|
+
exports.searchParse = searchParse_1.default;
|
|
17
|
+
const queryParse = searchParse_1.default;
|
|
18
|
+
exports.queryParse = queryParse;
|
|
19
|
+
exports.default = {
|
|
20
|
+
parse: parse_1.default,
|
|
21
|
+
format: format_1.default,
|
|
22
|
+
queryStringify: queryStringify_1.default,
|
|
23
|
+
queryParse,
|
|
24
|
+
params: params_1.default,
|
|
25
|
+
searchParse: searchParse_1.default,
|
|
26
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function params(url: string, ...args: Array<Record<string, string>>): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const merge_1 = __importDefault(require("merge"));
|
|
7
|
+
const parse_1 = __importDefault(require("./parse"));
|
|
8
|
+
const queryStringify_1 = __importDefault(require("./queryStringify"));
|
|
9
|
+
function params(url, ...args) {
|
|
10
|
+
const { query, pathname, origin, hash } = (0, parse_1.default)(url);
|
|
11
|
+
const params = (0, merge_1.default)(query, ...args); // 合并多个对象,覆盖前一个
|
|
12
|
+
return `${origin}${pathname}${(0, queryStringify_1.default)(params)}${hash}`;
|
|
13
|
+
}
|
|
14
|
+
exports.default = params;
|
package/lib/url/parse.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
const regexp_1 = require("../regexp");
|
|
18
|
+
const searchParse_1 = __importDefault(require("./searchParse"));
|
|
19
|
+
const URL_REGEX = new regexp_1.NamedRegexp({
|
|
20
|
+
regex: /(((?:http|https):)?(?:\/\/)?(([^:/?#]*)(?::(\d+))?))?([^?#]+)?(\?[^#]*)?(#.*)?/,
|
|
21
|
+
groups: ['origin', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash'],
|
|
22
|
+
});
|
|
23
|
+
function parse(href) {
|
|
24
|
+
/* istanbul ignore next */
|
|
25
|
+
const { groups = {} } = URL_REGEX.exec(href) || { groups: {} };
|
|
26
|
+
Object.keys(groups).forEach((key) => {
|
|
27
|
+
if (!groups[key]) {
|
|
28
|
+
groups[key] = '';
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
let query = {};
|
|
32
|
+
if (groups.search) {
|
|
33
|
+
query = (0, searchParse_1.default)(groups.search);
|
|
34
|
+
}
|
|
35
|
+
const { pathname } = groups, restGroups = __rest(groups, ["pathname"]);
|
|
36
|
+
return Object.assign({ query,
|
|
37
|
+
href, pathname: pathname ? pathname : '/' }, restGroups);
|
|
38
|
+
}
|
|
39
|
+
exports.default = parse;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function queryStringify(query, options = {}) {
|
|
4
|
+
const { prefix = true } = options;
|
|
5
|
+
const search = [];
|
|
6
|
+
const keys = Object.keys(query);
|
|
7
|
+
if (keys.length === 0) {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
keys.forEach((key) => {
|
|
11
|
+
const value = query[key];
|
|
12
|
+
search.push([key, value].join('='));
|
|
13
|
+
});
|
|
14
|
+
return `${prefix ? '?' : ''}${search.join('&')}`;
|
|
15
|
+
}
|
|
16
|
+
exports.default = queryStringify;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* 分割url search key value,返回 [key, value]
|
|
5
|
+
*/
|
|
6
|
+
function splitSearchKeyValue(str) {
|
|
7
|
+
const [key, ...valueArr] = str.split('=');
|
|
8
|
+
const value = valueArr.length ? valueArr.join('=') : '';
|
|
9
|
+
return [key, value];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 解析 url.search 返回对象类型
|
|
13
|
+
* @param search URL search 信息
|
|
14
|
+
*/
|
|
15
|
+
function searchParse(search) {
|
|
16
|
+
const query = {};
|
|
17
|
+
if (!search)
|
|
18
|
+
return query;
|
|
19
|
+
search = search.replace(/^\?/, ''); // 移除首字符 问号
|
|
20
|
+
search.split('&').forEach((i) => {
|
|
21
|
+
const [key, value] = splitSearchKeyValue(i);
|
|
22
|
+
query[key] = value;
|
|
23
|
+
});
|
|
24
|
+
return query;
|
|
25
|
+
}
|
|
26
|
+
exports.default = searchParse;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/shared",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "Ray shared (Node Client)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ray"
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"@babel/preset-env": "^7.22.14",
|
|
27
27
|
"@babel/preset-typescript": "^7.22.11",
|
|
28
28
|
"@babel/register": "^7.22.5",
|
|
29
|
-
"@ray-js/types": "^1.4.
|
|
29
|
+
"@ray-js/types": "^1.4.3",
|
|
30
30
|
"change-case": "^4.1.2",
|
|
31
31
|
"fs-extra": "^10.1.0",
|
|
32
32
|
"merge": "^2.1.1",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"access": "public",
|
|
42
42
|
"registry": "https://registry.npmjs.org"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "14cb935df10f020e714b44bb41d4899646db751c"
|
|
45
45
|
}
|
package/LICENSE.md
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
Copyright © 2014-2022 Tuya.inc
|
|
2
|
-
|
|
3
|
-
MIT License
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
-
|
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
-
|
|
9
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|