number.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var test = require('tape');
  2. var v = require('es-value-fixtures');
  3. var forEach = require('for-each');
  4. var inspect = require('../');
  5. test('negative zero', function (t) {
  6. t.equal(inspect(0), '0', 'inspect(0) === "0"');
  7. t.equal(inspect(Object(0)), 'Object(0)', 'inspect(Object(0)) === "Object(0)"');
  8. t.equal(inspect(-0), '-0', 'inspect(-0) === "-0"');
  9. t.equal(inspect(Object(-0)), 'Object(-0)', 'inspect(Object(-0)) === "Object(-0)"');
  10. t.end();
  11. });
  12. test('numericSeparator', function (t) {
  13. forEach(v.nonBooleans, function (nonBoolean) {
  14. t['throws'](
  15. function () { inspect(true, { numericSeparator: nonBoolean }); },
  16. TypeError,
  17. inspect(nonBoolean) + ' is not a boolean'
  18. );
  19. });
  20. t.test('3 digit numbers', function (st) {
  21. var failed = false;
  22. for (var i = -999; i < 1000; i += 1) {
  23. var actual = inspect(i);
  24. var actualSepNo = inspect(i, { numericSeparator: false });
  25. var actualSepYes = inspect(i, { numericSeparator: true });
  26. var expected = String(i);
  27. if (actual !== expected || actualSepNo !== expected || actualSepYes !== expected) {
  28. failed = true;
  29. t.equal(actual, expected);
  30. t.equal(actualSepNo, expected);
  31. t.equal(actualSepYes, expected);
  32. }
  33. }
  34. st.notOk(failed, 'all 3 digit numbers passed');
  35. st.end();
  36. });
  37. t.equal(inspect(1e3), '1000', '1000');
  38. t.equal(inspect(1e3, { numericSeparator: false }), '1000', '1000, numericSeparator false');
  39. t.equal(inspect(1e3, { numericSeparator: true }), '1_000', '1000, numericSeparator true');
  40. t.equal(inspect(-1e3), '-1000', '-1000');
  41. t.equal(inspect(-1e3, { numericSeparator: false }), '-1000', '-1000, numericSeparator false');
  42. t.equal(inspect(-1e3, { numericSeparator: true }), '-1_000', '-1000, numericSeparator true');
  43. t.equal(inspect(1234.5678, { numericSeparator: true }), '1_234.567_8', 'fractional numbers get separators');
  44. t.equal(inspect(1234.56789, { numericSeparator: true }), '1_234.567_89', 'fractional numbers get separators');
  45. t.equal(inspect(1234.567891, { numericSeparator: true }), '1_234.567_891', 'fractional numbers get separators');
  46. t.end();
  47. });