utils.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //import _ from 'lodash';
  2. export function sleep(ms) {
  3. return new Promise(resolve => setTimeout(resolve, ms));
  4. }
  5. export function keyEventToCode(event) {
  6. let result = [];
  7. let code = event.code;
  8. const modCode = code.substring(0, 3);
  9. if (event.metaKey && modCode != 'Met')
  10. result.push('Meta');
  11. if (event.ctrlKey && modCode != 'Con')
  12. result.push('Ctrl');
  13. if (event.shiftKey && modCode != 'Shi')
  14. result.push('Shift');
  15. if (event.altKey && modCode != 'Alt')
  16. result.push('Alt');
  17. if (modCode == 'Dig') {
  18. code = code.substring(5, 6);
  19. } else if (modCode == 'Key') {
  20. code = code.substring(3, 4);
  21. }
  22. result.push(code);
  23. return result.join('+');
  24. }
  25. export function wordEnding(num, type = 0) {
  26. const endings = [
  27. ['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'],
  28. ['й', 'я', 'и', 'и', 'и', 'й', 'й', 'й', 'й', 'й'],
  29. ['о', '', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],
  30. ['ий', 'ие', 'ия', 'ия', 'ия', 'ий', 'ий', 'ий', 'ий', 'ий']
  31. ];
  32. const deci = num % 100;
  33. if (deci > 10 && deci < 20) {
  34. return endings[type][0];
  35. } else {
  36. return endings[type][num % 10];
  37. }
  38. }
  39. export async function copyTextToClipboard(text) {
  40. let result = false;
  41. try {
  42. await navigator.clipboard.writeText(text);
  43. result = true;
  44. } catch (e) {
  45. //
  46. }
  47. return result;
  48. }
  49. export function makeValidFilename(filename, repl = '_') {
  50. let f = filename.replace(/[\x00\\/:*"<>|]/g, repl); // eslint-disable-line no-control-regex
  51. f = f.trim();
  52. while (f.length && (f[f.length - 1] == '.' || f[f.length - 1] == '_')) {
  53. f = f.substring(0, f.length - 1);
  54. }
  55. if (f)
  56. return f;
  57. else
  58. throw new Error('Invalid filename');
  59. }