currency.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. currency.js - v2.0.4
  3. http://scurker.github.io/currency.js
  4. Copyright (c) 2021 Jason Wilson
  5. Released under MIT license
  6. */
  7. (function (e, g) {
  8. "object" === typeof exports && "undefined" !== typeof module
  9. ? (module.exports = g())
  10. : "function" === typeof define && define.amd
  11. ? define(g)
  12. : ((e = e || self), (e.currency = g()));
  13. })(this, function () {
  14. function e(b, a) {
  15. if (!(this instanceof e)) return new e(b, a);
  16. a = Object.assign({}, m, a);
  17. var d = Math.pow(10, a.precision);
  18. this.intValue = b = g(b, a);
  19. this.value = b / d;
  20. a.increment = a.increment || 1 / d;
  21. a.groups = a.useVedic ? n : p;
  22. this.s = a;
  23. this.p = d;
  24. }
  25. function g(b, a) {
  26. var d = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : !0;
  27. var c = a.decimal;
  28. var h = a.errorOnInvalid,
  29. k = a.fromCents,
  30. l = Math.pow(10, a.precision),
  31. f = b instanceof e;
  32. if (f && k) return b.intValue;
  33. if ("number" === typeof b || f) c = f ? b.value : b;
  34. else if ("string" === typeof b)
  35. (h = new RegExp("[^-\\d" + c + "]", "g")),
  36. (c = new RegExp("\\" + c, "g")),
  37. (c =
  38. (c = b
  39. .replace(/\((.*)\)/, "-$1")
  40. .replace(h, "")
  41. .replace(c, ".")) || 0);
  42. else {
  43. if (h) throw Error("Invalid Input");
  44. c = 0;
  45. }
  46. k || (c = (c * l).toFixed(4));
  47. return d ? Math.round(c) : c;
  48. }
  49. var m = {
  50. symbol: "$",
  51. separator: ",",
  52. decimal: ".",
  53. errorOnInvalid: !1,
  54. precision: 2,
  55. pattern: "!#",
  56. negativePattern: "-!#",
  57. format: function (b, a) {
  58. var d = a.pattern,
  59. c = a.negativePattern,
  60. h = a.symbol,
  61. k = a.separator,
  62. l = a.decimal;
  63. a = a.groups;
  64. var f = ("" + b).replace(/^-/, "").split("."),
  65. q = f[0];
  66. f = f[1];
  67. return (0 <= b.value ? d : c)
  68. .replace("!", h)
  69. .replace("#", q.replace(a, "$1" + k) + (f ? l + f : ""));
  70. },
  71. fromCents: !1,
  72. },
  73. p = /(\d)(?=(\d{3})+\b)/g,
  74. n = /(\d)(?=(\d\d)+\d\b)/g;
  75. e.prototype = {
  76. add: function (b) {
  77. var a = this.s,
  78. d = this.p;
  79. return e((this.intValue + g(b, a)) / (a.fromCents ? 1 : d), a);
  80. },
  81. subtract: function (b) {
  82. var a = this.s,
  83. d = this.p;
  84. return e((this.intValue - g(b, a)) / (a.fromCents ? 1 : d), a);
  85. },
  86. multiply: function (b) {
  87. var a = this.s;
  88. return e(
  89. (this.intValue * b) / (a.fromCents ? 1 : Math.pow(10, a.precision)),
  90. a
  91. );
  92. },
  93. divide: function (b) {
  94. var a = this.s;
  95. return e(this.intValue / g(b, a, !1), a);
  96. },
  97. distribute: function (b) {
  98. var a = this.intValue,
  99. d = this.p,
  100. c = this.s,
  101. h = [],
  102. k = Math[0 <= a ? "floor" : "ceil"](a / b),
  103. l = Math.abs(a - k * b);
  104. for (d = c.fromCents ? 1 : d; 0 !== b; b--) {
  105. var f = e(k / d, c);
  106. 0 < l-- && (f = f[0 <= a ? "add" : "subtract"](1 / d));
  107. h.push(f);
  108. }
  109. return h;
  110. },
  111. dollars: function () {
  112. return ~~this.value;
  113. },
  114. cents: function () {
  115. return ~~(this.intValue % this.p);
  116. },
  117. format: function (b) {
  118. var a = this.s;
  119. return "function" === typeof b
  120. ? b(this, a)
  121. : a.format(this, Object.assign({}, a, b));
  122. },
  123. toString: function () {
  124. var b = this.s,
  125. a = b.increment;
  126. return (Math.round(this.intValue / this.p / a) * a).toFixed(b.precision);
  127. },
  128. toJSON: function () {
  129. return this.value;
  130. },
  131. };
  132. return e;
  133. });