@tmagic/utils 1.5.13 → 1.5.15

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.
@@ -190,6 +190,65 @@ const getUrlParam = (param, url) => {
190
190
  }
191
191
  return "";
192
192
  };
193
+ const setUrlParam = (name, value, url = globalThis.location.href) => {
194
+ const reg = new RegExp(`[?&#]${name}=([^&#]*)`, "gi");
195
+ const matches = url.match(reg);
196
+ const key = `{key${(/* @__PURE__ */ new Date()).getTime()}}`;
197
+ let strArr;
198
+ if (matches && matches.length > 0) {
199
+ strArr = matches[matches.length - 1];
200
+ } else {
201
+ strArr = "";
202
+ }
203
+ const extra = `${name}=${value}`;
204
+ if (strArr) {
205
+ const first = strArr.charAt(0);
206
+ url = url.replace(strArr, key);
207
+ url = url.replace(key, value ? first + extra : "");
208
+ } else if (value) {
209
+ if (url.indexOf("?") > -1) {
210
+ url += `&${extra}`;
211
+ } else {
212
+ url += `?${extra}`;
213
+ }
214
+ }
215
+ return url;
216
+ };
217
+ const getSearchObj = (search = globalThis.location.search ? globalThis.location.search.substring(1) : "") => {
218
+ return search.split("&").reduce((obj, item) => {
219
+ const [a, b = ""] = item.split("=");
220
+ return { ...obj, [a]: b };
221
+ }, {});
222
+ };
223
+ const delQueStr = (url, ref) => {
224
+ let str = "";
225
+ if (url.indexOf("?") !== -1) {
226
+ str = url.substring(url.indexOf("?") + 1);
227
+ } else {
228
+ return url;
229
+ }
230
+ let arr = [];
231
+ let returnurl = "";
232
+ const isHit = Array.isArray(ref) ? function(v) {
233
+ return ~ref.indexOf(v);
234
+ } : function(v) {
235
+ return v === ref;
236
+ };
237
+ if (str.indexOf("&") !== -1) {
238
+ arr = str.split("&");
239
+ for (let i = 0, len = arr.length; i < len; i++) {
240
+ if (!isHit(arr[i].split("=")[0])) {
241
+ returnurl = `${returnurl + arr[i].split("=")[0]}=${arr[i].split("=")[1]}&`;
242
+ }
243
+ }
244
+ return returnurl ? `${url.substr(0, url.indexOf("?"))}?${returnurl.substr(0, returnurl.length - 1)}` : url.substr(0, url.indexOf("?"));
245
+ }
246
+ arr = str.split("=");
247
+ if (isHit(arr[0])) {
248
+ return url.substr(0, url.indexOf("?"));
249
+ }
250
+ return url;
251
+ };
193
252
  const isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]";
194
253
  const isPop = (node) => Boolean(node?.type?.toLowerCase().endsWith("pop"));
195
254
  const isPage = (node) => {
@@ -371,6 +430,7 @@ const getDefaultValueFromFields = (fields) => {
371
430
  data[field.name] = JSON.parse(field.defaultValue);
372
431
  } catch (e) {
373
432
  data[field.name] = defaultValue.object;
433
+ console.warn("defaultValue 解析失败", field.defaultValue, e);
374
434
  }
375
435
  return;
376
436
  }
@@ -455,4 +515,4 @@ const isValueIncludeDataSource = (value) => {
455
515
  return false;
456
516
  };
457
517
 
458
- export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, replaceChildNode, setDslDomRelateConfig, setIdToEl, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
518
+ export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, delQueStr, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getSearchObj, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, replaceChildNode, setDslDomRelateConfig, setIdToEl, setUrlParam, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
@@ -2563,6 +2563,65 @@
2563
2563
  }
2564
2564
  return "";
2565
2565
  };
