diff --git a/README.md b/README.md index b06759892..338084f17 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Validator | Description **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`

`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'it-IT', 'ku-IQ', nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. **isDivisibleBy(str, number)** | check if the string is a number that's divisible by another. **isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by GMail. -**isEmpty(str)** | check if the string has a length of zero. +**isEmpty(str [, options])** | check if the string has a length of zero.

`options` is an object which defaults to `{ ignore_whitespace:false }`. **isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false }`. **isFloat(str [, options])** | check if the string is a float.

`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.

`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.

`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. **isFullWidth(str)** | check if the string contains any full-width chars. diff --git a/lib/isEmpty.js b/lib/isEmpty.js index f3e39e4df..fd1cab61d 100644 --- a/lib/isEmpty.js +++ b/lib/isEmpty.js @@ -9,10 +9,20 @@ var _assertString = require('./util/assertString'); var _assertString2 = _interopRequireDefault(_assertString); +var _merge = require('./util/merge'); + +var _merge2 = _interopRequireDefault(_merge); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -function isEmpty(str) { +var default_is_empty_options = { + ignore_whitespace: false +}; + +function isEmpty(str, options) { (0, _assertString2.default)(str); - return str.length === 0; + options = (0, _merge2.default)(options, default_is_empty_options); + + return (options.ignore_whitespace ? str.trim().length : str.length) === 0; } module.exports = exports['default']; \ No newline at end of file diff --git a/src/lib/isEmpty.js b/src/lib/isEmpty.js index 1b1d906b3..eeb5997d0 100644 --- a/src/lib/isEmpty.js +++ b/src/lib/isEmpty.js @@ -1,6 +1,13 @@ import assertString from './util/assertString'; +import merge from './util/merge'; -export default function isEmpty(str) { +const default_is_empty_options = { + ignore_whitespace: false, +}; + +export default function isEmpty(str, options) { assertString(str); - return str.length === 0; + options = merge(options, default_is_empty_options); + + return (options.ignore_whitespace ? str.trim().length : str.length) === 0; } diff --git a/test/validators.js b/test/validators.js index d45b49a7b..2e1c03360 100644 --- a/test/validators.js +++ b/test/validators.js @@ -2510,6 +2510,30 @@ describe('Validators', () => { '3', ], }); + test({ + validator: 'isEmpty', + args: [{ ignore_whitespace: false }], + valid: [ + '', + ], + invalid: [ + ' ', + 'foo', + '3', + ], + }); + test({ + validator: 'isEmpty', + args: [{ ignore_whitespace: true }], + valid: [ + '', + ' ', + ], + invalid: [ + 'foo', + '3', + ], + }); }); it('should validate strings against an expected value', () => {