-
Notifications
You must be signed in to change notification settings - Fork 60
feat: Enhance VariableEditor to support BigNumber type handling #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react' | ||
import DataTypeLabel from './DataTypeLabel' | ||
|
||
// theme | ||
import Theme from './../../themes/getStyle' | ||
|
||
export default class extends React.PureComponent { | ||
render () { | ||
const type_name = 'bigNumber' | ||
const { props } = this | ||
return ( | ||
<div {...Theme(props.theme, 'bigNumber')}> | ||
<DataTypeLabel type_name={type_name} {...props} /> | ||
{this.props.value.toString()} | ||
</div> | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,12 +245,12 @@ class RjvObject extends React.PureComponent { | |
} | ||
|
||
keys.forEach(name => { | ||
variable = new JsonVariable(name, variables[name]) | ||
variable = new JsonVariable(name, variables[name], props.bigNumber) | ||
|
||
if (parent_type === 'array_group' && index_offset) { | ||
variable.name = parseInt(variable.name) + index_offset | ||
} | ||
if (!variables.hasOwnProperty(name)) { | ||
if (!Object.prototype.hasOwnProperty.call(variables, name)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Suntgr what's the motivation for this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variables might be a special object (like a json-bigint instance) that either doesn't inherit methods from Object.prototype, or has its hasOwnProperty method overridden/deleted. example: const JSONbig = require('json-bigint')
<JsonViewer
bigNumber={BigNumber}
src={{a: JSONbig.parse('{"c": 900719977777777999999999123}')}}
... |
||
} else if (variable.type === 'object') { | ||
elements.push( | ||
<JsonObject | ||
|
@@ -286,6 +286,7 @@ class RjvObject extends React.PureComponent { | |
/> | ||
) | ||
} else { | ||
// include bigNumber | ||
elements.push( | ||
<VariableEditor | ||
key={variable.name + '_' + namespace} | ||
|
@@ -305,10 +306,10 @@ class RjvObject extends React.PureComponent { | |
|
||
// just store name, value and type with a variable | ||
class JsonVariable { | ||
constructor (name, value) { | ||
constructor (name, value, bigNumber) { | ||
this.name = name | ||
this.value = value | ||
this.type = toType(value) | ||
this.type = toType(value, bigNumber) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
export default function parseInput (input) { | ||
export default function parseInput (input, bigNumber) { | ||
// following code is to make a best guess at | ||
// the type for a variable being submitted. | ||
|
||
// we are working with a serialized data representation | ||
input = input.trim() | ||
try { | ||
input = JSON.stringify(JSON.parse(input)) | ||
input = structuredClone(input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great |
||
if (input[0] === '[') { | ||
// array | ||
return formatResponse('array', JSON.parse(input)) | ||
|
@@ -16,6 +16,10 @@ export default function parseInput (input) { | |
input.match(/\-?\d+\.\d+/) && | ||
input.match(/\-?\d+\.\d+/)[0] === input | ||
) { | ||
// big number | ||
if (bigNumber && parseFloat(input).toString() !== input) { | ||
return formatResponse('bigNumber', input) | ||
} | ||
// float | ||
return formatResponse('float', parseFloat(input)) | ||
} else if ( | ||
|
@@ -25,6 +29,10 @@ export default function parseInput (input) { | |
// scientific float | ||
return formatResponse('float', Number(input)) | ||
} else if (input.match(/\-?\d+/) && input.match(/\-?\d+/)[0] === input) { | ||
// big number | ||
if (bigNumber && parseInt(input).toString() !== input) { | ||
return formatResponse('bigNumber', input) | ||
} | ||
// integer | ||
return formatResponse('integer', parseInt(input)) | ||
} else if ( | ||
|
Uh oh!
There was an error while loading. Please reload this page.