Skip to content

Commit

Permalink
feat(contains): add ignoreCase option (#1334)
Browse files Browse the repository at this point in the history
* Fixes #1303

* Added postal code for nepal

* Added NP locale to postal code

* Added country code to passport READNE

* Added norway identity card

* Fix order in readme

* Added ignoreCase to contains

* Update README
  • Loading branch information
parasg1999 authored May 30, 2020
1 parent 3223f58 commit e501b9c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Here is a list of the validators currently available.

Validator | Description
--------------------------------------- | --------------------------------------
***contains(str, seed)*** | check if the string contains the seed.
**contains(str, seed [, options ])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false}`.<br/>`ignoreCase` specified whether the case of the substring be same or not.
**equals(str, comparison)** | check if the string matches the comparison.
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
**isAlpha(str [, locale])** | check if the string contains only letters (a-zA-Z).<br/><br/>Locale 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', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`.
Expand Down
9 changes: 7 additions & 2 deletions es/lib/contains.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import assertString from './util/assertString';
import toString from './util/toString';
export default function contains(str, elem) {
import merge from './util/merge';
var defaulContainsOptions = {
ignoreCase: false
};
export default function contains(str, elem, options) {
assertString(str);
return str.indexOf(toString(elem)) >= 0;
options = merge(options, defaulContainsOptions);
return options.ignoreCase ? str.toLowerCase().indexOf(toString(elem).toLowerCase()) >= 0 : str.indexOf(toString(elem)) >= 0;
}
11 changes: 9 additions & 2 deletions lib/contains.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ var _assertString = _interopRequireDefault(require("./util/assertString"));

var _toString = _interopRequireDefault(require("./util/toString"));

var _merge = _interopRequireDefault(require("./util/merge"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function contains(str, elem) {
var defaulContainsOptions = {
ignoreCase: false
};

function contains(str, elem, options) {
(0, _assertString.default)(str);
return str.indexOf((0, _toString.default)(elem)) >= 0;
options = (0, _merge.default)(options, defaulContainsOptions);
return options.ignoreCase ? str.toLowerCase().indexOf((0, _toString.default)(elem).toLowerCase()) >= 0 : str.indexOf((0, _toString.default)(elem)) >= 0;
}

module.exports = exports.default;
Expand Down
12 changes: 10 additions & 2 deletions src/lib/contains.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import assertString from './util/assertString';
import toString from './util/toString';
import merge from './util/merge';

export default function contains(str, elem) {
const defaulContainsOptions = {
ignoreCase: false,
};

export default function contains(str, elem, options) {
assertString(str);
return str.indexOf(toString(elem)) >= 0;
options = merge(options, defaulContainsOptions);
return options.ignoreCase ?
str.toLowerCase().indexOf(toString(elem).toLowerCase()) >= 0 :
str.indexOf(toString(elem)) >= 0;
}
9 changes: 9 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3520,6 +3520,15 @@ describe('Validators', () => {
valid: ['foo', 'foobar', 'bazfoo'],
invalid: ['bar', 'fobar'],
});

test({
validator: 'contains',
args: ['foo', {
ignoreCase: true,
}],
valid: ['Foo', 'FOObar', 'BAZfoo'],
invalid: ['bar', 'fobar', 'baxoof'],
});
});

it('should validate strings against a pattern', () => {
Expand Down

0 comments on commit e501b9c

Please sign in to comment.