index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1733984946602, function(require, module, exports) {
  8. /*!
  9. * currency.js - v2.0.4
  10. * http://scurker.github.io/currency.js
  11. *
  12. * Copyright (c) 2021 Jason Wilson
  13. * Released under MIT license
  14. */
  15. var defaults = {
  16. symbol: '$',
  17. separator: ',',
  18. decimal: '.',
  19. errorOnInvalid: false,
  20. precision: 2,
  21. pattern: '!#',
  22. negativePattern: '-!#',
  23. format: format,
  24. fromCents: false
  25. };
  26. var round = function round(v) {
  27. return Math.round(v);
  28. };
  29. var pow = function pow(p) {
  30. return Math.pow(10, p);
  31. };
  32. var rounding = function rounding(value, increment) {
  33. return round(value / increment) * increment;
  34. };
  35. var groupRegex = /(\d)(?=(\d{3})+\b)/g;
  36. var vedicRegex = /(\d)(?=(\d\d)+\d\b)/g;
  37. /**
  38. * Create a new instance of currency.js
  39. * @param {number|string|currency} value
  40. * @param {object} [opts]
  41. */
  42. function currency(value, opts) {
  43. var that = this;
  44. if (!(that instanceof currency)) {
  45. return new currency(value, opts);
  46. }
  47. var settings = Object.assign({}, defaults, opts),
  48. precision = pow(settings.precision),
  49. v = parse(value, settings);
  50. that.intValue = v;
  51. that.value = v / precision; // Set default incremental value
  52. settings.increment = settings.increment || 1 / precision; // Support vedic numbering systems
  53. // see: https://en.wikipedia.org/wiki/Indian_numbering_system
  54. if (settings.useVedic) {
  55. settings.groups = vedicRegex;
  56. } else {
  57. settings.groups = groupRegex;
  58. } // Intended for internal usage only - subject to change
  59. this.s = settings;
  60. this.p = precision;
  61. }
  62. function parse(value, opts) {
  63. var useRounding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  64. var v = 0,
  65. decimal = opts.decimal,
  66. errorOnInvalid = opts.errorOnInvalid,
  67. decimals = opts.precision,
  68. fromCents = opts.fromCents,
  69. precision = pow(decimals),
  70. isNumber = typeof value === 'number',
  71. isCurrency = value instanceof currency;
  72. if (isCurrency && fromCents) {
  73. return value.intValue;
  74. }
  75. if (isNumber || isCurrency) {
  76. v = isCurrency ? value.value : value;
  77. } else if (typeof value === 'string') {
  78. var regex = new RegExp('[^-\\d' + decimal + ']', 'g'),
  79. decimalString = new RegExp('\\' + decimal, 'g');
  80. v = value.replace(/\((.*)\)/, '-$1') // allow negative e.g. (1.99)
  81. .replace(regex, '') // replace any non numeric values
  82. .replace(decimalString, '.'); // convert any decimal values
  83. v = v || 0;
  84. } else {
  85. if (errorOnInvalid) {
  86. throw Error('Invalid Input');
  87. }
  88. v = 0;
  89. }
  90. if (!fromCents) {
  91. v *= precision; // scale number to integer value
  92. v = v.toFixed(4); // Handle additional decimal for proper rounding.
  93. }
  94. return useRounding ? round(v) : v;
  95. }
  96. /**
  97. * Formats a currency object
  98. * @param currency
  99. * @param {object} [opts]
  100. */
  101. function format(currency, settings) {
  102. var pattern = settings.pattern,
  103. negativePattern = settings.negativePattern,
  104. symbol = settings.symbol,
  105. separator = settings.separator,
  106. decimal = settings.decimal,
  107. groups = settings.groups,
  108. split = ('' + currency).replace(/^-/, '').split('.'),
  109. dollars = split[0],
  110. cents = split[1];
  111. return (currency.value >= 0 ? pattern : negativePattern).replace('!', symbol).replace('#', dollars.replace(groups, '$1' + separator) + (cents ? decimal + cents : ''));
  112. }
  113. currency.prototype = {
  114. /**
  115. * Adds values together.
  116. * @param {number} number
  117. * @returns {currency}
  118. */
  119. add: function add(number) {
  120. var intValue = this.intValue,
  121. _settings = this.s,
  122. _precision = this.p;
  123. return currency((intValue += parse(number, _settings)) / (_settings.fromCents ? 1 : _precision), _settings);
  124. },
  125. /**
  126. * Subtracts value.
  127. * @param {number} number
  128. * @returns {currency}
  129. */
  130. subtract: function subtract(number) {
  131. var intValue = this.intValue,
  132. _settings = this.s,
  133. _precision = this.p;
  134. return currency((intValue -= parse(number, _settings)) / (_settings.fromCents ? 1 : _precision), _settings);
  135. },
  136. /**
  137. * Multiplies values.
  138. * @param {number} number
  139. * @returns {currency}
  140. */
  141. multiply: function multiply(number) {
  142. var intValue = this.intValue,
  143. _settings = this.s;
  144. return currency((intValue *= number) / (_settings.fromCents ? 1 : pow(_settings.precision)), _settings);
  145. },
  146. /**
  147. * Divides value.
  148. * @param {number} number
  149. * @returns {currency}
  150. */
  151. divide: function divide(number) {
  152. var intValue = this.intValue,
  153. _settings = this.s;
  154. return currency(intValue /= parse(number, _settings, false), _settings);
  155. },
  156. /**
  157. * Takes the currency amount and distributes the values evenly. Any extra pennies
  158. * left over from the distribution will be stacked onto the first set of entries.
  159. * @param {number} count
  160. * @returns {array}
  161. */
  162. distribute: function distribute(count) {
  163. var intValue = this.intValue,
  164. _precision = this.p,
  165. _settings = this.s,
  166. distribution = [],
  167. split = Math[intValue >= 0 ? 'floor' : 'ceil'](intValue / count),
  168. pennies = Math.abs(intValue - split * count),
  169. precision = _settings.fromCents ? 1 : _precision;
  170. for (; count !== 0; count--) {
  171. var item = currency(split / precision, _settings); // Add any left over pennies
  172. pennies-- > 0 && (item = item[intValue >= 0 ? 'add' : 'subtract'](1 / precision));
  173. distribution.push(item);
  174. }
  175. return distribution;
  176. },
  177. /**
  178. * Returns the dollar value.
  179. * @returns {number}
  180. */
  181. dollars: function dollars() {
  182. return ~~this.value;
  183. },
  184. /**
  185. * Returns the cent value.
  186. * @returns {number}
  187. */
  188. cents: function cents() {
  189. var intValue = this.intValue,
  190. _precision = this.p;
  191. return ~~(intValue % _precision);
  192. },
  193. /**
  194. * Formats the value as a string according to the formatting settings.
  195. * @param {boolean} useSymbol - format with currency symbol
  196. * @returns {string}
  197. */
  198. format: function format(options) {
  199. var _settings = this.s;
  200. if (typeof options === 'function') {
  201. return options(this, _settings);
  202. }
  203. return _settings.format(this, Object.assign({}, _settings, options));
  204. },
  205. /**
  206. * Formats the value as a string according to the formatting settings.
  207. * @returns {string}
  208. */
  209. toString: function toString() {
  210. var intValue = this.intValue,
  211. _precision = this.p,
  212. _settings = this.s;
  213. return rounding(intValue / _precision, _settings.increment).toFixed(_settings.precision);
  214. },
  215. /**
  216. * Value for JSON serialization.
  217. * @returns {float}
  218. */
  219. toJSON: function toJSON() {
  220. return this.value;
  221. }
  222. };
  223. module.exports = currency;
  224. }, function(modId) {var map = {}; return __REQUIRE__(map[modId], modId); })
  225. return __REQUIRE__(1733984946602);
  226. })()
  227. //miniprogram-npm-outsideDeps=[]
  228. //# sourceMappingURL=index.js.map