• 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

79.41
/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 'object':
35
      switch (info.subtype) {
20!
36
        case 'Map':
37
        case 'Set':
38
        case 'WeakMap':
39
        case 'WeakSet':
UNCOV
40
          return [...value];
×
41
        default:
42
          return value;
20✔
43
      }
44
    default:
45
      return String(value);
1✔
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