Skip to content

Commit 47613e5

Browse files
committed
refactoring to es6
1 parent b57e375 commit 47613e5

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/legacy.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
*/
88

99
// These values are established by empiricism with tests (tradeoff: performance VS precision)
10-
var NEWTON_ITERATIONS = 4
11-
var NEWTON_MIN_SLOPE = 0.001
12-
var SUBDIVISION_PRECISION = 0.0000001
13-
var SUBDIVISION_MAX_ITERATIONS = 10
10+
const NEWTON_ITERATIONS = 4
11+
const NEWTON_MIN_SLOPE = 0.001
12+
const SUBDIVISION_PRECISION = 0.0000001
13+
const SUBDIVISION_MAX_ITERATIONS = 10
1414

15-
var kSplineTableSize = 11
16-
var kSampleStepSize = 1.0 / (kSplineTableSize - 1.0)
15+
const kSplineTableSize = 11
16+
const kSampleStepSize = 1.0 / (kSplineTableSize - 1.0)
1717

18-
var float32ArraySupported = typeof Float32Array === 'function'
18+
const float32ArraySupported = typeof Float32Array === 'function'
1919

20-
function A(aA1, aA2) {
20+
const A = (aA1, aA2) => {
2121
return 1.0 - 3.0 * aA2 + 3.0 * aA1
2222
}
2323
function B(aA1, aA2) {
@@ -28,16 +28,16 @@ function C(aA1) {
2828
}
2929

3030
// Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2.
31-
function calcBezier(aT, aA1, aA2) {
31+
const calcBezier = (aT, aA1, aA2) => {
3232
return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT
3333
}
3434

3535
// Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2.
36-
function getSlope(aT, aA1, aA2) {
36+
const getSlope = (aT, aA1, aA2) => {
3737
return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1)
3838
}
3939

40-
function binarySubdivide(aX, aA, aB, mX1, mX2) {
40+
const binarySubdivide = (aX, aA, aB, mX1, mX2) => {
4141
var currentX,
4242
currentT,
4343
i = 0
@@ -56,7 +56,7 @@ function binarySubdivide(aX, aA, aB, mX1, mX2) {
5656
return currentT
5757
}
5858

59-
function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
59+
const newtonRaphsonIterate = (aX, aGuessT, mX1, mX2) => {
6060
for (var i = 0; i < NEWTON_ITERATIONS; ++i) {
6161
var currentSlope = getSlope(aGuessT, mX1, mX2)
6262
if (currentSlope === 0.0) {
@@ -68,7 +68,7 @@ function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
6868
return aGuessT
6969
}
7070

71-
function bezier(mX1, mY1, mX2, mY2) {
71+
const bezier = (mX1, mY1, mX2, mY2) => {
7272
if (!(0 <= mX1 && mX1 <= 1 && 0 <= mX2 && mX2 <= 1)) {
7373
throw new Error('bezier x values must be in [0, 1] range')
7474
}
@@ -83,7 +83,7 @@ function bezier(mX1, mY1, mX2, mY2) {
8383
}
8484
}
8585

86-
function getTForX(aX) {
86+
const getTForX = aX => {
8787
var intervalStart = 0.0
8888
var currentSample = 1
8989
var lastSample = kSplineTableSize - 1
@@ -119,7 +119,7 @@ function bezier(mX1, mY1, mX2, mY2) {
119119
}
120120
}
121121

122-
return function BezierEasing(x) {
122+
return x => {
123123
if (mX1 === mY1 && mX2 === mY2) {
124124
return x // linear
125125
}
@@ -135,15 +135,15 @@ function bezier(mX1, mY1, mX2, mY2) {
135135
}
136136

137137
// Predefined set of animations. Similar to CSS easing functions
138-
var animations = {
138+
const animations = {
139139
ease: bezier(0.25, 0.1, 0.25, 1),
140140
easeIn: bezier(0.42, 0, 1, 1),
141141
easeOut: bezier(0, 0, 0.58, 1),
142142
easeInOut: bezier(0.42, 0, 0.58, 1),
143143
linear: bezier(0, 0, 1, 1),
144144
}
145145

146-
export function animate(source, target, options) {
146+
export const animate = (source, target, options) => {
147147
var start = Object.create(null)
148148
var diff = Object.create(null)
149149
options = options || {}
@@ -167,7 +167,7 @@ export function animate(source, target, options) {
167167
var scheduler = getScheduler(options.scheduler)
168168

169169
var keys = Object.keys(target)
170-
keys.forEach(function(key) {
170+
keys.forEach(key => {
171171
start[key] = source[key]
172172
diff[key] = target[key] - source[key]
173173
})

src/transition.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ export interface Options extends CalculateOptions {
3838
easing?: 'ease' | 'easeIn' | 'easeOut' | 'easeInOut' | 'linear'
3939
}
4040

41-
function isBoolean(options: boolean | Options): options is boolean {
41+
const isBoolean = (options: boolean | Options): options is boolean => {
4242
return typeof options === 'boolean'
4343
}
4444

45-
export default function scrollIntoViewIfNeeded(
45+
const scrollIntoViewIfNeeded = (
4646
target: Element,
4747
options: boolean | Options,
4848
animateOptions?: AnimateOptions,
4949
finalElement?: Element,
5050
offsetOptions: OffsetConfig = {}
51-
) {
51+
) => {
5252
if (!target || !(target instanceof HTMLElement))
5353
throw new Error('Element is required in scrollIntoViewIfNeeded')
5454

@@ -89,3 +89,5 @@ export default function scrollIntoViewIfNeeded(
8989

9090
return calculate(target, config)
9191
}
92+
93+
export default scrollIntoViewIfNeeded

0 commit comments

Comments
 (0)