From 157bdc52082c8553338a59e0760331d7a31cda2b Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 27 Jul 2022 13:57:35 -0600 Subject: [PATCH 1/2] Use Array.prototype.find --- src/ol/array.js | 19 ------------ src/ol/source/WMTS.js | 15 +++++----- src/ol/tilegrid/WMTS.js | 3 +- test/browser/spec/ol/format/kml.test.js | 3 +- test/node/ol/array.test.js | 39 ------------------------- 5 files changed, 9 insertions(+), 70 deletions(-) diff --git a/src/ol/array.js b/src/ol/array.js index 7a17fe0197..06185a4d8a 100644 --- a/src/ol/array.js +++ b/src/ol/array.js @@ -168,25 +168,6 @@ export function remove(arr, obj) { return found; } -/** - * @param {Array} arr The array to search in. - * @param {function(VALUE, number, ?) : boolean} func The function to compare. - * @template VALUE - * @return {VALUE|null} The element found or null. - */ -export function find(arr, func) { - const length = arr.length >>> 0; - let value; - - for (let i = 0; i < length; i++) { - value = arr[i]; - if (func(value, i, arr)) { - return value; - } - } - return null; -} - /** * @param {Array|Uint8ClampedArray} arr1 The first array to compare. * @param {Array|Uint8ClampedArray} arr2 The second array to compare. diff --git a/src/ol/source/WMTS.js b/src/ol/source/WMTS.js index 5c442c145e..72ac18b4ee 100644 --- a/src/ol/source/WMTS.js +++ b/src/ol/source/WMTS.js @@ -9,7 +9,7 @@ import {containsExtent} from '../extent.js'; import {createFromCapabilitiesMatrixSet} from '../tilegrid/WMTS.js'; import {createFromTileUrlFunctions, expandUrl} from '../tileurlfunction.js'; import {equivalent, get as getProjection, transformExtent} from '../proj.js'; -import {find, findIndex} from '../array.js'; +import {findIndex} from '../array.js'; /** * Request encoding. One of 'KVP', 'REST'. @@ -372,10 +372,10 @@ export default WMTS; */ export function optionsFromCapabilities(wmtsCap, config) { const layers = wmtsCap['Contents']['Layer']; - const l = find(layers, function (elt, index, array) { + const l = layers.find(function (elt) { return elt['Identifier'] == config['layer']; }); - if (l === null) { + if (!l) { return null; } const tileMatrixSets = wmtsCap['Contents']['TileMatrixSet']; @@ -383,7 +383,7 @@ export function optionsFromCapabilities(wmtsCap, config) { if (l['TileMatrixSetLink'].length > 1) { if ('projection' in config) { idx = findIndex(l['TileMatrixSetLink'], function (elt, index, array) { - const tileMatrixSet = find(tileMatrixSets, function (el) { + const tileMatrixSet = tileMatrixSets.find(function (el) { return el['Identifier'] == elt['TileMatrixSet']; }); const supportedCRS = tileMatrixSet['SupportedCRS']; @@ -442,7 +442,7 @@ export function optionsFromCapabilities(wmtsCap, config) { } const matrixSets = wmtsCap['Contents']['TileMatrixSet']; - const matrixSetObj = find(matrixSets, function (elt, index, array) { + const matrixSetObj = matrixSets.find(function (elt) { return elt['Identifier'] == matrixSet; }); @@ -477,8 +477,7 @@ export function optionsFromCapabilities(wmtsCap, config) { //in case of matrix limits, use matrix limits to calculate extent if (matrixLimits) { selectedMatrixLimit = matrixLimits[matrixLimits.length - 1]; - const m = find( - matrixSetObj.TileMatrix, + const m = matrixSetObj.TileMatrix.find( (tileMatrixValue) => tileMatrixValue.Identifier === selectedMatrixLimit.TileMatrix || matrixSetObj.Identifier + ':' + tileMatrixValue.Identifier === @@ -556,7 +555,7 @@ export function optionsFromCapabilities(wmtsCap, config) { for (let i = 0, ii = gets.length; i < ii; ++i) { if (gets[i]['Constraint']) { - const constraint = find(gets[i]['Constraint'], function (element) { + const constraint = gets[i]['Constraint'].find(function (element) { return element['name'] == 'GetEncoding'; }); const encodings = constraint['AllowedValues']['Value']; diff --git a/src/ol/tilegrid/WMTS.js b/src/ol/tilegrid/WMTS.js index f46b31558d..41ed62a817 100644 --- a/src/ol/tilegrid/WMTS.js +++ b/src/ol/tilegrid/WMTS.js @@ -3,7 +3,6 @@ */ import TileGrid from './TileGrid.js'; -import {find} from '../array.js'; import {get as getProjection} from '../proj.js'; /** @@ -138,7 +137,7 @@ export function createFromCapabilitiesMatrixSet( // use of matrixLimits to filter TileMatrices from GetCapabilities // TileMatrixSet from unavailable matrix levels. if (matrixLimits.length > 0) { - matrixAvailable = find(matrixLimits, function (elt_ml) { + matrixAvailable = matrixLimits.find(function (elt_ml) { if (elt[identifierPropName] == elt_ml[matrixIdsPropName]) { return true; } diff --git a/test/browser/spec/ol/format/kml.test.js b/test/browser/spec/ol/format/kml.test.js index 79b0eddb19..36a54a665e 100644 --- a/test/browser/spec/ol/format/kml.test.js +++ b/test/browser/spec/ol/format/kml.test.js @@ -31,7 +31,6 @@ import { get as getProjection, transform, } from '../../../../../src/ol/proj.js'; -import {find} from '../../../../../src/ol/array.js'; import {parse} from '../../../../../src/ol/xml.js'; import {remove as removeTransform} from '../../../../../src/ol/proj/transforms.js'; @@ -4256,7 +4255,7 @@ describe('ol.format.KML', function () { }); it('creates a Point and a MultiPolygon for Alaska', function () { - const alaska = find(features, function (feature) { + const alaska = features.find(function (feature) { return feature.get('name') === 'Alaska'; }); expect(alaska).to.be.an(Feature); diff --git a/test/node/ol/array.test.js b/test/node/ol/array.test.js index fe5db08b19..d314660abf 100644 --- a/test/node/ol/array.test.js +++ b/test/node/ol/array.test.js @@ -3,7 +3,6 @@ import { binarySearch, equals, extend, - find, findIndex, isSorted, linearFindNearest, @@ -383,44 +382,6 @@ describe('ol/array.js', function () { }); }); - describe('find', function () { - it('finds numbers in an array', function () { - 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; - }); - expect(b).to.be(2); - }); - - it('returns null when an item in an array is not found', function () { - 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 () { - 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'; - }); - expect(b).to.be('C'); - }); - - it('returns null when nothing in an array-like is found', function () { - const a = 'abcd'; - const b = find(a, function (val, index, a2) { - return val >= 'A' && val <= 'Z'; - }); - expect(b).to.be(null); - }); - }); - describe('findIndex', function () { it('finds index of numbers in an array', function () { const a = [0, 1, 2, 3]; From 82ceaea4ab7187e34addf8c26d2917154c22f135 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 27 Jul 2022 14:00:06 -0600 Subject: [PATCH 2/2] Use Array.prototype.findIndex --- src/ol/array.js | 14 -------------- src/ol/source/WMTS.js | 7 +++---- test/node/ol/array.test.js | 21 --------------------- 3 files changed, 3 insertions(+), 39 deletions(-) diff --git a/src/ol/array.js b/src/ol/array.js index 06185a4d8a..518538641c 100644 --- a/src/ol/array.js +++ b/src/ol/array.js @@ -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 {Function} [opt_func] Comparison function. diff --git a/src/ol/source/WMTS.js b/src/ol/source/WMTS.js index 72ac18b4ee..a9f2322d5f 100644 --- a/src/ol/source/WMTS.js +++ b/src/ol/source/WMTS.js @@ -9,7 +9,6 @@ import {containsExtent} from '../extent.js'; import {createFromCapabilitiesMatrixSet} from '../tilegrid/WMTS.js'; import {createFromTileUrlFunctions, expandUrl} from '../tileurlfunction.js'; import {equivalent, get as getProjection, transformExtent} from '../proj.js'; -import {findIndex} from '../array.js'; /** * Request encoding. One of 'KVP', 'REST'. @@ -382,7 +381,7 @@ export function optionsFromCapabilities(wmtsCap, config) { let idx; if (l['TileMatrixSetLink'].length > 1) { if ('projection' in config) { - idx = findIndex(l['TileMatrixSetLink'], function (elt, index, array) { + idx = l['TileMatrixSetLink'].findIndex(function (elt) { const tileMatrixSet = tileMatrixSets.find(function (el) { return el['Identifier'] == elt['TileMatrixSet']; }); @@ -396,7 +395,7 @@ export function optionsFromCapabilities(wmtsCap, config) { } }); } else { - idx = findIndex(l['TileMatrixSetLink'], function (elt, index, array) { + idx = l['TileMatrixSetLink'].findIndex(function (elt) { return elt['TileMatrixSet'] == config['matrixSet']; }); } @@ -417,7 +416,7 @@ export function optionsFromCapabilities(wmtsCap, config) { if ('format' in config) { format = config['format']; } - idx = findIndex(l['Style'], function (elt, index, array) { + idx = l['Style'].findIndex(function (elt) { if ('style' in config) { return elt['Title'] == config['style']; } else { diff --git a/test/node/ol/array.test.js b/test/node/ol/array.test.js index d314660abf..c881b808f2 100644 --- a/test/node/ol/array.test.js +++ b/test/node/ol/array.test.js @@ -3,7 +3,6 @@ import { binarySearch, equals, extend, - findIndex, isSorted, linearFindNearest, 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 () { it('works with just an array as argument', function () { expect(isSorted([1, 2, 3])).to.be(true);