Skip to content

Commit 772f037

Browse files
MarcholioMarkus
authored andcommitted
feat(contains): add possibility to check that string contains seed multiple times (#1836)
* Added feature to require minimum number of occurrences for the seed in 'contains' * Changed regex to split Co-authored-by: Markus Tyrkkö <[email protected]> Co-authored-by: Markus <[email protected]>
1 parent 6cb7fd2 commit 772f037

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

README.md

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

8989
Validator | Description
9090
--------------------------------------- | --------------------------------------
91-
**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.
91+
**contains(str, seed [, options ])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.<br />Options: <br/> `ignoreCase`: Ignore case when doing comparison, default false<br/>`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1.
9292
**equals(str, comparison)** | check if the string matches the comparison.
9393
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
9494
**isAlpha(str [, locale, options])** | 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', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', '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`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.

src/lib/contains.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import merge from './util/merge';
44

55
const defaulContainsOptions = {
66
ignoreCase: false,
7+
minOccurrences: 1,
78
};
89

910
export default function contains(str, elem, options) {
1011
assertString(str);
1112
options = merge(options, defaulContainsOptions);
12-
return options.ignoreCase ?
13-
str.toLowerCase().indexOf(toString(elem).toLowerCase()) >= 0 :
14-
str.indexOf(toString(elem)) >= 0;
13+
14+
if (options.ignoreCase) {
15+
return str.toLowerCase().split(toString(elem).toLowerCase()).length > options.minOccurrences;
16+
}
17+
18+
return str.split(toString(elem)).length > options.minOccurrences;
1519
}

test/validators.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4327,6 +4327,15 @@ describe('Validators', () => {
43274327
valid: ['Foo', 'FOObar', 'BAZfoo'],
43284328
invalid: ['bar', 'fobar', 'baxoof'],
43294329
});
4330+
4331+
test({
4332+
validator: 'contains',
4333+
args: ['foo', {
4334+
minOccurrences: 2,
4335+
}],
4336+
valid: ['foofoofoo', '12foo124foo', 'fofooofoooofoooo', 'foo1foo'],
4337+
invalid: ['foo', 'foobar', 'Fooofoo', 'foofo'],
4338+
});
43304339
});
43314340

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

0 commit comments

Comments
 (0)