index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { nanoid } from 'nanoid'
  2. export * from './BoundsUtils'
  3. export * from './PointUtils'
  4. export * from './KeyUtils'
  5. export * from './GeomUtils'
  6. export * from './PolygonUtils'
  7. export * from './SvgPathUtils'
  8. export * from './DataUtils'
  9. export * from './TextUtils'
  10. export function uniqueId() {
  11. return nanoid()
  12. }
  13. // via https://github.com/bameyrick/throttle-typescript
  14. export function throttle<T extends (...args: any) => any>(
  15. func: T,
  16. limit: number
  17. ): (...args: Parameters<T>) => ReturnType<T> {
  18. let inThrottle: boolean
  19. let lastResult: ReturnType<T>
  20. return function (this: any, ...args: any[]): ReturnType<T> {
  21. if (!inThrottle) {
  22. inThrottle = true
  23. setTimeout(() => (inThrottle = false), limit)
  24. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  25. // @ts-ignore
  26. lastResult = func(...args)
  27. }
  28. return lastResult
  29. }
  30. }
  31. export function debounce<T extends (...args: any[]) => void>(fn: T, ms = 0) {
  32. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  33. let timeoutId: number | any
  34. return function (...args: Parameters<T>) {
  35. clearTimeout(timeoutId)
  36. timeoutId = setTimeout(() => fn.apply(args), ms)
  37. }
  38. }
  39. /** Linear interpolate between two values. */
  40. export function lerp(a: number, b: number, t: number) {
  41. return a + (b - a) * t
  42. }
  43. /** Find whether the current device is a Mac / iOS / iPadOS. */
  44. export function isDarwin(): boolean {
  45. return /Mac|iPod|iPhone|iPad/.test(window.navigator.platform)
  46. }
  47. /**
  48. * Get whether an event is command (mac) or control (pc).
  49. *
  50. * @param e
  51. */
  52. export function modKey(e: any): boolean {
  53. return isDarwin() ? e.metaKey : e.ctrlKey
  54. }