Browse Source

Refactoring with `Media#set()`. Add `transformIgnorePatterns` to fix the issue that jest fails to rename caches.

Naotoshi Fujita 3 years ago
parent
commit
514a2ccff2

+ 13 - 13
dist/js/splide.cjs.js

@@ -610,11 +610,7 @@ function Media(Splide2, Components2, options) {
       return merge(merged2, entry[1].matches ? entry[0] : {});
     }, {});
     omit(options);
-    merge(options, merged);
-
-    if (!state.is(CREATED)) {
-      Splide2.emit(EVENT_UPDATED, options);
-    }
+    set(merged);
 
     if (options.destroy) {
       Splide2.destroy(options.destroy === "completely");
@@ -632,10 +628,20 @@ function Media(Splide2, Components2, options) {
     }
   }
 
+  function set(opts, user) {
+    merge(options, opts);
+    user && merge(Object.getPrototypeOf(options), opts);
+
+    if (!state.is(CREATED)) {
+      Splide2.emit(EVENT_UPDATED, options);
+    }
+  }
+
   return {
     setup: setup,
     destroy: destroy,
-    reduce: reduce
+    reduce: reduce,
+    set: set
   };
 }
 
@@ -3087,13 +3093,7 @@ var _Splide = /*#__PURE__*/function () {
       return this._o;
     },
     set: function set(options) {
-      var _o = this._o;
-      merge(_o, options);
-      merge(Object.getPrototypeOf(_o), options);
-
-      if (!this.state.is(CREATED)) {
-        this.emit(EVENT_UPDATED, _o);
-      }
+      this._C.Media.set(options, true);
     }
   }, {
     key: "length",

+ 13 - 13
dist/js/splide.esm.js

@@ -605,11 +605,7 @@ function Media(Splide2, Components2, options) {
       return merge(merged2, entry[1].matches ? entry[0] : {});
     }, {});
     omit(options);
-    merge(options, merged);
-
-    if (!state.is(CREATED)) {
-      Splide2.emit(EVENT_UPDATED, options);
-    }
+    set(merged);
 
     if (options.destroy) {
       Splide2.destroy(options.destroy === "completely");
@@ -627,10 +623,20 @@ function Media(Splide2, Components2, options) {
     }
   }
 
+  function set(opts, user) {
+    merge(options, opts);
+    user && merge(Object.getPrototypeOf(options), opts);
+
+    if (!state.is(CREATED)) {
+      Splide2.emit(EVENT_UPDATED, options);
+    }
+  }
+
   return {
     setup: setup,
     destroy: destroy,
-    reduce: reduce
+    reduce: reduce,
+    set: set
   };
 }
 
@@ -3082,13 +3088,7 @@ var _Splide = /*#__PURE__*/function () {
       return this._o;
     },
     set: function set(options) {
-      var _o = this._o;
-      merge(_o, options);
-      merge(Object.getPrototypeOf(_o), options);
-
-      if (!this.state.is(CREATED)) {
-        this.emit(EVENT_UPDATED, _o);
-      }
+      this._C.Media.set(options, true);
     }
   }, {
     key: "length",

+ 13 - 13
dist/js/splide.js

@@ -606,11 +606,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
         return merge(merged2, entry[1].matches ? entry[0] : {});
       }, {});
       omit(options);
-      merge(options, merged);
-
-      if (!state.is(CREATED)) {
-        Splide2.emit(EVENT_UPDATED, options);
-      }
+      set(merged);
 
       if (options.destroy) {
         Splide2.destroy(options.destroy === "completely");
@@ -628,10 +624,20 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
       }
     }
 
+    function set(opts, user) {
+      merge(options, opts);
+      user && merge(Object.getPrototypeOf(options), opts);
+
+      if (!state.is(CREATED)) {
+        Splide2.emit(EVENT_UPDATED, options);
+      }
+    }
+
     return {
       setup: setup,
       destroy: destroy,
-      reduce: reduce
+      reduce: reduce,
+      set: set
     };
   }
 
