Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add decimal options to isCurrency #713

Merged
merged 3 commits into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Validator | Description
**isBoolean(str)** | check if a string is a boolean.
**isByteLength(str, options)** | check if the string's length (in UTF-8 bytes) falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`.
**isCreditCard(str)** | check if the string is a credit card.
**isCurrency(str, options)** | check if the string is a valid currency amount.<br/><br/>`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_space_after_digits: false}`.
**isCurrency(str, options)** | check if the string is a valid currency amount.<br/><br/>`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.<br/>**Note:** The array digits_after_decimal is filled with the exact number of digits allowd not a range, for example a range 1 to 3 will be given as [1, 2, 3].
**isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs).
**isDecimal(str)** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
**isDivisibleBy(str, number)** | check if the string is a number that's divisible by another.
Expand Down
11 changes: 9 additions & 2 deletions lib/isCurrency.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ var _assertString2 = _interopRequireDefault(_assertString);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function currencyRegex(options) {
var decimal_digits = '\\d{' + options.digits_after_decimal[0] + '}';
options.digits_after_decimal.forEach(function (digit, index) {
if (index !== 0) decimal_digits = decimal_digits + '|\\d{' + digit + '}';
});
var symbol = '(\\' + options.symbol.replace(/\./g, '\\.') + ')' + (options.require_symbol ? '' : '?'),
negative = '-?',
whole_dollar_amount_without_sep = '[1-9]\\d*',
whole_dollar_amount_with_sep = '[1-9]\\d{0,2}(\\' + options.thousands_separator + '\\d{3})*',
valid_whole_dollar_amounts = ['0', whole_dollar_amount_without_sep, whole_dollar_amount_with_sep],
whole_dollar_amount = '(' + valid_whole_dollar_amounts.join('|') + ')?',
decimal_amount = '(\\' + options.decimal_separator + '\\d{2})?';
var pattern = whole_dollar_amount + decimal_amount;
decimal_amount = '(\\' + options.decimal_separator + '(' + decimal_digits + '))' + (options.require_decimal ? '' : '?');
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)
if (options.allow_negatives && !options.parens_for_negatives) {
Expand Down Expand Up @@ -74,6 +78,9 @@ var default_currency_options = {
allow_negative_sign_placeholder: false,
thousands_separator: ',',
decimal_separator: '.',
allow_decimal: true,
require_decimal: false,
digits_after_decimal: [2],
allow_space_after_digits: false
};

Expand Down
9 changes: 7 additions & 2 deletions src/lib/isCurrency.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import merge from './util/merge';
import assertString from './util/assertString';

function currencyRegex(options) {
let decimal_digits = `\\d{${options.digits_after_decimal[0]}}`;
options.digits_after_decimal.forEach((digit, index) => { if (index !== 0) decimal_digits = `${decimal_digits}|\\d{${digit}}`; });
const symbol =
`(\\${options.symbol.replace(/\./g, '\\.')})${(options.require_symbol ? '' : '?')}`,
negative = '-?',
Expand All @@ -10,8 +12,8 @@ function currencyRegex(options) {
valid_whole_dollar_amounts = [
'0', whole_dollar_amount_without_sep, whole_dollar_amount_with_sep],
whole_dollar_amount = `(${valid_whole_dollar_amounts.join('|')})?`,
decimal_amount = `(\\${options.decimal_separator}\\d{2})?`;
let pattern = whole_dollar_amount + decimal_amount;
decimal_amount = `(\\${options.decimal_separator}(${decimal_digits}))${options.require_decimal ? '' : '?'}`;
let 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)
if (options.allow_negatives && !options.parens_for_negatives) {
Expand Down Expand Up @@ -63,6 +65,9 @@ const default_currency_options = {
allow_negative_sign_placeholder: false,
thousands_separator: ',',
decimal_separator: '.',
allow_decimal: true,
require_decimal: false,
digits_after_decimal: [2],
allow_space_after_digits: false,
};

Expand Down
170 changes: 170 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3322,6 +3322,176 @@ describe('Validators', function () {
],
});

test({
validator: 'isCurrency',
args: [
{
allow_decimal: false,
},
'-$##,###.## (en-US, en-CA, en-AU, en-NZ, en-HK)',
],
valid: [
'-$10,123',
'$10,123',
'$10123',
'10,123',
'10123',
'10,123',
'1,123,456',
'1123456',
'1',
'0',
'$0',
'-$0',
'$100,234,567',
'$10,123',
'10,123',
'-10123',
],
invalid: [
'-$10,123.45',
'$10,123.45',
'$10123.45',
'10,123.45',
'10123.45',
'1.39',
'.03',
'0.10',
'$0.10',
'-$0.01',
'-$.99',
'$100,234,567.89',
'1.234',
'$1.1',
'$ 32.50',
'.0001',
'$.001',
'$0.001',
'12,34.56',
'123,4',
',123',
'$-,123',
'$',
'.',
',',
'00',
'$-',
'$-,.',
'-',
'-$',
'',
'- $',
],
});

test({
validator: 'isCurrency',
args: [
{
require_decimal: true,
},
'-$##,###.## (en-US, en-CA, en-AU, en-NZ, en-HK)',
],
valid: [
'-$10,123.45',
'$10,123.45',
'$10123.45',
'10,123.45',
'10123.45',
'10,123.00',
'1.39',
'.03',
'0.10',
'$0.10',
'-$0.01',
'-$.99',
'$100,234,567.89',
],
invalid: [
'$10,123',
'10,123',
'-10123',
'1,123,456',
'1123456',
'1.234',
'$1.1',
'$ 32.50',
'500$',
'.0001',
'$.001',
'$0.001',
'12,34.56',
'123456,123,123456',
'123,4',
',123',
'$-,123',
'$',
'.',
',',
'00',
'$-',
'$-,.',
'-',
'-$',
'',
'- $',
],
});

test({
validator: 'isCurrency',
args: [
{
digits_after_decimal: [1, 3],
},
'-$##,###.## (en-US, en-CA, en-AU, en-NZ, en-HK)',
],
valid: [
'-$10,123.4',
'$10,123.454',
'$10123.452',
'10,123.453',
'10123.450',
'10,123',
'1,123,456',
'1123456',
'1.3',
'.030',
'0.100',
'$0.1',
'-$0.0',
'-$.9',
'$100,234,567.893',
'$10,123',
'10,123.123',
'-10123.1',
],
invalid: [
'1.23',
'$1.13322',
'$ 32.50',
'500$',
'.0001',
'$.01',
'$0.01',
'12,34.56',
'123456,123,123456',
'123,4',
',123',
'$-,123',
'$',
'.',
',',
'00',
'$-',
'$-,.',
'-',
'-$',
'',
'- $',
],
});

test({
validator: 'isCurrency',
args: [
Expand Down
11 changes: 9 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,14 +975,18 @@ function isMobilePhone(str, locale) {
}

function currencyRegex(options) {
var decimal_digits = '\\d{' + options.digits_after_decimal[0] + '}';
options.digits_after_decimal.forEach(function (digit, index) {
if (index !== 0) decimal_digits = decimal_digits + '|\\d{' + digit + '}';
});
var symbol = '(\\' + options.symbol.replace(/\./g, '\\.') + ')' + (options.require_symbol ? '' : '?'),
negative = '-?',
whole_dollar_amount_without_sep = '[1-9]\\d*',
whole_dollar_amount_with_sep = '[1-9]\\d{0,2}(\\' + options.thousands_separator + '\\d{3})*',
valid_whole_dollar_amounts = ['0', whole_dollar_amount_without_sep, whole_dollar_amount_with_sep],
whole_dollar_amount = '(' + valid_whole_dollar_amounts.join('|') + ')?',
decimal_amount = '(\\' + options.decimal_separator + '\\d{2})?';
var pattern = whole_dollar_amount + decimal_amount;
decimal_amount = '(\\' + options.decimal_separator + '(' + decimal_digits + '))' + (options.require_decimal ? '' : '?');
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)
if (options.allow_negatives && !options.parens_for_negatives) {
Expand Down Expand Up @@ -1033,6 +1037,9 @@ var default_currency_options = {
allow_negative_sign_placeholder: false,
thousands_separator: ',',
decimal_separator: '.',
allow_decimal: true,
require_decimal: false,
digits_after_decimal: [2],
allow_space_after_digits: false
};

Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.