stylelint
Version:
A mighty, modern CSS linter.
68 lines (60 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = containsString;
/**
* Checks if a string contains a value. The comparison value can be a string or
* an array of strings.
*
* Any strings starting and ending with `/` are ignored. Use the
* matchesStringOrRegExp() util to match regexes.
*
* @param {string} input
* @param {string|array} comparison
* @return {boolean|object} `false` if no match is found.
* If a match is found, returns an object with these properties:
* - `match`: the `input` value that had a match
* - `pattern`: the `comparison` pattern that had a match
*/
function containsString(input, comparison) {
if (!Array.isArray(comparison)) {
return testAgainstString(input, comparison);
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = comparison[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var comparisonItem = _step.value;
var testResult = testAgainstString(input, comparisonItem);
if (testResult) {
return testResult;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return false;
}
function testAgainstString(value, comparison) {
if (!comparison) return false;
if (comparison[0] === "/" && comparison[comparison.length - 1] === "/") {
return false;
}
if (value.indexOf(comparison) >= 0) {
return { match: value, pattern: comparison };
}
return false;
}