Skip to content

Commit 86c14ce

Browse files
SuntgrKikobeats
authored andcommitted
docs: Update README and Demo for BigNumber
1 parent 98d2d1e commit 86c14ce

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import ReactJsonView from '@microlink/react-json-view'
8383
| `validationMessage` | `string` | "Validation Error" | Custom message for validation failures to `onEdit`, `onAdd`, or `onDelete` callbacks. |
8484
| `displayArrayKey` | `boolean` | `true` | When set to `true`, the index of the elements prefix values. |
8585
| `escapeStrings` | `boolean` | `true` | When set to `true`, strings sequences such as \n, \t, \r, \f will be escaped. |
86-
| `bigNumber` | `BigNumber \| Decimal \| Big` | `null` | When set to a BigNumber class (from `bignumber.js`, `decimal.js` or `big.js`), numbers will be parsed and displayed using that class for high precision decimal handling. |
86+
| `bigNumber` | `Class` | `null` | A custom class for handling large numbers. The class should have a constructor that accepts a numeric string/value and a `name` property for display purposes. You can use existing libraries like `bignumber.js`, `decimal.js`, `big.js`, or provide your own implementation. |
8787

8888
#### Callbacks
8989

Diff for: dev-server/src/index.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@ import Moment from 'moment'
99
//import the react-json-view component (installed with npm)
1010
import JsonViewer from './../../src/js/index'
1111

12+
// custom big number class, You can use existing libraries like `bignumber.js`, `decimal.js`, `big.js` etc.
13+
class BigNumber {
14+
name = 'customName'
15+
constructor(value) {
16+
this.value = value
17+
}
18+
toString() {
19+
return this.value.toString()
20+
}
21+
}
22+
1223
//render 2 different examples of the react-json-view component
1324
ReactDom.render(
1425
<div>
1526
{/* just pass in your JSON to the src attribute */}
1627
<JsonViewer
28+
bigNumber={BigNumber}
1729
sortKeys
1830
style={{ padding: '30px', backgroundColor: 'white' }}
1931
src={getExampleJson1()}
@@ -59,6 +71,7 @@ ReactDom.render(
5971
{/* use a base16 theme */}
6072
<JsonViewer
6173
src={getExampleJson1()}
74+
bigNumber={BigNumber}
6275
theme='railscasts'
6376
validationMessage="You're doing something wrong."
6477
collapseStringsAfterLength={15}
@@ -139,6 +152,7 @@ ReactDom.render(
139152
<JsonViewer
140153
enableClipboard={false}
141154
src={getExampleJson1()}
155+
bigNumber={BigNumber}
142156
shouldCollapse={({ src, namespace, type }) =>
143157
namespace.indexOf('moment') > -1
144158
}
@@ -220,7 +234,8 @@ function getExampleJson1 () {
220234
string_number: '1234',
221235
date: new Date(),
222236
moment: Moment(),
223-
regexp: /[0-9]/gi
237+
regexp: /[0-9]/gi,
238+
bigNumber: new BigNumber('0.0060254656709730629123')
224239
}
225240
}
226241

Diff for: src/js/helpers/parseInput.js

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function parseInput (input, bigNumber) {
1616
input.match(/\-?\d+\.\d+/) &&
1717
input.match(/\-?\d+\.\d+/)[0] === input
1818
) {
19+
// big number
1920
if (bigNumber && parseFloat(input).toString() !== input) {
2021
return formatResponse('bigNumber', input)
2122
}
@@ -28,6 +29,7 @@ export default function parseInput (input, bigNumber) {
2829
// scientific float
2930
return formatResponse('float', Number(input))
3031
} else if (input.match(/\-?\d+/) && input.match(/\-?\d+/)[0] === input) {
32+
// big number
3133
if (bigNumber && parseInt(input).toString() !== input) {
3234
return formatResponse('bigNumber', input)
3335
}

Diff for: src/js/helpers/util.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// returns a string "type" of input object
22
export function toType (obj, bigNumber) {
3+
4+
/* Check if the object is an instance of the custom BigNumber class passed in as a prop
5+
* If it matches, return 'bigNumber' type so it can be displayed appropriately
6+
*/
37
if (bigNumber && obj?.constructor?.name === bigNumber?.name) {
48
return 'bigNumber'
59
}

0 commit comments

Comments
 (0)