• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Haixing-Hu / js-common-util / 1e98184f-0522-4515-a343-e44e919e1313

08 Dec 2023 07:22AM UTC coverage: 55.49% (-10.3%) from 65.775%
1e98184f-0522-4515-a343-e44e919e1313

push

circleci

Haixing-Hu
style: fix coding style

274 of 513 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

47 existing lines in 7 files now uncovered.

287 of 498 relevant lines covered (57.63%)

12.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.16
/src/remove-empty-fields.js
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
//    Copyright (c) 2022 - 2023.
4
//    Haixing Hu, Qubit Co. Ltd.
5
//
6
//    All rights reserved.
7
//
8
////////////////////////////////////////////////////////////////////////////////
9
import typeInfo from '@haixing_hu/typeinfo';
10

11
/**
12
 * Create a new object based on an object, but remove all its attribute values
13
 * that are empty strings or `null`.
14
 *
15
 * @param {any} obj
16
 *    The object or value to be converted.
17
 * @return {any}
18
 *    - If `obj` is `undefined`, returns `undefined`;
19
 *    - If `obj` is `null`, returns `undefined`;
20
 *    - If `obj` is a string, returns `undefined` if it is an empty string, and
21
 *      returns `obj` for other strings;
22
 *    - If `obj` is a `number`, `boolean`, `bigint`, `symbol` or `function`,
23
 *      returns `obj`;
24
 *    - If `obj` is a `Array`, `Set`, `WeakSet`, `Map`, or `WeakMap`, a shallow
25
 *      copy of `obj` is returned;
26
 *    - Otherwise, returns a copy of `obj`, but change all its attribute values
27
 *      that are empty strings to `undefined`.
28
 * @author Haixing Hu
29
 */
30
function removeEmptyFields(obj) {
31
  const info = typeInfo(obj);
108✔
32
  switch (info.type) {
108✔
33
    case 'undefined':       // fall down
34
    case 'null':
35
      return undefined;
8✔
36
    case 'string':
37
      return (obj === '' ? undefined : obj);    // recursion end point
42✔
38
    case 'boolean':         // fall down
39
    case 'number':          // fall down
40
    case 'bigint':          // fall down
41
    case 'symbol':          // fall down
42
    case 'function':
43
      return obj;
25✔
44
    case 'object':          // fall down
45
    default:
46
      break;
33✔
47
  }
48
  switch (info.subtype) {
33!
49
    case 'Array':
50
      // Process each element recursively
51
      return obj.map((v) => removeEmptyFields(v)).filter((v) => v !== undefined);
17✔
52
    case 'Map':
53
    case 'WeakMap': {
UNCOV
54
      const result = new Map();
×
UNCOV
55
      for (const key of obj.keys()) {
×
UNCOV
56
        const value = obj.get(key);
×
UNCOV
57
        const newValue = removeEmptyFields(value);      // Process each element recursively
×
UNCOV
58
        if (newValue !== undefined) {
×
UNCOV
59
          result.set(key, newValue);
×
60
        }
61
      }
UNCOV
62
      return result;
×
63
    }
64
    case 'Set':
65
    case 'WeakSet': {
UNCOV
66
      const result = new Set();
×
UNCOV
67
      for (const value of obj.values()) {
×
UNCOV
68
        const newValue = removeEmptyFields(value);      // Process each element recursively
×
UNCOV
69
        if (newValue !== undefined) {
×
UNCOV
70
          result.add(newValue);
×
71
        }
72
      }
73
      return result;
×
74
    }
75
    case 'Object':
76
    default:
77
      if (info.isBuiltIn) {
26✔
78
        return obj;                                   // recursion end point
2✔
79
      } else {
80
        // Create objects of the same type. Note that Object.create() cannot be used.
81
        const result = new obj.constructor();
24✔
82
        Object.keys(obj).forEach((key) => {
24✔
83
          const value = removeEmptyFields(obj[key]);  // Process each attribute recursively
69✔
84
          if (value !== undefined) {
69✔
85
            result[key] = value;
56✔
86
          } else {
87
            delete result[key];
13✔
88
          }
89
        });
90
        return result;
24✔
91
      }
92
  }
93
}
94

95
export default removeEmptyFields;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc