@wxhccc/es-util 1.3.0 → 1.5.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.
@@ -0,0 +1,49 @@
1
+ export interface Node {
2
+ [key: string]: any;
3
+ }
4
+ export interface TreeNode {
5
+ [key: string]: any;
6
+ }
7
+ interface Options {
8
+ primaryKey?: string;
9
+ parentKey?: string;
10
+ childrenKey?: string;
11
+ }
12
+ interface TreeOptions extends Options {
13
+ createRoot?: boolean | ((nodes: TreeNode[]) => any);
14
+ parentRefKey?: boolean | string;
15
+ }
16
+ interface NodeOptions extends Options {
17
+ hasParentKey?: boolean;
18
+ returnObject?: boolean;
19
+ }
20
+ declare type Tree = TreeNode[] | {
21
+ [children: string]: TreeNode[];
22
+ };
23
+ /**
24
+ * translate array to tree
25
+ * @param {Array} array the node array need to be translate
26
+ * @param {Object} options the options
27
+ * @param {string} primaryKey the primary key of node
28
+ * @param {string} parentKey the parent's key of node
29
+ * @param {string} childrenKey the child's key of node
30
+ * @param {boolean | Function} createNode whether create root node, or function which can return root node
31
+ * @param {boolean | string} parentRefKey parent reference key of node
32
+ */
33
+ export declare function array2tree(array: Node[], options?: TreeOptions): Tree;
34
+ declare type Nodes = Node[];
35
+ declare type NodeMap = {
36
+ [id: string]: Node;
37
+ };
38
+ /**
39
+ * translate tree to array
40
+ * @param {Array/Object} tree the node tree need to be translate
41
+ * @param {Object} options the options
42
+ * @param {string} primaryKey the primary key of tree node
43
+ * @param {string} parentKey the parent's key of tree node
44
+ * @param {string} childrenKey the child's key of tree node
45
+ * @param {boolean} hasParentKey whether create parent id in node
46
+ * @param {boolean} returnObject whether return object or array
47
+ */
48
+ export declare function tree2array(tree: Tree, options?: NodeOptions): NodeMap | Nodes;
49
+ export {};
@@ -0,0 +1,6 @@
1
+ export * from './array-tree-switch';
2
+ export * from './validate';
3
+ export * from './object-array';
4
+ export * from './value-string-switch';
5
+ export * from './promise';
6
+ export * from './optional';
package/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v1.2.0
2
+ * @wxhccc/es-util v1.5.0
3
3
  * (c) 2021 wxhccc
4
4
  * @license MIT
5
5
  */
