helper.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. export function convert2DArrayToArrayObj(values) {
  2. let keyIndex = 0;
  3. const keys = values.shift();
  4. const result = [];
  5. for (let columnIndex = 0; columnIndex < values.length; columnIndex += 1) {
  6. const currentColumn = {};
  7. for (
  8. let rowIndex = 0;
  9. rowIndex < values[columnIndex].length;
  10. rowIndex += 1
  11. ) {
  12. let key = keys[rowIndex];
  13. if (!key) {
  14. keyIndex += 1;
  15. key = `_row${keyIndex}`;
  16. keys.push(key);
  17. }
  18. currentColumn[key] = values[columnIndex][rowIndex];
  19. result.push(currentColumn);
  20. }
  21. }
  22. return result;
  23. }
  24. export function parseJSON(data, def) {
  25. try {
  26. const result = JSON.parse(data);
  27. return result;
  28. } catch (error) {
  29. return def;
  30. }
  31. }
  32. export function replaceMustache(str, replacer) {
  33. /* eslint-disable-next-line */
  34. return str.replace(/\{\{(.*?)\}\}/g, replacer);
  35. }
  36. export function openFilePicker(acceptedFileTypes = [], attrs = {}) {
  37. return new Promise((resolve, reject) => {
  38. const input = document.createElement('input');
  39. input.type = 'file';
  40. input.accept = acceptedFileTypes.join(',');
  41. Object.entries(attrs).forEach(([key, value]) => {
  42. input[key] = value;
  43. });
  44. input.onchange = (event) => {
  45. const file = event.target.files[0];
  46. if (!file || !acceptedFileTypes.includes(file.type)) {
  47. reject(new Error(`Invalid ${file.type} file type`));
  48. return;
  49. }
  50. resolve(file);
  51. };
  52. input.click();
  53. });
  54. }
  55. export function fileSaver(fileName, data) {
  56. const anchor = document.createElement('a');
  57. anchor.download = fileName;
  58. anchor.href = data;
  59. anchor.dispatchEvent(new MouseEvent('click'));
  60. anchor.remove();
  61. }
  62. export function countDuration(started, ended) {
  63. const duration = Math.round((ended - started) / 1000);
  64. const minutes = parseInt((duration / 60) % 60, 10);
  65. const seconds = parseInt(duration % 60, 10);
  66. const getText = (num, suffix) => (num > 0 ? `${num}${suffix}` : '');
  67. return `${getText(minutes, 'm')} ${seconds}s`;
  68. }
  69. export function toCamelCase(str) {
  70. const result = str.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => {
  71. return index === 0 ? letter.toLowerCase() : letter.toUpperCase();
  72. });
  73. return result.replace(/\s+|[-]/g, '');
  74. }
  75. export function isObject(obj) {
  76. return typeof obj === 'object' && obj !== null;
  77. }
  78. export function objectHasKey(obj, key) {
  79. return Object.prototype.hasOwnProperty.call(obj, key);
  80. }
  81. export function isWhitespace(str) {
  82. return !/\S/.test(str);
  83. }
  84. export function debounce(callback, time = 200) {
  85. let interval;
  86. return (...args) => {
  87. clearTimeout(interval);
  88. return new Promise((resolve) => {
  89. interval = setTimeout(() => {
  90. interval = null;
  91. callback(...args);
  92. resolve();
  93. }, time);
  94. });
  95. };
  96. }