Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions types/plotly.js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,16 @@ export type Calendar =
| "thai"
| "ummalqura";

export type XAxisName = "x" | "x2" | "x3" | "x4" | "x5" | "x6" | "x7" | "x8" | "x9" | "x10" | "x11";
export type YAxisName = "y" | "y2" | "y3" | "y4" | "y5" | "y6" | "y7" | "y8" | "y9" | "y10" | "y11";
// regex from documentation: "/^x([2-9]|[1-9][0-9]+)?( domain)?$/" | "/^y([2-9]|[1-9][0-9]+)?( domain)?$/"
// regex allows for an unlimited amount of digits for the 'axis number', but the following typescript definition is limited to two digits
type xYAxisNames = `${
| ""
| `${2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`
| `${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`}${"" | " domain"}`;

export type XAxisName = `x${xYAxisNames}`;
export type YAxisName = `y${xYAxisNames}`;

export type AxisName = XAxisName | YAxisName;

export interface LayoutAxis extends Axis {
Expand Down
4 changes: 4 additions & 0 deletions types/plotly.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
{
"name": "John Abdou",
"githubUsername": "jpabdou"
},
{
"name": "Martin Borst",
"githubUsername": "mrtnbrst"
}
]
}
88 changes: 87 additions & 1 deletion types/plotly.js/test/index-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Plotly from "plotly.js";
import { Config, Datum, Layout, newPlot, PlotData, Template } from "plotly.js";
import { Config, Datum, Layout, newPlot, PlotData, Template, XAxisName, YAxisName } from "plotly.js";

const graphDiv = "#test";

Expand Down Expand Up @@ -1169,3 +1169,89 @@ function rand() {
data: [{ ids: ids }],
}]);
});

//////////////////////////////////////////////////////////////////////
// x and y axis names
(() => {
// x

const validXAxes: XAxisName[] = [
"x",
"x2",
"x10 domain",
"x99 domain",
"x9",
"x10",
"x99",
"x domain",
"x2 domain",
"x10 domain",
];

// @ts-expect-error
const invalidXAxes: XAxisName[] = ["xx", "X", "xy", "x100", "x00", "x -1"];

// taken from https://plotly.com/python/reference/layout/yaxis/
const regexXAxis = /^x([2-9]|[1-9][0-9]+)?( domain)?$/;

if (
!validXAxes.every(str => (
regexXAxis.test(str)
))
) {
throw new Error(
"Values accepted by ts definition as valid are not matching according to plotly documentation regex.",
);
}

if (
!invalidXAxes.every(str => (
!regexXAxis.test(str)
))
) {
throw new Error(
"Values not accepted by ts definition as valid are matching according to plotly documentation regex.",
);
}

// y

const validYAxes: YAxisName[] = [
"y",
"y2",
"y10 domain",
"y99 domain",
"y9",
"y10",
"y99",
"y domain",
"y2 domain",
"y10 domain",
];

// @ts-expect-error
const invalidYAxes: YAxisName[] = ["yy", "Y", "yx", "y100", "y00", "y -1"];

// taken from https://plotly.com/python/reference/layout/yaxis/
const regexYAxis = /^y([2-9]|[1-9][0-9]+)?( domain)?$/;

if (
!validYAxes.every(str => (
regexYAxis.test(str)
))
) {
throw new Error(
"Values accepted by ts definition as valid are not matching according to plotly documentation regex.",
);
}

if (
!invalidYAxes.every(str => (
!regexXAxis.test(str)
))
) {
throw new Error(
"Values not accepted by ts definition as valid are matching according to plotly documentation regex.",
);
}
})();