isCurrency.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 decimal_digits = '\\d{' + options.digits_after_decimal[0] + '}';
  13. options.digits_after_decimal.forEach(function (digit, index) {
  14. if (index !== 0) decimal_digits = decimal_digits + '|\\d{' + digit + '}';
  15. });
  16. var symbol = '(\\' + options.symbol.replace(/\./g, '\\.') + ')' + (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}(\\' + 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 = '(' + valid_whole_dollar_amounts.join('|') + ')?',
  22. decimal_amount = '(\\' + options.decimal_separator + '(' + decimal_digits + '))' + (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 = '( (?!\\-))?' + pattern;
  35. } else if (options.allow_space_after_symbol) {
  36. pattern = ' ?' + 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 = '(\\(' + pattern + '\\)|' + 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)' + 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, _assertString2.default)(str);
  75. options = (0, _merge2.default)(options, default_currency_options);
  76. return currencyRegex(options).test(str);
  77. }
  78. module.exports = exports['default'];