velocity-verification
Version:
JavaScript SDK view layer for identity verification
101 lines (84 loc) • 3.11 kB
JavaScript
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()
export const copyToClipboard = async (text, callback) => {
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text)
callback()
} else {
const textArea = document.createElement('textarea')
textArea.value = text
textArea.style.position = 'fixed'
textArea.style.left = '-999999px'
textArea.style.top = '-999999px'
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
document.execCommand('copy')
textArea.remove()
callback()
}
} catch (error) {
const textArea = document.createElement('textarea')
textArea.value = text
textArea.style.position = 'fixed'
textArea.style.left = '-999999px'
textArea.style.top = '-999999px'
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
document.execCommand('copy')
textArea.remove()
callback()
}
}