@@ -12,6 +12,7 @@ import { extend, getScrollBarSize } from './utils/general';
12
12
import { addOffset , attachmentToOffset , autoToFixedAttachment , offsetToPx , parseTopLeft } from './utils/offset' ;
13
13
import { getBounds , removeUtilElements } from './utils/bounds' ;
14
14
import { getOffsetParent , getScrollParents } from './utils/parents' ;
15
+ import { isNumber , isObject , isString , isUndefined } from './utils/type-check' ;
15
16
16
17
const TetherBase = { modules : [ Constraint , Abutment , Shift ] } ;
17
18
@@ -26,7 +27,7 @@ function within(a, b, diff = 1) {
26
27
}
27
28
28
29
const transformKey = ( ( ) => {
29
- if ( typeof document === 'undefined' ) {
30
+ if ( isUndefined ( document ) ) {
30
31
return '' ;
31
32
}
32
33
const el = document . createElement ( 'div' ) ;
@@ -59,7 +60,7 @@ function now() {
59
60
let pendingTimeout = null ;
60
61
61
62
const tick = ( ) => {
62
- if ( typeof lastDuration !== 'undefined' && lastDuration > 16 ) {
63
+ if ( ! isUndefined ( lastDuration ) && lastDuration > 16 ) {
63
64
// We voluntarily throttle ourselves if we can't manage 60fps
64
65
lastDuration = Math . min ( lastDuration - 16 , 250 ) ;
65
66
@@ -68,7 +69,7 @@ function now() {
68
69
return ;
69
70
}
70
71
71
- if ( typeof lastCall !== 'undefined' && ( now ( ) - lastCall ) < 10 ) {
72
+ if ( ! isUndefined ( lastCall ) && ( now ( ) - lastCall ) < 10 ) {
72
73
// Some browsers call events a little too frequently, refuse to run more than is reasonable
73
74
return ;
74
75
}
@@ -83,7 +84,7 @@ function now() {
83
84
lastDuration = now ( ) - lastCall ;
84
85
} ;
85
86
86
- if ( typeof window !== 'undefined' && typeof window . addEventListener !== 'undefined' ) {
87
+ if ( ! isUndefined ( window ) && ! isUndefined ( window . addEventListener ) ) {
87
88
[ 'resize' , 'scroll' , 'touchmove' ] . forEach ( ( event ) => {
88
89
window . addEventListener ( event , tick ) ;
89
90
} ) ;
@@ -102,7 +103,7 @@ class TetherClass extends Evented {
102
103
this . setOptions ( options , false ) ;
103
104
104
105
TetherBase . modules . forEach ( ( module ) => {
105
- if ( typeof module . initialize !== 'undefined' ) {
106
+ if ( ! isUndefined ( module . initialize ) ) {
106
107
module . initialize . call ( this ) ;
107
108
}
108
109
} ) ;
@@ -112,7 +113,7 @@ class TetherClass extends Evented {
112
113
113
114
getClass ( key = '' ) {
114
115
const { classes } = this . options ;
115
- if ( typeof classes !== 'undefined' && typeof classes [ key ] !== 'undefined' ) {
116
+ if ( ! isUndefined ( classes ) && ! isUndefined ( classes [ key ] ) ) {
116
117
if ( classes [ key ] === false ) {
117
118
return '' ;
118
119
}
@@ -148,13 +149,13 @@ class TetherClass extends Evented {
148
149
}
149
150
150
151
[ 'element' , 'target' ] . forEach ( ( key ) => {
151
- if ( typeof this [ key ] === 'undefined' ) {
152
+ if ( isUndefined ( this [ key ] ) ) {
152
153
throw new Error ( 'Tether Error: Both element and target must be defined' ) ;
153
154
}
154
155
155
- if ( typeof this [ key ] . jquery !== 'undefined' ) {
156
+ if ( ! isUndefined ( this [ key ] . jquery ) ) {
156
157
this [ key ] = this [ key ] [ 0 ] ;
157
- } else if ( typeof this [ key ] === 'string' ) {
158
+ } else if ( isString ( this [ key ] ) ) {
158
159
this [ key ] = document . querySelector ( this [ key ] ) ;
159
160
}
160
161
} ) ;
@@ -170,7 +171,7 @@ class TetherClass extends Evented {
170
171
this . offset = parseTopLeft ( this . options . offset ) ;
171
172
this . targetOffset = parseTopLeft ( this . options . targetOffset ) ;
172
173
173
- if ( typeof this . scrollParents !== 'undefined' ) {
174
+ if ( ! isUndefined ( this . scrollParents ) ) {
174
175
this . disable ( ) ;
175
176
}
176
177
@@ -186,7 +187,7 @@ class TetherClass extends Evented {
186
187
}
187
188
188
189
getTargetBounds ( ) {
189
- if ( typeof this . targetModifier !== 'undefined' ) {
190
+ if ( ! isUndefined ( this . targetModifier ) ) {
190
191
if ( this . targetModifier === 'visible' ) {
191
192
if ( this . target === document . body ) {
192
193
return { top : pageYOffset , left : pageXOffset , height : innerHeight , width : innerWidth } ;
@@ -286,11 +287,11 @@ class TetherClass extends Evented {
286
287
cache ( k , getter ) {
287
288
// More than one module will often need the same DOM info, so
288
289
// we keep a cache which is cleared on each position call
289
- if ( typeof this . _cache === 'undefined' ) {
290
+ if ( isUndefined ( this . _cache ) ) {
290
291
this . _cache = { } ;
291
292
}
292
293
293
- if ( typeof this . _cache [ k ] === 'undefined' ) {
294
+ if ( isUndefined ( this . _cache [ k ] ) ) {
294
295
this . _cache [ k ] = getter . call ( this ) ;
295
296
}
296
297
@@ -320,7 +321,7 @@ class TetherClass extends Evented {
320
321
removeClass ( this . element , this . getClass ( 'enabled' ) ) ;
321
322
this . enabled = false ;
322
323
323
- if ( typeof this . scrollParents !== 'undefined' ) {
324
+ if ( ! isUndefined ( this . scrollParents ) ) {
324
325
this . scrollParents . forEach ( ( parent ) => {
325
326
parent . removeEventListener ( 'scroll' , this . position ) ;
326
327
} ) ;
@@ -349,14 +350,14 @@ class TetherClass extends Evented {
349
350
targetAttach = targetAttach || this . targetAttachment ;
350
351
const sides = [ 'left' , 'top' , 'bottom' , 'right' , 'middle' , 'center' ] ;
351
352
352
- if ( typeof this . _addAttachClasses !== 'undefined' && this . _addAttachClasses . length ) {
353
+ if ( ! isUndefined ( this . _addAttachClasses ) && this . _addAttachClasses . length ) {
353
354
// updateAttachClasses can be called more than once in a position call, so
354
355
// we need to clean up after ourselves such that when the last defer gets
355
356
// ran it doesn't add any extra classes from previous calls.
356
357
this . _addAttachClasses . splice ( 0 , this . _addAttachClasses . length ) ;
357
358
}
358
359
359
- if ( typeof this . _addAttachClasses === 'undefined' ) {
360
+ if ( isUndefined ( this . _addAttachClasses ) ) {
360
361
this . _addAttachClasses = [ ] ;
361
362
}
362
363
this . add = this . _addAttachClasses ;
@@ -381,7 +382,7 @@ class TetherClass extends Evented {
381
382
} ) ;
382
383
383
384
defer ( ( ) => {
384
- if ( ! ( typeof this . _addAttachClasses !== 'undefined' ) ) {
385
+ if ( isUndefined ( this . _addAttachClasses ) ) {
385
386
return ;
386
387
}
387
388
@@ -415,7 +416,7 @@ class TetherClass extends Evented {
415
416
416
417
let { width, height } = elementPos ;
417
418
418
- if ( width === 0 && height === 0 && typeof this . lastSize !== 'undefined' ) {
419
+ if ( width === 0 && height === 0 && ! isUndefined ( this . lastSize ) ) {
419
420
// We cache the height and width to make it possible to position elements that are
420
421
// getting hidden.
421
422
( { width, height } = this . lastSize ) ;
@@ -461,7 +462,7 @@ class TetherClass extends Evented {
461
462
462
463
if ( ret === false ) {
463
464
return false ;
464
- } else if ( typeof ret === 'undefined' || typeof ret !== 'object' ) {
465
+ } else if ( isUndefined ( ret ) || ! isObject ( ret ) ) {
465
466
continue ;
466
467
} else {
467
468
( { top, left } = ret ) ;
@@ -509,9 +510,9 @@ class TetherClass extends Evented {
509
510
next . page . right = doc . body . scrollWidth - left - width ;
510
511
}
511
512
512
- if ( typeof this . options . optimizations !== 'undefined' &&
513
+ if ( ! isUndefined ( this . options . optimizations ) &&
513
514
this . options . optimizations . moveElement !== false &&
514
- ! ( typeof this . targetModifier !== 'undefined' ) ) {
515
+ isUndefined ( this . targetModifier ) ) {
515
516
const offsetParent = this . cache ( 'target-offsetparent' , ( ) => getOffsetParent ( this . target ) ) ;
516
517
const offsetPosition = this . cache ( 'target-offsetparent-bounds' , ( ) => getBounds ( offsetParent ) ) ;
517
518
const offsetParentStyle = getComputedStyle ( offsetParent ) ;
@@ -560,7 +561,7 @@ class TetherClass extends Evented {
560
561
561
562
// THE ISSUE
562
563
move ( pos ) {
563
- if ( ! ( typeof this . element . parentNode !== 'undefined' ) ) {
564
+ if ( isUndefined ( this . element . parentNode ) ) {
564
565
return ;
565
566
}
566
567
@@ -574,7 +575,7 @@ class TetherClass extends Evented {
574
575
575
576
for ( let i = 0 ; i < this . history . length ; ++ i ) {
576
577
const point = this . history [ i ] ;
577
- if ( typeof point [ type ] !== 'undefined' &&
578
+ if ( ! isUndefined ( point [ type ] ) &&
578
579
! within ( point [ type ] [ key ] , pos [ type ] [ key ] ) ) {
579
580
found = true ;
580
581
break ;
@@ -591,7 +592,7 @@ class TetherClass extends Evented {
591
592
let css = { top : '' , left : '' , right : '' , bottom : '' } ;
592
593
593
594
const transcribe = ( _same , _pos ) => {
594
- const hasOptimizations = typeof this . options . optimizations !== 'undefined' ;
595
+ const hasOptimizations = ! isUndefined ( this . options . optimizations ) ;
595
596
const gpu = hasOptimizations ? this . options . optimizations . gpu : null ;
596
597
if ( gpu !== false ) {
597
598
let yPos , xPos ;
@@ -611,7 +612,7 @@ class TetherClass extends Evented {
611
612
xPos = - _pos . right ;
612
613
}
613
614
614
- if ( typeof window . devicePixelRatio === 'number' && devicePixelRatio % 1 === 0 ) {
615
+ if ( isNumber ( window . devicePixelRatio ) && devicePixelRatio % 1 === 0 ) {
615
616
xPos = Math . round ( xPos * devicePixelRatio ) / devicePixelRatio ;
616
617
yPos = Math . round ( yPos * devicePixelRatio ) / devicePixelRatio ;
617
618
}
@@ -639,7 +640,7 @@ class TetherClass extends Evented {
639
640
}
640
641
} ;
641
642
642
- const hasOptimizations = typeof this . options . optimizations !== 'undefined' ;
643
+ const hasOptimizations = ! isUndefined ( this . options . optimizations ) ;
643
644
let allowPositionFixed = true ;
644
645
645
646
if ( hasOptimizations && this . options . optimizations . allowPositionFixed === false ) {
@@ -654,7 +655,7 @@ class TetherClass extends Evented {
654
655
} else if ( allowPositionFixed && ( same . viewport . top || same . viewport . bottom ) && ( same . viewport . left || same . viewport . right ) ) {
655
656
css . position = 'fixed' ;
656
657
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 ) {
658
659
css . position = 'absolute' ;
659
660
const offsetParent = this . cache ( 'target-offsetparent' , ( ) => getOffsetParent ( this . target ) ) ;
660
661
@@ -773,8 +774,7 @@ Tether.modules.push({
773
774
const offset = offsets [ type ] ;
774
775
for ( let side in offset ) {
775
776
let val = offset [ side ] ;
776
- const notString = typeof val !== 'string' ;
777
- if ( notString ||
777
+ if ( ! isString ( val ) ||
778
778
val . indexOf ( '%' ) === - 1 &&
779
779
val . indexOf ( 'px' ) === - 1 ) {
780
780
val += 'px' ;
0 commit comments