isCurrency.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isCurrency;
  6. var _merge = _interopRequireDefault(require("./util/merge"));
  7. var _assertString = _interopRequireDefault(require("./util/assertString"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function currencyRegex(options) {
  10. var decimal_digits = "\\d{".concat(options.digits_after_decimal[0], "}");
  11. options.digits_after_decimal.forEach(function (digit, index) {
  12. if (index !== 0) decimal_digits = "".concat(decimal_digits, "|\\d{").concat(digit, "}");
  13. });
  14. var symbol = "(".concat(options.symbol.replace(/\W/, function (m) {
  15. return "\\".concat(m);
  16. }), ")").concat(options.require_symbol ? '' : '?'),
  17. negative = '-?',
  18. whole_dollar_amount_without_sep = '[1-9]\\d*',
  19. whole_dollar_amount_with_sep = "[1-9]\\d{0,2}(\\".concat(options.thousands_separator, "\\d{3})*"),
  20. valid_whole_dollar_amounts = ['0', whole_dollar_amount_without_sep, whole_dollar_amount_with_sep],
  21. whole_dollar_amount = "(".concat(valid_whole_dollar_amounts.join('|'), ")?"),
  22. decimal_amount = "(\\".concat(options.decimal_separator, "(").concat(decimal_digits, "))").concat(options.require_decimal ? '' : '?');
  23. var pattern = whole_dollar_amount + (options.allow_decimal || options.require_decimal ? decimal_amount : '');
  24. // default is negative sign before symbol, but there are two other options (besides parens)
  25. if (options.allow_negatives && !options.parens_for_negatives) {
  26. if (options.negative_sign_after_digits) {
  27. pattern += negative;
  28. } else if (options.negative_sign_before_digits) {
  29. pattern = negative + pattern;
  30. }
  31. }
  32. // South African Rand, for example, uses R 123 (space) and R-123 (no space)
  33. if (options.allow_negative_sign_placeholder) {
  34. pattern = "( (?!\\-))?".concat(pattern);
  35. } else if (options.allow_space_after_symbol) {
  36. pattern = " ?".concat(pattern);
  37. } else if (options.allow_space_after_digits) {
  38. pattern += '( (?!$))?';
  39. }
  40. if (options.symbol_after_digits) {
  41. pattern += symbol;
  42. } else {
  43. pattern = symbol + pattern;
  44. }
  45. if (options.allow_negatives) {
  46. if (options.parens_for_negatives) {
  47. pattern = "(\\(".concat(pattern, "\\)|").concat(pattern, ")");
  48. } else if (!(options.negative_sign_before_digits || options.negative_sign_after_digits)) {
  49. pattern = negative + pattern;
  50. }
  51. }
  52. // ensure there's a dollar and/or decimal amount, and that
  53. // it doesn't start with a space or a negative sign followed by a space
  54. return new RegExp("^(?!-? )(?=.*\\d)".concat(pattern, "$"));
  55. }
  56. var default_currency_options = {
  57. symbol: '$',
  58. require_symbol: false,
  59. allow_space_after_symbol: false,
  60. symbol_after_digits: false,
  61. allow_negatives: true,
  62. parens_for_negatives: false,
  63. negative_sign_before_digits: false,
  64. negative_sign_after_digits: false,
  65. allow_negative_sign_placeholder: false,
  66. thousands_separator: ',',
  67. decimal_separator: '.',
  68. allow_decimal: true,
  69. require_decimal: false,
  70. digits_after_decimal: [2],
  71. allow_space_after_digits: false
  72. };
  73. function isCurrency(str, options) {
  74. (0, _assertString.default)(str);
  75. options = (0, _merge.default)(options, default_currency_options);
  76. return currencyRegex(options).test(str);
  77. }
  78. module.exports = exports.default;
  79. module.exports.default = exports.default;