2566
+ const setUrlParam = (name, value, url = globalThis.location.href) => {
2567
+ const reg = new RegExp(`[?&#]${name}=([^&#]*)`, "gi");
2568
+ const matches = url.match(reg);
2569
+ const key = `{key${(/* @__PURE__ */ new Date()).getTime()}}`;
2570
+ let strArr;
2571
+ if (matches && matches.length > 0) {
2572
+ strArr = matches[matches.length - 1];
2573
+ } else {
2574
+ strArr = "";
2575
+ }
2576
+ const extra = `${name}=${value}`;
2577
+ if (strArr) {
2578
+ const first = strArr.charAt(0);
2579
+ url = url.replace(strArr, key);
2580
+ url = url.replace(key, value ? first + extra : "");
2581
+ } else if (value) {
2582
+ if (url.indexOf("?") > -1) {
2583
+ url += `&${extra}`;
2584
+ } else {
2585
+ url += `?${extra}`;
2586
+ }
2587
+ }
2588
+ return url;
2589
+ };
2590
+ const getSearchObj = (search = globalThis.location.search ? globalThis.location.search.substring(1) : "") => {
2591
+ return search.split("&").reduce((obj, item) => {
2592
+ const [a, b = ""] = item.split("=");
2593
+ return { ...obj, [a]: b };
2594
+ }, {});
2595
+ };
2596
+ const delQueStr = (url, ref) => {
2597
+ let str = "";
2598
+ if (url.indexOf("?") !== -1) {
2599
+ str = url.substring(url.indexOf("?") + 1);
2600
+ } else {
2601
+ return url;
2602
+ }
2603
+ let arr = [];
2604
+ let returnurl = "";
2605
+ const isHit = Array.isArray(ref) ? function(v) {
2606
+ return ~ref.indexOf(v);
2607
+ } : function(v) {
2608
+ return v === ref;
2609
+ };
2610
+ if (str.indexOf("&") !== -1) {
2611
+ arr = str.split("&");
2612
+ for (let i = 0, len = arr.length; i < len; i++) {
2613
+ if (!isHit(arr[i].split("=")[0])) {
2614
+ returnurl = `${returnurl + arr[i].split("=")[0]}=${arr[i].split("=")[1]}&`;
2615
+ }
2616
+ }
2617
+ return returnurl ? `${url.substr(0, url.indexOf("?"))}?${returnurl.substr(0, returnurl.length - 1)}` : url.substr(0, url.indexOf("?"));
2618
+ }
2619
+ arr = str.split("=");
2620
+ if (isHit(arr[0])) {
2621
+ return url.substr(0, url.indexOf("?"));
2622
+ }
2623
+ return url;
2624
+ };
2566
2625
  const isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]";
2567
2626
  const isPop = (node) => Boolean(node?.type?.toLowerCase().endsWith("pop"));
2568
2627
  const isPage = (node) => {
@@ -2744,6 +2803,7 @@
2744
2803
  data[field.name] = JSON.parse(field.defaultValue);
2745
2804
  } catch (e) {
2746
2805
  data[field.name] = defaultValue.object;
2806
+ console.warn("defaultValue 解析失败", field.defaultValue, e);
2747
2807
  }
2748
2808
  return;
2749
2809
  }
@@ -2843,6 +2903,7 @@
2843
2903
  exports.convertToNumber = convertToNumber;
2844
2904
  exports.createDiv = createDiv;
2845
2905
  exports.dataSourceTemplateRegExp = dataSourceTemplateRegExp;
2906
+ exports.delQueStr = delQueStr;
2846
2907
  exports.emptyFn = emptyFn;
2847
2908
  exports.filterXSS = filterXSS;
2848
2909
  exports.getDefaultValueFromFields = getDefaultValueFromFields;
@@ -2858,6 +2919,7 @@
2858
2919
  exports.getNodeInfo = getNodeInfo;
2859
2920
  exports.getNodePath = getNodePath;
2860
2921
  exports.getNodes = getNodes;
2922
+ exports.getSearchObj = getSearchObj;
2861
2923
  exports.getUrlParam = getUrlParam;
2862
2924
  exports.getValueByKeyPath = getValueByKeyPath;
2863
2925
  exports.guid = guid;
