Commit 4187069a authored by Loïc Correnson's avatar Loïc Correnson
Browse files

[dome] stronger types for primitives

parent d29ffa3a
......@@ -114,10 +114,10 @@ export const byMarkerInfoData: Compare.Order<markerInfoData> =
Compare.byFields
<{ key: Json.key<'#markerInfo'>, kind: markerKind, name: string,
descr: string }>({
key: Compare.primitive,
key: Compare.string,
kind: byMarkerKind,
name: Compare.alpha,
descr: Compare.primitive,
descr: Compare.string,
});
/** Signal for array [`markerInfo`](#markerinfo) */
......@@ -242,9 +242,9 @@ export const jFunctionsDataSafe: Json.Safe<functionsData> =
export const byFunctionsData: Compare.Order<functionsData> =
Compare.byFields
<{ key: Json.key<'#functions'>, name: string, signature: string }>({
key: Compare.primitive,
key: Compare.string,
name: Compare.alpha,
signature: Compare.primitive,
signature: Compare.string,
});
/** Signal for array [`functions`](#functions) */
......
......@@ -27,7 +27,7 @@ export const jMarkdownSafe: Json.Safe<markdown> =
Json.jFail(Json.jString,'String expected');
/** Natural order for `markdown` */
export const byMarkdown: Compare.Order<markdown> = Compare.primitive;
export const byMarkdown: Compare.Order<markdown> = Compare.string;
/** Rich text format uses `[tag; …text ]` to apply the tag `tag` to the enclosed text. Empty tag `""` can also used to simply group text together. */
export type text = null | string | text[];
......
......@@ -36,9 +36,9 @@ export const jProjectInfoSafe: Json.Safe<projectInfo> =
export const byProjectInfo: Compare.Order<projectInfo> =
Compare.byFields
<{ id: Json.key<'#project'>, name: string, current: boolean }>({
id: Compare.primitive,
id: Compare.string,
name: Compare.alpha,
current: Compare.primitive,
current: Compare.boolean,
});
/** Request to be executed on the specified project. */
......@@ -62,8 +62,8 @@ export const jProjectRequestSafe: Json.Safe<projectRequest> =
export const byProjectRequest: Compare.Order<projectRequest> =
Compare.byFields
<{ project: Json.key<'#project'>, request: string, data: Json.json }>({
project: Compare.primitive,
request: Compare.primitive,
project: Compare.string,
request: Compare.string,
data: Compare.structural,
});
......
......@@ -275,17 +275,17 @@ export const byStatusData: Compare.Order<statusData> =
names: string[], status: propStatus, function?: Json.key<'#fct'>,
kinstr?: Json.key<'#stmt'>, source: source, alarm?: string,
alarm_descr?: string, predicate?: string }>({
key: Compare.primitive,
descr: Compare.primitive,
key: Compare.string,
descr: Compare.string,
kind: byPropKind,
names: Compare.array(Compare.primitive),
names: Compare.array(Compare.string),
status: byPropStatus,
function: Compare.defined(Compare.primitive),
kinstr: Compare.defined(Compare.primitive),
function: Compare.defined(Compare.string),
kinstr: Compare.defined(Compare.string),
source: bySource,
alarm: Compare.defined(Compare.primitive),
alarm_descr: Compare.defined(Compare.primitive),
predicate: Compare.defined(Compare.primitive),
alarm: Compare.defined(Compare.string),
alarm_descr: Compare.defined(Compare.string),
predicate: Compare.defined(Compare.string),
});
/** Signal for array [`status`](#status) */
......
......@@ -74,10 +74,10 @@ export const jSourceSafe: Json.Safe<source> =
export const bySource: Compare.Order<source> =
Compare.byFields
<{ dir: string, base: string, file: string, line: number }>({
dir: Compare.primitive,
base: Compare.primitive,
file: Compare.primitive,
line: Compare.primitive,
dir: Compare.string,
base: Compare.string,
file: Compare.string,
line: Compare.number,
});
/** Log messages categories. */
......@@ -149,8 +149,8 @@ export const byLog: Compare.Order<log> =
source?: source }>({
kind: byLogkind,
plugin: Compare.alpha,
message: Compare.primitive,
category: Compare.defined(Compare.primitive),
message: Compare.string,
category: Compare.defined(Compare.string),
source: Compare.defined(bySource),
});
......
......@@ -219,8 +219,9 @@ let makeOrder ~self ~names fmt js =
let rec pp fmt = function
| Jnull -> Format.pp_print_string fmt "Compare.equal"
| Jalpha -> Format.pp_print_string fmt "Compare.alpha"
| Jnumber | Jstring | Jboolean | Jkey _ | Jindex _
-> Format.pp_print_string fmt "Compare.primitive"
| Jnumber | Jindex _ -> Format.pp_print_string fmt "Compare.number"
| Jstring | Jkey _ -> Format.pp_print_string fmt "Compare.string"
| Jboolean -> Format.pp_print_string fmt "Compare.boolean"
| Jself -> jcall names fmt (Pkg.Derived.order self)
| Jdata id -> jcall names fmt (Pkg.Derived.order id)
| Joption js ->
......
......@@ -41,28 +41,37 @@ export function isBigNum(x: any): x is bignum {
return typeof (x) === 'bigint' || (typeof (x) === 'number' && !Number.isNaN(x));
}
/**
Primitive comparison.
Can only compare arguments that have
comparable primitive type.
This includes symbols, boolean, non-NaN numbers, bigints and strings.
Numbers and big-ints can also be compared with each others.
*/
export function primitive(x: symbol, y: symbol): number;
export function primitive(x: boolean, y: boolean): number;
export function primitive(x: bignum, y: bignum): number;
export function primitive(x: string, y: string): number;
export function primitive(x: any, y: any) {
/** @internal */
function primitive(x: any, y: any) {
if (x < y) return -1;
if (x > y) return 1;
return 0;
}
/**
Primitive comparison for numbers (NaN included).
Primitive comparison for symbols.
*/
export const symbol: Order<symbol> = primitive;
/**
Primitive comparison for booleans.
*/
export const boolean: Order<boolean> = primitive;
/**
Primitive comparison for strings. See also [[alpha]].
*/
export const string: Order<string> = primitive;
/**
Primitive comparison for (big) integers (non NaN numbers included).
*/
export const bignum: Order<bignum> = primitive;
/**
Primitive comparison for number (NaN included).
*/
export function float(x: number, y: number) {
export function number(x: number, y: number) {
const nx = Number.isNaN(x);
const ny = Number.isNaN(y);
if (nx && ny) return 0;
......
......@@ -202,7 +202,7 @@ function ColumnTag<Row>(props: ColumnProps<Row, States.Tag>) {
// -------------------------------------------------------------------------
const bySource =
Compare.byFields<SourceLoc>({ file: Compare.alpha, line: Compare.primitive });
Compare.byFields<SourceLoc>({ file: Compare.alpha, line: Compare.number });
const byStatus =
Compare.byRank(
......@@ -227,7 +227,7 @@ const byProperty: Compare.ByFields<Property> = {
alarm: Compare.defined(Compare.alpha),
names: Compare.array(Compare.alpha),
predicate: Compare.defined(Compare.alpha),
key: Compare.primitive,
key: Compare.string,
kinstr: Compare.structural,
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment