Use Array.prototype.find

This commit is contained in:
Tim Schaub
2022-07-27 13:57:35 -06:00
parent f2d989b299
commit 157bdc5208
5 changed files with 9 additions and 70 deletions

View File

@@ -168,25 +168,6 @@ export function remove(arr, obj) {
return found; return found;
} }
/**
* @param {Array<VALUE>} 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} arr1 The first array to compare.
* @param {Array|Uint8ClampedArray} arr2 The second array to compare. * @param {Array|Uint8ClampedArray} arr2 The second array to compare.

View File

@@ -9,7 +9,7 @@ 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 {find, findIndex} from '../array.js'; import {findIndex} from '../array.js';
/** /**
* Request encoding. One of 'KVP', 'REST'. * Request encoding. One of 'KVP', 'REST'.
@@ -372,10 +372,10 @@ export default WMTS;
*/ */
export function optionsFromCapabilities(wmtsCap, config) { export function optionsFromCapabilities(wmtsCap, config) {
const layers = wmtsCap['Contents']['Layer']; const layers = wmtsCap['Contents']['Layer'];
const l = find(layers, function (elt, index, array) { const l = layers.find(function (elt) {
return elt['Identifier'] == config['layer']; return elt['Identifier'] == config['layer'];
}); });
if (l === null) { if (!l) {
return null; return null;
} }
const tileMatrixSets = wmtsCap['Contents']['TileMatrixSet']; const tileMatrixSets = wmtsCap['Contents']['TileMatrixSet'];
@@ -383,7 +383,7 @@ export function optionsFromCapabilities(wmtsCap, config) {
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 = findIndex(l['TileMatrixSetLink'], function (elt, index, array) {
const tileMatrixSet = find(tileMatrixSets, function (el) { const tileMatrixSet = tileMatrixSets.find(function (el) {
return el['Identifier'] == elt['TileMatrixSet']; return el['Identifier'] == elt['TileMatrixSet'];
}); });
const supportedCRS = tileMatrixSet['SupportedCRS']; const supportedCRS = tileMatrixSet['SupportedCRS'];
@@ -442,7 +442,7 @@ export function optionsFromCapabilities(wmtsCap, config) {
} }
const matrixSets = wmtsCap['Contents']['TileMatrixSet']; const matrixSets = wmtsCap['Contents']['TileMatrixSet'];
const matrixSetObj = find(matrixSets, function (elt, index, array) { const matrixSetObj = matrixSets.find(function (elt) {
return elt['Identifier'] == matrixSet; return elt['Identifier'] == matrixSet;
}); });
@@ -477,8 +477,7 @@ export function optionsFromCapabilities(wmtsCap, config) {
//in case of matrix limits, use matrix limits to calculate extent //in case of matrix limits, use matrix limits to calculate extent
if (matrixLimits) { if (matrixLimits) {
selectedMatrixLimit = matrixLimits[matrixLimits.length - 1]; selectedMatrixLimit = matrixLimits[matrixLimits.length - 1];
const m = find( const m = matrixSetObj.TileMatrix.find(
matrixSetObj.TileMatrix,
(tileMatrixValue) => (tileMatrixValue) =>
tileMatrixValue.Identifier === selectedMatrixLimit.TileMatrix || tileMatrixValue.Identifier === selectedMatrixLimit.TileMatrix ||
matrixSetObj.Identifier + ':' + tileMatrixValue.Identifier === matrixSetObj.Identifier + ':' + tileMatrixValue.Identifier ===
@@ -556,7 +555,7 @@ export function optionsFromCapabilities(wmtsCap, config) {
for (let i = 0, ii = gets.length; i < ii; ++i) { for (let i = 0, ii = gets.length; i < ii; ++i) {
if (gets[i]['Constraint']) { if (gets[i]['Constraint']) {
const constraint = find(gets[i]['Constraint'], function (element) { const constraint = gets[i]['Constraint'].find(function (element) {
return element['name'] == 'GetEncoding'; return element['name'] == 'GetEncoding';
}); });
const encodings = constraint['AllowedValues']['Value']; const encodings = constraint['AllowedValues']['Value'];

View File

@@ -3,7 +3,6 @@
*/ */
import TileGrid from './TileGrid.js'; import TileGrid from './TileGrid.js';
import {find} from '../array.js';
import {get as getProjection} from '../proj.js'; import {get as getProjection} from '../proj.js';
/** /**
@@ -138,7 +137,7 @@ export function createFromCapabilitiesMatrixSet(
// use of matrixLimits to filter TileMatrices from GetCapabilities // use of matrixLimits to filter TileMatrices from GetCapabilities
// TileMatrixSet from unavailable matrix levels. // TileMatrixSet from unavailable matrix levels.
if (matrixLimits.length > 0) { if (matrixLimits.length > 0) {
matrixAvailable = find(matrixLimits, function (elt_ml) { matrixAvailable = matrixLimits.find(function (elt_ml) {
if (elt[identifierPropName] == elt_ml[matrixIdsPropName]) { if (elt[identifierPropName] == elt_ml[matrixIdsPropName]) {
return true; return true;
} }

View File

@@ -31,7 +31,6 @@ import {
get as getProjection, get as getProjection,
transform, transform,
} from '../../../../../src/ol/proj.js'; } from '../../../../../src/ol/proj.js';
import {find} from '../../../../../src/ol/array.js';
import {parse} from '../../../../../src/ol/xml.js'; import {parse} from '../../../../../src/ol/xml.js';
import {remove as removeTransform} from '../../../../../src/ol/proj/transforms.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 () { 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'; return feature.get('name') === 'Alaska';
}); });
expect(alaska).to.be.an(Feature); expect(alaska).to.be.an(Feature);

View File

@@ -3,7 +3,6 @@ import {
binarySearch, binarySearch,
equals, equals,
extend, extend,
find,
findIndex, findIndex,
isSorted, isSorted,
linearFindNearest, 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 () { describe('findIndex', function () {
it('finds index of numbers in an array', function () { it('finds index of numbers in an array', function () {
const a = [0, 1, 2, 3]; const a = [0, 1, 2, 3];