Skip to content

Commit e501b9c

Browse files
authored
feat(contains): add ignoreCase option (#1334)
* 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
1 parent 3223f58 commit e501b9c

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Here is a list of the validators currently available.
8181

8282
Validator | Description
8383
--------------------------------------- | --------------------------------------
84-
***contains(str, seed)*** | check if the string contains the seed.
84+
**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.
8585
**equals(str, comparison)** | check if the string matches the comparison.
8686
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
8787
**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`.

es/lib/contains.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import assertString from './util/assertString';
22
import toString from './util/toString';
3-
export default function contains(str, elem) {
3+
import merge from './util/merge';
4+
var defaulContainsOptions = {
5+
ignoreCase: false
6+
};
7+
export default function contains(str, elem, options) {
48
assertString(str);
5-
return str.indexOf(toString(elem)) >= 0;
9+
options = merge(options, defaulContainsOptions);
10+
return options.ignoreCase ? str.toLowerCase().indexOf(toString(elem).toLowerCase()) >= 0 : str.indexOf(toString(elem)) >= 0;
611
}

lib/contains.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ var _assertString = _interopRequireDefault(require("./util/assertString"));
99

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

12+
var _merge = _interopRequireDefault(require("./util/merge"));
13+
1214
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1315

14-
function contains(str, elem) {
16+
var defaulContainsOptions = {
17+
ignoreCase: false
18+
};
19+
20+
function contains(str, elem, options) {
1521
(0, _assertString.default)(str);
16-
return str.indexOf((0, _toString.default)(elem)) >= 0;
22+
options = (0, _merge.default)(options, defaulContainsOptions);
23+
return options.ignoreCase ? str.toLowerCase().indexOf((0, _toString.default)(elem).toLowerCase()) >= 0 : str.indexOf((0, _toString.default)(elem)) >= 0;
1724
}
1825

1926
module.exports = exports.default;

src/lib/contains.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import assertString from './util/assertString';
22
import toString from './util/toString';
3+
import merge from './util/merge';
34

4-
export default function contains(str, elem) {
5+
const defaulContainsOptions = {
6+
ignoreCase: false,
7+
};
8+
9+
export default function contains(str, elem, options) {
510
assertString(str);
6-
return str.indexOf(toString(elem)) >= 0;
11+
options = merge(options, defaulContainsOptions);
12+
return options.ignoreCase ?
13+
str.toLowerCase().indexOf(toString(elem).toLowerCase()) >= 0 :
14+
str.indexOf(toString(elem)) >= 0;
715
}

test/validators.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,15 @@ describe('Validators', () => {
35203520
valid: ['foo', 'foobar', 'bazfoo'],
35213521
invalid: ['bar', 'fobar'],
35223522
});
3523+
3524+
test({
3525+
validator: 'contains',
3526+
args: ['foo', {
3527+
ignoreCase: true,
3528+
}],
3529+
valid: ['Foo', 'FOObar', 'BAZfoo'],
3530+
invalid: ['bar', 'fobar', 'baxoof'],
3531+
});
35233532
});
35243533

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

0 commit comments

Comments
 (0)