checkSome function with recursion - can check if a string contains any words from array. add __and in the array to test weather string contains ALL elements in this particular array. If array element is an array, recursively checks the array using same rules.
Trying to save it...
let allWords = ['__and', 'connexion', 'roaming'];
let someWords = ['2G', '3G', '4G', '5G', /H\+/, 'GPRS'];
let notWords = ['fail'];
let finalWords = ['__and', allWords, someWords, ['__not', notWords]];
let testTrue = 'Data transfer via connexion 3g roaming';
let testFalse = 'Data transfer via connexion 3g roaming FAIL';
function checkSome(name, values, settings) {
// validity check - name is string, values is string or array, array is not empty
if (
!name
|| !values
|| !isString(name)
|| (!isString(values) && !Array.isArray(values))
|| (Array.isArray(values) && values.length === 0)
) {
return false;
}
if (!isString(settings)) {
settings = 'i'; // default settings - case insensitive
}
values = values.slice(); // make a copy of the array so we don't modify it by accident
if (values.includes('__and')) {
values.splice(values.indexOf('__and'), 1);
return _.every(values, testValues);
} else if (values.includes('__not')) {
values.splice(values.indexOf('__not'), 1);
return !_.some(values, testValues);
} else {
return _.some(values, testValues);
}
function nameIncludesValue(name, value) {
if (!value) {
return false;
}
let r = new RegExp(value, settings);
return r.test(name);
}
function testValues(value) {
if (Array.isArray(value)) {
return checkSome(name, value);
} else {
return nameIncludesValue(name, value);
}
}
function isString(val) {
return (typeof val === 'string' && val.length > 0) || val instanceof String;
}
}
let resTrue = checkSome(testTrue, finalWords);
let resFalse = checkSome(testFalse, finalWords)
//console.log('expected true is ' + resTrue);
//console.log('expected false is ' + resFalse);
if (resTrue && !resFalse) {
let el = document.getElementById('test');
el.innerHTML = 'true';
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.