Use Array.prototype.findIndex
This commit is contained in:
@@ -208,20 +208,6 @@ export function stableSort(arr, compareFnc) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {Array<*>} arr The array to search in.
|
|
||||||
* @param {Function} func Comparison function.
|
|
||||||
* @return {number} Return index.
|
|
||||||
*/
|
|
||||||
export function findIndex(arr, func) {
|
|
||||||
let index;
|
|
||||||
const found = !arr.every(function (el, idx) {
|
|
||||||
index = idx;
|
|
||||||
return !func(el, idx, arr);
|
|
||||||
});
|
|
||||||
return found ? index : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Array<*>} arr The array to test.
|
* @param {Array<*>} arr The array to test.
|
||||||
* @param {Function} [opt_func] Comparison function.
|
* @param {Function} [opt_func] Comparison function.
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import {containsExtent} from '../extent.js';
|
|||||||
import {createFromCapabilitiesMatrixSet} from '../tilegrid/WMTS.js';
|
import {createFromCapabilitiesMatrixSet} from '../tilegrid/WMTS.js';
|
||||||
import {createFromTileUrlFunctions, expandUrl} from '../tileurlfunction.js';
|
import {createFromTileUrlFunctions, expandUrl} from '../tileurlfunction.js';
|
||||||
import {equivalent, get as getProjection, transformExtent} from '../proj.js';
|
import {equivalent, get as getProjection, transformExtent} from '../proj.js';
|
||||||
import {findIndex} from '../array.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request encoding. One of 'KVP', 'REST'.
|
* Request encoding. One of 'KVP', 'REST'.
|
||||||
@@ -382,7 +381,7 @@ export function optionsFromCapabilities(wmtsCap, config) {
|
|||||||
let idx;
|
let idx;
|
||||||
if (l['TileMatrixSetLink'].length > 1) {
|
if (l['TileMatrixSetLink'].length > 1) {
|
||||||
if ('projection' in config) {
|
if ('projection' in config) {
|
||||||
idx = findIndex(l['TileMatrixSetLink'], function (elt, index, array) {
|
idx = l['TileMatrixSetLink'].findIndex(function (elt) {
|
||||||
const tileMatrixSet = tileMatrixSets.find(function (el) {
|
const tileMatrixSet = tileMatrixSets.find(function (el) {
|
||||||
return el['Identifier'] == elt['TileMatrixSet'];
|
return el['Identifier'] == elt['TileMatrixSet'];
|
||||||
});
|
});
|
||||||
@@ -396,7 +395,7 @@ export function optionsFromCapabilities(wmtsCap, config) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
idx = findIndex(l['TileMatrixSetLink'], function (elt, index, array) {
|
idx = l['TileMatrixSetLink'].findIndex(function (elt) {
|
||||||
return elt['TileMatrixSet'] == config['matrixSet'];
|
return elt['TileMatrixSet'] == config['matrixSet'];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -417,7 +416,7 @@ export function optionsFromCapabilities(wmtsCap, config) {
|
|||||||
if ('format' in config) {
|
if ('format' in config) {
|
||||||
format = config['format'];
|
format = config['format'];
|
||||||
}
|
}
|
||||||
idx = findIndex(l['Style'], function (elt, index, array) {
|
idx = l['Style'].findIndex(function (elt) {
|
||||||
if ('style' in config) {
|
if ('style' in config) {
|
||||||
return elt['Title'] == config['style'];
|
return elt['Title'] == config['style'];
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import {
|
|||||||
binarySearch,
|
binarySearch,
|
||||||
equals,
|
equals,
|
||||||
extend,
|
extend,
|
||||||
findIndex,
|
|
||||||
isSorted,
|
isSorted,
|
||||||
linearFindNearest,
|
linearFindNearest,
|
||||||
numberSafeCompareFunction,
|
numberSafeCompareFunction,
|
||||||
@@ -382,26 +381,6 @@ describe('ol/array.js', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findIndex', function () {
|
|
||||||
it('finds index of numbers in an array', function () {
|
|
||||||
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;
|
|
||||||
});
|
|
||||||
expect(b).to.be(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns -1 when an item in an array is not found', function () {
|
|
||||||
const a = [0, 1, 2, 3];
|
|
||||||
const b = findIndex(a, function (val, index, a2) {
|
|
||||||
return val > 100;
|
|
||||||
});
|
|
||||||
expect(b).to.be(-1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('isSorted', function () {
|
describe('isSorted', function () {
|
||||||
it('works with just an array as argument', function () {
|
it('works with just an array as argument', function () {
|
||||||
expect(isSorted([1, 2, 3])).to.be(true);
|
expect(isSorted([1, 2, 3])).to.be(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user