Skip to content

Commit 9a2d6a3

Browse files
authored
🤖 Merge PR DefinitelyTyped#74500 Add types for d3-tile by @NaciriMouad
1 parent 794d9e4 commit 9a2d6a3

File tree

5 files changed

+289
-0
lines changed

5 files changed

+289
-0
lines changed

types/d3-tile/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts

types/d3-tile/d3-tile-tests.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import * as d3 from "d3";
2+
3+
// test variables
4+
let tileLayout: d3.TileLayout;
5+
let tile: d3.Tile;
6+
let tiles: d3.Tiles;
7+
let zoomTransform: d3.ZoomTransform;
8+
9+
// test tile() constructor
10+
tileLayout = d3.tile();
11+
12+
// test invoke with no arguments
13+
tiles = tileLayout();
14+
15+
// test invoke with ZoomTransform argument
16+
zoomTransform = d3.zoomTransform(document.createElement("svg"));
17+
tiles = tileLayout(zoomTransform);
18+
19+
// test size getter/setter
20+
let sizeValue: [number, number] = tileLayout.size();
21+
tileLayout = tileLayout.size([800, 600]);
22+
23+
// test scale getter/setter with number
24+
let scaleFunc: (...args: any[]) => number = tileLayout.scale();
25+
tileLayout = tileLayout.scale(256);
26+
tileLayout = tileLayout.scale((...args: any[]) => 256);
27+
28+
// test translate getter/setter with array
29+
let translateFunc: (...args: any[]) => [number, number] = tileLayout.translate();
30+
tileLayout = tileLayout.translate([0, 0]);
31+
tileLayout = tileLayout.translate((...args: any[]) => [0, 0]);
32+
33+
// test zoomDelta getter/setter
34+
let zoomDeltaValue: number = tileLayout.zoomDelta();
35+
tileLayout = tileLayout.zoomDelta(0);
36+
tileLayout = tileLayout.zoomDelta(-1);
37+
tileLayout = tileLayout.zoomDelta(1);
38+
39+
// test clamp getter/setter
40+
let clampValue: boolean = tileLayout.clamp();
41+
tileLayout = tileLayout.clamp(true);
42+
tileLayout = tileLayout.clamp(false);
43+
44+
// test clampX getter/setter
45+
let clampXValue: boolean = tileLayout.clampX();
46+
tileLayout = tileLayout.clampX(true);
47+
tileLayout = tileLayout.clampX(false);
48+
49+
// test clampY getter/setter
50+
let clampYValue: boolean = tileLayout.clampY();
51+
tileLayout = tileLayout.clampY(true);
52+
tileLayout = tileLayout.clampY(false);
53+
54+
// test tileSize getter/setter
55+
let tileSizeValue: number = tileLayout.tileSize();
56+
tileLayout = tileLayout.tileSize(256);
57+
tileLayout = tileLayout.tileSize(512);
58+
59+
// test extent getter/setter
60+
let extentValue: [[number, number], [number, number]] = tileLayout.extent();
61+
tileLayout = tileLayout.extent([[0, 0], [960, 500]]);
62+
tileLayout = tileLayout.extent([[0, 0], [1920, 1080]]);
63+
64+
// test tiles array properties
65+
let tilesLength: number = tiles.length;
66+
let tilesScale: number = tiles.scale;
67+
let tilesTranslate: [number, number] = tiles.translate;
68+
69+
// test tile coordinates
70+
for (let i = 0; i < tiles.length; i++) {
71+
let tileCoord: d3.Tile = tiles[i];
72+
let x: number = tileCoord[0];
73+
let y: number = tileCoord[1];
74+
let z: number = tileCoord[2];
75+
}
76+
77+
// test tileWrap function
78+
let wrappedTile: [number, number, number] = d3.tileWrap([1, 2, 3]);
79+
let wrappedX: number = wrappedTile[0];
80+
let wrappedY: number = wrappedTile[1];
81+
let wrappedZ: number = wrappedTile[2];
82+
83+
// test method chaining
84+
tileLayout
85+
.size([800, 600])
86+
.scale(256)
87+
.translate([0, 0])
88+
.zoomDelta(0)
89+
.clamp(true)
90+
.clampX(true)
91+
.clampY(true)
92+
.tileSize(256)
93+
.extent([[0, 0], [960, 500]]);

