Skip to content

Commit

Permalink
feat(contains): add possibility to check that string contains seed mu…
Browse files Browse the repository at this point in the history
…ltiple times (#1836)

* Added feature to require minimum number of occurrences for the seed in 'contains'

* Changed regex to split

Co-authored-by: Markus Tyrkkö <markus.tyrkko@nitor.com>
Co-authored-by: Markus <markus.tyrkko@gmail.com>
  • Loading branch information
3 people authored and profnandaa committed Oct 31, 2021
1 parent 6cb7fd2 commit 772f037
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Here is a list of the validators currently available.

Validator | Description
--------------------------------------- | --------------------------------------
**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.
**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.
**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, 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.
Expand Down
10 changes: 7 additions & 3 deletions src/lib/contains.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import merge from './util/merge';

const defaulContainsOptions = {
ignoreCase: false,
minOccurrences: 1,
};

export default function contains(str, elem, options) {
assertString(str);
options = merge(options, defaulContainsOptions);
return options.ignoreCase ?
str.toLowerCase().indexOf(toString(elem).toLowerCase()) >= 0 :
str.indexOf(toString(elem)) >= 0;

if (options.ignoreCase) {
return str.toLowerCase().split(toString(elem).toLowerCase()).length > options.minOccurrences;
}

return str.split(toString(elem)).length > options.minOccurrences;
}
9 changes: 9 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4327,6 +4327,15 @@ describe('Validators', () => {
valid: ['Foo', 'FOObar', 'BAZfoo'],
invalid: ['bar', 'fobar', 'baxoof'],
});

test({
validator: 'contains',
args: ['foo', {
minOccurrences: 2,
}],
valid: ['foofoofoo', '12foo124foo', 'fofooofoooofoooo', 'foo1foo'],
invalid: ['foo', 'foobar', 'Fooofoo', 'foofo'],
});
});

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

0 comments on commit 772f037

Please sign in to comment.