index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import Diacritics from "diacritic";
  2. import { customAlphabet } from "nanoid";
  3. const cleanWord = (word) => Diacritics.clean(word).toLowerCase();
  4. export const search = (term, string) => {
  5. let strings = string;
  6. if (typeof string === "string") {
  7. strings = [string];
  8. }
  9. const cleanedTerm = cleanWord(term);
  10. return strings.some((s) => cleanWord(s).includes(cleanedTerm));
  11. };
  12. export const hasClass = (element, className) =>
  13. element.classList && element.classList.contains(className);
  14. export const insideClass = (element, className) => {
  15. if (hasClass(element, className)) {
  16. return element;
  17. }
  18. if (!element.parentNode) {
  19. return false;
  20. }
  21. return insideClass(element.parentNode, className);
  22. };
  23. /**
  24. * Shuffles array in place.
  25. * @param {Array} a An array containing the items.
  26. */
  27. export const shuffle = (a) => {
  28. // eslint-disable-next-line no-plusplus
  29. for (let i = a.length - 1; i > 0; i--) {
  30. const j = Math.floor(Math.random() * (i + 1));
  31. // eslint-disable-next-line no-param-reassign
  32. [a[i], a[j]] = [a[j], a[i]];
  33. }
  34. return a;
  35. };
  36. export const randInt = (min, max) =>
  37. Math.floor(Math.random() * (max - min + 1)) + min;
  38. const isPointInsideRect = (point, rect) =>
  39. point.x > rect.left &&
  40. point.x < rect.left + rect.width &&
  41. point.y > rect.top &&
  42. point.y < rect.top + rect.height;
  43. export const isItemInsideElement = (itemElement, otherElem) => {
  44. const rect = otherElem.getBoundingClientRect();
  45. const fourElem = Array.from(itemElement.querySelectorAll(".corner"));
  46. return fourElem.every((corner) => {
  47. const { top: y, left: x } = corner.getBoundingClientRect();
  48. return isPointInsideRect({ x, y }, rect);
  49. });
  50. };
  51. const alpha = "23456789ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
  52. // Custom uid generator
  53. export const uid = customAlphabet(alpha, 10);
  54. // Custom small uid generator
  55. export const smallUid = customAlphabet(alpha, 5);