mirror of
https://github.com/mokuappio/serverless-invoices.git
synced 2025-10-28 08:21:08 -04:00
75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
/* eslint-disable */
|
|
export function uuidv4() {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
var r = Math.random() * 16 | 0,
|
|
v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
return v.toString(16);
|
|
});
|
|
}
|
|
|
|
export function range(end, start = 0, step = 1) {
|
|
const arr = [];
|
|
for (let i = start; i < end; i += step) {
|
|
arr.push(i);
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
/**
|
|
* Returns a subset of the object based on a map of keys
|
|
*
|
|
* @param obj
|
|
* @param map
|
|
*/
|
|
export function pick(obj, map) {
|
|
const pickedKeys = Object.keys(obj)
|
|
.filter(field => map.hasOwnProperty(field));
|
|
const objSubset = {};
|
|
pickedKeys.forEach((key) => {
|
|
objSubset[map[key]] = obj[key];
|
|
});
|
|
return objSubset;
|
|
}
|
|
|
|
|
|
export function validate(neededFields, fieldsToValidate) {
|
|
const validationErrors = {};
|
|
|
|
for (const [key, value] of Object.entries(neededFields)) {
|
|
if (Array.isArray(fieldsToValidate[key])) {
|
|
fieldsToValidate[key].forEach((item) => {
|
|
});
|
|
}
|
|
if (!fieldsToValidate.hasOwnProperty(key) || !fieldsToValidate[key] || fieldsToValidate[key].length === 0) {
|
|
validationErrors[key] = [`Field ${value} is required`];
|
|
}
|
|
}
|
|
|
|
console.log('errors', validationErrors);
|
|
return { errors: validationErrors };
|
|
}
|
|
|
|
|
|
export function generateInvoiceNumber(date, number) {
|
|
return `${date}-${number}`;
|
|
}
|
|
|
|
export function download(data, filename, type) {
|
|
var file = new Blob([data], { type: type });
|
|
if (window.navigator.msSaveOrOpenBlob) // IE10+
|
|
{
|
|
window.navigator.msSaveOrOpenBlob(file, filename);
|
|
} else { // Others
|
|
var a = document.createElement('a'),
|
|
url = URL.createObjectURL(file);
|
|
a.href = url;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
setTimeout(function () {
|
|
document.body.removeChild(a);
|
|
window.URL.revokeObjectURL(url);
|
|
}, 0);
|
|
}
|
|
}
|