• 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

88.24
/src/json-stringify.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
import jsonBeautify from 'json-beautify';
11
import decycle from './decycle';
12

13
/**
14
 * Replacer for JSON serialization of Set, Map, BigInt objects.
15
 *
16
 * @param {String} key
17
 *     The current primary key.
18
 * @param {any} value
19
 *     current value.
20
 * @return {any}
21
 *     The value to be replaced.
22
 * @author Haixing Hu
23
 * @private
24
 */
25
function replacer(key, value) {
26
  const info = typeInfo(value);
70✔
27
  switch (info.type) {
70!
28
    case 'null':
29
    case 'undefined':
30
    case 'string':
31
    case 'boolean':
32
    case 'number':
33
      return value;
49✔
34
    case 'bigint':
35
      return `${String(value)}n`;
1✔
36
    case 'object':
37
      switch (info.subtype) {
20✔
38
        case 'Map':
39
        case 'Set':
40
          return [...value];
2✔
41
        default:
42
          return value;
18✔
43
      }
44
    default:
UNCOV
45
      return String(value);
×
46
  }
47
}
48

49
/**
50
 * Serialize a value to a JSON string.
51
 *
52
 * This function functions similarly to `JSON.stringify()`, but supports some
53
 * built-in objects above ES6 and can format the printed JSON serialization
54
 * result string according to the arguments.
55
 *
56
 * @param {any} value
57
 *     The value.
58
 * @param {boolean} beautify
59
 *     Optional, indicating whether to beautify the output JSON string. The
60
 *     default value is `false`.
61
 * @return {String}
62
 *     The JSON string of the serialization of the specified value, which will
63
 *     be formatted if the `beautify` argument is `true`.
64
 * @author Haixing Hu
65
 */
66
function jsonStringify(value, beautify = false) {
10✔
67
  if (value === undefined) {
17✔
68
    return 'undefined';
1✔
69
  } else if (value === null) {
16✔
70
    return 'null';
1✔
71
  }
72
  value = decycle(value);  // Convert objects that may have circular references
15✔
73
  if (beautify) {
15✔
74
    return jsonBeautify(value, replacer, 2, 80);
4✔
75
  } else {
76
    return JSON.stringify(value, replacer);
11✔
77
  }
78
}
79

80
export default jsonStringify;
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