Skip to content

Commit 0144a28

Browse files
ryan-williamsclaude
andcommitted
convert 12 medium lib modules to TypeScript
extend, d3-compat, trace_categories, keyed_container, queue, geometry2d, events (MiniEmitter → class), matrix, dom, nested_property, polygon, array. Added local interfaces: KeyedContainerObj, Point, PolygonTester, NestedPropertyResult, TextLocation, SVGPathLike, etc. Total: 48 of 53 lib files now TypeScript. Remaining 5 large files: coerce (574L), dates (506L), geo_location_utils (377L), svg_text_utils (983L), index (1,656L). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6e9dfbe commit 0144a28

File tree

12 files changed

+369
-310
lines changed

12 files changed

+369
-310
lines changed

src/lib/array.js renamed to src/lib/array.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ var isArray = Array.isArray;
66
var ab = ArrayBuffer;
77
var dv = DataView;
88

9-
function isTypedArray(a) {
9+
function isTypedArray(a: any): boolean {
1010
return ab.isView(a) && !(a instanceof dv);
1111
}
1212
export { isTypedArray };
1313

14-
function isArrayOrTypedArray(a) {
14+
function isArrayOrTypedArray(a: any): boolean {
1515
return isArray(a) || isTypedArray(a);
1616
}
1717
export { isArrayOrTypedArray };
@@ -24,12 +24,12 @@ export { isArrayOrTypedArray };
2424
* Looks only at the first element, if the dimensionality is
2525
* not consistent we won't figure that out here.
2626
*/
27-
function isArray1D(a) {
27+
function isArray1D(a: any): boolean {
2828
return !isArrayOrTypedArray(a[0]);
2929
}
3030
export { isArray1D };
3131

32-
export var ensureArray = function(out, n) {
32+
export var ensureArray = function(out: any, n: number): any[] {
3333
// TODO: typed array support here? This is only used in
3434
// traces/carpet/compute_control_points
3535
if(!isArray(out)) out = [];
@@ -41,7 +41,7 @@ export var ensureArray = function(out, n) {
4141
return out;
4242
};
4343

44-
var typedArrays = {
44+
var typedArrays: Record<string, any> = {
4545
u1c: typeof Uint8ClampedArray === 'undefined' ? undefined :
4646
Uint8ClampedArray, // not supported in numpy?
4747

@@ -89,25 +89,25 @@ typedArrays.int32 = typedArrays.i4;
8989
typedArrays.float32 = typedArrays.f4;
9090
typedArrays.float64 = typedArrays.f8;
9191

92-
function isArrayBuffer(a) {
92+
function isArrayBuffer(a: any): boolean {
9393
return a.constructor === ArrayBuffer;
9494
}
9595
export { isArrayBuffer };
9696

97-
export var decodeTypedArraySpec = function(vIn) {
98-
var out = [];
97+
export var decodeTypedArraySpec = function(vIn: any): any {
98+
var out: any = [];
9999
var v = coerceTypedArraySpec(vIn);
100100
var dtype = v.dtype;
101101

102102
var T = typedArrays[dtype];
103103
if(!T) throw new Error('Error in dtype: "' + dtype + '"');
104-
var BYTES_PER_ELEMENT = T.BYTES_PER_ELEMENT;
104+
var BYTES_PER_ELEMENT: number = T.BYTES_PER_ELEMENT;
105105

106106
var buffer = v.bdata;
107107
if(!isArrayBuffer(buffer)) {
108108
buffer = b64decode(buffer);
109109
}
110-
var shape = v.shape === undefined ?
110+
var shape: any[] = v.shape === undefined ?
111111
// detect 1-d length
112112
[buffer.byteLength / BYTES_PER_ELEMENT] :
113113
// convert number to string and split to array
@@ -116,7 +116,7 @@ export var decodeTypedArraySpec = function(vIn) {
116116
shape.reverse(); // i.e. to match numpy order
117117
var ndim = shape.length;
118118

119-
var nj, j;
119+
var nj: number, j: number;
120120
var ni = +shape[0];
121121

122122
var rowBytes = BYTES_PER_ELEMENT * ni;
@@ -154,7 +154,7 @@ export var decodeTypedArraySpec = function(vIn) {
154154
return out;
155155
};
156156

157-
export var isTypedArraySpec = function(v) {
157+
export var isTypedArraySpec = function(v: any): boolean {
158158
return (
159159
isPlainObject(v) &&
160160
v.hasOwnProperty('dtype') && (typeof v.dtype === 'string') &&
@@ -167,23 +167,23 @@ export var isTypedArraySpec = function(v) {
167167
);
168168
};
169169

170-
function coerceTypedArraySpec(v) {
170+
function coerceTypedArraySpec(v: any): { bdata: any; dtype: string; shape: any } {
171171
return {
172172
bdata: v.bdata,
173173
dtype: v.dtype,
174174
shape: v.shape
175175
};
176176
}
177177

178-
export var concat = function() {
179-
var args = [];
178+
export var concat = function(...arrays: any[]): any {
179+
var args: any[] = [];
180180
var allArray = true;
181181
var totalLen = 0;
182182

183-
var _constructor, arg0, i, argi, posi, leni, out, j;
183+
var _constructor: any, arg0: any, i: number, argi: any, posi: number, leni: number, out: any, j: number;
184184

185-
for(i = 0; i < arguments.length; i++) {
186-
argi = arguments[i];
185+
for(i = 0; i < arrays.length; i++) {
186+
argi = arrays[i];
187187
leni = argi.length;
188188
if(leni) {
189189
if(arg0) args.push(argi);
@@ -219,8 +219,8 @@ export var concat = function() {
219219
out.set(arg0);
220220
for(i = 0; i < args.length; i++) {
221221
argi = args[i];
222-
out.set(argi, posi);
223-
posi += argi.length;
222+
out.set(argi, posi!);
223+
posi! += argi.length;
224224
}
225225
return out;
226226
}
@@ -230,21 +230,21 @@ export var concat = function() {
230230
for(j = 0; j < arg0.length; j++) out[j] = arg0[j];
231231
for(i = 0; i < args.length; i++) {
232232
argi = args[i];
233-
for(j = 0; j < argi.length; j++) out[posi + j] = argi[j];
234-
posi += j;
233+
for(j = 0; j < argi.length; j++) out[posi! + j] = argi[j];
234+
posi! += j;
235235
}
236236
return out;
237237
};
238238

239-
export var maxRowLength = function(z) {
239+
export var maxRowLength = function(z: any): number {
240240
return _rowLength(z, Math.max, 0);
241241
};
242242

243-
export var minRowLength = function(z) {
243+
export var minRowLength = function(z: any): number {
244244
return _rowLength(z, Math.min, Infinity);
245245
};
246246

247-
function _rowLength(z, fn, len0) {
247+
function _rowLength(z: any, fn: (a: number, b: number) => number, len0: number): number {
248248
if(isArrayOrTypedArray(z)) {
249249
if(isArrayOrTypedArray(z[0])) {
250250
var len = len0;
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
import { selection } from 'd3-selection';
1515
import 'd3-transition';
1616

17-
var _origStyle = selection.prototype.style;
18-
var _origAttr = selection.prototype.attr;
19-
var _origEnter = selection.prototype.enter;
20-
var _origSelect = selection.prototype.select;
17+
var _origStyle = (selection.prototype as any).style;
18+
var _origAttr = (selection.prototype as any).attr;
19+
var _origEnter = (selection.prototype as any).enter;
20+
var _origSelect = (selection.prototype as any).select;
2121

2222
// Patch .select() to NOT propagate parent data to child (v3 behavior).
2323
// In d3 v4+, select() copies parent.__data__ to child.__data__, which breaks
2424
// plotly.js code that uses .select() just to find elements without affecting data.
25-
selection.prototype.select = function(selector) {
25+
(selection.prototype as any).select = function(this: any, selector: any): any {
2626
if(typeof selector === 'string') {
2727
// Save __data__ on all elements that might be affected
28-
var savedData = [];
28+
var savedData: Array<{ node: any; data: any }> = [];
2929
var groups = this._groups;
3030
for(var j = 0; j < groups.length; ++j) {
3131
for(var i = 0; i < groups[j].length; ++i) {
@@ -49,7 +49,7 @@ selection.prototype.select = function(selector) {
4949
};
5050

5151
// Patch .style() to accept object arg
52-
selection.prototype.style = function(nameOrObj, value, priority) {
52+
(selection.prototype as any).style = function(this: any, nameOrObj: any, value?: any, priority?: any): any {
5353
if(typeof nameOrObj === 'object' && nameOrObj !== null) {
5454
for(var key in nameOrObj) {
5555
_origStyle.call(this, key, nameOrObj[key]);
@@ -60,7 +60,7 @@ selection.prototype.style = function(nameOrObj, value, priority) {
6060
};
6161

6262
// Patch .attr() to accept object arg AND handle null-node getters
63-
selection.prototype.attr = function(nameOrObj, value) {
63+
(selection.prototype as any).attr = function(this: any, nameOrObj: any, value?: any): any {
6464
// Object form: .attr({key: val, ...})
6565
if(typeof nameOrObj === 'object' && nameOrObj !== null && !(nameOrObj instanceof String)) {
6666
for(var key in nameOrObj) {
@@ -79,13 +79,13 @@ selection.prototype.attr = function(nameOrObj, value) {
7979
// In d3 v3, after enter().append(), the appended nodes were automatically part of the
8080
// update selection. In v4+, enter and update are separate selections. This polyfill
8181
// wraps .append() and .insert() on enter selections to merge entered nodes back.
82-
selection.prototype.enter = function() {
82+
(selection.prototype as any).enter = function(this: any): any {
8383
var enterSel = _origEnter.call(this);
8484
var updateSel = this;
8585
var _origAppend = enterSel.append;
8686
var _origInsert = enterSel.insert;
8787

88-
enterSel.append = function() {
88+
enterSel.append = function(this: any): any {
8989
var result = _origAppend.apply(this, arguments);
9090
// Merge entered nodes into the update selection's _groups
9191
var enterGroups = result._groups;
@@ -100,7 +100,7 @@ selection.prototype.enter = function() {
100100
return result;
101101
};
102102

103-
enterSel.insert = function() {
103+
enterSel.insert = function(this: any): any {
104104
var result = _origInsert.apply(this, arguments);
105105
var enterGroups = result._groups;
106106
var updateGroups = updateSel._groups;

0 commit comments

Comments
 (0)