utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import dayjs from 'dayjs';
  2. import {Buffer} from 'safe-buffer';
  3. //import _ from 'lodash';
  4. export function sleep(ms) {
  5. return new Promise(resolve => setTimeout(resolve, ms));
  6. }
  7. export function toHex(buf) {
  8. return Buffer.from(buf).toString('hex');
  9. }
  10. export function keyEventToCode(event) {
  11. let result = [];
  12. let code = event.code;
  13. const modCode = code.substring(0, 3);
  14. if (event.metaKey && modCode != 'Met')
  15. result.push('Meta');
  16. if (event.ctrlKey && modCode != 'Con')
  17. result.push('Ctrl');
  18. if (event.shiftKey && modCode != 'Shi')
  19. result.push('Shift');
  20. if (event.altKey && modCode != 'Alt')
  21. result.push('Alt');
  22. if (modCode == 'Dig') {
  23. code = code.substring(5, 6);
  24. } else if (modCode == 'Key') {
  25. code = code.substring(3, 4);
  26. }
  27. result.push(code);
  28. return result.join('+');
  29. }
  30. export function wordEnding(num, type = 0) {
  31. const endings = [
  32. ['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'],//0
  33. ['й', 'я', 'и', 'и', 'и', 'й', 'й', 'й', 'й', 'й'],//1
  34. ['о', '', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//2
  35. ['ий', 'ие', 'ия', 'ия', 'ия', 'ий', 'ий', 'ий', 'ий', 'ий'],//3
  36. ['о', 'а', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//4
  37. ['ок', 'ка', 'ки', 'ки', 'ки', 'ок', 'ок', 'ок', 'ок', 'ок'],//5
  38. ['ых', 'ое', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых'],//6
  39. ['о', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//7
  40. ];
  41. const deci = num % 100;
  42. if (deci > 10 && deci < 20) {
  43. return endings[type][0];
  44. } else {
  45. return endings[type][num % 10];
  46. }
  47. }
  48. export function fallbackCopyTextToClipboard(text) {
  49. let textArea = document.createElement('textarea');
  50. textArea.value = text;
  51. document.body.appendChild(textArea);
  52. textArea.focus();
  53. textArea.select();
  54. let result = false;
  55. try {
  56. result = document.execCommand('copy');
  57. } catch (e) {
  58. console.error(e);
  59. }
  60. document.body.removeChild(textArea);
  61. return result;
  62. }
  63. export async function copyTextToClipboard(text) {
  64. if (!navigator.clipboard) {
  65. return fallbackCopyTextToClipboard(text);
  66. }
  67. let result = false;
  68. try {
  69. await navigator.clipboard.writeText(text);
  70. result = true;
  71. } catch (e) {
  72. console.error(e);
  73. }
  74. return result;
  75. }
  76. export function isDigit(c) {
  77. return !isNaN(parseInt(c, 10));
  78. }
  79. export function dateFormat(date, format = 'DD.MM.YYYY') {
  80. return dayjs(date).format(format);
  81. }
  82. export function sqlDateFormat(date, format = 'DD.MM.YYYY') {
  83. return dayjs(date, 'YYYY-MM-DD').format(format);
  84. }
  85. export function isManualDate(date) {
  86. return date && (date[0] == ',' || (isDigit(date[0]) && isDigit(date[1])));
  87. }