Skip to content

Commit 8101f67

Browse files
committed
follow #1008 on pXX
1 parent 26d3ce0 commit 8101f67

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

src/api.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import type {DataArray, Datum, Data, NumberChannel, Series, TextChannel, Value, ValueArray} from "./data.js";
3-
import type {percentile} from "./options.js";
3+
import type {pXX} from "./options.js";
44
/**
55
* Plot API
66
* @link https://github.com/observablehq/plot/blob/main/README.md
@@ -278,7 +278,9 @@ export type Basis =
278278
| "min"
279279
| "max"
280280
| "median"
281-
| percentile
281+
| "p50"
282+
| "p95"
283+
| pXX
282284
| "sum"
283285
| "extent"
284286
| "deviation"
@@ -330,7 +332,9 @@ export type AggregationMethod =
330332
| "mean"
331333
| "median"
332334
| "mode"
333-
| percentile
335+
| "p25"
336+
| "p95"
337+
| pXX
334338
| "deviation"
335339
| "variance"
336340
| AggregationFunction
@@ -642,7 +646,9 @@ export type WindowOptions = {
642646
| "mean"
643647
| "median"
644648
| "mode"
645-
| percentile
649+
| "p25"
650+
| "p95"
651+
| pXX
646652
| "sum"
647653
| "deviation"
648654
| "variance"

src/options.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ export const constant = (x: Value) => () => x;
122122
/**
123123
* A perticentile reducer, specified by a string like “p25”
124124
*/
125-
export type percentile = `p${number}`;
126-
// `p${Digit}${Digit}`; // would be nicer but expands to p00 p01…p99
127-
// type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
125+
// eslint-disable-next-line @typescript-eslint/ban-types
126+
export type pXX = `p${Digit}${Digit}` & {};
127+
type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
128128

129129
// Converts a string like “p25” into a function that takes an index I and an
130130
// accessor function f, returning the corresponding percentile value.
131-
export function percentile(reduce: percentile) {
131+
export function percentile(reduce: pXX) {
132132
const p = +`${reduce}`.slice(1) / 100;
133133
return (I: Series, f: (i: index) => any) => quantile(I, p, f);
134134
}

src/transforms/group.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {AggregationMethod, Aggregate, BinExtent, MarkOptions, OutputOptions, Reducer} from "../api.js";
22
import type {DataArray, index, Series, Value, ValueArray} from "../data.js";
3-
import type {ValueAccessor} from "../options.js";
3+
import type {ValueAccessor, pXX} from "../options.js";
44

55
/* eslint-disable @typescript-eslint/no-non-null-assertion */
66

@@ -238,7 +238,7 @@ export function maybeGroup(I: Series, X: ValueArray | null | undefined): Array<[
238238
export function maybeReduce(reduce: AggregationMethod, value: ValueAccessor): Aggregate {
239239
if (reduce && typeof reduce.reduce === "function") return reduce as Aggregate;
240240
if (typeof reduce === "function") return reduceFunction(reduce);
241-
if (/^p\d{2}$/i.test(reduce as string)) return reduceAccessor(percentile(reduce as `p${number}`) as ArrayReducer);
241+
if (/^p\d{2}$/i.test(reduce as string)) return reduceAccessor(percentile(reduce as pXX) as ArrayReducer);
242242
switch (`${reduce}`.toLowerCase()) {
243243
case "first":
244244
return reduceFirst;

src/transforms/normalize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {Basis, MarkOptions} from "../api.js";
22
import type {Value, index, Series, ValueArray} from "../data.js";
3-
import type {percentile as pxx} from "../options.js";
3+
import type {pXX} from "../options.js";
44

55
import {extent, deviation, max, mean, median, min, sum} from "d3";
66
import {defined} from "../defined.js";
@@ -20,7 +20,7 @@ export function normalizeY(basis: Basis | MarkOptions | undefined, options?: Mar
2020
export function normalize(basis: Basis) {
2121
if (basis === undefined) return normalizeFirst;
2222
if (typeof basis === "function") return normalizeBasis((I: Series, S: ValueArray) => basis(take(S, I)));
23-
if (/^p\d{2}$/i.test(basis)) return normalizeAccessor(percentile(basis as pxx));
23+
if (/^p\d{2}$/i.test(basis)) return normalizeAccessor(percentile(basis as pXX));
2424
switch (`${basis}`.toLowerCase()) {
2525
case "deviation":
2626
return normalizeDeviation;

src/transforms/window.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {MarkOptions, WindowOptions} from "../api.js";
22
import type {Value, index, NumericArray, Series, ValueArray} from "../data.js";
3-
import type {percentile as pxx} from "../options.js";
3+
import type {pXX} from "../options.js";
44

55
import {deviation, max, min, median, mode, quantile, variance} from "d3";
66
import {defined} from "../defined.js";
@@ -57,7 +57,7 @@ function maybeShift(shift: string) {
5757

5858
function maybeReduce(reduce: WindowOptions["reduce"] = "mean") {
5959
if (typeof reduce === "string") {
60-
if (/^p\d{2}$/i.test(reduce)) return reduceNumbers(percentile(reduce as pxx));
60+
if (/^p\d{2}$/i.test(reduce)) return reduceNumbers(percentile(reduce as pXX));
6161
switch (reduce.toLowerCase()) {
6262
case "deviation":
6363
return reduceNumbers(deviation);
@@ -327,7 +327,7 @@ function reduceLast(k: number, s: number, strict: boolean | undefined) {
327327
}
328328

329329
// takes an array of values
330-
function percentile(reduce: pxx) {
330+
function percentile(reduce: pXX) {
331331
const p = +`${reduce}`.slice(1) / 100;
332332
return (X: NumericArray) => quantile(X, p);
333333
}

0 commit comments

Comments
 (0)