currency.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Node
  2. if (typeof module !== 'undefined' && module.exports) {
  3. var numeral = require('../../numeral');
  4. var expect = require('chai').expect;
  5. }
  6. describe('Currency', function() {
  7. after(function() {
  8. numeral.reset();
  9. });
  10. it('should format to currency', function() {
  11. var tests = [
  12. [0,'$0.00','$0.00'],
  13. [null,'$0.00','$0.00'],
  14. [0.99,'$0,0.00','$0.99'],
  15. [1000.234,'$0,0.00','$1,000.23'],
  16. [1001,'$ 0,0.[00]','$ 1,001'],
  17. [1000.234,'0,0.00 $','1,000.23 $'],
  18. [-1000.234,'0,0.00 $','-1,000.23 $'],
  19. [-1000.234,'($0,0)','($1,000)'],
  20. [-1000.234,'(0,0$)','(1,000$)'],
  21. [-1000.234,'(0,0 $)','(1,000 $)'],
  22. [-1000.234,'$0.00','-$1000.23'],
  23. [-1000.234,'$ 0.00','-$ 1000.23'],
  24. [1230974,'($0.00 a)','$1.23 m'],
  25. [-1000.234,'$ (0,0)','$ (1,000)'],
  26. [-1000.234,'$(0,0)','$(1,000)'],
  27. [-1000.234,'$ (0,0.00)','$ (1,000.23)'],
  28. [-1000.234,'$(0,0.00)','$(1,000.23)'],
  29. [-1000.238,'$(0,0.00)','$(1,000.24)'],
  30. [-1000.234,'$-0,0','$-1,000'],
  31. [-1000.234,'$ -0,0','$ -1,000'],
  32. [1000.234,'$ (0,0)','$ 1,000'],
  33. [1000.234,'$(0,0)','$1,000'],
  34. [1000.234,'$ (0,0.00)','$ 1,000.23'],
  35. [1000.234,'$(0,0.00)','$1,000.23'],
  36. [1000.238,'$(0,0.00)','$1,000.24'],
  37. [1000.234,'$-0,0','$1,000'],
  38. [1000.234,'$ -0,0','$ 1,000']
  39. ],
  40. i;
  41. for (i = 0; i < tests.length; i++) {
  42. expect(numeral(tests[i][0]).format(tests[i][1])).to.equal(tests[i][2]);
  43. }
  44. });
  45. it('should unformat to currency', function() {
  46. var tests = [
  47. ['$0.00', 0],
  48. ['$0.99', 0.99],
  49. ['$1,000.23', 1000.23],
  50. ['1,000.23 $', 1000.23],
  51. ['($1,000)', -1000],
  52. ['-1,000$', -1000],
  53. ['$1.23 m', 1230000],
  54. ],
  55. i;
  56. for (i = 0; i < tests.length; i++) {
  57. expect(numeral(tests[i][0]).value()).to.equal(tests[i][1]);
  58. }
  59. });
  60. });