utils.js 1.9 KB

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