Use blocked scoped variables

In addition to using const and let, this also upgrades our linter config and removes lint (mostly whitespace).
This commit is contained in:
Tim Schaub
2018-01-11 23:32:36 -07:00
parent 0bf2b04dee
commit ad62739a6e
684 changed files with 18120 additions and 18184 deletions

View File

@@ -17,15 +17,15 @@ describe('ol.array', function() {
describe('binarySearch', function() {
var insertionPoint = function(position) {
const insertionPoint = function(position) {
return -(position + 1);
};
var revNumCompare = function(a, b) {
const revNumCompare = function(a, b) {
return b - a;
};
describe('default comparison on array of String(s)', function() {
var a = [
const a = [
'1000', '9', 'AB', 'ABC', 'ABCABC', 'ABD', 'ABDA', 'B', 'B', 'B',
'C', 'CA', 'CC', 'ZZZ', 'ab', 'abc', 'abcabc', 'abd', 'abda', 'b',
'c', 'ca', 'cc', 'zzz'
@@ -41,81 +41,81 @@ describe('ol.array', function() {
expect(binarySearch(a, 'C')).to.be(10);
});
it('should find \'B\' at index 7 || 8 || 9', function() {
var pos = binarySearch(a, 'B');
const pos = binarySearch(a, 'B');
expect(pos == 7 || pos == 8 || pos == 9).to.be.ok();
});
it('should not find \'100\'', function() {
var pos = binarySearch(a, '100');
const pos = binarySearch(a, '100');
expect(pos < 0).to.be.ok();
});
it('should have an insertion point of 0 for \'100\'', function() {
var pos = binarySearch(a, '100');
const pos = binarySearch(a, '100');
expect(insertionPoint(pos)).to.be(0);
});
it('should not find \'zzz0\'', function() {
var pos = binarySearch(a, 'zzz0');
const pos = binarySearch(a, 'zzz0');
expect(pos < 0).to.be.ok();
});
it('should have an insertion point of ' + (a.length) + ' for \'zzz0\'',
function() {
var pos = binarySearch(a, 'zzz0');
expect(insertionPoint(pos)).to.be(a.length);
}
function() {
const pos = binarySearch(a, 'zzz0');
expect(insertionPoint(pos)).to.be(a.length);
}
);
it('should not find \'BA\'', function() {
var pos = binarySearch(a, 'zzz0');
const pos = binarySearch(a, 'zzz0');
expect(pos < 0).to.be.ok();
});
it('should have an insertion point of 10 for \'BA\'',
function() {
var pos = binarySearch(a, 'BA');
expect(insertionPoint(pos)).to.be(10);
}
function() {
const pos = binarySearch(a, 'BA');
expect(insertionPoint(pos)).to.be(10);
}
);
});
describe('0 length array with default comparison', function() {
var b = [];
const b = [];
it('should not find \'a\'', function() {
expect(binarySearch(b, 'a') < 0).to.be.ok();
});
it('should have an insertion point of 0 for \'a\'',
function() {
var pos = binarySearch(b, 'a');
expect(insertionPoint(pos)).to.be(0);
}
function() {
const pos = binarySearch(b, 'a');
expect(insertionPoint(pos)).to.be(0);
}
);
});
describe('single element array with default lexiographical comparison',
function() {
var c = ['only item'];
it('should find \'only item\' at index 0', function() {
expect(binarySearch(c, 'only item')).to.be(0);
});
it('should not find \'a\'', function() {
expect(binarySearch(c, 'a') < 0).to.be.ok();
});
it('should have an insertion point of 0 for \'a\'',
function() {
var pos = binarySearch(c, 'a');
expect(insertionPoint(pos)).to.be(0);
}
);
it('should not find \'z\'', function() {
expect(binarySearch(c, 'z') < 0).to.be.ok();
});
it('should have an insertion point of 1 for \'z\'',
function() {
var pos = binarySearch(c, 'z');
expect(insertionPoint(pos)).to.be(1);
}
);
}
function() {
const c = ['only item'];
it('should find \'only item\' at index 0', function() {
expect(binarySearch(c, 'only item')).to.be(0);
});
it('should not find \'a\'', function() {
expect(binarySearch(c, 'a') < 0).to.be.ok();
});
it('should have an insertion point of 0 for \'a\'',
function() {
const pos = binarySearch(c, 'a');
expect(insertionPoint(pos)).to.be(0);
}
);
it('should not find \'z\'', function() {
expect(binarySearch(c, 'z') < 0).to.be.ok();
});
it('should have an insertion point of 1 for \'z\'',
function() {
const pos = binarySearch(c, 'z');
expect(insertionPoint(pos)).to.be(1);
}
);
}
);
describe('default comparison on array of Number(s)', function() {
var d = [
const d = [
-897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0, 0,
0.31255, 5, 142.88888708, 334, 342, 453, 54254
];
@@ -129,124 +129,124 @@ describe('ol.array', function() {
expect(binarySearch(d, -3)).to.be(5);
});
it('should find 0 at index 6 || 7 || 8', function() {
var pos = binarySearch(d, 0);
const pos = binarySearch(d, 0);
expect(pos == 6 || pos == 7 || pos == 8).to.be(true);
});
it('should not find -900000', function() {
var pos = binarySearch(d, -900000);
const pos = binarySearch(d, -900000);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for -900000', function() {
var pos = binarySearch(d, -900000);
const pos = binarySearch(d, -900000);
expect(insertionPoint(pos)).to.be(0);
});
it('should not find 54255', function() {
var pos = binarySearch(d, 54255);
const pos = binarySearch(d, 54255);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of ' + (d.length) + ' for 54255',
function() {
var pos = binarySearch(d, 54255);
expect(insertionPoint(pos)).to.be(d.length);
}
function() {
const pos = binarySearch(d, 54255);
expect(insertionPoint(pos)).to.be(d.length);
}
);
it('should not find 1.1', function() {
var pos = binarySearch(d, 1.1);
const pos = binarySearch(d, 1.1);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 10 for 1.1', function() {
var pos = binarySearch(d, 1.1);
const pos = binarySearch(d, 1.1);
expect(insertionPoint(pos)).to.be(10);
});
});
describe('custom comparison function, which reverse orders numbers',
function() {
var e = [
54254, 453, 342, 334, 142.88888708, 5, 0.31255, 0, 0, 0, -3,
-9, -324, -1321.3124, -321434.58758, -897123.9
];
it('should find 54254 at index 0', function() {
var pos = binarySearch(e, 54254, revNumCompare);
expect(pos).to.be(0);
});
it('should find -897123.9 at index ' + (e.length - 1), function() {
var pos = binarySearch(e, -897123.9, revNumCompare);
expect(pos).to.be(e.length - 1);
});
it('should find -3 at index 10', function() {
var pos = binarySearch(e, -3, revNumCompare);
expect(pos).to.be(10);
});
it('should find 0 at index 7 || 8 || 9', function() {
var pos = binarySearch(e, 0, revNumCompare);
expect(pos == 7 || pos == 8 || pos == 9).to.be(true);
});
it('should not find 54254.1', function() {
var pos = binarySearch(e, 54254.1, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 54254.1', function() {
var pos = binarySearch(e, 54254.1, revNumCompare);
expect(insertionPoint(pos)).to.be(0);
});
it('should not find -897124', function() {
var pos = binarySearch(e, -897124, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of ' + e.length + ' for -897124',
function() {
var pos = binarySearch(e, -897124, revNumCompare);
expect(insertionPoint(pos)).to.be(e.length);
}
);
it('should not find 1.1', function() {
var pos = binarySearch(e, 1.1, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 1.1', function() {
var pos = binarySearch(e, 1.1, revNumCompare);
expect(insertionPoint(pos)).to.be(6);
});
}
function() {
const e = [
54254, 453, 342, 334, 142.88888708, 5, 0.31255, 0, 0, 0, -3,
-9, -324, -1321.3124, -321434.58758, -897123.9
];
it('should find 54254 at index 0', function() {
const pos = binarySearch(e, 54254, revNumCompare);
expect(pos).to.be(0);
});
it('should find -897123.9 at index ' + (e.length - 1), function() {
const pos = binarySearch(e, -897123.9, revNumCompare);
expect(pos).to.be(e.length - 1);
});
it('should find -3 at index 10', function() {
const pos = binarySearch(e, -3, revNumCompare);
expect(pos).to.be(10);
});
it('should find 0 at index 7 || 8 || 9', function() {
const pos = binarySearch(e, 0, revNumCompare);
expect(pos == 7 || pos == 8 || pos == 9).to.be(true);
});
it('should not find 54254.1', function() {
const pos = binarySearch(e, 54254.1, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 54254.1', function() {
const pos = binarySearch(e, 54254.1, revNumCompare);
expect(insertionPoint(pos)).to.be(0);
});
it('should not find -897124', function() {
const pos = binarySearch(e, -897124, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of ' + e.length + ' for -897124',
function() {
const pos = binarySearch(e, -897124, revNumCompare);
expect(insertionPoint(pos)).to.be(e.length);
}
);
it('should not find 1.1', function() {
const pos = binarySearch(e, 1.1, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 1.1', function() {
const pos = binarySearch(e, 1.1, revNumCompare);
expect(insertionPoint(pos)).to.be(6);
});
}
);
describe('0 length array with custom comparison function', function() {
var f = [];
const f = [];
it('should not find 0', function() {
var pos = binarySearch(f, 0, revNumCompare);
const pos = binarySearch(f, 0, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 0', function() {
var pos = binarySearch(f, 0, revNumCompare);
const pos = binarySearch(f, 0, revNumCompare);
expect(insertionPoint(pos)).to.be(0);
});
});
describe('single element array with custom comparison function',
function() {
var g = [1];
it('should find 1 at index 0', function() {
var pos = binarySearch(g, 1, revNumCompare);
expect(pos).to.be(0);
});
it('should not find 2', function() {
var pos = binarySearch(g, 2, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 2', function() {
var pos = binarySearch(g, 2, revNumCompare);
expect(insertionPoint(pos)).to.be(0);
});
it('should not find 0', function() {
var pos = binarySearch(g, 0, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 1 for 0', function() {
var pos = binarySearch(g, 0, revNumCompare);
expect(insertionPoint(pos)).to.be(1);
});
}
function() {
const g = [1];
it('should find 1 at index 0', function() {
const pos = binarySearch(g, 1, revNumCompare);
expect(pos).to.be(0);
});
it('should not find 2', function() {
const pos = binarySearch(g, 2, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 2', function() {
const pos = binarySearch(g, 2, revNumCompare);
expect(insertionPoint(pos)).to.be(0);
});
it('should not find 0', function() {
const pos = binarySearch(g, 0, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 1 for 0', function() {
const pos = binarySearch(g, 0, revNumCompare);
expect(insertionPoint(pos)).to.be(1);
});
}
);
describe('finding first index when multiple candidates', function() {
@@ -259,55 +259,55 @@ describe('ol.array', function() {
});
describe('Don\'t use Array#slice, Function#apply and Function#call',
function() {
var a = [1, 5, 7, 11, 13, 16, 19, 24, 28, 31, 33, 36, 40, 50, 52, 55];
var calls = {
'Array#slice': false,
'Function#apply': false,
'Function#call': false
};
var origArraySlice;
var origFunctionApply;
var origFunctionCall;
function() {
const a = [1, 5, 7, 11, 13, 16, 19, 24, 28, 31, 33, 36, 40, 50, 52, 55];
const calls = {
'Array#slice': false,
'Function#apply': false,
'Function#call': false
};
let origArraySlice;
let origFunctionApply;
let origFunctionCall;
it('does not use potentially slow methods (default & custom compare)',
function() {
// Mockup (I failed to use sinon.spy and beforeEach-hooks)
origArraySlice = Array.prototype.slice;
origFunctionApply = Function.prototype.apply;
origFunctionCall = Function.prototype.call;
Array.prototype.slice = function() {
calls['Array#slice'] = true;
};
Function.prototype.apply = function() {
calls['Function#apply'] = true;
};
Function.prototype.call = function() {
calls['Function#call'] = true;
};
it('does not use potentially slow methods (default & custom compare)',
function() {
// Mockup (I failed to use sinon.spy and beforeEach-hooks)
origArraySlice = Array.prototype.slice;
origFunctionApply = Function.prototype.apply;
origFunctionCall = Function.prototype.call;
Array.prototype.slice = function() {
calls['Array#slice'] = true;
};
Function.prototype.apply = function() {
calls['Function#apply'] = true;
};
Function.prototype.call = function() {
calls['Function#call'] = true;
};
// Now actually call and test the method twice
binarySearch(a, 48);
binarySearch(a, 13, function(a, b) {
return a > b ? 1 : a < b ? -1 : 0;
});
// Now actually call and test the method twice
binarySearch(a, 48);
binarySearch(a, 13, function(a, b) {
return a > b ? 1 : a < b ? -1 : 0;
});
// Restore mocked up methods
Array.prototype.slice = origArraySlice;
Function.prototype.apply = origFunctionApply;
Function.prototype.call = origFunctionCall;
// Restore mocked up methods
Array.prototype.slice = origArraySlice;
Function.prototype.apply = origFunctionApply;
Function.prototype.call = origFunctionCall;
// Expectations
expect(calls['Array#slice']).to.be(false);
expect(calls['Function#apply']).to.be(false);
expect(calls['Function#call']).to.be(false);
}
);
}
// Expectations
expect(calls['Array#slice']).to.be(false);
expect(calls['Function#apply']).to.be(false);
expect(calls['Function#call']).to.be(false);
}
);
}
);
describe('when items are not found', function() {
var arr = [1, 2, 2, 2, 3, 5, 9];
const arr = [1, 2, 2, 2, 3, 5, 9];
it('should return the index of where the item would go plus one, negated, if the item is not found', function() {
expect(binarySearch(arr, 4)).to.equal(-6);
@@ -358,19 +358,19 @@ describe('ol.array', function() {
});
describe('extend', function() {
it('extends an array in place with an array', function() {
var a = [0, 1];
const a = [0, 1];
extend(a, [2, 3]);
expect(a).to.eql([0, 1, 2, 3]);
});
it('extends an array in place with a number', function() {
var a = [0, 1];
const a = [0, 1];
extend(a, 2);
expect(a).to.eql([0, 1, 2]);
});
it('extends an array in place with a big array', function() {
var a = [];
var i = 250000; // original test has 1.000.000, but that was too slow
var bigArray = Array(i);
const a = [];
let i = 250000; // original test has 1.000.000, but that was too slow
const bigArray = Array(i);
while (i--) {
bigArray[i] = i;
}
@@ -381,8 +381,8 @@ describe('ol.array', function() {
describe('find', function() {
it('finds numbers in an array', function() {
var a = [0, 1, 2, 3];
var b = find(a, function(val, index, a2) {
const a = [0, 1, 2, 3];
const b = find(a, function(val, index, a2) {
expect(a).to.equal(a2);
expect(typeof index).to.be('number');
return val > 1;
@@ -391,16 +391,16 @@ describe('ol.array', function() {
});
it('returns null when an item in an array is not found', function() {
var a = [0, 1, 2, 3];
var b = find(a, function(val, index, a2) {
const a = [0, 1, 2, 3];
const b = find(a, function(val, index, a2) {
return val > 100;
});
expect(b).to.be(null);
});
it('finds items in an array-like', function() {
var a = 'abCD';
var b = find(a, function(val, index, a2) {
const a = 'abCD';
const b = find(a, function(val, index, a2) {
expect(a).to.equal(a2);
expect(typeof index).to.be('number');
return val >= 'A' && val <= 'Z';
@@ -409,8 +409,8 @@ describe('ol.array', function() {
});
it('returns null when nothing in an array-like is found', function() {
var a = 'abcd';
var b = find(a, function(val, index, a2) {
const a = 'abcd';
const b = find(a, function(val, index, a2) {
return val >= 'A' && val <= 'Z';
});
expect(b).to.be(null);
@@ -419,8 +419,8 @@ describe('ol.array', function() {
describe('findIndex', function() {
it('finds index of numbers in an array', function() {
var a = [0, 1, 2, 3];
var b = findIndex(a, function(val, index, a2) {
const a = [0, 1, 2, 3];
const b = findIndex(a, function(val, index, a2) {
expect(a).to.equal(a2);
expect(typeof index).to.be('number');
return val > 1;
@@ -429,8 +429,8 @@ describe('ol.array', function() {
});
it('returns -1 when an item in an array is not found', function() {
var a = [0, 1, 2, 3];
var b = findIndex(a, function(val, index, a2) {
const a = [0, 1, 2, 3];
const b = findIndex(a, function(val, index, a2) {
return val > 100;
});
expect(b).to.be(-1);
@@ -461,7 +461,7 @@ describe('ol.array', function() {
describe('linearFindNearest', function() {
it('returns expected value', function() {
var arr = [1000, 500, 100];
const arr = [1000, 500, 100];
expect(linearFindNearest(arr, 10000, 0)).to.eql(0);
expect(linearFindNearest(arr, 10000, 1)).to.eql(0);
@@ -511,7 +511,7 @@ describe('ol.array', function() {
describe('numberSafeCompareFunction', function() {
it('sorts as expected', function() {
var arr = [40, 200, 3000];
const arr = [40, 200, 3000];
// default sort would yield [200, 3000, 40]
arr.sort(numberSafeCompareFunction);
expect(arr).to.eql(arr);
@@ -520,7 +520,7 @@ describe('ol.array', function() {
describe('remove', function() {
it('removes elements from an array', function() {
var a = ['a', 'b', 'c', 'd'];
const a = ['a', 'b', 'c', 'd'];
remove(a, 'c');
expect(a).to.eql(['a', 'b', 'd']);
remove(a, 'x');
@@ -530,8 +530,8 @@ describe('ol.array', function() {
describe('reverseSubArray', function() {
it('returns expected value', function() {
var arr;
var expected = [1, 2, 3, 4, 5, 6];
let arr;
const expected = [1, 2, 3, 4, 5, 6];
arr = [1, 5, 4, 3, 2, 6];
reverseSubArray(arr, 1, 4);
@@ -552,7 +552,7 @@ describe('ol.array', function() {
});
describe('stableSort', function() {
var arr, wantedSortedValues;
let arr, wantedSortedValues;
beforeEach(function() {
arr = [{key: 3, val: 'a'}, {key: 2, val: 'b'}, {key: 3, val: 'c'},
@@ -565,8 +565,8 @@ describe('ol.array', function() {
return obj1.key - obj2.key;
}
stableSort(arr, comparisonFn);
var sortedValues = [];
for (var i = 0; i < arr.length; i++) {
const sortedValues = [];
for (let i = 0; i < arr.length; i++) {
sortedValues.push(arr[i].val);
}
expect(wantedSortedValues).to.eql(sortedValues);