isCurrency.js 3.3 KB

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