@@ -3080,13 +3086,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
         return this._o;
       },
       set: function set(options) {
-        var _o = this._o;
-        merge(_o, options);
-        merge(Object.getPrototypeOf(_o), options);
-
-        if (!this.state.is(CREATED)) {
-          this.emit(EVENT_UPDATED, _o);
-        }
+        this._C.Media.set(options, true);
       }
     }, {
       key: "length",

File diff suppressed because it is too large
+ 0 - 0
dist/js/splide.min.js


BIN
dist/js/splide.min.js.gz


File diff suppressed because it is too large
+ 0 - 0
dist/js/splide.min.js.map


+ 1 - 0
dist/types/index.d.ts

@@ -6,6 +6,7 @@
 interface MediaComponent extends BaseComponent {
     /** @internal */
     reduce(reduced: boolean): void;
+    set(options: Options, userOptions?: boolean): void;
 }
 
 /**

+ 8 - 4
jest.config.js

@@ -1,10 +1,14 @@
 module.exports = {
-  rootDir  : './src',
+  rootDir: './src',
   transform: {
     '^.+\\.(ts|tsx)$': 'ts-jest',
   },
   testEnvironment: 'jsdom',
-	setupFiles: [
-		'./js/test/jest/setup.ts',
-	],
+  setupFiles: [
+    './js/test/jest/setup.ts',
+  ],
+  transformIgnorePatterns: [
+    '<rootDir>/node_modules/@babel',
+    '<rootDir>/node_modules/@jest',
+  ],
 };

+ 22 - 5
src/js/components/Media/Media.ts

@@ -15,6 +15,7 @@ import { EVENT_UPDATED } from "../../constants/events";
 export interface MediaComponent extends BaseComponent {
   /** @internal */
   reduce( reduced: boolean ): void;
+  set( options: Options, userOptions?: boolean ): void;
 }
 
 /**
@@ -90,11 +91,7 @@ export function Media( Splide: Splide, Components: Components, options: Options
     }, {} );
 
     omit( options );
-    merge( options, merged );
-
-    if ( ! state.is( CREATED ) ) {
-      Splide.emit( EVENT_UPDATED, options );
-    }
+    set( merged );
 
     if ( options.destroy ) {
       Splide.destroy( options.destroy === 'completely' );
@@ -110,6 +107,8 @@ export function Media( Splide: Splide, Components: Components, options: Options
    * Disables or enables `reducedMotion` options.
    * This method does nothing when the document does not match the query.
    *
+   * @internal
+   *
    * @param enable - Determines whether to apply `reducedMotion` options or not.
    */
   function reduce( enable: boolean ): void {
@@ -118,9 +117,27 @@ export function Media( Splide: Splide, Components: Components, options: Options
     }
   }
 
+  /**
+   * Sets options.
+   *
+   * @internal
+   *
+   * @param opts - New options.
+   * @param user - Optional. Determines whether to also update user options or not.
+   */
+  function set( opts: Options, user?: boolean ): void {
+    merge( options, opts );
+    user && merge( Object.getPrototypeOf( options ), opts );
+
+    if ( ! state.is( CREATED ) ) {
+      Splide.emit( EVENT_UPDATED, options );
+    }
+  }
+
   return {
     setup,
     destroy,
     reduce,
+    set,
   };
 }

+ 2 - 8
src/js/core/Splide/Splide.ts

@@ -2,7 +2,7 @@ import * as ComponentConstructors from '../../components';
 import { SlideMatcher } from '../../components/Slides/Slides';
 import { CLASS_INITIALIZED } from '../../constants/classes';
 import { DEFAULTS } from '../../constants/defaults';
-import { EVENT_DESTROY, EVENT_MOUNTED, EVENT_READY, EVENT_REFRESH, EVENT_UPDATED } from '../../constants/events';
+import { EVENT_DESTROY, EVENT_MOUNTED, EVENT_READY, EVENT_REFRESH } from '../../constants/events';
 import { DATA_ATTRIBUTE } from '../../constants/project';
 import { CREATED, DESTROYED, IDLE, STATES } from '../../constants/states';
 import { FADE } from '../../constants/types';
@@ -379,13 +379,7 @@ export class Splide {
    * @param options - An object with new options.
    */
   set options( options: Options ) {
-    const { _o } = this;
-    merge( _o, options );
-    merge( Object.getPrototypeOf( _o ), options );
-
-    if ( ! this.state.is( CREATED ) ) {
-      this.emit( EVENT_UPDATED, _o );
-    }
+    this._C.Media.set( options, true );
   }
 
   /**

Some files were not shown because too many files changed in this diff