• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / react-map-gl / 5897932269

18 Aug 2023 01:56AM UTC coverage: 83.532% (-0.2%) from 83.762%
5897932269

push

github

web-flow
Fix maplibre get started (#2256)

270 of 335 branches covered (80.6%)

Branch coverage included in aggregate %.

2388 of 2847 relevant lines covered (83.88%)

10.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.92
/src/mapbox/create-ref.ts
1
import type {
1✔
2
  MapInstance,
1✔
3
  MapInstanceInternal,
1✔
4
  MapStyle,
1✔
5
  Callbacks,
1✔
6
  LngLatLike,
1✔
7
  PointLike
1✔
8
} from '../types';
1✔
9
import type Mapbox from './mapbox';
1✔
10

1✔
11
/** These methods may break the react binding if called directly */
1✔
12
const skipMethods = [
1✔
13
  'setMaxBounds',
1✔
14
  'setMinZoom',
1✔
15
  'setMaxZoom',
1✔
16
  'setMinPitch',
1✔
17
  'setMaxPitch',
1✔
18
  'setRenderWorldCopies',
1✔
19
  'setProjection',
1✔
20
  'setStyle',
1✔
21
  'addSource',
1✔
22
  'removeSource',
1✔
23
  'addLayer',
1✔
24
  'removeLayer',
1✔
25
  'setLayerZoomRange',
1✔
26
  'setFilter',
1✔
27
  'setPaintProperty',
1✔
28
  'setLayoutProperty',
1✔
29
  'setLight',
1✔
30
  'setTerrain',
1✔
31
  'setFog',
1✔
32
  'remove'
1✔
33
] as const;
1✔
34

1✔
35
export type MapRef<MapT extends MapInstance> = {
1✔
36
  getMap(): MapT;
1✔
37
} & Omit<MapT, typeof skipMethods[number]>;
1✔
38

1✔
39
export default function createRef<
11✔
40
  StyleT extends MapStyle,
11✔
41
  CallbacksT extends Callbacks,
11✔
42
  MapT extends MapInstance
11✔
43
>(mapInstance: Mapbox<StyleT, CallbacksT, MapT>): MapRef<MapT> {
11✔
44
  if (!mapInstance) {
11!
45
    return null;
×
46
  }
×
47

11✔
48
  const map = mapInstance.map as MapInstanceInternal<MapT>;
11✔
49
  const result: any = {
11✔
50
    getMap: () => map,
11✔
51

11✔
52
    // Overwrite getters to use our shadow transform
11✔
53
    getCenter: () => mapInstance.transform.center,
11✔
54
    getZoom: () => mapInstance.transform.zoom,
11✔
55
    getBearing: () => mapInstance.transform.bearing,
11✔
56
    getPitch: () => mapInstance.transform.pitch,
11✔
57
    getPadding: () => mapInstance.transform.padding,
11✔
58
    getBounds: () => mapInstance.transform.getBounds(),
11✔
59
    project: (lnglat: LngLatLike) => {
11✔
60
      const tr = map.transform;
×
61
      map.transform = mapInstance.transform;
×
62
      const result = map.project(lnglat);
×
63
      map.transform = tr;
×
64
      return result;
×
65
    },
11✔
66
    unproject: (point: PointLike) => {
11✔
67
      const tr = map.transform;
×
68
      map.transform = mapInstance.transform;
×
69
      const result = map.unproject(point);
×
70
      map.transform = tr;
×
71
      return result;
×
72
    },
11✔
73
    // options diverge between mapbox and maplibre
11✔
74
    queryTerrainElevation: (lnglat: LngLatLike, options?: any) => {
11✔
75
      const tr = map.transform;
×
76
      map.transform = mapInstance.transform;
×
77
      const result = map.queryTerrainElevation(lnglat, options);
×
78
      map.transform = tr;
×
79
      return result;
×
80
    }
×
81
  };
11✔
82

11✔
83
  for (const key of getMethodNames(map)) {
11✔
84
    // @ts-expect-error
418✔
85
    if (!(key in result) && !skipMethods.includes(key)) {
418✔
86
      result[key] = map[key].bind(map);
187✔
87
    }
187✔
88
  }
418✔
89

11✔
90
  return result;
11✔
91
}
11✔
92

1✔
93
function getMethodNames(obj) {
11✔
94
  const result = new Set<string>();
11✔
95

11✔
96
  let proto = obj;
11✔
97
  while (proto) {
11✔
98
    for (const key of Object.getOwnPropertyNames(proto)) {
44✔
99
      if (
715✔
100
        key[0] !== '_' &&
715✔
101
        typeof obj[key] === 'function' &&
715✔
102
        key !== 'fire' &&
715✔
103
        key !== 'setEventedParent'
451✔
104
      ) {
715✔
105
        result.add(key);
440✔
106
      }
440✔
107
    }
715✔
108
    proto = Object.getPrototypeOf(proto);
44✔
109
  }
44✔
110
  return Array.from(result);
11✔
111
}
11✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc