Merge pull request #13887 from tschaub/array-find

Remove find and findIndex from array module
This commit is contained in:
Tim Schaub
2022-07-27 18:09:42 -06:00
committed by GitHub
5 changed files with 11 additions and 108 deletions

View File

@@ -168,25 +168,6 @@ export function remove(arr, obj) {
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} arr2 The second array to compare.
@@ -227,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.

View File

@@ -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 {find, findIndex} from '../array.js';
/**
* Request encoding. One of 'KVP', 'REST'.
@@ -372,18 +371,18 @@ 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'];
let idx;
if (l['TileMatrixSetLink'].length > 1) {
if ('projection' in config) {
idx = findIndex(l['TileMatrixSetLink'], function (elt, index, array) {
const tileMatrixSet = find(tileMatrixSets, function (el) {
idx = l['TileMatrixSetLink'].findIndex(function (elt) {
const tileMatrixSet = tileMatrixSets.find(function (el) {
return el['Identifier'] == elt['TileMatrixSet'];
});
const supportedCRS = tileMatrixSet['SupportedCRS'];
@@ -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 {
@@ -442,7 +441,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 +476,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 +554,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'];

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -3,8 +3,6 @@ import {
binarySearch,
equals,
extend,
find,
findIndex,
isSorted,
linearFindNearest,
numberSafeCompareFunction,
@@ -383,64 +381,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];
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);