isCurrency.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isCurrency;
  6. var _merge = require('./util/merge');
  7. var _merge2 = _interopRequireDefault(_merge);
  8. var _assertString = require('./util/assertString');
  9. var _assertString2 = _interopRequireDefault(_assertString);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function currencyRegex(options) {
  12. var symbol = '(\\' + options.symbol.replace(/\./g, '\\.') + ')' + (options.require_symbol ? '' : '?'),
  13. negative = '-?',
  14. whole_dollar_amount_without_sep = '[1-9]\\d*',
  15. whole_dollar_amount_with_sep = '[1-9]\\d{0,2}(\\' + options.thousands_separator + '\\d{3})*',
  16. valid_whole_dollar_amounts = ['0', whole_dollar_amount_without_sep, whole_dollar_amount_with_sep],
  17. whole_dollar_amount = '(' + valid_whole_dollar_amounts.join('|') + ')?',
  18. decimal_amount = '(\\' + options.decimal_separator + '\\d{2})?';
  19. var pattern = whole_dollar_amount + decimal_amount;
  20. // default is negative sign before symbol, but there are two other options (besides parens)
  21. if (options.allow_negatives && !options.parens_for_negatives) {
  22. if (options.negative_sign_after_digits) {
  23. pattern += negative;
  24. } else if (options.negative_sign_before_digits) {
  25. pattern = negative + pattern;
  26. }
  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 = '( (?!\\-))?' + pattern;
  31. } else if (options.allow_space_after_symbol) {
  32. pattern = ' ?' + 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 = '(\\(' + pattern + '\\)|' + pattern + ')';
  44. } else if (!(options.negative_sign_before_digits || options.negative_sign_after_digits)) {
  45. pattern = negative + pattern;
  46. }
  47. }
  48. /* eslint-disable prefer-template */
  49. return new RegExp('^' +
  50. // ensure there's a dollar and/or decimal amount, and that
  51. // it doesn't start with a space or a negative sign followed by a space
  52. '(?!-? )(?=.*\\d)' + pattern + '$');
  53. /* eslint-enable prefer-template */
  54. }
  55. var default_currency_options = {
  56. symbol: '$',
  57. require_symbol: false,
  58. allow_space_after_symbol: false,
  59. symbol_after_digits: false,
  60. allow_negatives: true,
  61. parens_for_negatives: false,
  62. negative_sign_before_digits: false,
  63. negative_sign_after_digits: false,
  64. allow_negative_sign_placeholder: false,
  65. thousands_separator: ',',
  66. decimal_separator: '.',
  67. allow_space_after_digits: false
  68. };
  69. function isCurrency(str, options) {
  70. (0, _assertString2.default)(str);
  71. options = (0, _merge2.default)(options, default_currency_options);
  72. return currencyRegex(options).test(str);
  73. }
  74. module.exports = exports['default'];