@@ -249,9 +249,9 @@ function byteStringify(byteNum, options) {
249
249
  function byteParse(byteStr, options = {}) {
250
250
  }
251
251
 
252
- const { hasOwnProperty, toString } = Object.prototype;
252
+ const { hasOwnProperty, toString: toString$1 } = Object.prototype;
253
253
  const objType = (val) => {
254
- const typeKeys = toString.call(val).match(/^\[object (.*)\]$/);
254
+ const typeKeys = toString$1.call(val).match(/^\[object (.*)\]$/);
255
255
  return typeKeys ? typeKeys[1] : '';
256
256
  };
257
257
  /**
@@ -279,20 +279,52 @@ function checkContext(context) {
279
279
  }
280
280
  return 'unknown';
281
281
  }
282
- /**
283
- * wrap promise with lock, support reactive environment like react or vue
284
- * @param promise promise
285
- * @param wrap whether use awaitWrap to wrap result
286
- */
282
+ const emptyPromise = () => {
283
+ const ep = Promise.resolve(undefined);
284
+ return Object.assign(ep, {
285
+ then(...args) {
286
+ const [onfulfilled] = args;
287
+ // if then has be called by Promise.all, let ite pass
288
+ if (args.length === 2 && typeof onfulfilled === 'function') {
289
+ return onfulfilled(undefined);
290
+ }
291
+ return ep;
292
+ },
293
+ catch: (onrejected) => ep,
294
+ finally: () => ep
295
+ });
296
+ };
287
297
  const lockCtx = {};
288
- function wp(promise, wrap) {
298
+ function wrapPromise(promise, wrapOrOptions) {
289
299
  const contextType = checkContext(this);
290
300
  const isReactiveIns = contextType !== 'unknown';
291
301
  const context = isReactiveIns ? this : lockCtx;
292
302
  const stateKey = contextType === 'react' ? 'state' : '';
293
303
  const contextState = stateKey ? context[stateKey] : context;
304
+ const { wrap, lock, syncRefHandle, manualUnlock } = typeof wrapOrOptions === 'boolean' ? { wrap: wrapOrOptions } : { ...wrapOrOptions };
305
+ let lockSwitchHook = undefined;
306
+ let lockRefHandle = undefined;
307
+ let lockKey = [];
308
+ let needLock = false;
309
+ let ignoreLock = false;
294
310
  const has = (val, key) => !!val && hasOwnProperty.call(val, key);
295
311
  const isObj = (obj) => objType(obj) === 'Object';
312
+ const getValue = (obj, path) => {
313
+ let result = false;
314
+ if (obj && isObj(obj) && Array.isArray(path) && path.length) {
315
+ let curObj = obj;
316
+ for (let i = 0; i < path.length; i++) {
317
+ const key = path[i];
318
+ if (typeof curObj !== 'object' || !has(curObj, key)) {
319
+ break;
320
+ }
321
+ curObj = curObj[key];
322
+ i === path.length - 1 &&
323
+ (result = (typeof curObj === 'boolean') ? curObj : false);
324
+ }
325
+ }
326
+ return result;
327
+ };
296
328
  const setValue = (obj, path, value) => {
297
329
  // if vue2 and path[0] not defined, do nothing
298
330
  if (contextType === 'vue' && context.$set && !has(obj, path[0]))
@@ -321,80 +353,176 @@ function wp(promise, wrap) {
321
353
  // trigger setState when run in react class component
322
354
  isStateRect && canSet && context.setState({ [path[0]]: originObj[path[0]] });
323
355
  };
324
- const getValue = (obj, path) => {
325
- // use refHandle if contextState not update sync
326
- if (lockRefHandle)
327
- return lockRefHandle[0][lockRefHandle[1]];
328
- let result = false;
329
- if (obj && isObj(obj) && Array.isArray(path)) {
330
- let curObj = obj;
331
- for (let i = 0; i < path.length; i++) {
332
- const key = path[i];
333
- if (typeof curObj !== 'object' || !has(curObj, key)) {
334
- break;
335
- }
336
- curObj = curObj[key];
337
- i === path.length - 1 &&
338
- (result = typeof curObj === 'boolean' ? curObj : false);
339
- }
340
- }
341
- return result;
342
- };
343
356
  const stateLock = (bool) => {
344
- if (lockKey.length)
345
- return setValue(contextState, lockKey, bool);
357
+ if (lockKey.length) {
358
+ // if not reactive instance, save lock key as string, no need to structure nested object
359
+ isReactiveIns ? setValue(contextState, lockKey, bool) : (lockCtx[lock] = bool);
360
+ return;
361
+ }
346
362
  if (lockRefHandle)
347
363
  lockRefHandle[0][lockRefHandle[1]] = bool;
348
364
  if (lockSwitchHook)
349
365
  lockSwitchHook(bool);
350
366
  };
351
- const checkLock = () => getValue(contextState, lockKey);
352
- let lockSwitchHook;
353
- let lockRefHandle;
354
- let lockKey = [];
367
+ const checkLock = () => {
368
+ // use refHandle if contextState not update sync
369
+ if (lockRefHandle)
370
+ return lockRefHandle[0][lockRefHandle[1]];
371
+ if (!isReactiveIns && typeof lock === 'string')
372
+ return contextState[lock];
373
+ return getValue(contextState, lockKey);
374
+ };
375
+ if (lock) {
376
+ const isRefHandle = (val) => Array.isArray(val) && val.length === 2;
377
+ if (typeof lock === 'string') {
378
+ lockKey = lock.split('.');
379
+ }
380
+ else if (isRefHandle(lock)) {
381
+ lockRefHandle = lock;
382
+ }
383
+ else if (typeof lock === 'function') {
384
+ lockSwitchHook = lock;
385
+ if (isRefHandle(syncRefHandle)) {
386
+ lockRefHandle = syncRefHandle;
387
+ }
388
+ }
389
+ needLock = lockKey.length > 0 || !!lockRefHandle || !!lockSwitchHook;
390
+ needLock && (ignoreLock = checkLock());
391
+ }
355
392
  let corePromsie;
393
+ // if promise is a function, here need to call it in promise.then to use locking check to prevent next call
356
394
  if (typeof promise === 'function') {
357
- if (checkLock())
358
- return;
359
- corePromsie = wrap ? awaitWrapper(promise()) : promise();
395
+ corePromsie = ignoreLock ? emptyPromise() : wrap ? awaitWrapper(promise()) : promise();
360
396
  }
361
397
  else {
362
398
  corePromsie = wrap ? awaitWrapper(promise) : promise;
363
399
  }
400
+ const unlock = () => stateLock(false);
364
401
  Object.defineProperties(corePromsie, {
365
- __lockValue: { get: checkLock },
366
- lock: {
367
- value: (keyOrHookOrHandle, syncRefHandle) => {
368
- const isRefHandle = (val) => Array.isArray(val) && val.length === 2;
369
- if (typeof keyOrHookOrHandle === 'string') {
370
- lockKey = keyOrHookOrHandle.split('.');
371
- }
372
- else if (isRefHandle(keyOrHookOrHandle)) {
373
- lockRefHandle = keyOrHookOrHandle;
374
- }
375
- else if (typeof keyOrHookOrHandle === 'function') {
376
- lockSwitchHook = keyOrHookOrHandle;
377
- if (isRefHandle(syncRefHandle)) {
378
- lockRefHandle = syncRefHandle;
379
- }
380
- }
381
- stateLock(true);
382
- return corePromsie;
383
- }
384
- }
402
+ '__lockValue': { get: checkLock },
403
+ _checkLockKey: { value: (key) => lockCtx[key] },
404
+ unlock: { value: unlock }
385
405
  });
386
- corePromsie.finally(() => stateLock(false));
406
+ if (needLock && !ignoreLock) {
407
+ stateLock(true);
408
+ !manualUnlock && corePromsie.finally(unlock);
409
+ }
387
410
  return corePromsie;
388
411
  }
412
+ const wp = Object.defineProperty(wrapPromise, '_checkLockKey', { value: (key) => lockCtx[key] });
389
413
  const wpVuePlugin = {
390
- install(appOrVue) {
414
+ install(appOrVue, key = '$wp') {
391
415
  if (appOrVue.config && 'globalProperties' in appOrVue.config) {
392
- appOrVue.config.globalProperties.$wp = wp;
416
+ appOrVue.config.globalProperties[key] = wrapPromise;
393
417
  }
394
418
  else {
395
- appOrVue.prototype.$wp = wp;
419
+ appOrVue.prototype[key] = wrapPromise;
396
420
  }
397
421
  }
398
422
  };
399
423
 
400
- export { ChinaIdCardValid, array2tree, awaitWrapper, byteParse, byteStringify, checkoutBy, formulaValidate, mapToObject, objType, pickRenameKeys, tree2array, wp, wpVuePlugin };
424
+ function safeJsonParse(json = '', faileValue) {
425
+ try {
426
+ return JSON.parse(json);
427
+ }
428
+ catch (_e) {
429
+ return faileValue;
430
+ }
431
+ }
432
+ /**
433
+ * check value whether empty text
434
+ * 判断给定变量是否是字面量空值 */
435
+ const isNullable = (value) => value === '' || value === undefined || value === null;
436
+ /**
437
+ * 过滤对象中属性为空('' | undefined | null)的字段
438
+ * @param obj 给定对象
439
+ * @param deep 是否需要递归过滤
440
+ * @returns 过滤后的对象
441
+ */
442
+ const filterNullable = (obj, deep) => {
443
+ const result = {};
444
+ Object.keys(obj).forEach(key => {
445
+ const value = obj[key];
446
+ if (value !== '' && value !== undefined && value !== null) {
447
+ result[key] = deep && Object.prototype.toString.call(value) === '[object Object]' ? filterNullable(value) : value;
448
+ }
449
+ });
450
+ return result;
451
+ };
452
+ const { toString } = Object.prototype;
453
+ /**
454
+ * 将对象中属性值为null的转换为undefined
455
+ * @param data 对象数据
456
+ * @returns
457
+ */
458
+ const switchNullToUndefined = (data) => {
459
+ if (data === null) {
460
+ return undefined;
461
+ }
462
+ if (Array.isArray(data)) {
463
+ return data.map(switchNullToUndefined);
464
+ }
465
+ else if (toString.call(data) === '[object Object]') {
466
+ Object.keys(data).forEach((key) => {
467
+ data[key] = switchNullToUndefined(data[key]);
468
+ });
469
+ }
470
+ return data;
471
+ };
472
+ /**
473
+ * 将搜素条件里的数组字段转换为其他格式,通常用于搜索表单的字段转换
474
+ * @param formData 传入参数
475
+ * @param maps 转换的映射关系,eg:{ date: ['startTime', 'endTime'] }
476
+ * @param filterNullableValue 是否需要过滤掉空数据项
477
+ */
478
+ const formParamsSwitch = (formData, maps, options) => {
479
+ const dateKeys = Object.keys(maps);
480
+ const dateData = {};
481
+ const restData = {};
482
+ Object.keys(formData).forEach(key => {
483
+ if (dateKeys.includes(key)) {
484
+ dateData[key] = formData[key];
485
+ }
486
+ else {
487
+ restData[key] = formData[key];
488
+ }
489
+ });
490
+ const accessKeys = Object.keys(dateData);
491
+ // 如无需要转换的数据时,直接返回原对象
492
+ if (accessKeys.length === 0) {
493
+ return formData;
494
+ }
495
+ const { filterNullable: filterNullableValue, dateFormat } = { filterNullable: true, ...options };
496
+ const handleData = accessKeys.reduce((acc, key) => {
497
+ const values = dateData[key];
498
+ if (!values) {
499
+ return acc;
500
+ }
501
+ const mapValue = maps[key];
502
+ // 数组合并操作/取最后一位操作
503
+ if (mapValue === 'join' || mapValue === 'pop') {
504
+ if (Array.isArray(values) && values.length) {
505
+ acc[key] = mapValue === 'join' ? values.join(',') : values.slice(-1)[0];
506
+ }
507
+ }
508
+ else if (mapValue.length === 2 || mapValue.length === 3) {
509
+ if (!values || values.length !== 2) {
510
+ return acc;
511
+ }
512
+ const [startKey, endKey, format] = mapValue;
513
+ const [startValue, endValue] = values;
514
+ Object.assign(acc, {
515
+ [startKey]: dateFormat instanceof Function ? dateFormat(startValue, format) : startValue,
516
+ [endKey]: dateFormat instanceof Function ? dateFormat(endValue, format) : endValue
517
+ });
518
+ }
519
+ return acc;
520
+ }, {});
521
+ const result = {
522
+ ...restData,
523
+ ...handleData
524
+ };
525
+ return filterNullableValue ? filterNullable(result) : result;
526
+ };
527
+
528
+ export { ChinaIdCardValid, array2tree, awaitWrapper, byteParse, byteStringify, checkoutBy, filterNullable, formParamsSwitch, formulaValidate, isNullable, mapToObject, objType, pickRenameKeys, safeJsonParse, switchNullToUndefined, tree2array, wp, wpVuePlugin };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v1.2.0
2
+ * @wxhccc/es-util v1.5.0
3
3
  * (c) 2021 wxhccc
4
4
  * @license MIT
5
5
  */
@@ -253,9 +253,9 @@ function byteStringify(byteNum, options) {
253
253
  function byteParse(byteStr, options = {}) {
254
254
  }
255
255
 
256
- const { hasOwnProperty, toString } = Object.prototype;
256
+ const { hasOwnProperty, toString: toString$1 } = Object.prototype;
257
257
  const objType = (val) => {
258
- const typeKeys = toString.call(val).match(/^\[object (.*)\]$/);
258
+ const typeKeys = toString$1.call(val).match(/^\[object (.*)\]$/);
259
259
  return typeKeys ? typeKeys[1] : '';
260
260
  };
261
261
  /**
@@ -283,20 +283,52 @@ function checkContext(context) {
283
283
  }
284
284
  return 'unknown';
285
285
  }
286
- /**
287
- * wrap promise with lock, support reactive environment like react or vue
288
- * @param promise promise
289
- * @param wrap whether use awaitWrap to wrap result
290
- */
286
+ const emptyPromise = () => {
287
+ const ep = Promise.resolve(undefined);
288
+ return Object.assign(ep, {
289
+ then(...args) {
290
+ const [onfulfilled] = args;
291
+ // if then has be called by Promise.all, let ite pass
292
+ if (args.length === 2 && typeof onfulfilled === 'function') {
293
+ return onfulfilled(undefined);
294
+ }
295
+ return ep;
296
+ },
297
+ catch: (onrejected) => ep,
298
+ finally: () => ep
299
+ });
300
+ };
291
301
  const lockCtx = {};
292
- function wp(promise, wrap) {
302
+ function wrapPromise(promise, wrapOrOptions) {
293
303
  const contextType = checkContext(this);
294
304
  const isReactiveIns = contextType !== 'unknown';
295
305
  const context = isReactiveIns ? this : lockCtx;
296
306
  const stateKey = contextType === 'react' ? 'state' : '';
297
307
  const contextState = stateKey ? context[stateKey] : context;
308
+ const { wrap, lock, syncRefHandle, manualUnlock } = typeof wrapOrOptions === 'boolean' ? { wrap: wrapOrOptions } : { ...wrapOrOptions };
309
+ let lockSwitchHook = undefined;
310
+ let lockRefHandle = undefined;
311
+ let lockKey = [];
312
+ let needLock = false;
313
+ let ignoreLock = false;
298
314
  const has = (val, key) => !!val && hasOwnProperty.call(val, key);
299
315
  const isObj = (obj) => objType(obj) === 'Object';
316
+ const getValue = (obj, path) => {
317
+ let result = false;
318
+ if (obj && isObj(obj) && Array.isArray(path) && path.length) {
319
+ let curObj = obj;
320
+ for (let i = 0; i < path.length; i++) {
321
+ const key = path[i];
322
+ if (typeof curObj !== 'object' || !has(curObj, key)) {
323
+ break;
324
+ }
325
+ curObj = curObj[key];
326
+ i === path.length - 1 &&
327
+ (result = (typeof curObj === 'boolean') ? curObj : false);
328
+ }
329
+ }
330
+ return result;
331
+ };
300
332
  const setValue = (obj, path, value) => {
301
333
  // if vue2 and path[0] not defined, do nothing
302
334
  if (contextType === 'vue' && context.$set && !has(obj, path[0]))
@@ -325,92 +357,193 @@ function wp(promise, wrap) {
325
357
  // trigger setState when run in react class component
326
358
  isStateRect && canSet && context.setState({ [path[0]]: originObj[path[0]] });
327
359
  };
328
- const getValue = (obj, path) => {
329
- // use refHandle if contextState not update sync
330
- if (lockRefHandle)
331
- return lockRefHandle[0][lockRefHandle[1]];
332
- let result = false;
333
- if (obj && isObj(obj) && Array.isArray(path)) {
334
- let curObj = obj;
335
- for (let i = 0; i < path.length; i++) {
336
- const key = path[i];
337
- if (typeof curObj !== 'object' || !has(curObj, key)) {
338
- break;
339
- }
340
- curObj = curObj[key];
341
- i === path.length - 1 &&
342
- (result = typeof curObj === 'boolean' ? curObj : false);
343
- }
344
- }
345
- return result;
346
- };
347
360
  const stateLock = (bool) => {
348
- if (lockKey.length)
349
- return setValue(contextState, lockKey, bool);
361
+ if (lockKey.length) {
362
+ // if not reactive instance, save lock key as string, no need to structure nested object
363
+ isReactiveIns ? setValue(contextState, lockKey, bool) : (lockCtx[lock] = bool);
364
+ return;
365
+ }
350
366
  if (lockRefHandle)
351
367
  lockRefHandle[0][lockRefHandle[1]] = bool;
352
368
  if (lockSwitchHook)
353
369
  lockSwitchHook(bool);
354
370
  };
355
- const checkLock = () => getValue(contextState, lockKey);
356
- let lockSwitchHook;
357
- let lockRefHandle;
358
- let lockKey = [];
371
+ const checkLock = () => {
372
+ // use refHandle if contextState not update sync
373
+ if (lockRefHandle)
374
+ return lockRefHandle[0][lockRefHandle[1]];
375
+ if (!isReactiveIns && typeof lock === 'string')
376
+ return contextState[lock];
377
+ return getValue(contextState, lockKey);
378
+ };
379
+ if (lock) {
380
+ const isRefHandle = (val) => Array.isArray(val) && val.length === 2;
381
+ if (typeof lock === 'string') {
382
+ lockKey = lock.split('.');
383
+ }
384
+ else if (isRefHandle(lock)) {
385
+ lockRefHandle = lock;
386
+ }
387
+ else if (typeof lock === 'function') {
388
+ lockSwitchHook = lock;
389
+ if (isRefHandle(syncRefHandle)) {
390
+ lockRefHandle = syncRefHandle;
391
+ }
392
+ }
393
+ needLock = lockKey.length > 0 || !!lockRefHandle || !!lockSwitchHook;
394
+ needLock && (ignoreLock = checkLock());
395
+ }
359
396
  let corePromsie;
397
+ // if promise is a function, here need to call it in promise.then to use locking check to prevent next call
360
398
  if (typeof promise === 'function') {
361
- if (checkLock())
362
- return;
363
- corePromsie = wrap ? awaitWrapper(promise()) : promise();
399
+ corePromsie = ignoreLock ? emptyPromise() : wrap ? awaitWrapper(promise()) : promise();
364
400
  }
365
401
  else {
366
402
  corePromsie = wrap ? awaitWrapper(promise) : promise;
367
403
  }
404
+ const unlock = () => stateLock(false);
368
405
  Object.defineProperties(corePromsie, {
369
- __lockValue: { get: checkLock },
370
- lock: {
371
- value: (keyOrHookOrHandle, syncRefHandle) => {
372
- const isRefHandle = (val) => Array.isArray(val) && val.length === 2;
373
- if (typeof keyOrHookOrHandle === 'string') {
374
- lockKey = keyOrHookOrHandle.split('.');
375
- }
376
- else if (isRefHandle(keyOrHookOrHandle)) {
377
- lockRefHandle = keyOrHookOrHandle;
378
- }
379
- else if (typeof keyOrHookOrHandle === 'function') {
380
- lockSwitchHook = keyOrHookOrHandle;
381
- if (isRefHandle(syncRefHandle)) {
382
- lockRefHandle = syncRefHandle;
383
- }
384
- }
385
- stateLock(true);
386
- return corePromsie;
387
- }
388
- }
406
+ '__lockValue': { get: checkLock },
407
+ _checkLockKey: { value: (key) => lockCtx[key] },
408
+ unlock: { value: unlock }
389
409
  });
390
- corePromsie.finally(() => stateLock(false));
410
+ if (needLock && !ignoreLock) {
411
+ stateLock(true);
412
+ !manualUnlock && corePromsie.finally(unlock);
413
+ }
391
414
  return corePromsie;
392
415
  }
416
+ const wp = Object.defineProperty(wrapPromise, '_checkLockKey', { value: (key) => lockCtx[key] });
393
417
  const wpVuePlugin = {
394
- install(appOrVue) {
418
+ install(appOrVue, key = '$wp') {
395
419
  if (appOrVue.config && 'globalProperties' in appOrVue.config) {
396
- appOrVue.config.globalProperties.$wp = wp;
420
+ appOrVue.config.globalProperties[key] = wrapPromise;
397
421
  }
398
422
  else {
399
- appOrVue.prototype.$wp = wp;
423
+ appOrVue.prototype[key] = wrapPromise;
400
424
  }
401
425
  }
402
426
  };
403
427
 
428
+ function safeJsonParse(json = '', faileValue) {
429
+ try {
430
+ return JSON.parse(json);
431
+ }
432
+ catch (_e) {
433
+ return faileValue;
434
+ }
435
+ }
436
+ /**
437
+ * check value whether empty text
438
+ * 判断给定变量是否是字面量空值 */
439
+ const isNullable = (value) => value === '' || value === undefined || value === null;
440
+ /**
441
+ * 过滤对象中属性为空('' | undefined | null)的字段
442
+ * @param obj 给定对象
443
+ * @param deep 是否需要递归过滤
444
+ * @returns 过滤后的对象
445
+ */
446
+ const filterNullable = (obj, deep) => {
447
+ const result = {};
448
+ Object.keys(obj).forEach(key => {
449
+ const value = obj[key];
450
+ if (value !== '' && value !== undefined && value !== null) {
451
+ result[key] = deep && Object.prototype.toString.call(value) === '[object Object]' ? filterNullable(value) : value;
452
+ }
453
+ });
454
+ return result;
455
+ };
456
+ const { toString } = Object.prototype;
457
+ /**
458
+ * 将对象中属性值为null的转换为undefined
459
+ * @param data 对象数据
460
+ * @returns
461
+ */
462
+ const switchNullToUndefined = (data) => {
463
+ if (data === null) {
464
+ return undefined;
465
+ }
466
+ if (Array.isArray(data)) {
467
+ return data.map(switchNullToUndefined);
468
+ }
469
+ else if (toString.call(data) === '[object Object]') {
470
+ Object.keys(data).forEach((key) => {
471
+ data[key] = switchNullToUndefined(data[key]);
472
+ });
473
+ }
474
+ return data;
475
+ };
476
+ /**
477
+ * 将搜素条件里的数组字段转换为其他格式,通常用于搜索表单的字段转换
478
+ * @param formData 传入参数
479
+ * @param maps 转换的映射关系,eg:{ date: ['startTime', 'endTime'] }
480
+ * @param filterNullableValue 是否需要过滤掉空数据项
481
+ */
482
+ const formParamsSwitch = (formData, maps, options) => {
483
+ const dateKeys = Object.keys(maps);
484
+ const dateData = {};
485
+ const restData = {};
486
+ Object.keys(formData).forEach(key => {
487
+ if (dateKeys.includes(key)) {
488
+ dateData[key] = formData[key];
489
+ }
490
+ else {
491
+ restData[key] = formData[key];
492
+ }
493
+ });
494
+ const accessKeys = Object.keys(dateData);
495
+ // 如无需要转换的数据时,直接返回原对象
496
+ if (accessKeys.length === 0) {
497
+ return formData;
498
+ }
499
+ const { filterNullable: filterNullableValue, dateFormat } = { filterNullable: true, ...options };
500
+ const handleData = accessKeys.reduce((acc, key) => {
501
+ const values = dateData[key];
502
+ if (!values) {
503
+ return acc;
504
+ }
505
+ const mapValue = maps[key];
506
+ // 数组合并操作/取最后一位操作
507
+ if (mapValue === 'join' || mapValue === 'pop') {
508
+ if (Array.isArray(values) && values.length) {
509
+ acc[key] = mapValue === 'join' ? values.join(',') : values.slice(-1)[0];
510
+ }
511
+ }
512
+ else if (mapValue.length === 2 || mapValue.length === 3) {
513
+ if (!values || values.length !== 2) {
514
+ return acc;
515
+ }
516
+ const [startKey, endKey, format] = mapValue;
517
+ const [startValue, endValue] = values;
518
+ Object.assign(acc, {
519
+ [startKey]: dateFormat instanceof Function ? dateFormat(startValue, format) : startValue,
520
+ [endKey]: dateFormat instanceof Function ? dateFormat(endValue, format) : endValue
521
+ });
522
+ }
523
+ return acc;
524
+ }, {});
525
+ const result = {
526
+ ...restData,
527
+ ...handleData
528
+ };
529
+ return filterNullableValue ? filterNullable(result) : result;
530
+ };
531
+
404
532
  exports.ChinaIdCardValid = ChinaIdCardValid;
405
533
  exports.array2tree = array2tree;
406
534
  exports.awaitWrapper = awaitWrapper;
407
535
  exports.byteParse = byteParse;
408
536
  exports.byteStringify = byteStringify;
409
537
  exports.checkoutBy = checkoutBy;
538
+ exports.filterNullable = filterNullable;
539
+ exports.formParamsSwitch = formParamsSwitch;
410
540
  exports.formulaValidate = formulaValidate;
541
+ exports.isNullable = isNullable;
411
542
  exports.mapToObject = mapToObject;
412
543
  exports.objType = objType;
413
544
  exports.pickRenameKeys = pickRenameKeys;
545
+ exports.safeJsonParse = safeJsonParse;
546
+ exports.switchNullToUndefined = switchNullToUndefined;
414
547
  exports.tree2array = tree2array;
415
548
  exports.wp = wp;
416
549
  exports.wpVuePlugin = wpVuePlugin;