isCurrency.js 3.0 KB

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