Skip to content

Commit 96c7b77

Browse files
committed
Merge branch 'rafael-lima-master'
* rafael-lima-master: Changes conditionals, and reduces functions.
2 parents 6dcd3e3 + d540209 commit 96c7b77

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

src/index.js

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,17 @@
33
//
44

55
module.exports = function memoize (fn, options) {
6-
var cache
7-
var serializer
8-
var strategy
9-
10-
if (options && options.cache) {
11-
cache = options.cache
12-
} else {
13-
cache = cacheDefault
14-
}
6+
const cache = options && options.cache
7+
? options.cache
8+
: cacheDefault
159

16-
if (options && options.serializer) {
17-
serializer = options.serializer
18-
} else {
19-
serializer = serializerDefault
20-
}
10+
const serializer = options && options.serializer
11+
? options.serializer
12+
: serializerDefault
2113

22-
if (options && options.strategy) {
23-
strategy = options.strategy
24-
} else {
25-
strategy = strategyDefault
26-
}
14+
const strategy = options && options.strategy
15+
? options.strategy
16+
: strategyDefault
2717

2818
return strategy(fn, {
2919
cache,
@@ -35,21 +25,15 @@ module.exports = function memoize (fn, options) {
3525
// Strategy
3626
//
3727

38-
function isPrimitive (value) {
39-
return value == null || (typeof value !== 'function' && typeof value !== 'object')
40-
}
28+
const isPrimitive = (value) =>
29+
value == null || (typeof value !== 'function' && typeof value !== 'object')
4130

4231
function strategyDefault (fn, options) {
4332
function monadic (fn, cache, serializer, arg) {
44-
var cacheKey
45-
if (isPrimitive(arg)) {
46-
cacheKey = arg
47-
} else {
48-
cacheKey = serializer(arg)
49-
}
33+
const cacheKey = isPrimitive(arg) ? arg : serializer(arg)
5034

5135
if (!cache.has(cacheKey)) {
52-
var computedValue = fn.call(this, arg)
36+
const computedValue = fn.call(this, arg)
5337
cache.set(cacheKey, computedValue)
5438
return computedValue
5539
}
@@ -58,20 +42,18 @@ function strategyDefault (fn, options) {
5842
}
5943

6044
function variadic (fn, cache, serializer, ...args) {
61-
var cacheKey = serializer(args)
45+
const cacheKey = serializer(args)
6246

6347
if (!cache.has(cacheKey)) {
64-
var computedValue = fn.apply(this, args)
48+
const computedValue = fn.apply(this, args)
6549
cache.set(cacheKey, computedValue)
6650
return computedValue
6751
}
6852

6953
return cache.get(cacheKey)
7054
}
7155

72-
var memoized = fn.length === 1
73-
? monadic
74-
: variadic
56+
let memoized = fn.length === 1 ? monadic : variadic
7557

7658
memoized = memoized.bind(
7759
this,
@@ -87,9 +69,7 @@ function strategyDefault (fn, options) {
8769
// Serializer
8870
//
8971

90-
function serializerDefault (...args) {
91-
return JSON.stringify(args)
92-
}
72+
const serializerDefault = (...args) => JSON.stringify(args)
9373

9474
//
9575
// Cache

0 commit comments

Comments
 (0)