bigint-parse-test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var mocha = require('mocha')
  2. , assert = require('chai').assert
  3. , expect = require('chai').expect
  4. , BigNumber = require('bignumber.js')
  5. ;
  6. describe("Testing native BigInt support: parse", function () {
  7. if (typeof (BigInt) === 'undefined') {
  8. console.log('No native BigInt');
  9. return;
  10. }
  11. var input = '{"big":92233720368547758070,"small":123,"deci":1234567890.0123456,"shortExp":1.79e+308,"longExp":1.7976931348623157e+308}';
  12. it("Should show JSONbig does support parsing native BigInt", function (done) {
  13. var JSONbig = require('../index')({
  14. "useNativeBigInt": true
  15. });
  16. var obj = JSONbig.parse(input);
  17. expect(obj.small, "small int").to.equal(123);
  18. expect(obj.big.toString(), "big int").to.equal("92233720368547758070");
  19. expect(typeof obj.big, "big int").to.equal('bigint');
  20. done();
  21. });
  22. it("Should show JSONbig does support forced parsing to native BigInt", function (done) {
  23. var JSONbig = require('../index')({
  24. "alwaysParseAsBig": true,
  25. "useNativeBigInt": true
  26. });
  27. var obj = JSONbig.parse(input);
  28. expect(obj.big.toString(), "big int").to.equal("92233720368547758070");
  29. expect(typeof obj.big, "big int").to.equal('bigint');
  30. expect(obj.small.toString(), "small int").to.equal("123");
  31. expect(typeof obj.small, "small int").to.equal('bigint');
  32. done();
  33. });
  34. it("Should show JSONbig does support decimal and scientific notation parse/stringify roundtrip", function (done) {
  35. var JSONbig = require('../index')({
  36. "useNativeBigInt": true
  37. });
  38. var obj = JSONbig.parse(input);
  39. expect(obj.deci.toString(), "decimal number").to.equal("1234567890.0123456");
  40. expect(typeof obj.deci, "decimal number").to.equal('number');
  41. expect(obj.shortExp.toString(), "short exponential number").to.equal("1.79e+308");
  42. expect(typeof obj.shortExp, "short exponential number").to.equal('number');
  43. expect(obj.longExp.toString(), "long exponential number").to.equal("1.7976931348623157e+308");
  44. expect(typeof obj.longExp, "long exponential number").to.equal('number');
  45. var output = JSONbig.stringify(obj);
  46. expect(output).to.equal(input);
  47. done();
  48. });
  49. it("Should show JSONbig does support native Bigint parse/stringify roundtrip", function (done) {
  50. var JSONbig = require('../index')({
  51. "useNativeBigInt": true
  52. });
  53. var obj = JSONbig.parse(input);
  54. var output = JSONbig.stringify(obj);
  55. expect(output).to.equal(input);
  56. done();
  57. });
  58. it("Should show JSONbig does support native Bigint parse/stringify roundtrip when BigInt is forced", function (done) {
  59. var JSONbig = require('../index')({
  60. "alwaysParseAsBig": true,
  61. "useNativeBigInt": true
  62. });
  63. var obj = JSONbig.parse(input);
  64. var output = JSONbig.stringify(obj);
  65. expect(output).to.equal(input);
  66. done();
  67. });
  68. });