Merge pull request #13937 from MoonE/use-nicer-functions

Use nicer functions, remove old code
This commit is contained in:
MoonE
2022-08-05 21:19:38 +02:00
committed by GitHub
39 changed files with 112 additions and 135 deletions

View File

@@ -49,16 +49,6 @@ export function numberSafeCompareFunction(a, b) {
return a > b ? 1 : a < b ? -1 : 0;
}
/**
* Whether the array contains the given object.
* @param {Array<*>} arr The array to test for the presence of the element.
* @param {*} obj The object for which to test.
* @return {boolean} The object is in the array.
*/
export function includes(arr, obj) {
return arr.indexOf(obj) >= 0;
}
/**
* {@link module:ol/tilegrid/TileGrid~TileGrid#getZForResolution} can use a function
* of this type to determine which nearest resolution to use.

View File

@@ -168,11 +168,11 @@ function fromStringInternal_(s) {
}
}
color = [r, g, b, a / 255];
} else if (s.indexOf('rgba(') == 0) {
} else if (s.startsWith('rgba(')) {
// rgba()
color = s.slice(5, -1).split(',').map(Number);
normalize(color);
} else if (s.indexOf('rgb(') == 0) {
} else if (s.startsWith('rgb(')) {
// rgb()
color = s.slice(4, -1).split(',').map(Number);
color.push(1);
@@ -225,7 +225,5 @@ export function isStringColor(s) {
if (NAMED_COLOR_RE_.test(s)) {
s = fromNamed(s);
}
return (
HEX_COLOR_RE_.test(s) || s.indexOf('rgba(') === 0 || s.indexOf('rgb(') === 0
);
return HEX_COLOR_RE_.test(s) || s.startsWith('rgba(') || s.startsWith('rgb(');
}

View File

@@ -67,7 +67,7 @@ class Target extends Disposable {
}
const listeners = this.listeners_ || (this.listeners_ = {});
const listenersForType = listeners[type] || (listeners[type] = []);
if (listenersForType.indexOf(listener) === -1) {
if (!listenersForType.includes(listener)) {
listenersForType.push(listener);
}
}

View File

@@ -167,7 +167,7 @@ class GMLBase extends XMLFeature {
const child = /** @type {Element} */ (node.childNodes[i]);
if (child.nodeType === 1) {
const ft = child.nodeName.split(':').pop();
if (featureType.indexOf(ft) === -1) {
if (!featureType.includes(ft)) {
let key = '';
let count = 0;
const uri = child.namespaceURI;
@@ -206,10 +206,9 @@ class GMLBase extends XMLFeature {
/** @type {Object<string, import("../xml.js").Parser>} */
const parsers = {};
for (let i = 0, ii = featureTypes.length; i < ii; ++i) {
const featurePrefix =
featureTypes[i].indexOf(':') === -1
? defaultPrefix
: featureTypes[i].split(':')[0];
const featurePrefix = featureTypes[i].includes(':')
? featureTypes[i].split(':')[0]
: defaultPrefix;
if (featurePrefix === p) {
parsers[featureTypes[i].split(':').pop()] =
localName == 'featureMembers'

View File

@@ -262,7 +262,7 @@ class MVT extends FeatureFormat {
const pbfLayers = pbf.readFields(layersPBFReader, {});
const features = [];
for (const name in pbfLayers) {
if (layers && layers.indexOf(name) == -1) {
if (layers && !layers.includes(name)) {
continue;
}
const pbfLayer = pbfLayers[name];

View File

@@ -110,7 +110,7 @@ class TopoJSON extends JSONFeature {
const property = this.layerName_;
let feature;
for (const objectName in topoJSONFeatures) {
if (this.layers_ && this.layers_.indexOf(objectName) == -1) {
if (this.layers_ && !this.layers_.includes(objectName)) {
continue;
}
if (topoJSONFeatures[objectName].type === 'GeometryCollection') {

View File

@@ -822,7 +822,7 @@ function getTypeName(featurePrefix, featureType) {
featurePrefix = featurePrefix ? featurePrefix : FEATURE_PREFIX;
const prefix = featurePrefix + ':';
// The featureType already contains the prefix.
if (featureType.indexOf(prefix) === 0) {
if (featureType.startsWith(prefix)) {
return featureType;
} else {
return prefix + featureType;

View File

@@ -498,10 +498,10 @@ class WkbWriter {
*/
writeWkbHeader(wkbType, srid) {
wkbType %= 1000; // Assume 1000 is an upper limit for type ID
if (this.layout_.indexOf('Z') >= 0) {
if (this.layout_.includes('Z')) {
wkbType += this.isEWKB_ ? 0x80000000 : 1000;
}
if (this.layout_.indexOf('M') >= 0) {
if (this.layout_.includes('M')) {
wkbType += this.isEWKB_ ? 0x40000000 : 2000;
}
if (this.isEWKB_ && Number.isInteger(srid)) {
@@ -864,7 +864,7 @@ class WKB extends FeatureFormat {
options.dataProjection && getProjection(options.dataProjection);
if (dataProjection) {
const code = dataProjection.getCode();
if (code.indexOf('EPSG:') === 0) {
if (code.startsWith('EPSG:')) {
srid = Number(code.substring(5));
}
}

View File

@@ -11,13 +11,13 @@ const ua =
* User agent string says we are dealing with Firefox as browser.
* @type {boolean}
*/
export const FIREFOX = ua.indexOf('firefox') !== -1;
export const FIREFOX = ua.includes('firefox');
/**
* User agent string says we are dealing with Safari as browser.
* @type {boolean}
*/
export const SAFARI = ua.indexOf('safari') !== -1 && ua.indexOf('chrom') == -1;
export const SAFARI = ua.includes('safari') && !ua.includes('chrom');
/**
* https://bugs.webkit.org/show_bug.cgi?id=237906
@@ -25,22 +25,20 @@ export const SAFARI = ua.indexOf('safari') !== -1 && ua.indexOf('chrom') == -1;
*/
export const SAFARI_BUG_237906 =
SAFARI &&
!!(
ua.indexOf('version/15.4') >= 0 ||
ua.match(/cpu (os|iphone os) 15_4 like mac os x/)
);
(ua.includes('version/15.4') ||
/cpu (os|iphone os) 15_4 like mac os x/.test(ua));
/**
* User agent string says we are dealing with a WebKit engine.
* @type {boolean}
*/
export const WEBKIT = ua.indexOf('webkit') !== -1 && ua.indexOf('edge') == -1;
export const WEBKIT = ua.includes('webkit') && !ua.includes('edge');
/**
* User agent string says we are dealing with a Mac as platform.
* @type {boolean}
*/
export const MAC = ua.indexOf('macintosh') !== -1;
export const MAC = ua.includes('macintosh');
/**
* The ratio between physical pixels and device-independent pixels

View File

@@ -455,7 +455,7 @@ class Modify extends PointerInteraction {
const segment = segments[i];
for (let s = 0, ss = segment.length; s < ss; ++s) {
const feature = segment[s].feature;
if (feature && features.indexOf(feature) === -1) {
if (feature && !features.includes(feature)) {
this.featuresBeingModified_.push(feature);
}
}
@@ -887,11 +887,11 @@ class Modify extends PointerInteraction {
const dragSegment = this.dragSegments_[i];
const segmentData = dragSegment[0];
const feature = segmentData.feature;
if (features.indexOf(feature) === -1) {
if (!features.includes(feature)) {
features.push(feature);
}
const geometry = segmentData.geometry;
if (geometries.indexOf(geometry) === -1) {
if (!geometries.includes(geometry)) {
geometries.push(geometry);
}
const depth = segmentData.depth;

View File

@@ -24,7 +24,7 @@ export function jsonp(url, callback, opt_errback, opt_callbackParam) {
script.async = true;
script.src =
url +
(url.indexOf('?') == -1 ? '?' : '&') +
(url.includes('?') ? '&' : '?') +
(opt_callbackParam || 'callback') +
'=' +
key;
@@ -131,7 +131,7 @@ export function getJSON(url) {
* @return {string} The full URL.
*/
export function resolveUrl(base, url) {
if (url.indexOf('://') >= 0) {
if (url.includes('://')) {
return url;
}
return new URL(url, base).href;

View File

@@ -231,7 +231,7 @@ class ExecutorGroup {
if (
!declutteredFeatures ||
(builderType !== 'Image' && builderType !== 'Text') ||
declutteredFeatures.indexOf(feature) !== -1
declutteredFeatures.includes(feature)
) {
const idx = (indexes[i] - 3) / 4;
const x = hitTolerance - (idx % contextSize);

View File

@@ -330,7 +330,7 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
}
if (
!this.newTiles_ &&
(inTransition || this.renderedTiles.indexOf(tile) === -1)
(inTransition || !this.renderedTiles.includes(tile))
) {
this.newTiles_ = true;
}

View File

@@ -195,11 +195,7 @@ class Zoomify extends TileImage {
});
let url = options.url;
if (
url &&
url.indexOf('{TileGroup}') == -1 &&
url.indexOf('{tileIndex}') == -1
) {
if (url && !url.includes('{TileGroup}') && !url.includes('{tileIndex}')) {
url += '{TileGroup}/{z}-{x}-{y}.jpg';
}
const urls = expandUrl(url);

View File

@@ -114,7 +114,7 @@ export function getMapTileUrlTemplate(links, mediaType) {
}
if (knownMapMediaTypes[link.type]) {
fallbackUrlTemplate = link.href;
} else if (!fallbackUrlTemplate && link.type.indexOf('image/') === 0) {
} else if (!fallbackUrlTemplate && link.type.startsWith('image/')) {
fallbackUrlTemplate = link.href;
}
}

View File

@@ -204,7 +204,7 @@ export function isTypeUnique(valueType) {
*/
export function numberToGlsl(v) {
const s = v.toString();
return s.indexOf('.') === -1 ? s + '.0' : s;
return s.includes('.') ? s : s + '.0';
}
/**
@@ -395,7 +395,7 @@ Operators['get'] = {
assertArgsCount(args, 1);
assertString(args[0]);
const value = args[0].toString();
if (context.attributes.indexOf(value) === -1) {
if (!context.attributes.includes(value)) {
context.attributes.push(value);
}
const prefix = context.inFragmentShader ? 'v_' : 'a_';
@@ -420,7 +420,7 @@ Operators['var'] = {
assertArgsCount(args, 1);
assertString(args[0]);
const value = args[0].toString();
if (context.variables.indexOf(value) === -1) {
if (!context.variables.includes(value)) {
context.variables.push(value);
}
return uniformNameForVariable(value);

View File

@@ -143,7 +143,7 @@ export function createFromCapabilitiesMatrixSet(
}
// Fallback for tileMatrix identifiers that don't get prefixed
// by their tileMatrixSet identifiers.
if (elt[identifierPropName].indexOf(':') === -1) {
if (!elt[identifierPropName].includes(':')) {
return (
matrixSet[identifierPropName] + ':' + elt[identifierPropName] ===
elt_ml[matrixIdsPropName]

View File

@@ -22,6 +22,6 @@ export function appendParams(uri, params) {
// remove any trailing ? or &
uri = uri.replace(/[?&]$/, '');
// append ? or & depending on whether uri has existing parameters
uri = uri.indexOf('?') === -1 ? uri + '?' : uri + '&';
uri += uri.includes('?') ? '&' : '?';
return uri + qs;
}

View File

@@ -562,7 +562,7 @@ export function parseLiteralStyle(style) {
// for each feature attribute used in the fragment shader, define a varying that will be used to pass data
// from the vertex to the fragment shader, as well as an attribute in the vertex shader (if not already present)
fragContext.attributes.forEach(function (attrName) {
if (vertContext.attributes.indexOf(attrName) === -1) {
if (!vertContext.attributes.includes(attrName)) {
vertContext.attributes.push(attrName);
}
builder.addVarying(`v_${attrName}`, 'float', `a_${attrName}`);