Skip to content

Commit b49ea57

Browse files
authored
Improve typings of exported methods, delete dead code (#1205)
1 parent 7aeec88 commit b49ea57

File tree

6 files changed

+36
-194
lines changed

6 files changed

+36
-194
lines changed

src/declass.test.ts

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/declass.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/deref.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {derefLayers} from './deref';
1+
import {derefLayers, type LayerWithRef} from './deref';
22
import {describe, test, expect} from 'vitest';
33

44
describe('deref', () => {
@@ -7,11 +7,11 @@ describe('deref', () => {
77
{
88
'id': 'parent',
99
'type': 'line'
10-
},
10+
} as LayerWithRef,
1111
{
1212
'id': 'child',
1313
'ref': 'parent'
14-
}
14+
} as LayerWithRef
1515
])).toEqual([
1616
{
1717
'id': 'parent',
@@ -29,11 +29,11 @@ describe('deref', () => {
2929
{
3030
'id': 'child',
3131
'ref': 'parent'
32-
},
32+
} as LayerWithRef,
3333
{
3434
'id': 'parent',
3535
'type': 'line'
36-
}
36+
} as LayerWithRef
3737
])).toEqual([
3838
{
3939
'id': 'child',

src/deref.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22
import {refProperties} from './util/ref_properties';
3+
import {LayerSpecification} from './types.g';
34

4-
function deref(layer, parent) {
5-
const result = {};
5+
export type LayerWithRef = LayerSpecification & { ref?: string };
6+
7+
function deref(layer: LayerWithRef, parent: LayerSpecification): LayerSpecification {
8+
const result: Partial<LayerSpecification> = {};
69

710
for (const k in layer) {
811
if (k !== 'ref') {
@@ -16,23 +19,20 @@ function deref(layer, parent) {
1619
}
1720
});
1821

19-
return result;
22+
return result as LayerSpecification;
2023
}
2124

2225
/**
23-
* Given an array of layers, some of which may contain `ref` properties
24-
* whose value is the `id` of another property, return a new array where
25-
* such layers have been augmented with the 'type', 'source', etc. properties
26-
* from the parent layer, and the `ref` property has been removed.
2726
*
2827
* The input is not modified. The output may contain references to portions
2928
* of the input.
3029
*
31-
* @private
32-
* @param {Array<Layer>} layers
33-
* @returns {Array<Layer>}
30+
* @param layers - array of layers, some of which may contain `ref` properties
31+
* whose value is the `id` of another property
32+
* @returns a new array where such layers have been augmented with the 'type', 'source', etc. properties
33+
* from the parent layer, and the `ref` property has been removed.
3434
*/
35-
export function derefLayers(layers) {
35+
export function derefLayers(layers: LayerWithRef[]): LayerSpecification[] {
3636
layers = layers.slice();
3737

3838
const map = Object.create(null);

src/group_by_layout.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import {groupByLayout} from './group_by_layout';
22
import {describe, test, expect} from 'vitest';
3+
import type {LayerSpecification} from './types.g';
34

45
describe('group by layout', () => {
56
test('group layers whose ref properties are identical', () => {
67
const a = {
78
'id': 'parent',
89
'type': 'line'
9-
};
10+
} as LayerSpecification;
1011
const b = {
1112
'id': 'child',
1213
'type': 'line'
13-
};
14+
} as LayerSpecification;
1415
expect(groupByLayout([a, b], {})).toEqual([[a, b]]);
1516
expect(groupByLayout([a, b], {})[0][0]).toBe(a);
1617
expect(groupByLayout([a, b], {})[0][1]).toBe(b);
@@ -21,11 +22,11 @@ describe('group by layout', () => {
2122
{
2223
'id': 'parent',
2324
'type': 'line'
24-
},
25+
} as LayerSpecification,
2526
{
2627
'id': 'child',
2728
'type': 'fill'
28-
}
29+
} as LayerSpecification
2930
], {})).toEqual([
3031
[{
3132
'id': 'parent',
@@ -43,12 +44,12 @@ describe('group by layout', () => {
4344
'id': 'parent',
4445
'type': 'line',
4546
'layout': {'a': 1, 'b': 2}
46-
},
47+
} as any as LayerSpecification,
4748
{
4849
'id': 'child',
4950
'type': 'line',
5051
'layout': {'b': 2, 'a': 1}
51-
}
52+
} as any as LayerSpecification
5253
], {})).toEqual([[
5354
{
5455
'id': 'parent',

src/group_by_layout.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
import {refProperties} from './util/ref_properties';
3+
import type {LayerSpecification} from './types.g';
34

4-
function stringify(obj) {
5+
function stringify(obj: any): string {
56
const type = typeof obj;
67
if (type === 'number' || type === 'boolean' || type === 'string' || obj === undefined || obj === null)
78
return JSON.stringify(obj);
@@ -23,7 +24,7 @@ function stringify(obj) {
2324
return `${str}}`;
2425
}
2526

26-
function getKey(layer) {
27+
function getKey(layer: LayerSpecification): string {
2728
let key = '';
2829
for (const k of refProperties) {
2930
key += `/${stringify(layer[k])}`;
@@ -32,26 +33,25 @@ function getKey(layer) {
3233
}
3334

3435
/**
35-
* Given an array of layers, return an array of arrays of layers where all
36-
* layers in each group have identical layout-affecting properties. These
37-
* are the properties that were formerly used by explicit `ref` mechanism
36+
* Groups layers by their layout-affecting properties.
37+
* These are the properties that were formerly used by explicit `ref` mechanism
3838
* for layers: 'type', 'source', 'source-layer', 'minzoom', 'maxzoom',
3939
* 'filter', and 'layout'.
4040
*
4141
* The input is not modified. The output layers are references to the
4242
* input layers.
4343
*
44-
* @private
45-
* @param {Array<Layer>} layers
46-
* @param {Object} [cachedKeys] - an object to keep already calculated keys.
47-
* @returns {Array<Array<Layer>>}
44+
* @param layers - an array of {@link LayerSpecification}.
45+
* @param cachedKeys - an object to keep already calculated keys.
46+
* @returns an array of arrays of {@link LayerSpecification} objects, where each inner array
47+
* contains layers that share the same layout-affecting properties.
4848
*/
49-
export function groupByLayout(layers, cachedKeys) {
50-
const groups = {};
49+
export function groupByLayout(layers: LayerSpecification[], cachedKeys?: Record<string, string>): LayerSpecification[][] {
50+
const groups: Record<string, LayerSpecification[]> = {};
5151

5252
for (let i = 0; i < layers.length; i++) {
5353

54-
const k = (cachedKeys && cachedKeys[layers[i].id]) || getKey(layers[i]);
54+
const k: string = (cachedKeys && cachedKeys[layers[i].id]) || getKey(layers[i]);
5555
// update the cache if there is one
5656
if (cachedKeys)
5757
cachedKeys[layers[i].id] = k;
@@ -63,7 +63,7 @@ export function groupByLayout(layers, cachedKeys) {
6363
group.push(layers[i]);
6464
}
6565

66-
const result = [];
66+
const result: LayerSpecification[][] = [];
6767

6868
for (const k in groups) {
6969
result.push(groups[k]);

0 commit comments

Comments
 (0)