utils.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. function isObject(o) {
  2. return (
  3. typeof o === 'object' &&
  4. o !== null &&
  5. o.constructor &&
  6. Object.prototype.toString.call(o).slice(8, -1) === 'Object'
  7. );
  8. }
  9. function extend(target, src) {
  10. const noExtend = ['__proto__', 'constructor', 'prototype'];
  11. Object.keys(src)
  12. .filter((key) => noExtend.indexOf(key) < 0)
  13. .forEach((key) => {
  14. if (typeof target[key] === 'undefined') target[key] = src[key];
  15. else if (isObject(src[key]) && isObject(target[key]) && Object.keys(src[key]).length > 0) {
  16. if (src[key].__swiper__) target[key] = src[key];
  17. else extend(target[key], src[key]);
  18. } else {
  19. target[key] = src[key];
  20. }
  21. });
  22. }
  23. function needsNavigation(props = {}) {
  24. return (
  25. props.navigation &&
  26. typeof props.navigation.nextEl === 'undefined' &&
  27. typeof props.navigation.prevEl === 'undefined'
  28. );
  29. }
  30. function needsPagination(props = {}) {
  31. return props.pagination && typeof props.pagination.el === 'undefined';
  32. }
  33. function needsScrollbar(props = {}) {
  34. return props.scrollbar;
  35. }
  36. function uniqueClasses(classNames = '') {
  37. const classes = classNames
  38. .split(' ')
  39. .map((c) => c.trim())
  40. .filter((c) => !!c);
  41. const unique = [];
  42. classes.forEach((c) => {
  43. if (unique.indexOf(c) < 0) unique.push(c);
  44. });
  45. return unique.join(' ');
  46. }
  47. export {
  48. isObject,
  49. extend,
  50. needsNavigation,
  51. needsPagination,
  52. needsScrollbar,
  53. uniqueClasses
  54. };