amos-apptool 1.1.0 → 1.2.0

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/esm/index.js CHANGED
@@ -72,4 +72,4 @@ export * from "./math/calendarUtils";
72
72
 
73
73
  export { default as encodeUrl } from "./url/encodeUrl";
74
74
 
75
- export { default as restfulUrl } from "./url/restfulUrl";
75
+ export * from "./url/restfulUrl";
@@ -1,7 +1,40 @@
1
1
  import utils from "./../utils";
2
2
 
3
+ import parseText from "./../parseText";
4
+
5
+ import _trim from "./../_trim";
6
+
3
7
  const _regex = /\{\s*([^\|\}]+?)\s*(?:\|([^\}]*))?\s*\}/g;
4
8
 
5
- export default function restfulUrl(e, r, t) {
6
- return t || (t = _regex), e.replace ? e.replace(t, (e, t) => utils.isUndefined(r[t]) ? e : r[t]) : e;
7
- }
9
+ export function restfulUrl(t, r, e) {
10
+ return e || (e = _regex), t.replace ? t.replace(e, (t, e) => utils.isUndefined(r[e]) ? t : r[e]) : t;
11
+ }
12
+
13
+ const defaultRE = /\{((?:.|\n)+?)\}/g;
14
+
15
+ export function formatUrl(t, r = {}, e) {
16
+ return e || (e = defaultRE), parseText(t, r, e);
17
+ }
18
+
19
+ export function changeParam(t, r) {
20
+ if (!t || utils.isEmpty(r)) return t;
21
+ let e = t;
22
+ return Object.keys(r).forEach(function(t) {
23
+ let i = r[t];
24
+ utils.isUndefined(i) ? i = "" : utils.isArray(i) || utils.isObject(i) && (i = JSON.stringify(i)),
25
+ e = function(t, r, e) {
26
+ const i = r + "=([^&]*)", n = r + "=" + e;
27
+ if (t.match(i)) {
28
+ const e = new RegExp(`(${r}=)([^&]*)`, "gi");
29
+ return t.replace(e, n);
30
+ }
31
+ return t.match("[?]") ? t + "&" + n : t + "?" + n;
32
+ }(e, t, i);
33
+ }), e;
34
+ }
35
+
36
+ export const completePrefix = (t = "", r = "") => {
37
+ if (!t || "" === t) return r;
38
+ return t.endsWith("/") && (t = t.substring(0, t.length - 1)), t = _trim(t), r.startsWith("/") && (r = r.substring(1)),
39
+ [ t, r = (r = _trim(r)).replace(/\/\/+/g, "/") ].join("/");
40
+ };
package/index.d.ts CHANGED
@@ -291,6 +291,58 @@ export function encodeUrl(url: string): string;
291
291
  */
292
292
  export function restfulUrl(url: string, options: {}, regexps: RegExp): string;
293
293
 
