forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefined.ts
30 lines (23 loc) · 836 Bytes
/
defined.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type {Primitive} from "d3";
import {ascending, descending} from "d3";
export function defined(x: Primitive | undefined): boolean {
return x != null && !Number.isNaN(x);
}
export function ascendingDefined(a: Primitive | undefined, b: Primitive | undefined): number {
return +defined(b) - +defined(a) || ascending(a, b);
}
export function descendingDefined(a: Primitive | undefined, b: Primitive | undefined): number {
return +defined(b) - +defined(a) || descending(a, b);
}
export function nonempty(x: unknown): boolean {
return x != null && `${x}` !== "";
}
export function finite(x: number): number {
return isFinite(x) ? x : NaN;
}
export function positive(x: number): number {
return x > 0 && isFinite(x) ? x : NaN;
}
export function negative(x: number): number {
return x < 0 && isFinite(x) ? x : NaN;
}