Skip to content

Commit 2f0bfc7

Browse files
committed
Refactor test/ for ES2015+
1 parent 96c7b77 commit 2f0bfc7

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

test/index.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,55 @@
11
/* global test, expect */
22

3-
var memoize = require('../src')
3+
const memoize = require('../src')
44

5-
test('speed', function () {
5+
test('speed', () => {
66
// Vanilla Fibonacci
77

88
function vanillaFibonacci (n) {
99
return n < 2 ? n : vanillaFibonacci(n - 1) + vanillaFibonacci(n - 2)
1010
}
1111

12-
var vanillaExecTimeStart = Date.now()
12+
const vanillaExecTimeStart = Date.now()
1313
vanillaFibonacci(35)
14-
var vanillaExecTime = Date.now() - vanillaExecTimeStart
14+
const vanillaExecTime = Date.now() - vanillaExecTimeStart
1515

1616
// Memoized
1717

18-
var fibonacci = function (n) {
19-
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2)
20-
}
18+
let fibonacci = n => n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2)
2119

2220
fibonacci = memoize(fibonacci)
23-
var memoizedFibonacci = fibonacci
21+
const memoizedFibonacci = fibonacci
2422

25-
var memoizedExecTimeStart = Date.now()
23+
const memoizedExecTimeStart = Date.now()
2624
memoizedFibonacci(35)
27-
var memoizedExecTime = Date.now() - memoizedExecTimeStart
25+
const memoizedExecTime = Date.now() - memoizedExecTimeStart
2826

2927
// Assertion
3028

3129
expect(memoizedExecTime < vanillaExecTime).toBe(true)
3230
})
3331

34-
test('memoize functions with single primitive argument', function () {
32+
test('memoize functions with single primitive argument', () => {
3533
function plusPlus (number) {
3634
return number + 1
3735
}
3836

39-
var memoizedPlusPlus = memoize(plusPlus)
37+
const memoizedPlusPlus = memoize(plusPlus)
4038

4139
// Assertions
4240

4341
expect(memoizedPlusPlus(1)).toBe(2)
4442
expect(memoizedPlusPlus(1)).toBe(2)
4543
})
4644

47-
test('memoize functions with single non-primitive argument', function () {
48-
var numberOfCalls = 0
45+
test('memoize functions with single non-primitive argument', () => {
46+
let numberOfCalls = 0
4947
function plusPlus (obj) {
5048
numberOfCalls += 1
5149
return obj.number + 1
5250
}
5351

54-
var memoizedPlusPlus = memoize(plusPlus)
52+
const memoizedPlusPlus = memoize(plusPlus)
5553

5654
// Assertions
5755
expect(memoizedPlusPlus({number: 1})).toBe(2)
@@ -60,47 +58,47 @@ test('memoize functions with single non-primitive argument', function () {
6058
expect(numberOfCalls).toBe(1)
6159
})
6260

63-
test('memoize functions with N arguments', function () {
61+
test('memoize functions with N arguments', () => {
6462
function nToThePower (n, power) {
6563
return Math.pow(n, power)
6664
}
6765

68-
var memoizedNToThePower = memoize(nToThePower)
66+
const memoizedNToThePower = memoize(nToThePower)
6967

7068
// Assertions
7169

7270
expect(memoizedNToThePower(2, 3)).toBe(8)
7371
expect(memoizedNToThePower(2, 3)).toBe(8)
7472
})
7573

76-
test('inject custom cache', function () {
77-
var hasMethodExecutionCount = 0
78-
var setMethodExecutionCount = 0
74+
test('inject custom cache', () => {
75+
let hasMethodExecutionCount = 0
76+
let setMethodExecutionCount = 0
7977

8078
// a custom cache instance must implement:
8179
// - has
8280
// - get
8381
// - set
8482
// - delete
85-
var customCacheProto = {
86-
has: function (key) {
83+
const customCacheProto = {
84+
has (key) {
8785
hasMethodExecutionCount++
8886
return (key in this.cache)
8987
},
90-
get: function (key) {
88+
get (key) {
9189
return this.cache[key]
9290
},
93-
set: function (key, value) {
91+
set (key, value) {
9492
setMethodExecutionCount++
9593
this.cache[key] = value
9694
},
97-
delete: function (key) {
95+
delete (key) {
9896
delete this.cache[key]
9997
}
10098
}
101-
var customCache = {
102-
create: function () {
103-
var cache = Object.create(customCacheProto)
99+
const customCache = {
100+
create () {
101+
const cache = Object.create(customCacheProto)
104102
cache.cache = Object.create(null)
105103
return cache
106104
}
@@ -110,7 +108,7 @@ test('inject custom cache', function () {
110108
return a - b
111109
}
112110

113-
var memoizedMinus = memoize(minus, {
111+
const memoizedMinus = memoize(minus, {
114112
cache: customCache
115113
})
116114
memoizedMinus(3, 1)
@@ -122,8 +120,8 @@ test('inject custom cache', function () {
122120
expect(setMethodExecutionCount).toBe(1)
123121
})
124122

125-
test('inject custom serializer', function () {
126-
var serializerMethodExecutionCount = 0
123+
test('inject custom serializer', () => {
124+
let serializerMethodExecutionCount = 0
127125

128126
function serializer () {
129127
serializerMethodExecutionCount++
@@ -134,8 +132,8 @@ test('inject custom serializer', function () {
134132
return a - b
135133
}
136134

137-
var memoizedMinus = memoize(minus, {
138-
serializer: serializer
135+
const memoizedMinus = memoize(minus, {
136+
serializer
139137
})
140138
memoizedMinus(3, 1)
141139
memoizedMinus(3, 1)

0 commit comments

Comments
 (0)