JSFiddle - React, Tailwind, and code Playground

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...

by Yurii Predborskyi

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
<div id="test" class="false">
false
</div>

CSS

#test {
  height: 100px;
  width: 100px;
  font-size: 30px;
  text-align: center;
  line-height: 100px;
}

.false {
    background-color: red;
}

.true {
  background-color: lime;
}

JavaScript

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';
 ...