Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
305 changes: 273 additions & 32 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"schema": "node tasks/schema.mjs",
"stats": "node tasks/stats.js",
"find-strings": "node tasks/find_locale_strings.js",
"preprocess": "node tasks/preprocess.js",
"preprocess": "ts-node tasks/preprocess.js",
"use-draftlogs": "node tasks/use_draftlogs.js",
"empty-draftlogs": "node tasks/empty_draftlogs.js",
"empty-dist": "node tasks/empty_dist.js",
Expand All @@ -40,12 +40,14 @@
"cibuild": "npm run empty-dist && npm run preprocess && node tasks/cibundle.mjs",
"lint": "npx @biomejs/biome lint",
"lint-fix": "npx @biomejs/biome format ./test/image/mocks --write; npx @biomejs/biome lint --write || true",
"typecheck": "tsc --noEmit",
"typecheck:watch": "tsc --noEmit --watch",
"pretest": "node tasks/pretest.js",
"test-jasmine": "karma start test/jasmine/karma.conf.js",
"test-mock": "node tasks/test_mock.mjs",
"test-image": "node test/image/compare_pixels_test.js",
"test-export": "node test/image/export_test.js",
"test-syntax": "node tasks/test_syntax.js && npm run find-strings -- --no-output",
"test-syntax": "ts-node tasks/test_syntax.js && npm run find-strings -- --no-output",
"test-bundle": "node tasks/test_bundle.js",
"test-plain-obj": "node tasks/test_plain_obj.mjs",
"test": "npm run test-jasmine -- --nowatch && npm run test-bundle && npm run test-image && npm run test-export && npm run test-syntax && npm run lint",
Expand Down Expand Up @@ -122,6 +124,8 @@
"@biomejs/biome": "2.2.0",
"@plotly/mathjax-v2": "npm:[email protected]",
"@plotly/mathjax-v3": "npm:mathjax@^3.2.2",
"@types/d3": "3.5.34",
"@types/node": "^24.10.0",
"amdefine": "^1.0.1",
"assert": "^2.1.0",
"browserify-transform-tools": "^1.7.0",
Expand Down Expand Up @@ -174,6 +178,8 @@
"through2": "^4.0.2",
"transform-loader": "^0.2.4",
"true-case-path": "^2.2.1",
"ts-node": "^10.9.2",
"typescript": "^5.9.3",
"virtual-webgl": "^1.0.6"
},
"overrides": {
Expand Down
2 changes: 1 addition & 1 deletion src/components/colorscale/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var colorbarAttrs = require('../colorbar/attributes');
var counterRegex = require('../../lib/regex').counter;
var sortObjectKeys = require('../../lib/sort_object_keys');
var sortObjectKeys = require('../../lib/sort_object_keys').default;

var palettes = require('./scales.js').scales;
var paletteStr = sortObjectKeys(palettes);
Expand Down
22 changes: 0 additions & 22 deletions src/lib/clean_number.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/lib/clean_number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

import isNumeric from 'fast-isnumeric';
import { BADNUM } from '../constants/numerical';

// precompile for speed
const JUNK = /^['"%,$#\s']+|[, ]|['"%,$#\s']+$/g;

/**
* cleanNumber: remove common leading and trailing cruft
* Always returns either a number or BADNUM.
*/
function cleanNumber(v: any): number | undefined {
if (typeof v === 'string') v = v.replace(JUNK, '');
if (isNumeric(v)) return Number(v);

return BADNUM;
}

export default cleanNumber;
4 changes: 2 additions & 2 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ lib.roundUp = searchModule.roundUp;
lib.sort = searchModule.sort;
lib.findIndexOfMin = searchModule.findIndexOfMin;

lib.sortObjectKeys = require('./sort_object_keys');
lib.sortObjectKeys = require('./sort_object_keys').default;

var statsModule = require('./stats');
lib.aggNums = statsModule.aggNums;
Expand Down Expand Up @@ -206,7 +206,7 @@ lib.pushUnique = require('./push_unique');

lib.increment = require('./increment');

lib.cleanNumber = require('./clean_number');
lib.cleanNumber = require('./clean_number').default;

lib.ensureNumber = function ensureNumber(v) {
if (!isNumeric(v)) return BADNUM;
Expand Down
15 changes: 4 additions & 11 deletions src/lib/mod.js → src/lib/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@
* sanitized modulus function that always returns in the range [0, d)
* rather than (-d, 0] if v is negative
*/
function mod(v, d) {
var out = v % d;
export function mod(v: number, d: number) {
const out = v % d;
return out < 0 ? out + d : out;
}

/**
* sanitized modulus function that always returns in the range [-d/2, d/2]
* rather than (-d, 0] if v is negative
*/
function modHalf(v, d) {
return Math.abs(v) > (d / 2) ?
v - Math.round(v / d) * d :
v;
export function modHalf(v: number, d: number) {
return Math.abs(v) > d / 2 ? v - Math.round(v / d) * d : v;
}

module.exports = {
mod: mod,
modHalf: modHalf
};
20 changes: 0 additions & 20 deletions src/lib/regex.js

This file was deleted.

21 changes: 21 additions & 0 deletions src/lib/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const NUMBER_REGEX = '([2-9]|[1-9][0-9]+)?';

/**
* make a regex for matching counter ids/names ie xaxis, xaxis2, xaxis10...
*
* @param head: the head of the pattern, eg 'x' matches 'x', 'x2', 'x10' etc.
* 'xy' is a special case for cartesian subplots: it matches 'x2y3' etc
* @param tail: a fixed piece after the id
* eg counterRegex('scene', '.annotations') for scene2.annotations etc.
* @param openEnded: if true, the string may continue past the match.
* @param matchBeginning: if false, the string may start before the match.
*/
export function counter(head: string, tail: string = '', openEnded: boolean, matchBeginning: boolean) {
const fullTail = tail + (openEnded ? '' : '$');
const startWithPrefix = matchBeginning === false ? '' : '^';
return head === 'xy'
? new RegExp(startWithPrefix + 'x' + NUMBER_REGEX + 'y' + NUMBER_REGEX + fullTail)
: new RegExp(startWithPrefix + head + NUMBER_REGEX + fullTail);
}
5 changes: 0 additions & 5 deletions src/lib/sort_object_keys.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/lib/sort_object_keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

function sortObjectKeys(obj: Record<string, any>) {
return Object.keys(obj).sort();
}

export default sortObjectKeys;
2 changes: 1 addition & 1 deletion src/plots/geo/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var domainAttrs = require('../domain').attributes;
var dash = require('../../components/drawing/attributes').dash;
var constants = require('./constants');
var overrideAll = require('../../plot_api/edit_types').overrideAll;
var sortObjectKeys = require('../../lib/sort_object_keys');
var sortObjectKeys = require('../../lib/sort_object_keys').default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camdecoster Is it worth considering moving away from default exports entirely as part of this transition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only matters when one mixes CJS and ESM. That said, I don't think that we can avoid that for a long time. I suppose we could make just pick a direction to go and standardize. I could go either way, but the esbuild docs make it clear that they don't like default exports.


var geoAxesAttrs = {
range: {
Expand Down
2 changes: 1 addition & 1 deletion src/plots/map/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var sortObjectKeys = require('../../lib/sort_object_keys');
var sortObjectKeys = require('../../lib/sort_object_keys').default;
var arcgisSatHybrid = require('./styles/arcgis-sat-hybrid'); // https://github.com/___raw___/go2garret/maps/v1.0.0/LICENSE
var arcgisSat = require('./styles/arcgis-sat');

Expand Down
2 changes: 1 addition & 1 deletion src/plots/mapbox/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var sortObjectKeys = require('../../lib/sort_object_keys');
var sortObjectKeys = require('../../lib/sort_object_keys').default;

var requiredVersion = '1.13.4';

Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatter3d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var DASHES = require('../../constants/gl3d_dashes');
var MARKER_SYMBOLS = require('../../constants/gl3d_markers');
var extendFlat = require('../../lib/extend').extendFlat;
var overrideAll = require('../../plot_api/edit_types').overrideAll;
var sortObjectKeys = require('../../lib/sort_object_keys');
var sortObjectKeys = require('../../lib/sort_object_keys').default;

var scatterLineAttrs = scatterAttrs.line;
var scatterMarkerAttrs = scatterAttrs.marker;
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scattergl/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var scatterAttrs = require('../scatter/attributes');
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
var colorScaleAttrs = require('../../components/colorscale/attributes');

var sortObjectKeys = require('../../lib/sort_object_keys');
var sortObjectKeys = require('../../lib/sort_object_keys').default;
var extendFlat = require('../../lib/extend').extendFlat;
var overrideAll = require('../../plot_api/edit_types').overrideAll;
var DASHES = require('./constants').DASHES;
Expand Down
63 changes: 63 additions & 0 deletions src/types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Types Directory

Centralized TypeScript type definitions for plotly.js.

## Quick Import

```typescript
// Import from main index (recommended)
import type { GraphDiv, Layout, PlotData } from '../types';

// Or use path alias
import type { GraphDiv } from '@types';
```

## Directory Structure

```
types/
├── index.d.ts # Main export - import from here
├── core/ # Core Plotly types (GraphDiv, Layout, Data, Config, Events)
├── traces/ # Trace-specific types
├── components/ # Component-specific types
├── plots/ # Plot-specific types
└── lib/ # Utility types
```

## Most Common Types

### GraphDiv
```typescript
import type { GraphDiv } from '../types';

function draw(gd: GraphDiv): void {
const layout = gd._fullLayout;
const data = gd._fullData;
}
```

### Layout
```typescript
import type { Layout } from '../types';

function updateLayout(layout: Partial<Layout>): void {
layout.title = 'New Title';
}
```

### PlotData (Traces)
```typescript
import type { PlotData, ScatterTrace } from '../types';

function processTrace(trace: PlotData): void {
if (trace.type === 'scatter') {
const scatter = trace as ScatterTrace;
}
}
```

## Adding New Types

1. Create file in appropriate subdirectory
2. Export from `index.d.ts`
3. Use in your TypeScript files
41 changes: 41 additions & 0 deletions src/types/components/common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Common component-related types
*
* Types shared across different component modules
*/

/**
* Component module interface
*/
export interface ComponentModule {
includeBasePlot?: (basePlotModule: string) => void;
layoutAttributes?: any;
name: string;
supplyLayoutDefaults?: (layoutIn: any, layoutOut: any, fullData: any) => void;
}

/**
* Drawing options (used by many components)
*/
export interface DrawingOptions {
duration?: number;
ease?: string;
redraw?: boolean;
}

/**
* SVG text utilities return type
*/
export interface TextBBox {
bottom: number;
height: number;
left: number;
right: number;
top: number;
width: number;
}

/**
* Color with alpha
*/
export type ColorString = string;
Loading