UNPKG

onfido-sdk-ui

Version:

JavaScript SDK view layer for Onfido identity verification

68 lines (52 loc) 2.15 kB
import parseUnit from 'parse-unit' import { h } from 'preact' import enumerateDevices from 'enumerate-devices' export const functionalSwitch = (key, hash) => (hash[key] || (()=>null))() export const getCSSValue = (expectedUnit, cssUnit) => { const [value, resUnit] = parseUnit(cssUnit) if (resUnit !== expectedUnit) { console.warn(`The css @value: ${cssUnit} unit is ${resUnit} but it should be ${expectedUnit}`) } return value } export const getCSSMilisecsValue = cssUnit => getCSSValue("ms", cssUnit) export const wrapWithClass = (className, children) => <div className={className}>{children}</div> export const preventDefaultOnClick = callback => event => { event.preventDefault() callback() } // Copied from https://github.com/muaz-khan/DetectRTC/blob/master/DetectRTC.js export const isDesktop = !(/Android|webOS|iPhone|iPad|iPod|BB10|BlackBerry|IEMobile|Opera Mini|Mobile|mobile/i.test(navigator.userAgent || '')) const enumerateDevicesInternal = (onSuccess, onError) => { try { enumerateDevices().then(onSuccess).catch(onError); } catch (exception){ onError(exception) } } const checkDevicesInfo = checkFn => onResult => enumerateDevicesInternal( devices => onResult(checkFn(devices)), () => onResult(false) ) const isVideoDevice = ({ kind = '' }) => kind.includes('video') const hasDevicePermission = ({ label }) => !!label export const checkIfHasWebcam = checkDevicesInfo( devices => devices.some(isVideoDevice) ) export const checkIfWebcamPermissionGranted = checkDevicesInfo( devices => devices.filter(isVideoDevice).some(hasDevicePermission) ) export const parseTags = (str, handleTag) => { const parser = new DOMParser(); const stringToXml = parser.parseFromString(`<l>${str}</l>`, 'application/xml') const xmlToNodesArray = Array.from(stringToXml.firstChild.childNodes) return xmlToNodesArray.map( node => node.nodeType === document.TEXT_NODE ? node.textContent : handleTag({type: node.tagName, text: node.textContent}) ) } export const currentSeconds = () => Math.floor(Date.now() / 1000) export const currentMilliseconds = () => new Date().getTime()