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

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

pending completion
#1215

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%)

90.19 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();
58✔
7
    try {
58✔
8
        input = JSON.stringify(JSON.parse(input));
58✔
9
        if (input[0] === '[') {
30✔
10
            //array
11
            return formatResponse('array', JSON.parse(input));
4✔
12
        } else if (input[0] === '{') {
26✔
13
            //object
14
            return formatResponse('object', JSON.parse(input));
4✔
15
        } else if (
22✔
16
            input.match(/\-?\d+\.\d+/) &&
26✔
17
            input.match(/\-?\d+\.\d+/)[0] === input
18
        ) {
19
            //float
20
            return formatResponse('float', parseFloat(input));
4✔
21
        } else if (
18✔
22
            input.match(/\-?\d+e-\d+/) &&
22✔
23
            input.match(/\-?\d+e-\d+/)[0] === input
24
        ) {
25
            //scientific float
26
            return formatResponse('float', Number(input));
4✔
27
        } else if (
14✔
28
            input.match(/\-?\d+/) &&
22✔
29
            input.match(/\-?\d+/)[0] === input
30
        ) {
31
            //integer
32
            return formatResponse('integer', parseInt(input));
8✔
33
        } else if (
6!
34
            input.match(/\-?\d+e\+\d+/) &&
6!
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();
34✔
46
    switch (input) {
34✔
47
        case 'undefined': {
48
            return formatResponse('undefined', undefined);
4✔
49
        }
50
        case 'nan': {
51
            return formatResponse('nan', NaN);
6✔
52
        }
53
        case 'null': {
54
            return formatResponse('null', null);
4✔
55
        }
56
        case 'true': {
57
            return formatResponse('boolean', true);
2✔
58
        }
59
        case 'false': {
60
            return formatResponse('boolean', false);
2✔
61
        }
62
        default: {
63
            //check to see if this is a date
64
            input = Date.parse(input);
16✔
65
            if (input) {
16✔
66
                return formatResponse('date', new Date(input));
2✔
67
            }
68
        }
69
    }
70

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

74
function formatResponse(type, value) {
75
    return {
58✔
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