types/d3-tile/index.d.ts

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { ZoomTransform } from "d3";
2+
3+
declare module "d3" {
4+
type Tile = [number, number, number];
5+
6+
interface Tiles extends Array<Tile> {
7+
translate: [number, number];
8+
scale: number;
9+
}
10+
11+
interface TileLayout {
12+
/**
13+
* Computes the set of tiles to display given the current settings,
14+
* computing the scale and translate by invoking the corresponding
15+
* accessors with the given arguments. Returns an array of [x, y, z]
16+
* arrays representing the x- (horizontal), y- (vertical) and z- (zoom)
17+
* coordinates of the visible tiles. The returned tiles array also has
18+
* tiles.scale and tiles.translate properties which together with an
19+
* individual tile’s x and y determine the intended location of the tile
20+
* in the viewport.
21+
*/
22+
(): Tiles;
23+
(t: ZoomTransform): Tiles;
24+
25+
/**
26+
* If size is specified, sets this tile layout’s viewport size to the
27+
* specified array of numbers [width, height] and returns this tile
28+
* layout. If size is not specified, returns the current viewport size,
29+
* which defaults to [960, 500]. This is a convenience method for setting
30+
* the viewport extent to [[0, 0], [width, height]].
31+
*/
32+
size(): [number, number];
33+
size(size: [number, number]): this;
34+
35+
/**
36+
* If scale is specified, sets this tile layout’s scale function and
37+
* returns this tile layout. If scale is a function, it is invoked when
38+
* the tile layout is invoked, being passed the same arguments as the tile
39+
* layout; this function must return a number indicating the desired width
40+
* and height of the world tile [0, 0, 0]. If scale is not a function, it
41+
* assumed to be a constant number, and is wrapped in a function which
42+
* returns the specified number. If scale is not specified, returns the
43+
* current layout scale function.
44+
*/
45+
scale(): (...args: any[]) => number;
46+
scale(scale: number | ((...args: any[]) => number)): this;
47+
48+
/**
49+
* If translate is specified, sets this tile layout’s translate function
50+
* and returns this tile layout. If translate is a function, it is invoked
51+
* when the tile layout is invoked, being passed the same arguments as the
52+
* tile layout; this function must return an array of numbers [x, y]
53+
* indicating the desired coordinates the center of the world tile [0, 0,
54+
* 0]. If translate is not a function, it is assumed to be a constant
55+
* array [x, y] and is wrapped in a function which returns the specified
56+
* array. If translate is not specified, returns the current layout
57+
* translate function.
58+
*/
59+
translate(): (...args: any[]) => [number, number];
60+
translate(
61+
translate: [
62+
number,
63+
number,
64+
] | ((...args: any[]) => [number, number]),
65+
): this;
66+
67+
/**
68+
* If zoomDelta is specified, sets this tile layout’s zoom offset to the
69+
* specified number zoomDelta and returns this tile layout. If zoomDelta
70+
* is not specified, returns the current zoom offset, which defaults to 0.
71+
* The zoom offset affects which z-coordinate is chosen based on the
72+
* current scale; the default zoom offset of 0 which choose the z that is
73+
* closest the displayed size; a zoom offset of -1 will use z - 1, giving
74+
* tiles that are twice as big (lower resolution); a zoom offset of +1
75+
* will use z + 1, giving tiles that are twice as small (higher
76+
* resolution). The latter might be appropriate for showing 256×256 tiles
77+
* in a 128×128 space on a high-resolution screen.
78+
*/
79+
zoomDelta(): number;
80+
zoomDelta(zoomDelta: number): this;
81+
82+
/**
83+
* If clamp is specified, sets tile.clampX and tile.clampY to the
84+
* specified boolean clamp and returns this tile layout. If clamp is not
85+
* specified, returns true if tile.clampX and tile.clampY are both true,
86+
* and false otherwise.
87+
*/
88+
clamp(): boolean;
89+
clamp(clamp: boolean): this;
90+
91+
/**
92+
* If clamp is specified, sets whether or not the visible tiles will be
93+
* clamped in the x-coordinate and returns this tile layout. If clamp is
94+
* not specified, returns the current x-clamp, which defaults to true. If
95+
* the x-clamp is false, then the tile layout will return tiles that are
96+
* outside the “world” tile [0, 0, 0], with x-coordinates that are outside
97+
* the normal bounds 0 ≤ x < 2^z. See d3.tileWrap for converting these
98+
* coordinates to wrapped in-world coordinates.
99+
*/
100+
clampX(): boolean;
101+
clampX(clamp: boolean): this;
102+
103+
/**
104+
* If clamp is specified, sets whether or not the visible tiles will be
105+
* clamped in the y-coordinate and returns this tile layout. If clamp is
106+
* not specified, returns the current y-clamp, which defaults to true. If
107+
* the y-clamp is false, then the tile layout will return tiles that are
108+
* outside the “world” tile [0, 0, 0], with y-coordinates that are outside
109+
* the normal bounds 0 ≤ y < 2^z. See d3.tileWrap for converting these
110+
* coordinates to wrapped in-world coordinates. See also tile.clampX.
111+
*/
112+
clampY(): boolean;
113+
clampY(clamp: boolean): this;
114+
115+
/**
116+
* If tileSize is specified, sets this tile layout’s tile width and height
117+
* to the specified number tileSize and returns this tile layout. If
118+
* tileSize is not specified, returns the current layout tile size, which
119+
* defaults to 256. 256×256 is the most common tile size among tile
120+
* providers.
121+
*/
122+
tileSize(): number;
123+
tileSize(tileSize: number): this;
124+
125+
/**
126+
* If extent is specified, sets this tile layout’s viewport extent to the
127+
* specified array [[x0, y0], [x1, y1]], where [x0, y0] is the top-left
128+
* corner and [x1, y1] is the bottom-right corner, and returns this tile
129+
* layout. If extent is not specified, returns the current viewport
130+
* extent, which defaults to [[0, 0], [960, 500]]. Setting the viewport
131+
* extent implicitly sets the viewport size.
132+
*/
133+
extent(): [[number, number], [number, number]];
134+
extent(extent: [[number, number], [number, number]]): this;
135+
}
136+
137+
/**
138+
* Constructs a new tile layout with the default settings.
139+
*/
140+
function tile(): TileLayout;
141+
142+
/**
143+
* Given tile coordinates [x, y, z], where x and y may be outside the
144+
* "world" tile [0, 0, 0], returns the wrapped tile coordinates [x′, y′, z]
145+
* where j = 2 ^ z, x′ = x - ⌊x / j⌋ * j and y′ = y - ⌊y / j⌋ * j. This
146+
* function is most commonly used in conjunction with tile.clampX to allow
147+
* horizontal wrapping of web Mercator tiles.
148+
*/
149+
function tileWrap(tile: [number, number, number]): [number, number, number];
150+
}

types/d3-tile/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"private": true,
3+
"name": "@types/d3-tile",
4+
"version": "1.0.9999",
5+
"projects": [
6+
"https://github.com/d3/d3-tile"
7+
],
8+
"dependencies": {
9+
"@types/d3": "*"
10+
},
11+
"devDependencies": {
12+
"@types/d3-tile": "workspace:."
13+
},
14+
"owners": [
15+
{
16+
"name": "Mouad Naciri",
17+
"githubUsername": "NaciriMouad"
18+
}
19+
]
20+
}

types/d3-tile/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6",
6+
"dom"
7+
],
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictNullChecks": true,
11+
"strictFunctionTypes": true,
12+
"types": [],
13+
"noEmit": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"esModuleInterop": true
16+
},
17+
"files": [
18+
"index.d.ts",
19+
"d3-tile-tests.ts"
20+
]
21+
}

0 commit comments

Comments
 (0)