From 61a3b48403a9e56276dee8e037add38016b54cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Tyrkk=C3=B6?= Date: Fri, 29 Oct 2021 14:22:26 +0300 Subject: [PATCH 1/2] Added feature to require minimum number of occurrences for the seed in 'contains' --- README.md | 2 +- src/lib/contains.js | 8 +++++--- test/validators.js | 9 +++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1ff1e00dc..924c3f1ce 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ Here is a list of the validators currently available. Validator | Description --------------------------------------- | -------------------------------------- -**contains(str, seed [, options ])** | check if the string contains the seed.

`options` is an object that defaults to `{ ignoreCase: false}`.
`ignoreCase` specified whether the case of the substring be same or not. +**contains(str, seed [, options ])** | check if the string contains the seed.

`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.
Options:
`ignoreCase`: Ignore case when doing comparison, default false
`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).

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', '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. diff --git a/src/lib/contains.js b/src/lib/contains.js index ec083fa18..c1d237c46 100644 --- a/src/lib/contains.js +++ b/src/lib/contains.js @@ -4,12 +4,14 @@ 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; + + const regex = new RegExp(toString(elem), `g${options.ignoreCase ? 'i' : ''}`); + + return (str.match(regex) || []).length >= options.minOccurrences; } diff --git a/test/validators.js b/test/validators.js index 91794d521..0729d92ff 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4239,6 +4239,15 @@ describe('Validators', () => { valid: ['Foo', 'FOObar', 'BAZfoo'], invalid: ['bar', 'fobar', 'baxoof'], }); + + test({ + validator: 'contains', + args: ['foo', { + minOccurrences: 2, + }], + valid: ['foofoofoo', '12foo124foo', 'fofooofoooofoooo'], + invalid: ['foo', 'foobar', 'Fooofoo', 'foofo'], + }); }); it('should validate strings against a pattern', () => { From a276b6bd2e17c499e40c8e2dac28009967cde240 Mon Sep 17 00:00:00 2001 From: Markus Date: Sat, 30 Oct 2021 19:35:02 +0300 Subject: [PATCH 2/2] Changed regex to split --- src/lib/contains.js | 6 ++++-- test/validators.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib/contains.js b/src/lib/contains.js index c1d237c46..7be314b04 100644 --- a/src/lib/contains.js +++ b/src/lib/contains.js @@ -11,7 +11,9 @@ export default function contains(str, elem, options) { assertString(str); options = merge(options, defaulContainsOptions); - const regex = new RegExp(toString(elem), `g${options.ignoreCase ? 'i' : ''}`); + if (options.ignoreCase) { + return str.toLowerCase().split(toString(elem).toLowerCase()).length > options.minOccurrences; + } - return (str.match(regex) || []).length >= options.minOccurrences; + return str.split(toString(elem)).length > options.minOccurrences; } diff --git a/test/validators.js b/test/validators.js index 0729d92ff..d945eea13 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4245,7 +4245,7 @@ describe('Validators', () => { args: ['foo', { minOccurrences: 2, }], - valid: ['foofoofoo', '12foo124foo', 'fofooofoooofoooo'], + valid: ['foofoofoo', '12foo124foo', 'fofooofoooofoooo', 'foo1foo'], invalid: ['foo', 'foobar', 'Fooofoo', 'foofo'], }); });