@wxhccc/es-util 1.3.1 → 1.5.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.
@@ -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.3.0
2
+ * @wxhccc/es-util v1.5.1
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,78 +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
- corePromsie = checkLock() ? Promise.reject(new Error('you can\'t recall promise method when it\'s locking')).catch(() => undefined) : wrap ? awaitWrapper(promise()) : promise();
395
+ corePromsie = ignoreLock ? emptyPromise() : wrap ? awaitWrapper(promise()) : promise();
358
396
  }
359
397
  else {
360
398
  corePromsie = wrap ? awaitWrapper(promise) : promise;
361
399
  }
400
+ const unlock = () => stateLock(false);
362
401
  Object.defineProperties(corePromsie, {
363
- __lockValue: { get: checkLock },
364
- lock: {
365
- value: (keyOrHookOrHandle, syncRefHandle) => {
366
- const isRefHandle = (val) => Array.isArray(val) && val.length === 2;
367
- if (typeof keyOrHookOrHandle === 'string') {
368
- lockKey = keyOrHookOrHandle.split('.');
369
- }
370
- else if (isRefHandle(keyOrHookOrHandle)) {
371
- lockRefHandle = keyOrHookOrHandle;
372
- }
373
- else if (typeof keyOrHookOrHandle === 'function') {
374
- lockSwitchHook = keyOrHookOrHandle;
375
- if (isRefHandle(syncRefHandle)) {
376
- lockRefHandle = syncRefHandle;
377
- }
378
- }
379
- stateLock(true);
380
- return corePromsie;
381
- }
382
- }
402
+ '__lockValue': { get: checkLock },
403
+ _checkLockKey: { value: (key) => lockCtx[key] },
404
+ unlock: { value: unlock }
383
405
  });
384
- corePromsie.finally(() => stateLock(false));
406
+ if (needLock && !ignoreLock) {
407
+ stateLock(true);
408
+ !manualUnlock && corePromsie.finally(unlock);
409
+ }
385
410
  return corePromsie;
386
411
  }
412
+ const wp = Object.defineProperty(wrapPromise, '_checkLockKey', { value: (key) => lockCtx[key] });
387
413
  const wpVuePlugin = {
388
- install(appOrVue) {
414
+ install(appOrVue, key = '$wp') {
389
415
  if (appOrVue.config && 'globalProperties' in appOrVue.config) {
390
- appOrVue.config.globalProperties.$wp = wp;
416
+ appOrVue.config.globalProperties[key] = wrapPromise;
417
+ }
418
+ else {
419
+ appOrVue.prototype[key] = wrapPromise;
420
+ }
421
+ }
422
+ };
423
+
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];
391
485
  }
392
486
  else {
393
- appOrVue.prototype.$wp = wp;
487
+ restData[key] = formData[key];
394
488
  }
489
+ });
490
+ const accessKeys = Object.keys(dateData);
491
+ // 如无需要转换的数据时,直接返回原对象
492
+ if (accessKeys.length === 0) {
493
+ return formData;
395
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;
396
526
  };
397
527
 
398
- export { ChinaIdCardValid, array2tree, awaitWrapper, byteParse, byteStringify, checkoutBy, formulaValidate, mapToObject, objType, pickRenameKeys, tree2array, wp, wpVuePlugin };
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.3.0
2
+ * @wxhccc/es-util v1.5.1
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,78 +357,176 @@ 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
- corePromsie = checkLock() ? Promise.reject(new Error('you can\'t recall promise method when it\'s locking')).catch(() => undefined) : wrap ? awaitWrapper(promise()) : promise();
399
+ corePromsie = ignoreLock ? emptyPromise() : wrap ? awaitWrapper(promise()) : promise();
362
400
  }
363
401
  else {
364
402
  corePromsie = wrap ? awaitWrapper(promise) : promise;
365
403
  }
404
+ const unlock = () => stateLock(false);
366
405
  Object.defineProperties(corePromsie, {
367
- __lockValue: { get: checkLock },
368
- lock: {
369
- value: (keyOrHookOrHandle, syncRefHandle) => {
370
- const isRefHandle = (val) => Array.isArray(val) && val.length === 2;
371
- if (typeof keyOrHookOrHandle === 'string') {
372
- lockKey = keyOrHookOrHandle.split('.');
373
- }
374
- else if (isRefHandle(keyOrHookOrHandle)) {
375
- lockRefHandle = keyOrHookOrHandle;
376
- }
377
- else if (typeof keyOrHookOrHandle === 'function') {
378
- lockSwitchHook = keyOrHookOrHandle;
379
- if (isRefHandle(syncRefHandle)) {
380
- lockRefHandle = syncRefHandle;
381
- }
382
- }
383
- stateLock(true);
384
- return corePromsie;
385
- }
386
- }
406
+ '__lockValue': { get: checkLock },
407
+ _checkLockKey: { value: (key) => lockCtx[key] },
408
+ unlock: { value: unlock }
387
409
  });
388
- corePromsie.finally(() => stateLock(false));
410
+ if (needLock && !ignoreLock) {
411
+ stateLock(true);
412
+ !manualUnlock && corePromsie.finally(unlock);
413
+ }
389
414
  return corePromsie;
390
415
  }
416
+ const wp = Object.defineProperty(wrapPromise, '_checkLockKey', { value: (key) => lockCtx[key] });
391
417
  const wpVuePlugin = {
392
- install(appOrVue) {
418
+ install(appOrVue, key = '$wp') {
393
419
  if (appOrVue.config && 'globalProperties' in appOrVue.config) {
394
- appOrVue.config.globalProperties.$wp = wp;
420
+ appOrVue.config.globalProperties[key] = wrapPromise;
421
+ }
422
+ else {
423
+ appOrVue.prototype[key] = wrapPromise;
424
+ }
425
+ }
426
+ };
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];
395
489
  }
396
490
  else {
397
- appOrVue.prototype.$wp = wp;
491
+ restData[key] = formData[key];
398
492
  }
493
+ });
494
+ const accessKeys = Object.keys(dateData);
495
+ // 如无需要转换的数据时,直接返回原对象
496
+ if (accessKeys.length === 0) {
497
+ return formData;
399
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;
400
530
  };
401
531
 
402
532
  exports.ChinaIdCardValid = ChinaIdCardValid;
@@ -405,10 +535,15 @@ exports.awaitWrapper = awaitWrapper;
405
535
  exports.byteParse = byteParse;
406
536
  exports.byteStringify = byteStringify;
407
537
  exports.checkoutBy = checkoutBy;
538
+ exports.filterNullable = filterNullable;
539
+ exports.formParamsSwitch = formParamsSwitch;
408
540
  exports.formulaValidate = formulaValidate;
541
+ exports.isNullable = isNullable;
409
542
  exports.mapToObject = mapToObject;
410
543
  exports.objType = objType;
411
544
  exports.pickRenameKeys = pickRenameKeys;
545
+ exports.safeJsonParse = safeJsonParse;
546
+ exports.switchNullToUndefined = switchNullToUndefined;
412
547
  exports.tree2array = tree2array;
413
548
  exports.wp = wp;
414
549
  exports.wpVuePlugin = wpVuePlugin;