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

Haixing-Hu / js-common-util / 5c83102e-ef35-409d-ab60-119010727bfd

20 Dec 2023 02:52PM UTC coverage: 52.114% (-1.2%) from 53.283%
5c83102e-ef35-409d-ab60-119010727bfd

push

circleci

Haixing-Hu
fix: fix the `emptyFieldsToNull()` function

217 of 451 branches covered (0.0%)

Branch coverage included in aggregate %.

11 of 22 new or added lines in 1 file covered. (50.0%)

276 of 495 relevant lines covered (55.76%)

13.2 hits per line

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

54.0
/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
 * Creates a new object based on an existing object, but sets all its empty
13
 * properties values 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);
122✔
31
  switch (info.category) {
122!
32
    case 'string':
33
    case 'typed-array':
34
      return (obj.length === 0 ? null : obj);         // recursion end point
46✔
35
    case 'array':
36
      if (obj.length === 0) {
12!
NEW
37
        return null;                                  // recursion end point
×
38
      } else {
39
        return obj.map((e) => emptyFieldsToNull(e));  // recursion
30✔
40
      }
41
    case 'map':
NEW
42
      if (obj.size === 0) {
×
NEW
43
        return null;
×
44
      } else {
NEW
45
        return new Map(Array.from(obj, ([k, v]) => [k, emptyFieldsToNull(v)])); // recursion
×
46
      }
47
    case 'set':
NEW
48
      if (obj.size === 0) {
×
NEW
49
        return null;
×
50
      } else {
NEW
51
        return new Set(Array.from(obj, (v) => emptyFieldsToNull(v))); // recursion
×
52
      }
53
    case 'object':
54
    case 'class': {
55
      let empty = false;
27✔
56
      if (typeof obj.isEmpty === 'function') {
27!
NEW
57
        empty = obj.isEmpty();
×
58
      } else if (typeof obj.length === 'number') {
27!
NEW
59
        empty = (obj.length === 0);
×
60
      } else if (typeof obj.size === 'number') {
27!
NEW
61
        empty = (obj.size === 0);
×
62
      }
63
      if (empty) {
27!
NEW
64
        return null;
×
65
      }
66
      // Create objects of the same type.
67
      // Note that Object.create() cannot be used.
68
      const result = new obj.constructor();
27✔
69
      Object.keys(obj).forEach((key) => {
27✔
70
        result[key] = emptyFieldsToNull(obj[key]);  // recursion
63✔
71
      });
72
      return result;
27✔
73
    }
74
    default:
75
      return obj;
37✔
76
  }
77
}
78

79
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