@@ -12,6 +12,7 @@ import { extend, getScrollBarSize } from './utils/general';
1212import { addOffset , attachmentToOffset , autoToFixedAttachment , offsetToPx , parseTopLeft } from './utils/offset' ;
1313import { getBounds , removeUtilElements } from './utils/bounds' ;
1414import { getOffsetParent , getScrollParents } from './utils/parents' ;
15+ import { isNumber , isObject , isString , isUndefined } from './utils/type-check' ;
1516
1617const TetherBase = { modules : [ Constraint , Abutment , Shift ] } ;
1718
@@ -26,7 +27,7 @@ function within(a, b, diff = 1) {
2627}
2728
2829const transformKey = ( ( ) => {
29- if ( typeof document === 'undefined' ) {
30+ if ( isUndefined ( document ) ) {
3031 return '' ;
3132 }
3233 const el = document . createElement ( 'div' ) ;
@@ -59,7 +60,7 @@ function now() {
5960 let pendingTimeout = null ;
6061
6162 const tick = ( ) => {
62- if ( typeof lastDuration !== 'undefined' && lastDuration > 16 ) {
63+ if ( ! isUndefined ( lastDuration ) && lastDuration > 16 ) {
6364 // We voluntarily throttle ourselves if we can't manage 60fps
6465 lastDuration = Math . min ( lastDuration - 16 , 250 ) ;
6566
@@ -68,7 +69,7 @@ function now() {
6869 return ;
6970 }
7071
71- if ( typeof lastCall !== 'undefined' && ( now ( ) - lastCall ) < 10 ) {
72+ if ( ! isUndefined ( lastCall ) && ( now ( ) - lastCall ) < 10 ) {
7273 // Some browsers call events a little too frequently, refuse to run more than is reasonable
7374 return ;
7475 }
@@ -83,7 +84,7 @@ function now() {
8384 lastDuration = now ( ) - lastCall ;
8485 } ;
8586
86- if ( typeof window !== 'undefined' && typeof window . addEventListener !== 'undefined' ) {
87+ if ( ! isUndefined ( window ) && ! isUndefined ( window . addEventListener ) ) {
8788 [ 'resize' , 'scroll' , 'touchmove' ] . forEach ( ( event ) => {
8889 window . addEventListener ( event , tick ) ;
8990 } ) ;
@@ -102,7 +103,7 @@ class TetherClass extends Evented {
102103 this . setOptions ( options , false ) ;
103104
104105 TetherBase . modules . forEach ( ( module ) => {
105- if ( typeof module . initialize !== 'undefined' ) {
106+ if ( ! isUndefined ( module . initialize ) ) {
106107 module . initialize . call ( this ) ;
107108 }
108109 } ) ;
@@ -112,7 +113,7 @@ class TetherClass extends Evented {
112113
113114 getClass ( key = '' ) {
114115 const { classes } = this . options ;
115- if ( typeof classes !== 'undefined' && typeof classes [ key ] !== 'undefined' ) {
116+ if ( ! isUndefined ( classes ) && ! isUndefined ( classes [ key ] ) ) {
116117 if ( classes [ key ] === false ) {
117118 return '' ;
118119 }
@@ -148,13 +149,13 @@ class TetherClass extends Evented {
148149 }
149150
150151 [ 'element' , 'target' ] . forEach ( ( key ) => {
151- if ( typeof this [ key ] === 'undefined' ) {
152+ if ( isUndefined ( this [ key ] ) ) {
152153 throw new Error ( 'Tether Error: Both element and target must be defined' ) ;
153154 }
154155
155- if ( typeof this [ key ] . jquery !== 'undefined' ) {
156+ if ( ! isUndefined ( this [ key ] . jquery ) ) {
156157 this [ key ] = this [ key ] [ 0 ] ;
157- } else if ( typeof this [ key ] === 'string' ) {
158+ } else if ( isString ( this [ key ] ) ) {
158159 this [ key ] = document . querySelector ( this [ key ] ) ;
159160 }
160161 } ) ;
@@ -170,7 +171,7 @@ class TetherClass extends Evented {
170171 this . offset = parseTopLeft ( this . options . offset ) ;
171172 this . targetOffset = parseTopLeft ( this . options . targetOffset ) ;
172173
173- if ( typeof this . scrollParents !== 'undefined' ) {
174+ if ( ! isUndefined ( this . scrollParents ) ) {
174175 this . disable ( ) ;
175176 }
176177
@@ -186,7 +187,7 @@ class TetherClass extends Evented {
186187 }
187188
188189 getTargetBounds ( ) {
189- if ( typeof this . targetModifier !== 'undefined' ) {
190+ if ( ! isUndefined ( this . targetModifier ) ) {
190191 if ( this . targetModifier === 'visible' ) {
191192 if ( this . target === document . body ) {
192193 return { top : pageYOffset , left : pageXOffset , height : innerHeight , width : innerWidth } ;
@@ -286,11 +287,11 @@ class TetherClass extends Evented {
286287 cache ( k , getter ) {
287288 // More than one module will often need the same DOM info, so
288289 // we keep a cache which is cleared on each position call
289- if ( typeof this . _cache === 'undefined' ) {
290+ if ( isUndefined ( this . _cache ) ) {
290291 this . _cache = { } ;
291292 }
292293
293- if ( typeof this . _cache [ k ] === 'undefined' ) {
294+ if ( isUndefined ( this . _cache [ k ] ) ) {
294295 this . _cache [ k ] = getter . call ( this ) ;
295296 }
296297
@@ -320,7 +321,7 @@ class TetherClass extends Evented {
320321 removeClass ( this . element , this . getClass ( 'enabled' ) ) ;
321322 this . enabled = false ;
322323
323- if ( typeof this . scrollParents !== 'undefined' ) {
324+ if ( ! isUndefined ( this . scrollParents ) ) {
324325 this . scrollParents . forEach ( ( parent ) => {
325326 parent . removeEventListener ( 'scroll' , this . position ) ;
326327 } ) ;
@@ -349,14 +350,14 @@ class TetherClass extends Evented {
349350 targetAttach = targetAttach || this . targetAttachment ;
350351 const sides = [ 'left' , 'top' , 'bottom' , 'right' , 'middle' , 'center' ] ;
351352
352- if ( typeof this . _addAttachClasses !== 'undefined' && this . _addAttachClasses . length ) {
353+ if ( ! isUndefined ( this . _addAttachClasses ) && this . _addAttachClasses . length ) {
353354 // updateAttachClasses can be called more than once in a position call, so
354355 // we need to clean up after ourselves such that when the last defer gets
355356 // ran it doesn't add any extra classes from previous calls.
356357 this . _addAttachClasses . splice ( 0 , this . _addAttachClasses . length ) ;
357358 }
358359
359- if ( typeof this . _addAttachClasses === 'undefined' ) {
360+ if ( isUndefined ( this . _addAttachClasses ) ) {
360361 this . _addAttachClasses = [ ] ;
361362 }
362363 this . add = this . _addAttachClasses ;
@@ -381,7 +382,7 @@ class TetherClass extends Evented {
381382 } ) ;
382383
383384 defer ( ( ) => {
384- if ( ! ( typeof this . _addAttachClasses !== 'undefined' ) ) {
385+ if ( isUndefined ( this . _addAttachClasses ) ) {
385386 return ;
386387 }
387388
@@ -415,7 +416,7 @@ class TetherClass extends Evented {
415416
416417 let { width, height } = elementPos ;
417418
418- if ( width === 0 && height === 0 && typeof this . lastSize !== 'undefined' ) {
419+ if ( width === 0 && height === 0 && ! isUndefined ( this . lastSize ) ) {
419420 // We cache the height and width to make it possible to position elements that are
420421 // getting hidden.
421422 ( { width, height } = this . lastSize ) ;
@@ -461,7 +462,7 @@ class TetherClass extends Evented {
461462
462463 if ( ret === false ) {
463464 return false ;
464- } else if ( typeof ret === 'undefined' || typeof ret !== 'object' ) {
465+ } else if ( isUndefined ( ret ) || ! isObject ( ret ) ) {
465466 continue ;
466467 } else {
467468 ( { top, left } = ret ) ;
@@ -509,9 +510,9 @@ class TetherClass extends Evented {
509510 next . page . right = doc . body . scrollWidth - left - width ;
510511 }
511512
512- if ( typeof this . options . optimizations !== 'undefined' &&
513+ if ( ! isUndefined ( this . options . optimizations ) &&
513514 this . options . optimizations . moveElement !== false &&
514- ! ( typeof this . targetModifier !== 'undefined' ) ) {
515+ isUndefined ( this . targetModifier ) ) {
515516 const offsetParent = this . cache ( 'target-offsetparent' , ( ) => getOffsetParent ( this . target ) ) ;
516517 const offsetPosition = this . cache ( 'target-offsetparent-bounds' , ( ) => getBounds ( offsetParent ) ) ;
517518 const offsetParentStyle = getComputedStyle ( offsetParent ) ;
@@ -560,7 +561,7 @@ class TetherClass extends Evented {
560561
561562 // THE ISSUE
562563 move ( pos ) {
563- if ( ! ( typeof this . element . parentNode !== 'undefined' ) ) {
564+ if ( isUndefined ( this . element . parentNode ) ) {
564565 return ;
565566 }
566567
@@ -574,7 +575,7 @@ class TetherClass extends Evented {
574575
575576 for ( let i = 0 ; i < this . history . length ; ++ i ) {
576577 const point = this . history [ i ] ;
577- if ( typeof point [ type ] !== 'undefined' &&
578+ if ( ! isUndefined ( point [ type ] ) &&
578579 ! within ( point [ type ] [ key ] , pos [ type ] [ key ] ) ) {
579580 found = true ;
580581 break ;
@@ -591,7 +592,7 @@ class TetherClass extends Evented {
591592 let css = { top : '' , left : '' , right : '' , bottom : '' } ;
592593
593594 const transcribe = ( _same , _pos ) => {
594- const hasOptimizations = typeof this . options . optimizations !== 'undefined' ;
595+ const hasOptimizations = ! isUndefined ( this . options . optimizations ) ;
595596 const gpu = hasOptimizations ? this . options . optimizations . gpu : null ;
596597 if ( gpu !== false ) {
597598 let yPos , xPos ;
@@ -611,7 +612,7 @@ class TetherClass extends Evented {
611612 xPos = - _pos . right ;
612613 }
613614
614- if ( typeof window . devicePixelRatio === 'number' && devicePixelRatio % 1 === 0 ) {
615+ if ( isNumber ( window . devicePixelRatio ) && devicePixelRatio % 1 === 0 ) {
615616 xPos = Math . round ( xPos * devicePixelRatio ) / devicePixelRatio ;
616617 yPos = Math . round ( yPos * devicePixelRatio ) / devicePixelRatio ;
617618 }
@@ -639,7 +640,7 @@ class TetherClass extends Evented {
639640 }
640641 } ;
641642
642- const hasOptimizations = typeof this . options . optimizations !== 'undefined' ;
643+ const hasOptimizations = ! isUndefined ( this . options . optimizations ) ;
643644 let allowPositionFixed = true ;
644645
645646 if ( hasOptimizations && this . options . optimizations . allowPositionFixed === false ) {
@@ -654,7 +655,7 @@ class TetherClass extends Evented {
654655 } else if ( allowPositionFixed && ( same . viewport . top || same . viewport . bottom ) && ( same . viewport . left || same . viewport . right ) ) {
655656 css . position = 'fixed' ;
656657 transcribe ( same . viewport , pos . viewport ) ;
657- } else if ( typeof same . offset !== 'undefined' && same . offset . top && same . offset . left ) {
658+ } else if ( ! isUndefined ( same . offset ) && same . offset . top && same . offset . left ) {
658659 css . position = 'absolute' ;
659660 const offsetParent = this . cache ( 'target-offsetparent' , ( ) => getOffsetParent ( this . target ) ) ;
660661
@@ -773,8 +774,7 @@ Tether.modules.push({
773774 const offset = offsets [ type ] ;
774775 for ( let side in offset ) {
775776 let val = offset [ side ] ;
776- const notString = typeof val !== 'string' ;
777- if ( notString ||
777+ if ( ! isString ( val ) ||
778778 val . indexOf ( '%' ) === - 1 &&
779779 val . indexOf ( 'px' ) === - 1 ) {
780780 val += 'px' ;
0 commit comments