@@ -2876,6 +2938,7 @@
2876
2938
  exports.replaceChildNode = replaceChildNode;
2877
2939
  exports.setDslDomRelateConfig = setDslDomRelateConfig;
2878
2940
  exports.setIdToEl = setIdToEl;
2941
+ exports.setUrlParam = setUrlParam;
2879
2942
  exports.setValueByKeyPath = setValueByKeyPath;
2880
2943
  exports.sleep = sleep;
2881
2944
  exports.toHump = toHump;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.5.13",
2
+ "version": "1.5.15",
3
3
  "name": "@tmagic/utils",
4
4
  "type": "module",
5
5
  "main": "dist/tmagic-utils.umd.cjs",
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "peerDependencies": {
36
36
  "typescript": "*",
37
- "@tmagic/schema": "1.5.13"
37
+ "@tmagic/schema": "1.5.15"
38
38
  },
39
39
  "peerDependenciesMeta": {
40
40
  "typescript": {
package/src/index.ts CHANGED
@@ -40,6 +40,7 @@ export * from './dom';
40
40
  // for typeof global checks without @types/node
41
41
  declare let global: {};
42
42
 
43
+ // eslint-disable-next-line @typescript-eslint/naming-convention
43
44
  let _globalThis: any;
44
45
  export const getGlobalThis = (): any =>
45
46
  _globalThis ||
@@ -47,12 +48,12 @@ export const getGlobalThis = (): any =>
47
48
  typeof globalThis !== 'undefined'
48
49
  ? globalThis
49
50
  : typeof self !== 'undefined'
50
- ? self
51
- : typeof window !== 'undefined'
52
- ? window
53
- : typeof global !== 'undefined'
54
- ? global
55
- : {});
51
+ ? self
52
+ : typeof window !== 'undefined'
53
+ ? window
54
+ : typeof global !== 'undefined'
55
+ ? global
56
+ : {});
56
57
 
57
58
  export const sleep = (ms: number): Promise<void> =>