294
+ /**
295
+ * 替换 url 参数。如果url中无指定的参数,则自动追加。
296
+ *
297
+ * 自动将对象中的 undefined 值,转化为 ''。null 值则直接返回 null 字符串。
298
+ *
299
+ * params 参数中的数据格式,建议调用处统一处理为 string 格式。内部仅统一处理 undefined 和 {} 格式。
300
+ * @param href 需要替换参数的 url。为空字符串则直接返回 href
301
+ * @param params 参数对象。为空对象,则直接返回 href。
302
+ * @example
303
+ * // 无参数,自动追加
304
+ * changeParam('a/b/d/g', { a: 1, b: 2, c: 3 }); // 'a/b/d/g?a=1&b=2&c=3'
305
+ * // 更新已有参数的值
306
+ * changeParam('a/b/d/g?a=aa&b=bb&c=cc', { a: 1, b: 2, c: 3 }); // 'a/b/d/g?a=1&b=2&c=3'
307
+ * // url 中无参数 d,自动追加参数
308
+ * changeParam('a/b/d/g?a=aa&b=bb&c=cc', { a: 1, b: 2, c: 3, d: 5 }); // 'a/b/d/g?a=1&b=2&c=3&d=5'
309
+ * // undefined 值转化为 ''
310
+ * changeParam('a/b/d/g', { a: 1, b: 2, c: undefined }); // 'a/b/d/g?a=1&b=2&c='
311
+ * // null 值转化为 'null'
312
+ * changeParam('a/b/d/g', { a: 1, b: null, c: undefined }); // 'a/b/d/g?a=1&b=null&c='
313
+ * // [] 和 {} 值。最好不要给参数中传入 {} 值
314
+ * changeParam('a/b/d/g', { a: { m: 3 }, b: [1,2] }); // a/b/d/g?a={"m":3}&b=1,2
315
+ * // 更换 token={token} 值,注意 原理并不是替换 {token} 值,而是更新 `token=xxx` 值,也就是目标是 `=` 左边的 `key` 与 params 中的 key 匹配
316
+ * changeParam('a/b/d/g?token={token}', { a: 'file', b: [1,2], token: 'mytoken' }); // a/b/d/g?token=mytoken&a=file&b=1,2
317
+ */
318
+ export function changeParam(href: String, params: Object): String;
319
+
320
+ /**
321
+ * 格式化url
322
+ * @param {string} targetStr
323
+ * @param {object} dataObj
324
+ * @param {string|RegExp} regexps 可选, default: /\{(.*)\}/g);
325
+ * @example
326
+ * formatUrl('a/{b}/{c}/d',{a: 1, b:2}) // 返回: 'a/1/2/d'
327
+ * formatUrl('a/{{b}}/{{c}}/d',{a: 1, b:2}, /\{\{(.*)\}\}/g) // 返回: 'a/1/2/d'
328
+ */
329
+ export function formatUrl(targetStr: string, dataObj: {}, regexps: string|RegExp): string;
330
+
331
+ /**
332
+ * 补全 url
333
+ * @param left 左边通用前缀
334
+ * @param right 右边具体的 url
335
+ * @return
336
+ * @example
337
+ * completePrefix('/aa/', 'b/c/d'); // '/aa/b/c/d'
338
+ * completePrefix('/aa /', ' b/c/d'); // '/aa/b/c/d'
339
+ * completePrefix('aa ', ' b c/d'); // 'aa/b c/d'
340
+ * completePrefix(' aa ', ' b c/d '); // 'aa/b c/d'
341
+ * // 注意,不会自动去除 url 内部的空格,会自动将 url 内部多余的 `/` 去掉
342
+ * completePrefix('// aa /', '/ b c///d /'); // '/ aa/b c/d /'
343
+ */
344
+ export function completePrefix(left = '', right = ''): string;
345
+
294
346
  /**
295
347
  *
296
348
  * @param str
package/lib/index.js CHANGED
@@ -39,8 +39,7 @@ var _exportNames = {
39
39
  subtraction: !0,
40
40
  accMul: !0,
41
41
  accDivide: !0,
42
- encodeUrl: !0,
43
- restfulUrl: !0
42
+ encodeUrl: !0
44
43
  };
45
44
 
46
45
  Object.defineProperty(exports, "Base64", {
@@ -178,11 +177,6 @@ Object.defineProperty(exports, "Base64", {
178
177
  get: function() {
179
178
  return _randomColor.default;
180
179
  }
181
- }), Object.defineProperty(exports, "restfulUrl", {
182
- enumerable: !0,
183
- get: function() {
184
- return _restfulUrl.default;
185
- }
186
180
  }), Object.defineProperty(exports, "shallowCopy", {
187
181
  enumerable: !0,
188
182
  get: function() {
@@ -259,4 +253,13 @@ Object.keys(_calendarUtils).forEach(function(e) {
259
253
  }));
260
254
  });
261
255
 
262
- var _encodeUrl = _interopRequireDefault(require("./url/encodeUrl")), _restfulUrl = _interopRequireDefault(require("./url/restfulUrl"));
256
+ var _encodeUrl = _interopRequireDefault(require("./url/encodeUrl")), _restfulUrl = require("./url/restfulUrl");
257
+
258
+ Object.keys(_restfulUrl).forEach(function(e) {
259
+ "default" !== e && "__esModule" !== e && (Object.prototype.hasOwnProperty.call(_exportNames, e) || e in exports && exports[e] === _restfulUrl[e] || Object.defineProperty(exports, e, {
260
+ enumerable: !0,
261
+ get: function() {
262
+ return _restfulUrl[e];
263
+ }
264
+ }));
265
+ });
@@ -4,12 +4,44 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
 
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: !0
7
- }), exports.default = restfulUrl;
7
+ }), exports.changeParam = changeParam, exports.completePrefix = void 0, exports.formatUrl = formatUrl,
8
+ exports.restfulUrl = restfulUrl;
8
9
 
9
- var _utils = _interopRequireDefault(require("./../utils")), _regex = /\{\s*([^\|\}]+?)\s*(?:\|([^\}]*))?\s*\}/g;
10
+ var _utils = _interopRequireDefault(require("./../utils")), _parseText = _interopRequireDefault(require("./../parseText")), _trim2 = _interopRequireDefault(require("./../_trim")), _regex = /\{\s*([^\|\}]+?)\s*(?:\|([^\}]*))?\s*\}/g;
10
11
 
11
12
  function restfulUrl(e, r, t) {
12
13
  return t || (t = _regex), e.replace ? e.replace(t, function(e, t) {
13
14
  return _utils.default.isUndefined(r[t]) ? e : r[t];
14
15
  }) : e;
15
- }
16
+ }
17
+
18
+ var defaultRE = /\{((?:.|\n)+?)\}/g;
19
+
20
+ function formatUrl(e) {
21
+ var r = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}, t = arguments.length > 2 ? arguments[2] : void 0;
22
+ return t || (t = defaultRE), (0, _parseText.default)(e, r, t);
23
+ }
24
+
25
+ function changeParam(e, r) {
26
+ if (!e || _utils.default.isEmpty(r)) return e;
27
+ var t = e;
28
+ return Object.keys(r).forEach(function(e) {
29
+ var i = r[e];
30
+ _utils.default.isUndefined(i) ? i = "" : _utils.default.isArray(i) || _utils.default.isObject(i) && (i = JSON.stringify(i)),
31
+ t = function(e, r, t) {
32
+ var i = r + "=([^&]*)", u = r + "=" + t;
33
+ if (e.match(i)) {
34
+ var a = new RegExp("(".concat(r, "=)([^&]*)"), "gi");
35
+ return e.replace(a, u);
36
+ }
37
+ return e.match("[?]") ? e + "&" + u : e + "?" + u;
38
+ }(t, e, i);
39
+ }), t;
40
+ }
41
+
42
+ var completePrefix = exports.completePrefix = function() {
43
+ var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "", r = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "";
44
+ return e && "" !== e ? (e.endsWith("/") && (e = e.substring(0, e.length - 1)), e = (0,
45
+ _trim2.default)(e), r.startsWith("/") && (r = r.substring(1)), [ e, r = (r = (0,
46
+ _trim2.default)(r)).replace(/\/\/+/g, "/") ].join("/")) : r;
47
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amos-apptool",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "amos app tool",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./esm/index.js",