@microblink/blinkinput-in-browser-sdk
Version:
A simple barcode scanning library for WebAssembly-enabled browsers.
71 lines (68 loc) • 1.88 kB
JavaScript
/**
* Copyright (c) Microblink Ltd. All rights reserved.
*/
function stringToArray(inputString) {
if (!inputString || !inputString.length) {
return [];
}
return inputString.split(',').map(el => el.trim());
}
function stringToObject(inputString) {
if (!inputString || !inputString.length) {
return {};
}
return JSON.parse(inputString);
}
function hasSupportedImageFiles(files) {
const imageRegex = RegExp(/^image\//);
for (let i = 0; i < files.length; ++i) {
if (imageRegex.exec(files[i].type)) {
return true;
}
}
return false;
}
function extractFilenameFromPath(path) {
return path.split('\\').pop();
}
/**
* Inspired by https://github.com/JedWatson/classnames.
* @param classes Class names and their conditions.
* @returns Joined string of class names.
*/
function classNames(classes) {
const result = [];
const keys = Object.keys(classes);
keys.forEach((key) => {
if (classes[key]) {
result.push(key);
}
});
return result.join(' ');
}
function getWebComponentParts(root) {
const partsChildren = root.querySelectorAll('[part]');
const parts = [];
partsChildren.forEach((el) => {
const elementParts = el.getAttribute('part').split(' ');
while (elementParts && elementParts.length) {
parts.push(elementParts.pop());
}
});
return parts;
}
function setWebComponentParts(hostEl) {
const partParts = [
hostEl.tagName.toLowerCase(),
hostEl.getAttribute('id')
];
hostEl.setAttribute('part', partParts.join(' ').trim());
}
exports.classNames = classNames;
exports.extractFilenameFromPath = extractFilenameFromPath;
exports.getWebComponentParts = getWebComponentParts;
exports.hasSupportedImageFiles = hasSupportedImageFiles;
exports.setWebComponentParts = setWebComponentParts;
exports.stringToArray = stringToArray;
exports.stringToObject = stringToObject;
;