Skip to content

Commit 6e9dfbe

Browse files
ryan-williamsclaude
andcommitted
convert 23 small lib modules to TypeScript
All lib utility files under 100 lines converted to .ts with type annotations. Total: 36 of 53 lib files now TypeScript. Remaining 17 are larger modules (100-1,656 lines): extend, d3-compat, keyed_container, queue, geometry2d, events, matrix, dom, nested_property, polygon, array, geo_location_utils, dates, coerce, svg_text_utils, trace_categories, index. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 35948c9 commit 6e9dfbe

24 files changed

+103
-85
lines changed
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1-
export var isLeftAnchor = function isLeftAnchor(opts) {
1+
interface AnchorOpts {
2+
xanchor: string;
3+
yanchor: string;
4+
x: number;
5+
y: number;
6+
}
7+
8+
export var isLeftAnchor = function isLeftAnchor(opts: AnchorOpts): boolean {
29
return (
310
opts.xanchor === 'left' ||
411
(opts.xanchor === 'auto' && opts.x <= 1 / 3)
512
);
613
};
714

8-
export var isCenterAnchor = function isCenterAnchor(opts) {
15+
export var isCenterAnchor = function isCenterAnchor(opts: AnchorOpts): boolean {
916
return (
1017
opts.xanchor === 'center' ||
1118
(opts.xanchor === 'auto' && opts.x > 1 / 3 && opts.x < 2 / 3)
1219
);
1320
};
1421

15-
export var isRightAnchor = function isRightAnchor(opts) {
22+
export var isRightAnchor = function isRightAnchor(opts: AnchorOpts): boolean {
1623
return (
1724
opts.xanchor === 'right' ||
1825
(opts.xanchor === 'auto' && opts.x >= 2 / 3)
1926
);
2027
};
2128

22-
export var isTopAnchor = function isTopAnchor(opts) {
29+
export var isTopAnchor = function isTopAnchor(opts: AnchorOpts): boolean {
2330
return (
2431
opts.yanchor === 'top' ||
2532
(opts.yanchor === 'auto' && opts.y >= 2 / 3)
2633
);
2734
};
2835

29-
export var isMiddleAnchor = function isMiddleAnchor(opts) {
36+
export var isMiddleAnchor = function isMiddleAnchor(opts: AnchorOpts): boolean {
3037
return (
3138
opts.yanchor === 'middle' ||
3239
(opts.yanchor === 'auto' && opts.y > 1 / 3 && opts.y < 2 / 3)
3340
);
3441
};
3542

36-
export var isBottomAnchor = function isBottomAnchor(opts) {
43+
export var isBottomAnchor = function isBottomAnchor(opts: AnchorOpts): boolean {
3744
return (
3845
opts.yanchor === 'bottom' ||
3946
(opts.yanchor === 'auto' && opts.y <= 1 / 3)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { BADNUM } = _numerical;
55
// precompile for speed
66
var JUNK = /^['"%,$#\s']+|[, ]|['"%,$#\s']+$/g;
77

8-
export default function cleanNumber(v) {
8+
export default function cleanNumber(v: any): number {
99
if(typeof v === 'string') {
1010
v = v.replace(JUNK, '');
1111
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export default function clearGlCanvases(gd) {
1+
export default function clearGlCanvases(gd: any): void {
22
var fullLayout = gd._fullLayout;
33

44
if(fullLayout._glcanvas && fullLayout._glcanvas.size()) {
5-
fullLayout._glcanvas.each(function(d) {
5+
fullLayout._glcanvas.each(function(d: any) {
66
if(d.regl) d.regl.clear({color: true, depth: true});
77
});
88
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export default function filterUnique(array) {
2-
var seen = {};
3-
var out = [];
1+
export default function filterUnique(array: any[]): any[] {
2+
var seen: Record<string, number> = {};
3+
var out: any[] = [];
44
var j = 0;
55

66
for(var i = 0; i < array.length; i++) {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export default function filterVisible(container) {
1+
export default function filterVisible(container: any[]): any[] {
22
var filterFn = isCalcData(container) ? calcDataFilter : baseFilter;
3-
var out = [];
3+
var out: any[] = [];
44

55
for(var i = 0; i < container.length; i++) {
66
var item = container[i];
@@ -10,16 +10,16 @@ export default function filterVisible(container) {
1010
return out;
1111
}
1212

13-
function baseFilter(item) {
13+
function baseFilter(item: any): boolean {
1414
return item.visible === true;
1515
}
1616

17-
function calcDataFilter(item) {
17+
function calcDataFilter(item: any): boolean {
1818
var trace = item[0].trace;
1919
return trace.visible === true && trace._length !== 0;
2020
}
2121

22-
function isCalcData(cont) {
22+
function isCalcData(cont: any[]): boolean {
2323
return (
2424
Array.isArray(cont) &&
2525
Array.isArray(cont[0]) &&
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import _numerical from '../constants/numerical.js';
22
const { BADNUM } = _numerical;
33

4-
export var calcTraceToLineCoords = function(calcTrace) {
4+
export var calcTraceToLineCoords = function(calcTrace: any[]): number[][][] {
55
var trace = calcTrace[0].trace;
66
var connectgaps = trace.connectgaps;
77

8-
var coords = [];
9-
var lineString = [];
8+
var coords: number[][][] = [];
9+
var lineString: number[][] = [];
1010

1111
for(var i = 0; i < calcTrace.length; i++) {
1212
var calcPt = calcTrace[i];
@@ -27,7 +27,12 @@ export var calcTraceToLineCoords = function(calcTrace) {
2727
return coords;
2828
};
2929

30-
export var makeLine = function(coords) {
30+
interface GeoJSONGeometry {
31+
type: string;
32+
coordinates: any;
33+
}
34+
35+
export var makeLine = function(coords: number[][][]): GeoJSONGeometry {
3136
if(coords.length === 1) {
3237
return {
3338
type: 'LineString',
@@ -41,7 +46,7 @@ export var makeLine = function(coords) {
4146
}
4247
};
4348

44-
export var makePolygon = function(coords) {
49+
export var makePolygon = function(coords: number[][][]): GeoJSONGeometry {
4550
if(coords.length === 1) {
4651
return {
4752
type: 'Polygon',
@@ -61,7 +66,7 @@ export var makePolygon = function(coords) {
6166
}
6267
};
6368

64-
export var makeBlank = function() {
69+
export var makeBlank = function(): GeoJSONGeometry {
6570
return {
6671
type: 'Point',
6772
coordinates: []
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,34 @@ import { isArrayOrTypedArray } from './array.js';
88
var colorDfltRgba = rgba(colorDflt);
99
var opacityDflt = 1;
1010

11-
function calculateColor(colorIn, opacityIn) {
11+
function calculateColor(colorIn: number[], opacityIn: number): number[] {
1212
var colorOut = colorIn;
1313
colorOut[3] *= opacityIn;
1414
return colorOut;
1515
}
1616

17-
function validateColor(colorIn) {
17+
function validateColor(colorIn: any): number[] {
1818
if(isNumeric(colorIn)) return colorDfltRgba;
1919

2020
var colorOut = rgba(colorIn);
2121

2222
return colorOut.length ? colorOut : colorDfltRgba;
2323
}
2424

25-
function validateOpacity(opacityIn) {
25+
function validateOpacity(opacityIn: any): number {
2626
return isNumeric(opacityIn) ? opacityIn : opacityDflt;
2727
}
2828

29-
function formatColor(containerIn, opacityIn, len) {
29+
function formatColor(containerIn: any, opacityIn: any, len: number): number[] | number[][] {
3030
var colorIn = containerIn.color;
3131
if(colorIn && colorIn._inputArray) colorIn = colorIn._inputArray;
3232

3333
var isArrayColorIn = isArrayOrTypedArray(colorIn);
3434
var isArrayOpacityIn = isArrayOrTypedArray(opacityIn);
3535
var cOpts = Colorscale.extractOpts(containerIn);
36-
var colorOut = [];
36+
var colorOut: any[] = [];
3737

38-
var sclFunc, getColor, getOpacity, colori, opacityi;
38+
var sclFunc: any, getColor: any, getOpacity: any, colori: any, opacityi: any;
3939

4040
if(cOpts.colorscale !== undefined) {
4141
sclFunc = Colorscale.makeColorScaleFuncFromTrace(containerIn);
@@ -44,14 +44,14 @@ function formatColor(containerIn, opacityIn, len) {
4444
}
4545

4646
if(isArrayColorIn) {
47-
getColor = function(c, i) {
47+
getColor = function(c: any, i: number): number[] {
4848
// FIXME: there is double work, considering that sclFunc does the opposite
4949
return c[i] === undefined ? colorDfltRgba : rgba(sclFunc(c[i]));
5050
};
5151
} else getColor = validateColor;
5252

5353
if(isArrayOpacityIn) {
54-
getOpacity = function(o, i) {
54+
getOpacity = function(o: any, i: number): number {
5555
return o[i] === undefined ? opacityDflt : validateOpacity(o[i]);
5656
};
5757
} else getOpacity = validateOpacity;
@@ -67,13 +67,13 @@ function formatColor(containerIn, opacityIn, len) {
6767
return colorOut;
6868
}
6969

70-
function parseColorScale(cont) {
70+
function parseColorScale(cont: any): { index: number; rgb: number[] }[] {
7171
var cOpts = Colorscale.extractOpts(cont);
7272

7373
var colorscale = cOpts.colorscale;
7474
if(cOpts.reversescale) colorscale = Colorscale.flipScale(cOpts.colorscale);
7575

76-
return colorscale.map(function(elem) {
76+
return colorscale.map(function(elem: any[]) {
7777
var index = elem[0];
7878
var color = tinycolor(elem[1]);
7979
var rgb = color.toRgb();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import identity from './identity.js';
22

3-
function wrap(d) {return [d];}
3+
function wrap(d: any): any[] {return [d];}
44

55
export default {
66

@@ -11,7 +11,7 @@ export default {
1111
// of elements, e.g. points, lines, rows, requiring an array as input.
1212
// The role of the `keyFun` is to identify what elements are being entered/exited/updated,
1313
// otherwise D3 reverts to using a plain index which would screw up `transition`s.
14-
keyFun: function(d) {return d.key;},
14+
keyFun: function(d: any): string {return d.key;},
1515
repeat: wrap,
1616
descend: identity,
1717

@@ -20,5 +20,5 @@ export default {
2020
// newcomer to the codebase may not know what the `[0]` is, and whether there can be further
2121
// elements (not atm).
2222
wrap: wrap,
23-
unwrap: function(d) {return d[0];}
23+
unwrap: function(d: any[]): any {return d[0];}
2424
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function incrementNumeric(x, delta) {
1+
export default function incrementNumeric(x: number, delta: number): number {
22
if(!delta) return x;
33

44
// Note 1:
@@ -20,7 +20,7 @@ export default function incrementNumeric(x, delta) {
2020
var lenX0 = String(x).length;
2121

2222
if(lenX1 >= lenX0 + lenDt) { // likely a rounding error!
23-
var s = parseFloat(newX).toPrecision(12);
23+
var s = parseFloat(newX as any).toPrecision(12);
2424
if(s.indexOf('e+') === -1) newX = +s;
2525
}
2626
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Registry from '../registry.js';
22

3-
export default function localize(gd, s) {
3+
export default function localize(gd: any, s: string): string {
44
var locale = gd._context.locale;
55

66
/*

0 commit comments

Comments
 (0)