Skip to content

Commit 64a6e8f

Browse files
committed
add BigInt support
1 parent 2feadce commit 64a6e8f

File tree

9 files changed

+73
-6
lines changed

9 files changed

+73
-6
lines changed

dev-server/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ function getExampleJson1() {
181181
return {
182182
string: 'this is a test string',
183183
integer: 42,
184+
bigint: BigInt('123456789012345678901234567890'),
184185
empty_array: [],
185186
empty_object: {},
186187
array: [1, 2, 3, 'test'],

package-lock.json

+34-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "dist/main.js",
66
"dependencies": {
77
"flux": "^4.0.1",
8+
"json-bigint": "^1.0.0",
89
"react-base16-styling": "^0.6.0",
910
"react-lifecycles-compat": "^3.0.4",
1011
"react-textarea-autosize": "^8.3.2"

src/js/components/CopyToClipboard.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import JSONBig from 'json-bigint';
23

34
import { toType } from './../helpers/util';
45

@@ -29,7 +30,7 @@ export default class extends React.PureComponent {
2930
const container = document.createElement('textarea');
3031
const { clickCallback, src, namespace } = this.props;
3132

32-
container.innerHTML = JSON.stringify(
33+
container.innerHTML = JSONBig.stringify(
3334
this.clipboardValue(src),
3435
null,
3536
' '

src/js/components/DataTypes/BigInt.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import DataTypeLabel from './DataTypeLabel';
3+
4+
//theme
5+
import Theme from './../../themes/getStyle';
6+
7+
export default class extends React.PureComponent {
8+
render() {
9+
const type_name = 'bigint';
10+
const { props } = this;
11+
return (
12+
<div {...Theme(props.theme, 'integer')}>
13+
<DataTypeLabel type_name={type_name} {...props} />
14+
{this.props.value.toString()}
15+
</div>
16+
);
17+
}
18+
}

src/js/components/DataTypes/DataTypes.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export { default as JsonFunction } from './Function';
55
export { default as JsonNan } from './Nan';
66
export { default as JsonNull } from './Null';
77
export { default as JsonInteger } from './Integer';
8+
export { default as JsonBigInt } from './BigInt';
89
export { default as JsonObject } from './Object';
910
export { default as JsonRegexp } from './Regexp';
1011
export { default as JsonString } from './String';

src/js/components/VariableEditor.js

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
JsonFloat,
1515
JsonFunction,
1616
JsonInteger,
17+
JsonBigInt,
1718
JsonNan,
1819
JsonNull,
1920
JsonRegexp,
@@ -226,6 +227,8 @@ class VariableEditor extends React.PureComponent {
226227
return <JsonString value={variable.value} {...props} />;
227228
case 'integer':
228229
return <JsonInteger value={variable.value} {...props} />;
230+
case 'bigint':
231+
return <JsonBigInt value={variable.value} {...props} />;
229232
case 'float':
230233
return <JsonFloat value={variable.value} {...props} />;
231234
case 'boolean':
@@ -436,6 +439,8 @@ class VariableEditor extends React.PureComponent {
436439
return <JsonString value={value} {...props} />;
437440
case 'integer':
438441
return <JsonInteger value={value} {...props} />;
442+
case 'bigint':
443+
return <JsonBigInt value={value} {...props} />;
439444
case 'float':
440445
return <JsonFloat value={value} {...props} />;
441446
case 'boolean':

src/js/helpers/parseInput.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import JSONBig from 'json-bigint';
2+
13
export default function parseInput(input) {
24
//following code is to make a best guess at
35
//the type for a variable being submitted.
46

57
//we are working with a serialized data representation
68
input = input.trim();
79
try {
8-
input = JSON.stringify(JSON.parse(input));
10+
input = JSONBig.stringify(JSONBig.parse(input));
911
if (input[0] === '[') {
1012
//array
11-
return formatResponse('array', JSON.parse(input));
13+
return formatResponse('array', JSONBig({ useNativeBigInt: true }).parse(input));
1214
} else if (input[0] === '{') {
1315
//object
14-
return formatResponse('object', JSON.parse(input));
16+
return formatResponse('object', JSONBig({ useNativeBigInt: true }).parse(input));
1517
} else if (
1618
input.match(/\-?\d+\.\d+/) &&
1719
input.match(/\-?\d+\.\d+/)[0] === input
@@ -28,6 +30,10 @@ export default function parseInput(input) {
2830
input.match(/\-?\d+/) &&
2931
input.match(/\-?\d+/)[0] === input
3032
) {
33+
//bigint
34+
if (Number(input) > Number.MAX_SAFE_INTEGER) {
35+
return formatResponse('bigint', BigInt(input));
36+
}
3137
//integer
3238
return formatResponse('integer', parseInt(input));
3339
} else if (

src/js/helpers/stringifyVariable.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { toType } from './util';
2+
import JSONBig from 'json-bigint';
23

34
export default value => {
45
const type = toType(value);
@@ -25,7 +26,7 @@ export default value => {
2526
break;
2627
default: {
2728
try {
28-
string_value = JSON.stringify(value, null, ' ');
29+
string_value = JSONBig.stringify(value, null, ' ');
2930
} catch (e) {
3031
string_value = '';
3132
}

0 commit comments

Comments
 (0)