Merge pull request #13887 from tschaub/array-find
Remove find and findIndex from array module
This commit is contained in:
@@ -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.
|
||||||
@@ -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 {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 {find, findIndex} from '../array.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request encoding. One of 'KVP', 'REST'.
|
* Request encoding. One of 'KVP', 'REST'.
|
||||||
@@ -372,18 +371,18 @@ 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'];
|
||||||
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 = 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'];
|
||||||
@@ -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 {
|
||||||
@@ -442,7 +441,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 +476,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 +554,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'];
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ import {
|
|||||||
binarySearch,
|
binarySearch,
|
||||||
equals,
|
equals,
|
||||||
extend,
|
extend,
|
||||||
find,
|
|
||||||
findIndex,
|
|
||||||
isSorted,
|
isSorted,
|
||||||
linearFindNearest,
|
linearFindNearest,
|
||||||
numberSafeCompareFunction,
|
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 () {
|
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