Skip to content

Commit ef75c23

Browse files
committed
chore: use let/const instead of var
1 parent a248086 commit ef75c23

File tree

4 files changed

+43
-41
lines changed

4 files changed

+43
-41
lines changed

benchmark/benchmark.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var slug = require('../slug')
1+
const slug = require('../slug')
22

33
const BENCHMARK_RUNS = 2048
44
const MAX_WORD_LENGTH = 16

slug.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global btoa */
22
(function (root) {
3-
var base64
3+
let base64
44

55
// This function's sole purpose is to help us ignore lone surrogates so that
66
// malformed strings don't throw in the browser while being processed
@@ -60,9 +60,10 @@
6060
// Polyfill for environments that don't have btoa or Buffer class (notably, React Native).
6161
// Based on https://github.com/davidchambers/Base64.js/blob/a121f75bb10c8dd5d557886c4b1069b31258d230/base64.js
6262
base64 = function (input) {
63-
var str = unescape(encodeURIComponent(input + ''))
63+
const str = unescape(encodeURIComponent(input + ''))
64+
let output = ''
6465
for (
65-
var block, charCode, idx = 0, map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', output = '';
66+
let block, charCode, idx = 0, map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
6667
str.charAt(idx | 0) || (map = '=', idx % 1);
6768
output += map.charAt(63 & block >> 8 - idx % 1 * 8)
6869
) {
@@ -95,12 +96,13 @@
9596
throw new TypeError('Cannot convert undefined or null to object')
9697
}
9798

98-
var to = Object(target)
99+
const to = Object(target)
99100

100-
for (var index = 1; index < arguments.length; index++) {
101-
var nextSource = arguments[index]
101+
for (let index = 1; index < arguments.length; index++) {
102+
const nextSource = arguments[index]
102103

103104
if (nextSource !== null && nextSource !== undefined) {
105+
// eslint-disable-next-line no-var
104106
for (var nextKey in nextSource) {
105107
// Avoid bugs when hasOwnProperty is shadowed
106108
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
@@ -117,7 +119,7 @@
117119
}
118120

119121
function slug (string, opts) {
120-
var result = slugify(string, opts)
122+
let result = slugify(string, opts)
121123
// If output is an empty string, try slug for base64 of string.
122124
if (result === '') {
123125
// Get rid of lone surrogates.
@@ -148,34 +150,34 @@
148150
if (typeof opts === 'string') { opts = { replacement: opts } }
149151
opts = opts ? Object.assign({}, opts) : {}
150152
opts.mode = opts.mode || slug.defaults.mode
151-
var defaults = slug.defaults.modes[opts.mode]
152-
var keys = ['replacement', 'multicharmap', 'charmap', 'remove', 'lower']
153+
const defaults = slug.defaults.modes[opts.mode]
154+
const keys = ['replacement', 'multicharmap', 'charmap', 'remove', 'lower']
153155
for (let key, i = 0, l = keys.length; i < l; i++) {
154156
key = keys[i]
155157
opts[key] = (key in opts) ? opts[key] : defaults[key]
156158
}
157159
const localeMap = locales[opts.locale] || {}
158160

159-
var lengths = []
161+
let lengths = []
160162
// "let" instead of "const" in next line is for IE11 compatibilty
161163
for (let key in opts.multicharmap) { // eslint-disable-line prefer-const
162164
if (!Object.prototype.hasOwnProperty.call(opts.multicharmap, key)) { continue }
163165

164-
var len = key.length
166+
const len = key.length
165167
if (lengths.indexOf(len) === -1) { lengths.push(len) }
166168
}
167169

168170
// We want to match the longest string if there are multiple matches, so
169171
// sort lengths in descending order.
170172
lengths = lengths.sort(function (a, b) { return b - a })
171173

172-
var result = ''
174+
let result = ''
173175
for (let char, i = 0, l = string.length; i < l; i++) {
174176
char = string[i]
175177
let matchedMultichar = false
176178
for (let j = 0; j < lengths.length; j++) {
177179
const len = lengths[j]
178-
var str = string.substr(i, len)
180+
const str = string.substr(i, len)
179181
if (opts.multicharmap[str]) {
180182
i += len - 1
181183
char = opts.multicharmap[str]

test/fuzz.test.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* global chai, describe, it, window */
22

3-
var slug = (typeof window !== 'undefined' && window.slug) || require('../slug')
4-
var assert = typeof chai === 'undefined' ? require('assert') : chai.assert
3+
const slug = (typeof window !== 'undefined' && window.slug) || require('../slug')
4+
const assert = typeof chai === 'undefined' ? require('assert') : chai.assert
55

66
// IE11 polyfill
77
if (!Array.from) {
88
Array.from = (function () {
9-
var symbolIterator
9+
let symbolIterator
1010
try {
1111
symbolIterator = Symbol.iterator
1212
? Symbol.iterator
@@ -15,33 +15,33 @@ if (!Array.from) {
1515
symbolIterator = 'Symbol(Symbol.iterator)'
1616
}
1717

18-
var toStr = Object.prototype.toString
19-
var isCallable = function (fn) {
18+
const toStr = Object.prototype.toString
19+
const isCallable = function (fn) {
2020
return (
2121
typeof fn === 'function' ||
2222
toStr.call(fn) === '[object Function]'
2323
)
2424
}
25-
var toInteger = function (value) {
26-
var number = Number(value)
25+
const toInteger = function (value) {
26+
const number = Number(value)
2727
if (isNaN(number)) return 0
2828
if (number === 0 || !isFinite(number)) return number
2929
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number))
3030
}
31-
var maxSafeInteger = Math.pow(2, 53) - 1
32-
var toLength = function (value) {
33-
var len = toInteger(value)
31+
const maxSafeInteger = Math.pow(2, 53) - 1
32+
const toLength = function (value) {
33+
const len = toInteger(value)
3434
return Math.min(Math.max(len, 0), maxSafeInteger)
3535
}
3636

37-
var setGetItemHandler = function setGetItemHandler (isIterator, items) {
38-
var iterator = isIterator && items[symbolIterator]()
37+
const setGetItemHandler = function setGetItemHandler (isIterator, items) {
38+
const iterator = isIterator && items[symbolIterator]()
3939
return function getItem (k) {
4040
return isIterator ? iterator.next() : items[k]
4141
}
4242
}
4343

44-
var getArray = function getArray (
44+
const getArray = function getArray (
4545
T,
4646
A,
4747
len,
@@ -50,13 +50,13 @@ if (!Array.from) {
5050
mapFn
5151
) {
5252
// 16. Let k be 0.
53-
var k = 0
53+
let k = 0
5454

5555
// 17. Repeat, while k < len… or while iterator is done (also steps a - h)
5656
// eslint-disable-next-line no-unmodified-loop-condition
5757
while (k < len || isIterator) {
58-
var item = getItem(k)
59-
var kValue = isIterator ? item.value : item
58+
const item = getItem(k)
59+
const kValue = isIterator ? item.value : item
6060

6161
if (isIterator && item.done) {
6262
return A
@@ -87,11 +87,11 @@ if (!Array.from) {
8787
// The length property of the from method is 1.
8888
return function from (arrayLikeOrIterator /*, mapFn, thisArg */) {
8989
// 1. Let C be the this value.
90-
var C = this
90+
const C = this
9191

9292
// 2. Let items be ToObject(arrayLikeOrIterator).
93-
var items = Object(arrayLikeOrIterator)
94-
var isIterator = isCallable(items[symbolIterator])
93+
const items = Object(arrayLikeOrIterator)
94+
const isIterator = isCallable(items[symbolIterator])
9595

9696
// 3. ReturnIfAbrupt(items).
9797
if (arrayLikeOrIterator == null && !isIterator) {
@@ -102,8 +102,8 @@ if (!Array.from) {
102102

103103
// 4. If mapfn is undefined, then let mapping be false.
104104
// eslint-disable-next-line no-void
105-
var mapFn = arguments.length > 1 ? arguments[1] : void undefined
106-
var T
105+
const mapFn = arguments.length > 1 ? arguments[1] : void undefined
106+
let T
107107
if (typeof mapFn !== 'undefined') {
108108
// 5. else
109109
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
@@ -121,13 +121,13 @@ if (!Array.from) {
121121

122122
// 10. Let lenValue be Get(items, "length").
123123
// 11. Let len be ToLength(lenValue).
124-
var len = toLength(items.length)
124+
const len = toLength(items.length)
125125

126126
// 13. If IsConstructor(C) is true, then
127127
// 13. a. Let A be the result of calling the [[Construct]] internal method
128128
// of C with an argument list containing the single item len.
129129
// 14. a. Else, Let A be ArrayCreate(len).
130-
var A = isCallable(C) ? Object(new C(len)) : new Array(len)
130+
const A = isCallable(C) ? Object(new C(len)) : new Array(len)
131131

132132
return getArray(
133133
T,

test/slug.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const inBrowser = typeof window !== 'undefined'
55
// Polyfill for IE11.
66
if (!String.fromCodePoint) {
77
(function (stringFromCharCode) {
8-
var fromCodePoint = function (_) {
9-
var codeUnits = []; var codeLen = 0; var result = ''
10-
for (var index = 0, len = arguments.length; index !== len; ++index) {
11-
var codePoint = +arguments[index]
8+
const fromCodePoint = function (_) {
9+
const codeUnits = []; let codeLen = 0; let result = ''
10+
for (let index = 0, len = arguments.length; index !== len; ++index) {
11+
let codePoint = +arguments[index]
1212
// correctly handles all cases including `NaN`, `-Infinity`, `+Infinity`
1313
// The surrounding `!(...)` is required to correctly handle `NaN` cases
1414
// The (codePoint>>>0) === codePoint clause handles decimals and negatives

0 commit comments

Comments
 (0)