• 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

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

83
export default emptyFieldsToNull;
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

© 2026 Coveralls, Inc