UNPKG

viam-projectv-scan

Version:

ViaCheck deposit application

167 lines (138 loc) 4.31 kB
const base64Arraybuffer = require('base64-arraybuffer'); const utif = require('utif'); export const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, defaultValue: 0 }); export const classNameType = rowType => { let result = ''; switch (rowType) { case 'NotScanned': result = 'backGroundError tableWidthColumnStatus'; break; case 'Duplicated': result = 'backGroundDuplicated tableWidthColumnStatus'; break; case 'Fine': result = 'backGroundOk tableWidthColumnStatus'; break; case 'InvalidAccount': // result = 'backGroundInvAccount tableWidthColumnStatus'; result = 'backGroundError tableWidthColumnStatus'; break; case 'InvalidRouting': // result = 'backGroundInvRouting tableWidthColumnStatus'; result = 'backGroundError tableWidthColumnStatus'; break; case 'InvalidCheckNumber': result = 'backGroundInvCheckNumber tableWidthColumnStatus'; break; default: result = 'NotScanned'; } return result; }; export function roundDecimal(value, ndec) { let n = 10; for (let i = 1; i < ndec; i += 1) { n *= 10; } if (!ndec || ndec <= 0) return Math.round(value); return Math.round(value * n) / n; } export const setAmountFormat = field => { const amountFormat = formatter.format(parseFloat(field)); return amountFormat; }; export const selectAll = (array, isSelectedAll) => { array.forEach(element => { const newElement = element; newElement.selected = !isSelectedAll; }); return array; }; export const loadImage = base64 => { const cnv = document.createElement('canvas'); if (base64 !== '' && !base64 !== undefined && base64 !== null) { const result = base64Arraybuffer.decode(base64); const buffer = new ArrayBuffer(base64.length); const bytes = new Uint8Array(buffer); base64.split('').forEach(function getbytes(str, i) { bytes[i] = str.charCodeAt(0); }); const ifds = utif.decode(result); utif.decodeImage(result, ifds[0]); const rgba = utif.toRGBA8(ifds[0]); const imageWidth = ifds[0].width; const imageHeight = ifds[0].height; cnv.width = imageWidth; cnv.height = imageHeight; if (imageWidth && imageHeight) { const ctx = cnv.getContext('2d'); const imageData = ctx.createImageData(imageWidth, imageHeight); for (let i = 0; i < rgba.length; i += 1) { imageData.data[i] = rgba[i]; } ctx.putImageData(imageData, 0, 0); } } return cnv.toDataURL(); }; const fixImageBase64 = base64 => { // console.log(base64); const bufferImage = window.atob(base64); const newImageBase64 = window.btoa(bufferImage); return newImageBase64; }; export const getImagePNGFormat = base64 => { try { const newImageBase64 = fixImageBase64(base64); return loadImage(newImageBase64).replace('data:image/png;base64,', ''); } catch (e) { return base64; } }; export const isNumeric = event => { const keycode = event.which; if (!(event.shiftKey === false && (keycode >= 48 && keycode <= 57))) { event.preventDefault(); } }; export const isDecimal = event => { const keycode = event.which; if ( !( event.shiftKey === false && ((keycode >= 48 && keycode <= 57) || keycode === 46) ) ) { event.preventDefault(); } }; export const isDate = event => { const keycode = event.which; if (!((keycode >= 48 && keycode <= 57) || keycode === 47)) { event.preventDefault(); } }; export const validateRequieredField = field => { let isValidField = false; if (field !== null && field !== undefined) { if (field !== '') { isValidField = true; } } return isValidField; }; export const replaceSpecialCharacter = field => { let newField = field; newField = newField.replace(/,/g, '').trim(); newField = newField.replace(/\./g, '').trim(); newField = newField.replace(/-/g, '').trim(); newField = newField.replace(/d/g, '').trim(); newField = newField.replace(/!/g, '').trim(); return newField; }; export default classNameType;