methods.js 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. $(document).ready(function() {
  2. module("Library Methods");
  3. test("accounting.unformat()", function() {
  4. equals(accounting.unformat("$12,345,678.90 USD"), 12345678.9, 'Can unformat currency to float');
  5. equals(accounting.unformat(1234567890), 1234567890, 'Returns same value when passed an integer');
  6. equals(accounting.unformat("string"), 0, 'Returns 0 for a string with no numbers');
  7. equals(accounting.unformat({joss:1}), 0, 'Returns 0 for object');
  8. accounting.settings.number.decimal = ',';
  9. equals(accounting.unformat("100,00"), 100, 'Uses decimal separator from settings');
  10. equals(accounting.unformat("¤1.000,00"), 1000, 'Uses decimal separator from settings');
  11. accounting.settings.number.decimal = '.';
  12. });
  13. test("accounting.toFixed()", function() {
  14. equals(accounting.toFixed(54321, 5), "54321.00000", 'Performs basic float zero-padding');
  15. equals(accounting.toFixed(0.615, 2), "0.62", 'Rounds 0.615 to "0.62" instead of "0.61"');
  16. });
  17. test("accounting.formatNumber()", function() {
  18. // Check custom precision and separators:
  19. equals(accounting.formatNumber(4999.99, 2, ".", ","), "4.999,99", 'Custom precision and decimal/thousand separators are a-ok')
  20. // check usage with options object parameter:
  21. equal(accounting.formatNumber(5318008, {
  22. precision : 3,
  23. thousand : "__",
  24. decimal : "--"
  25. }), "5__318__008--000", 'Correctly handles custom precision and separators passed in via second param options object');
  26. // check rounding:
  27. equals(accounting.formatNumber(0.615, 2), "0.62", 'Rounds 0.615 up to "0.62" with precision of 2');
  28. // manually and recursively formatted arrays should have same values:
  29. var numbers = [8008135, [1234, 5678], 1000];
  30. var formattedManually = [accounting.formatNumber(8008135), [accounting.formatNumber(1234), accounting.formatNumber(5678)], accounting.formatNumber(1000)];
  31. var formattedRecursively = accounting.formatNumber(numbers);
  32. equals(formattedRecursively.toString(), formattedManually.toString(), 'can recursively format multi-dimensional arrays');
  33. });
  34. test("accounting.formatMoney()", function() {
  35. equals(accounting.formatMoney(12345678), "$12,345,678.00", "Default usage with default parameters is ok");
  36. equals(accounting.formatMoney(4999.99, "$ ", 2, ".", ","), "$ 4.999,99", 'custom formatting via straight params works ok');
  37. equals(accounting.formatMoney(-500000, "£ ", 0), "£ -500,000", 'negative values, custom params, works ok');
  38. equals(accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" }), "5,318,008.00 GBP", "`format` parameter is observed in string output");
  39. equals(accounting.formatMoney(1000, { format: "test %v 123 %s test" }), "test 1,000.00 123 $ test", "`format` parameter is observed in string output, despite being rather strange");
  40. // Format param is an object:
  41. var format = {
  42. pos: "%s %v",
  43. neg: "%s (%v)",
  44. zero:"%s --"
  45. }
  46. equals(accounting.formatMoney(0, { symbol: "GBP", format:format}), "GBP --", "`format` parameter provided given as an object with `zero` format, correctly observed in string output");
  47. equals(accounting.formatMoney(-1000, { symbol: "GBP", format:format}), "GBP (1,000.00)", "`format` parameter provided given as an object with `neg` format, correctly observed in string output");
  48. equals(accounting.formatMoney(1000, { symbol: "GBP", format:{neg:"--%v %s"}}), "GBP1,000.00", "`format` parameter provided, but only `neg` value provided - positive value should be formatted by default format (%s%v)");
  49. accounting.settings.currency.format = "%s%v";
  50. accounting.formatMoney(0, {format:""});
  51. equals(typeof accounting.settings.currency.format, "object", "`settings.currency.format` default string value should be reformatted to an object, the first time it is used");
  52. });
  53. test("accounting.formatColumn()", function() {
  54. // standard usage:
  55. var list = [123, 12345];
  56. equals(accounting.formatColumn(list, "$ ", 0).toString(), (["$ 123", "$ 12,345"]).toString(), "formatColumn works as expected");
  57. // multi-dimensional array (formatColumn should be applied recursively):
  58. var list = [[1, 100], [900, 9]];
  59. equals(accounting.formatColumn(list).toString(), ([["$ 1.00", "$100.00"], ["$900.00", "$ 9.00"]]).toString(), "formatcolumn works on multi-dimensional array");
  60. // random numbers, must come back same length:
  61. var column = accounting.formatColumn([Math.random(), Math.random() * 1000, Math.random() * 10000000]);
  62. ok((column[0].length === column[2].length && column[1].length === column[2].length), "formatColumn() with 3 random numbers returned strings of matching length");
  63. // random numbers, must come back same length:
  64. var column = accounting.formatColumn([Math.random(), Math.random() * 1000, Math.random() * 10000000], {
  65. format: '(%v] --++== %s',
  66. thousand: ')(',
  67. decimal: ')[',
  68. precision: 3
  69. });
  70. ok((column[0].length === column[2].length && column[1].length === column[2].length), "formatColumn() with 3 random numbers returned strings of matching length, even with a weird custom `format` parameter");
  71. });
  72. });