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

mac-s-g / react-json-view / #1276

09 Mar 2021 03:27AM CUT coverage: 83.298%. Remained the same
#1276

push

web-flow
Merge pull request #342 from mac-s-g/bump-patch

bump patch version

284 of 349 branches covered (81.38%)

Branch coverage included in aggregate %.

504 of 597 relevant lines covered (84.42%)

45.1 hits per line

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

94.55
/src/js/helpers/parseInput.js
1
export default function parseInput(input) {
2
    //following code is to make a best guess at
3
    //the type for a variable being submitted.
4

5
    //we are working with a serialized data representation
6
    input = input.trim();
29✔
7
    try {
29✔
8
        input = JSON.stringify(JSON.parse(input));
29✔
9
        if (input[0] === '[') {
15✔
10
            //array
11
            return formatResponse('array', JSON.parse(input));
2✔
12
        } else if (input[0] === '{') {
13✔
13
            //object
14
            return formatResponse('object', JSON.parse(input));
2✔
15
        } else if (
11✔
16
            input.match(/\-?\d+\.\d+/) &&
13✔
17
            input.match(/\-?\d+\.\d+/)[0] === input
18
        ) {
19
            //float
20
            return formatResponse('float', parseFloat(input));
2✔
21
        } else if (
9✔
22
            input.match(/\-?\d+e-\d+/) &&
11✔
23
            input.match(/\-?\d+e-\d+/)[0] === input
24
        ) {
25
            //scientific float
26
            return formatResponse('float', Number(input));
2✔
27
        } else if (
7✔
28
            input.match(/\-?\d+/) &&
11✔
29
            input.match(/\-?\d+/)[0] === input
30
        ) {
31
            //integer
32
            return formatResponse('integer', parseInt(input));
4✔
33
        } else if (
3!
34
            input.match(/\-?\d+e\+\d+/) &&
3!
35
            input.match(/\-?\d+e\+\d+/)[0] === input
36
        ) {
37
            //scientific integer
38
            return formatResponse('integer', Number(input));
×
39
        }
40
    } catch (e) {
41
        // no-op
42
    }
43

44
    //run in case input was not serializable
45
    input = input.toLowerCase();
17✔
46
    switch (input) {
17✔
47
        case 'undefined': {
48
            return formatResponse('undefined', undefined);
2✔
49
        }
50
        case 'nan': {
51
            return formatResponse('nan', NaN);
3✔
52
        }
53
        case 'null': {
54
            return formatResponse('null', null);
2✔
55
        }
56
        case 'true': {
57
            return formatResponse('boolean', true);
1✔
58
        }
59
        case 'false': {
60
            return formatResponse('boolean', false);
1✔
61
        }
62
        default: {
63
            //check to see if this is a date
64
            input = Date.parse(input);
8✔
65
            if (input) {
8✔
66
                return formatResponse('date', new Date(input));
1✔
67
            }
68
        }
69
    }
70

71
    return formatResponse(false, null);
7✔
72
}
73

74
function formatResponse(type, value) {
75
    return {
29✔
76
        type: type,
77
        value: value
78
    };
79
}
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