58
59
  new Promise((resolve) => {
@@ -162,6 +163,99 @@ export const getUrlParam = (param: string, url?: string) => {
162
163
  return '';
163
164
  };
164
165
 
166
+ /**
167
+ * 设置url中指定的参数
168
+ *
169
+ * @param {string}
170
+ * name [参数名]
171
+ * @param {string}
172
+ * value [参数值]
173
+ * @param {string}
174
+ * url [发生替换的url地址|默认为location.href]
175
+ * @return {string} [返回处理后的url]
176
+ */
177
+ export const setUrlParam = (name: string, value: string, url = globalThis.location.href) => {
178
+ const reg = new RegExp(`[?&#]${name}=([^&#]*)`, 'gi');
179
+
180
+ const matches = url.match(reg);
181
+
182
+ const key = `{key${new Date().getTime()}}`;
183
+ let strArr;
184
+
185
+ if (matches && matches.length > 0) {
186
+ strArr = matches[matches.length - 1];
187
+ } else {
188
+ strArr = '';
189
+ }
190
+
191
+ const extra = `${name}=${value}`;
192
+
193
+ // 当原url中含有要替换的属性:value不为空时,仅对值做替换,为空时,直接把参数删除掉
194
+ if (strArr) {
195
+ const first = strArr.charAt(0);
196
+ url = url.replace(strArr, key);
197
+ url = url.replace(key, value ? first + extra : '');
198
+ } else if (value) {
199
+ // 当原url中不含有要替换的属性且value值不为空时,直接在url后面添加参数字符串
200
+ if (url.indexOf('?') > -1) {
201
+ url += `&${extra}`;
202
+ } else {
203
+ url += `?${extra}`;
204
+ }
205
+ }
206
+ // 其它情况直接返回原url
207
+ return url;
208
+ };
209
+
210
+ export const getSearchObj = (
211
+ search = globalThis.location.search ? globalThis.location.search.substring(1) : '',
212
+ ): Record<string, string> => {
213
+ return search.split('&').reduce((obj, item) => {
214
+ const [a, b = ''] = item.split('=');
215
+ return { ...obj, [a]: b };
216
+ }, {});
217
+ };
218
+
219
+ export const delQueStr = (url: string, ref: string[] | string) => {
220
+ let str = '';
221
+ if (url.indexOf('?') !== -1) {
222
+ str = url.substring(url.indexOf('?') + 1);
223
+ } else {
224
+ return url;
225
+ }
226
+ let arr = [];
227
+ let returnurl = '';
228
+
229
+ const isHit = Array.isArray(ref)
230
+ ? function (v: string) {
231
+ return ~ref.indexOf(v);
232
+ }
233
+ : function (v: string) {
234
+ return v === ref;
235
+ };
236
+
237
+ if (str.indexOf('&') !== -1) {
238
+ arr = str.split('&');
239
+ for (let i = 0, len = arr.length; i < len; i++) {
240
+ if (!isHit(arr[i].split('=')[0])) {
241
+ returnurl = `${returnurl + arr[i].split('=')[0]}=${arr[i].split('=')[1]}&`;
242
+ }
243
+ }
244
+
245
+ return returnurl
246
+ ? `${url.substr(0, url.indexOf('?'))}?${returnurl.substr(0, returnurl.length - 1)}`
247
+ : url.substr(0, url.indexOf('?'));
248
+ }
249
+
250
+ arr = str.split('=');
251
+
252
+ if (isHit(arr[0])) {
253
+ return url.substr(0, url.indexOf('?'));
254
+ }
255
+
256
+ return url;
257
+ };
258
+
165
259
  export const isObject = (obj: any) => Object.prototype.toString.call(obj) === '[object Object]';
166
260
 
167
261
  export const isPop = (node: MComponent | null): boolean => Boolean(node?.type?.toLowerCase().endsWith('pop'));
@@ -410,6 +504,7 @@ export const getDefaultValueFromFields = (fields: DataSchema[]) => {
410
504
  data[field.name] = JSON.parse(field.defaultValue);
411
505
  } catch (e) {
412
506
  data[field.name] = defaultValue.object;
507
+ console.warn('defaultValue 解析失败', field.defaultValue, e);
413
508
  }
414
509
  return;
415
510
  }
package/types/index.d.ts CHANGED
@@ -44,6 +44,20 @@ declare const getNodePath: (id: Id, data?: MNode$1[]) => MNode$1[];
44
44
  declare const getNodeInfo: (id: Id, root: Pick<MApp, "id" | "items"> | null) => EditorNodeInfo;
45
45
  declare const filterXSS: (str: string) => string;
46
46
  declare const getUrlParam: (param: string, url?: string) => string;
47
+ /**
48
+ * 设置url中指定的参数
49
+ *
50
+ * @param {string}
51
+ * name [参数名]
52
+ * @param {string}
53
+ * value [参数值]
54
+ * @param {string}
55
+ * url [发生替换的url地址|默认为location.href]
56
+ * @return {string} [返回处理后的url]
57
+ */
58
+ declare const setUrlParam: (name: string, value: string, url?: string) => string;
59
+ declare const getSearchObj: (search?: string) => Record<string, string>;
60
+ declare const delQueStr: (url: string, ref: string[] | string) => string;
47
61
  declare const isObject: (obj: any) => boolean;
48
62
  declare const isPop: (node: MComponent | null) => boolean;
49
63
  declare const isPage: (node?: MComponent | null) => boolean;
@@ -97,5 +111,5 @@ interface NodeItem {
97
111
  declare const traverseNode: <T extends NodeItem = NodeItem>(node: T, cb: (node: T, parents: T[]) => void, parents?: T[], evalCbAfter?: boolean) => void;
98
112
  declare const isValueIncludeDataSource: (value: any) => boolean;
99
113
 
100
- export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, replaceChildNode, setDslDomRelateConfig, setIdToEl, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
114
+ export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, delQueStr, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getSearchObj, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, replaceChildNode, setDslDomRelateConfig, setIdToEl, setUrlParam, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
101
115
  export type { NodeItem };