index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import Diacritics from "diacritic";
  2. import { customAlphabet } from "nanoid";
  3. export {
  4. isItemInsideElement,
  5. getItemElement,
  6. pointInItem,
  7. fastItemIntersect,
  8. } from "./item";
  9. const cleanWord = (word) => Diacritics.clean(word).toLowerCase();
  10. export const search = (term, string) => {
  11. let strings = string;
  12. if (typeof string === "string") {
  13. strings = [string];
  14. }
  15. const cleanedTerm = cleanWord(term);
  16. return strings.some((s) => cleanWord(s).includes(cleanedTerm));
  17. };
  18. export const hasClass = (element, className) =>
  19. element.classList && element.classList.contains(className);
  20. export const insideClass = (element, className) => {
  21. if (hasClass(element, className)) {
  22. return element;
  23. }
  24. if (!element.parentNode) {
  25. return false;
  26. }
  27. return insideClass(element.parentNode, className);
  28. };
  29. /**
  30. * Shuffles array in place.
  31. * @param {Array} a An array containing the items.
  32. */
  33. export const shuffle = (a) => {
  34. // eslint-disable-next-line no-plusplus
  35. for (let i = a.length - 1; i > 0; i -= 1) {
  36. const j = Math.floor(Math.random() * (i + 1));
  37. // eslint-disable-next-line no-param-reassign
  38. [a[i], a[j]] = [a[j], a[i]];
  39. }
  40. return a;
  41. };
  42. export const randInt = (min, max) =>
  43. Math.floor(Math.random() * (max - min + 1)) + min;
  44. const alpha = "23456789ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
  45. // Custom uid generator
  46. export const uid = customAlphabet(alpha, 10);
  47. // Custom small uid generator
  48. export const smallUid = customAlphabet(alpha, 5);
  49. export const objectIntersection = (o1, o2) => {
  50. const keys = [...new Set([...Object.keys(o1), ...Object.keys(o2)])];
  51. return keys.reduce((prev, key) => {
  52. if (
  53. prev[key] !== o2[key] &&
  54. JSON.stringify(prev[key]) !== JSON.stringify(o2[key])
  55. ) {
  56. delete prev[key];
  57. }
  58. return prev;
  59. }, JSON.parse(JSON.stringify(o1)));
  60. };
  61. export const objectDiff = (o1, o2) => {
  62. const keys = [...new Set([...Object.keys(o1), ...Object.keys(o2)])];
  63. const result = keys.reduce((diff, key) => {
  64. if (o1[key] === o2[key]) return diff;
  65. if (JSON.stringify(o1[key]) === JSON.stringify(o2[key])) return diff;
  66. return {
  67. ...diff,
  68. [key]: o2[key],
  69. };
  70. }, {});
  71. if (Object.keys(result).length === 0) {
  72. return null;
  73. }
  74. return result;
  75. };
  76. export const retry = (fn, condition, delay) => {
  77. if (condition()) {
  78. setTimeout(() => retry(fn, condition, delay), delay);
  79. } else {
  80. fn();
  81. }
  82. };
  83. const audioFiles = {};
  84. export const preloadAudio = (urls) => {
  85. urls.forEach((url) => {
  86. if (!audioFiles[url]) {
  87. audioFiles[url] = new Audio(url);
  88. }
  89. });
  90. };
  91. export const playAudio = (url, volume = 1) => {
  92. if (!audioFiles[url]) {
  93. audioFiles[url] = new Audio(url);
  94. }
  95. audioFiles[url].pause();
  96. audioFiles[url].currentTime = 0;
  97. audioFiles[url].volume = volume;
  98. audioFiles[url].play();
  99. };