isCurrency.js 3.3 KB

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