@quadible/web-sdk
Version:
The web sdk for Quadible's behavioral authentication service.
84 lines • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.includes = includes;
exports.excludes = excludes;
exports.toInt = toInt;
exports.toFloat = toFloat;
exports.replaceNaN = replaceNaN;
exports.countTruthy = countTruthy;
exports.round = round;
exports.parseSimpleCssSelector = parseSimpleCssSelector;
function includes(haystack, needle) {
for (let i = 0, l = haystack.length; i < l; ++i) {
if (haystack[i] === needle) {
return true;
}
}
return false;
}
function excludes(haystack, needle) {
return !includes(haystack, needle);
}
function toInt(value) {
return parseInt(value);
}
function toFloat(value) {
return parseFloat(value);
}
function replaceNaN(value, replacement) {
return typeof value === 'number' && isNaN(value) ? replacement : value;
}
function countTruthy(values) {
return values.reduce((sum, value) => sum + (value ? 1 : 0), 0);
}
function round(value, base = 1) {
if (Math.abs(base) >= 1) {
return Math.round(value / base) * base;
}
else {
// Sometimes when a number is multiplied by a small number, precision is lost,
// for example 1234 * 0.0001 === 0.12340000000000001, and it's more precise divide: 1234 / (1 / 0.0001) === 0.1234.
const counterBase = 1 / base;
return Math.round(value * counterBase) / counterBase;
}
}
function parseSimpleCssSelector(selector) {
const errorMessage = `Unexpected syntax '${selector}'`;
const tagMatch = /^\s*([a-z-]*)(.*)$/i.exec(selector);
const tag = tagMatch[1] || undefined;
const attributes = {};
const partsRegex = /([.:#][\w-]+|\[.+?\])/gi;
const addAttribute = (name, value) => {
attributes[name] = attributes[name] || [];
attributes[name].push(value);
};
for (;;) {
const match = partsRegex.exec(tagMatch[2]);
if (!match) {
break;
}
const part = match[0];
switch (part[0]) {
case '.':
addAttribute('class', part.slice(1));
break;
case '#':
addAttribute('id', part.slice(1));
break;
case '[': {
const attributeMatch = /^\[([\w-]+)([~|^$*]?=("(.*?)"|([\w-]+)))?(\s+[is])?\]$/.exec(part);
if (attributeMatch) {
addAttribute(attributeMatch[1], attributeMatch[4] ?? attributeMatch[5] ?? '');
}
else {
throw new Error(errorMessage);
}
break;
}
default:
throw new Error(errorMessage);
}
}
return [tag, attributes];
}
//# sourceMappingURL=data.js.map