Skip to content

Commit

Permalink
Merge pull request #880 from ftrevo/improvement/is-empty-allow-string…
Browse files Browse the repository at this point in the history
…-only-whitespaces

Improving isEmpty to allow strings with only whitespaces
  • Loading branch information
chriso authored Aug 17, 2018
2 parents c5462d4 + e8d0883 commit 783a69e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/><br/>`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`<br/><br/>`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']`.<br/>**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.<br/><br/>`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 <email-address>`. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name <email-address>`. 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.<br/><br/>`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).<br/><br/>`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.<br/><br/>`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.<br/><br/>`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.<br/><br/>`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.
Expand Down
14 changes: 12 additions & 2 deletions lib/isEmpty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
11 changes: 9 additions & 2 deletions src/lib/isEmpty.js
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 24 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 783a69e

Please sign in to comment.