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

Haixing-Hu / js-common-util / 37757a98-aa85-4dfb-bad6-f06a9651a7ac

09 Dec 2023 08:41AM UTC coverage: 57.398% (+1.9%) from 55.49%
37757a98-aa85-4dfb-bad6-f06a9651a7ac

push

circleci

Haixing-Hu
style: fix coding style

264 of 476 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 2 files covered. (50.0%)

4 existing lines in 4 files now uncovered.

283 of 477 relevant lines covered (59.33%)

13.73 hits per line

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

85.37
/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.category) {
33!
49
    case 'array':
50
      // Process each element recursively
51
      return obj.map((v) => removeEmptyFields(v)).filter((v) => v !== undefined);
17✔
52
    case 'map':
UNCOV
53
      return new Map(Array.from(obj, ([k, v]) => [k, removeEmptyFields(v)])
×
NEW
54
        .filter((e) => e[1] !== undefined));
×
55
    case 'set':
56
      return new Set(Array.from(obj, (v) => removeEmptyFields(v))
×
57
        .filter((v) => v !== undefined));
×
58
    default:
59
      if (info.isBuiltIn) {
26✔
60
        return obj;                                   // recursion end point
2✔
61
      } else {
62
        // Create objects of the same type. Note that Object.create() cannot be used.
63
        const result = new obj.constructor();
24✔
64
        Object.keys(obj).forEach((key) => {
24✔
65
          const value = removeEmptyFields(obj[key]);  // Process each attribute recursively
69✔
66
          if (value !== undefined) {
69✔
67
            result[key] = value;
56✔
68
          } else {
69
            delete result[key];
13✔
70
          }
71
        });
72
        return result;
24✔
73
      }
74
  }
75
}
76

77
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