Skip to content

Commit fb24a3a

Browse files
Use ES6 default values syntax in config.ts
1 parent 34bb189 commit fb24a3a

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

src/utils/config.ts

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
DragConfig,
77
Tuple,
88
GestureOptions,
9-
DragOptions,
109
InternalDragOptions,
1110
InternalGestureOptions,
1211
CoordinatesConfig,
@@ -18,9 +17,15 @@ import {
1817

1918
const DEFAULT_DRAG_DELAY = 180
2019
const DEFAULT_RUBBERBAND = 0.15
20+
const DEFAULT_SWIPE_VELOCITY = 0.5
21+
const DEFAULT_SWIPE_DISTANCE = 60
2122

22-
function getWindow() {
23-
return typeof window !== 'undefined' ? window : undefined
23+
const defaultWindow = typeof window !== 'undefined' ? window : undefined
24+
25+
const defaultCoordinatesOptions: CoordinatesOptions = {
26+
lockDirection: false,
27+
axis: undefined,
28+
bounds: undefined,
2429
}
2530

2631
/**
@@ -32,38 +37,28 @@ function getWindow() {
3237
* @returns {InternalGenericOptions}
3338
*/
3439
export function getInternalGenericOptions(config: Partial<GenericOptions> = {}): InternalGenericOptions {
35-
const defaultOptions: GenericOptions = {
36-
domTarget: undefined,
37-
eventOptions: { passive: true, capture: false, pointer: false },
38-
window: getWindow(),
39-
enabled: true,
40-
}
41-
42-
const { eventOptions: defaultEventOptions, window: defaultWindow, ...restDefault } = defaultOptions
43-
const { eventOptions, window, ...restConfig } = config
44-
const { passive, capture, pointer } = { ...defaultEventOptions, ...eventOptions }
40+
let {
41+
eventOptions: { passive = true, capture = false, pointer = false } = {},
42+
window = defaultWindow,
43+
domTarget = undefined,
44+
enabled = true,
45+
...restConfig
46+
} = config
4547

4648
return {
47-
...restDefault,
4849
...restConfig,
49-
window: window || defaultWindow,
50+
enabled,
51+
domTarget,
52+
window,
5053
// passive is always true if there's no domTarget
51-
eventOptions: { passive: !config.domTarget || !!passive, capture: !!capture },
54+
eventOptions: { passive: !domTarget || !!passive, capture: !!capture },
5255
captureString: capture ? 'Capture' : '',
5356
pointer: !!pointer,
5457
}
5558
}
5659

5760
export function getInternalGestureOptions(gestureConfig: Partial<GestureOptions>): InternalGestureOptions {
58-
const defaultGestureOptions: GestureOptions = {
59-
enabled: true,
60-
initial: [0, 0],
61-
threshold: undefined,
62-
rubberband: 0,
63-
}
64-
65-
const config = { ...defaultGestureOptions, ...gestureConfig }
66-
let { threshold, rubberband, enabled, initial } = config
61+
let { threshold = undefined, rubberband = 0, enabled = true, initial = [0, 0] } = gestureConfig
6762

6863
if (typeof rubberband === 'boolean') rubberband = rubberband ? DEFAULT_RUBBERBAND : 0
6964
if (threshold === void 0) threshold = 0
@@ -77,12 +72,6 @@ export function getInternalGestureOptions(gestureConfig: Partial<GestureOptions>
7772
}
7873

7974
export function getInternalCoordinatesOptions(coordinatesConfig: CoordinatesConfig = {}): InternalCoordinatesOptions {
80-
const defaultCoordinatesOptions: CoordinatesOptions = {
81-
lockDirection: false,
82-
axis: undefined,
83-
bounds: undefined,
84-
}
85-
8675
const { axis, lockDirection, bounds = {}, ...internalOptions } = coordinatesConfig
8776

8877
const boundsArray = [
@@ -115,18 +104,15 @@ export function getInternalDistanceAngleOptions(
115104
}
116105

117106
export function getInternalDragOptions(dragConfig: DragConfig = {}): InternalDragOptions {
118-
const defaultDragOptions: DragOptions = {
119-
filterTaps: false,
120-
swipeVelocity: 0.5,
121-
swipeDistance: 60,
122-
delay: false,
123-
}
124-
125107
let { enabled, threshold, bounds, rubberband, initial, ...dragOptions } = dragConfig
126-
let { swipeVelocity, swipeDistance, delay, filterTaps, axis, lockDirection } = {
127-
...defaultDragOptions,
128-
...dragOptions,
129-
}
108+
let {
109+
swipeVelocity = DEFAULT_SWIPE_VELOCITY,
110+
swipeDistance = DEFAULT_SWIPE_DISTANCE,
111+
delay = false,
112+
filterTaps = false,
113+
axis,
114+
lockDirection,
115+
} = dragOptions
130116

131117
if (threshold === void 0) {
132118
threshold = Math.max(0, filterTaps ? 3 : 0, lockDirection || axis ? 1 : 0)

0 commit comments

Comments
 (0)