1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = isCurrency;
|
7 |
|
8 | var _merge = _interopRequireDefault(require("./util/merge"));
|
9 |
|
10 | var _assertString = _interopRequireDefault(require("./util/assertString"));
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 | function currencyRegex(options) {
|
15 | var decimal_digits = "\\d{".concat(options.digits_after_decimal[0], "}");
|
16 | options.digits_after_decimal.forEach(function (digit, index) {
|
17 | if (index !== 0) decimal_digits = "".concat(decimal_digits, "|\\d{").concat(digit, "}");
|
18 | });
|
19 | var symbol = "(\\".concat(options.symbol.replace(/\./g, '\\.'), ")").concat(options.require_symbol ? '' : '?'),
|
20 | negative = '-?',
|
21 | whole_dollar_amount_without_sep = '[1-9]\\d*',
|
22 | whole_dollar_amount_with_sep = "[1-9]\\d{0,2}(\\".concat(options.thousands_separator, "\\d{3})*"),
|
23 | valid_whole_dollar_amounts = ['0', whole_dollar_amount_without_sep, whole_dollar_amount_with_sep],
|
24 | whole_dollar_amount = "(".concat(valid_whole_dollar_amounts.join('|'), ")?"),
|
25 | decimal_amount = "(\\".concat(options.decimal_separator, "(").concat(decimal_digits, "))").concat(options.require_decimal ? '' : '?');
|
26 | var pattern = whole_dollar_amount + (options.allow_decimal || options.require_decimal ? decimal_amount : '');
|
27 |
|
28 | if (options.allow_negatives && !options.parens_for_negatives) {
|
29 | if (options.negative_sign_after_digits) {
|
30 | pattern += negative;
|
31 | } else if (options.negative_sign_before_digits) {
|
32 | pattern = negative + pattern;
|
33 | }
|
34 | }
|
35 |
|
36 |
|
37 | if (options.allow_negative_sign_placeholder) {
|
38 | pattern = "( (?!\\-))?".concat(pattern);
|
39 | } else if (options.allow_space_after_symbol) {
|
40 | pattern = " ?".concat(pattern);
|
41 | } else if (options.allow_space_after_digits) {
|
42 | pattern += '( (?!$))?';
|
43 | }
|
44 |
|
45 | if (options.symbol_after_digits) {
|
46 | pattern += symbol;
|
47 | } else {
|
48 | pattern = symbol + pattern;
|
49 | }
|
50 |
|
51 | if (options.allow_negatives) {
|
52 | if (options.parens_for_negatives) {
|
53 | pattern = "(\\(".concat(pattern, "\\)|").concat(pattern, ")");
|
54 | } else if (!(options.negative_sign_before_digits || options.negative_sign_after_digits)) {
|
55 | pattern = negative + pattern;
|
56 | }
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | return new RegExp("^(?!-? )(?=.*\\d)".concat(pattern, "$"));
|
62 | }
|
63 |
|
64 | var default_currency_options = {
|
65 | symbol: '$',
|
66 | require_symbol: false,
|
67 | allow_space_after_symbol: false,
|
68 | symbol_after_digits: false,
|
69 | allow_negatives: true,
|
70 | parens_for_negatives: false,
|
71 | negative_sign_before_digits: false,
|
72 | negative_sign_after_digits: false,
|
73 | allow_negative_sign_placeholder: false,
|
74 | thousands_separator: ',',
|
75 | decimal_separator: '.',
|
76 | allow_decimal: true,
|
77 | require_decimal: false,
|
78 | digits_after_decimal: [2],
|
79 | allow_space_after_digits: false
|
80 | };
|
81 |
|
82 | function isCurrency(str, options) {
|
83 | (0, _assertString.default)(str);
|
84 | options = (0, _merge.default)(options, default_currency_options);
|
85 | return currencyRegex(options).test(str);
|
86 | }
|
87 |
|
88 | module.exports = exports.default; |
\ | No newline at end of file |