From db43cb6b5431509fbe07b8c2a7a16ebab006b5e7 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 31 Aug 2021 15:39:56 +0200 Subject: [PATCH 1/4] Use native endsWith function --- src/style/light.ts | 4 ++-- src/style/style_layer.ts | 6 +++--- src/util/util.ts | 9 --------- test/unit/util/util.test.js | 8 +------- 4 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/style/light.ts b/src/style/light.ts index 8c40cda1dc..69455644af 100644 --- a/src/style/light.ts +++ b/src/style/light.ts @@ -1,6 +1,6 @@ import styleSpec from '../style-spec/reference/latest'; -import {endsWith, extend, sphericalToCartesian} from '../util/util'; +import {extend, sphericalToCartesian} from '../util/util'; import {Evented} from '../util/evented'; import { validateStyle, @@ -101,7 +101,7 @@ class Light extends Evented { for (const name in light) { const value = light[name]; - if (endsWith(name, TRANSITION_SUFFIX)) { + if (name.endsWith(TRANSITION_SUFFIX)) { this._transitionable.setTransition(name.slice(0, -TRANSITION_SUFFIX.length) as keyof Props, value); } else { this._transitionable.setValue(name as keyof Props, value); diff --git a/src/style/style_layer.ts b/src/style/style_layer.ts index 0923876dac..38f884b3df 100644 --- a/src/style/style_layer.ts +++ b/src/style/style_layer.ts @@ -1,4 +1,4 @@ -import {endsWith, filterObject} from '../util/util'; +import {filterObject} from '../util/util'; import styleSpec from '../style-spec/reference/latest'; import { @@ -140,7 +140,7 @@ class StyleLayer extends Evented { } getPaintProperty(name: string) { - if (endsWith(name, TRANSITION_SUFFIX)) { + if (name.endsWith(TRANSITION_SUFFIX)) { return this._transitionablePaint.getTransition(name.slice(0, -TRANSITION_SUFFIX.length)); } else { return this._transitionablePaint.getValue(name); @@ -155,7 +155,7 @@ class StyleLayer extends Evented { } } - if (endsWith(name, TRANSITION_SUFFIX)) { + if (name.endsWith(TRANSITION_SUFFIX)) { this._transitionablePaint.setTransition(name.slice(0, -TRANSITION_SUFFIX.length), (value as any) || undefined); return false; } else { diff --git a/src/util/util.ts b/src/util/util.ts index 634064f317..78c4f79131 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -269,15 +269,6 @@ export function bindAll(fns: Array, context: any): void { }); } -/** - * Determine if a string ends with a particular substring - * - * @private - */ -export function endsWith(string: string, suffix: string): boolean { - return string.indexOf(suffix, string.length - suffix.length) !== -1; -} - /** * Create an object by mapping all the values of an existing object while * preserving their keys. diff --git a/test/unit/util/util.test.js b/test/unit/util/util.test.js index c6a575a461..80b8cec650 100644 --- a/test/unit/util/util.test.js +++ b/test/unit/util/util.test.js @@ -1,6 +1,6 @@ import {test} from '../../util/test'; -import {easeCubicInOut, keysDifference, extend, pick, uniqueId, bindAll, asyncAll, clamp, wrap, bezier, endsWith, mapObject, filterObject, deepEqual, clone, arraysIntersect, isCounterClockwise, isClosedPolygon, parseCacheControl, uuid, validateUuid, nextPowerOfTwo, isPowerOfTwo} from '../../../rollup/build/tsc/util/util'; +import {easeCubicInOut, keysDifference, extend, pick, uniqueId, bindAll, asyncAll, clamp, wrap, bezier, mapObject, filterObject, deepEqual, clone, arraysIntersect, isCounterClockwise, isClosedPolygon, parseCacheControl, uuid, validateUuid, nextPowerOfTwo, isPowerOfTwo} from '../../../rollup/build/tsc/util/util'; import Point from '../../../rollup/build/tsc/symbol/point'; test('util', (t) => { @@ -144,12 +144,6 @@ test('util', (t) => { }); }); - t.test('endsWith', (t) => { - t.ok(endsWith('mapbox', 'box')); - t.notOk(endsWith('mapbox', 'map')); - t.end(); - }); - t.test('mapObject', (t) => { t.plan(6); t.deepEqual(mapObject({}, () => { t.ok(false); }), {}); From 4c8bf72d4f999a635b8a030d4a6c5ac7ecac8d63 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 31 Aug 2021 15:47:03 +0200 Subject: [PATCH 2/4] Use native Object.values function --- bench/benchmarks/worker_transfer.js | 5 ++--- src/render/painter.ts | 3 +-- src/source/source_cache.ts | 4 ++-- src/source/worker_tile.ts | 4 ++-- src/style/style_layer_index.ts | 3 +-- src/util/util.ts | 16 ---------------- 6 files changed, 8 insertions(+), 27 deletions(-) diff --git a/bench/benchmarks/worker_transfer.js b/bench/benchmarks/worker_transfer.js index 2c5c37872d..008b9ddd9f 100644 --- a/bench/benchmarks/worker_transfer.js +++ b/bench/benchmarks/worker_transfer.js @@ -6,7 +6,6 @@ import fetchStyle from '../lib/fetch_style'; import TileParser from '../lib/tile_parser'; import {OverscaledTileID} from '../../src/source/tile_id'; import {serialize, deserialize} from '../../src/util/web_worker_transfer'; -import {values} from '../../src/util/util'; export default class WorkerTransfer extends Benchmark { parser: TileParser; @@ -48,8 +47,8 @@ export default class WorkerTransfer extends Benchmark { return Promise.all(tiles.map(tile => this.parser.parseTile(tile))); }).then((tileResults) => { const payload = tileResults - .concat(values(this.parser.icons)) - .concat(values(this.parser.glyphs)).map((obj) => serialize(obj, [])); + .concat(Object.values(this.parser.icons)) + .concat(Object.values(this.parser.glyphs)).map((obj) => serialize(obj, [])); this.payloadJSON = payload.map(barePayload); this.payloadTiles = payload.slice(0, tileResults.length); }); diff --git a/src/render/painter.ts b/src/render/painter.ts index 2410a7d7ac..ee7af16ab7 100644 --- a/src/render/painter.ts +++ b/src/render/painter.ts @@ -6,7 +6,6 @@ import EXTENT from '../data/extent'; import pixelsToTileUnits from '../source/pixels_to_tile_units'; import SegmentVector from '../data/segment'; import {RasterBoundsArray, PosArray, TriangleIndexArray, LineStripIndexArray} from '../data/array_types'; -import {values} from '../util/util'; import rasterBoundsAttributes from '../data/raster_bounds_attributes'; import posAttributes from '../data/pos_attributes'; import ProgramConfiguration from '../data/program_configuration'; @@ -458,7 +457,7 @@ class Painter { //Use source with highest maxzoom let selectedSource; let sourceCache; - const layers = values(this.style._layers); + const layers = Object.values(this.style._layers); layers.forEach((layer) => { if (layer.source && !layer.isHidden(this.transform.zoom)) { if (layer.source !== (sourceCache && sourceCache.id)) { diff --git a/src/source/source_cache.ts b/src/source/source_cache.ts index a3bd5a338d..8a6557bf06 100644 --- a/src/source/source_cache.ts +++ b/src/source/source_cache.ts @@ -4,7 +4,7 @@ import Tile from './tile'; import {Event, ErrorEvent, Evented} from '../util/evented'; import TileCache from './tile_cache'; import MercatorCoordinate from '../geo/mercator_coordinate'; -import {keysDifference, values} from '../util/util'; +import {keysDifference} from '../util/util'; import EXTENT from '../data/extent'; import Context from '../gl/context'; import Point from '../symbol/point'; @@ -185,7 +185,7 @@ class SourceCache extends Evented { * @private */ getIds(): Array { - return (values(this._tiles) as any).map((tile: Tile) => tile.tileID).sort(compareTileId).map(id => id.key); + return (Object.values(this._tiles) as any).map((tile: Tile) => tile.tileID).sort(compareTileId).map(id => id.key); } getRenderableIds(symbolLayer?: boolean): Array { diff --git a/src/source/worker_tile.ts b/src/source/worker_tile.ts index 45c3d3d77b..a0ab33f351 100644 --- a/src/source/worker_tile.ts +++ b/src/source/worker_tile.ts @@ -7,7 +7,7 @@ import SymbolBucket from '../data/bucket/symbol_bucket'; import LineBucket from '../data/bucket/line_bucket'; import FillBucket from '../data/bucket/fill_bucket'; import FillExtrusionBucket from '../data/bucket/fill_extrusion_bucket'; -import {warnOnce, mapObject, values} from '../util/util'; +import {warnOnce, mapObject} from '../util/util'; import assert from 'assert'; import ImageAtlas from '../render/image_atlas'; import GlyphAtlas from '../render/glyph_atlas'; @@ -200,7 +200,7 @@ class WorkerTile { this.status = 'done'; callback(null, { - buckets: values(buckets).filter(b => !b.isEmpty()), + buckets: Object.values(buckets).filter(b => !b.isEmpty()), featureIndex, collisionBoxArray: this.collisionBoxArray, glyphAtlasImage: glyphAtlas.image, diff --git a/src/style/style_layer_index.ts b/src/style/style_layer_index.ts index c97a0dd8e4..b132559ea8 100644 --- a/src/style/style_layer_index.ts +++ b/src/style/style_layer_index.ts @@ -1,7 +1,6 @@ import StyleLayer from './style_layer'; import createStyleLayer from './create_style_layer'; -import {values} from '../util/util'; import featureFilter from '../style-spec/feature_filter'; import groupByLayout from '../style-spec/group_by_layout'; @@ -52,7 +51,7 @@ class StyleLayerIndex { this.familiesBySource = {}; - const groups = groupByLayout(values(this._layerConfigs), this.keyCache); + const groups = groupByLayout(Object.values(this._layerConfigs), this.keyCache); for (const layerConfigs of groups) { const layers = layerConfigs.map((layerConfig) => this._layers[layerConfig.id]); diff --git a/src/util/util.ts b/src/util/util.ts index 78c4f79131..68c66c20ec 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -108,22 +108,6 @@ export function asyncAll( }); } -/* - * Polyfill for Object.values. Not fully spec compliant, but we don't - * need it to be. - * - * @private - */ -export function values( - obj: {[key: string]: T} -): Array { - const result = []; - for (const k in obj) { - result.push(obj[k]); - } - return result; -} - /* * Compute the difference between the keys in one object and the keys * in another object. From 55ad0bfb1b84e595b9b19119866a192e3872c192 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 31 Aug 2021 15:49:06 +0200 Subject: [PATCH 3/4] Use native Number.MAX_SAFE_INTEGER value --- src/data/feature_position_map.ts | 4 +--- src/style/style_layer/line_style_layer.ts | 4 ++-- src/util/util.ts | 3 --- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/data/feature_position_map.ts b/src/data/feature_position_map.ts index 82905b3e24..316027b052 100644 --- a/src/data/feature_position_map.ts +++ b/src/data/feature_position_map.ts @@ -82,11 +82,9 @@ export default class FeaturePositionMap { } } -const MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; - function getNumericId(value: unknown) { const numValue = +value; - if (!isNaN(numValue) && numValue <= MAX_SAFE_INTEGER) { + if (!isNaN(numValue) && numValue <= Number.MAX_SAFE_INTEGER) { return numValue; } return murmur3(String(value)); diff --git a/src/style/style_layer/line_style_layer.ts b/src/style/style_layer/line_style_layer.ts index 5c32953708..0b5099d291 100644 --- a/src/style/style_layer/line_style_layer.ts +++ b/src/style/style_layer/line_style_layer.ts @@ -5,7 +5,7 @@ import LineBucket from '../../data/bucket/line_bucket'; import {polygonIntersectsBufferedMultiLine} from '../../util/intersection_tests'; import {getMaximumPaintValue, translateDistance, translate} from '../query_utils'; import properties, {LayoutPropsPossiblyEvaluated, PaintPropsPossiblyEvaluated} from './line_style_layer_properties'; -import {extend, MAX_SAFE_INTEGER} from '../../util/util'; +import {extend} from '../../util/util'; import EvaluationParameters from '../evaluation_parameters'; import {Transitionable, Transitioning, Layout, PossiblyEvaluated, DataDrivenProperty} from '../properties'; @@ -58,7 +58,7 @@ class LineStyleLayer extends StyleLayer { if (name === 'line-gradient') { const expression: ZoomConstantExpression<"source"> = (this._transitionablePaint._values['line-gradient'].value.expression as any); this.stepInterpolant = expression._styleExpression.expression instanceof Step; - this.gradientVersion = (this.gradientVersion + 1) % MAX_SAFE_INTEGER; + this.gradientVersion = (this.gradientVersion + 1) % Number.MAX_SAFE_INTEGER; } } diff --git a/src/util/util.ts b/src/util/util.ts index 68c66c20ec..bd4cb845dc 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -4,9 +4,6 @@ import Point from '../symbol/point'; import type {Callback} from '../types/callback'; -// Number.MAX_SAFE_INTEGER not available in IE -export const MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; - /** * @module util * @private From 368d3800665866bbbe957d1c7e4d514eb377913e Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 31 Aug 2021 15:54:48 +0200 Subject: [PATCH 4/4] Fix typos, remove unused functions The functions are already defined in `src/util/mapbox.ts` --- src/util/util.ts | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/util/util.ts b/src/util/util.ts index bd4cb845dc..abf82fa7bd 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -337,7 +337,7 @@ export function isCounterClockwise(a: Point, b: Point, c: Point): boolean { } /** - * Returns the signed area for the polygon ring. Postive areas are exterior rings and + * Returns the signed area for the polygon ring. Positive areas are exterior rings and * have a clockwise winding. Negative areas are interior rings and have a counter clockwise * ordering. * @@ -409,7 +409,7 @@ export function sphericalToCartesian([r, azimuthal, polar]: [number, number, num /* global self, WorkerGlobalScope */ /** - * Retuns true if the when run in the web-worker context. + * Returns true if the when run in the web-worker context. * * @private * @returns {boolean} @@ -500,12 +500,3 @@ export function b64DecodeUnicode(str: string) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); //eslint-disable-line }).join('')); } - -const mapboxHTTPURLRe = /^((https?:)?\/\/)?([^\/]+\.)?mapbox\.c(n|om)(\/|\?|$)/i; -export function isMapboxHTTPURL(url: string): boolean { - return mapboxHTTPURLRe.test(url); -} - -export function hasCacheDefeatingSku(url: string) { - return url.indexOf('sku=') > 0 && isMapboxHTTPURL(url); -}