Skip to content

Commit

Permalink
Improved node docs (#1900)
Browse files Browse the repository at this point in the history
* Minor improvements

* Fixed example input data

* Code golfing

* console.log
  • Loading branch information
RunDevelopment committed Jun 29, 2023
1 parent 97ebf81 commit 4d37c0c
Show file tree
Hide file tree
Showing 19 changed files with 359 additions and 235 deletions.
2 changes: 1 addition & 1 deletion src/common/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export interface Output {
readonly label: string;
readonly kind: OutputKind;
readonly hasHandle: boolean;
readonly description?: string;
readonly description?: string | null;
readonly examples?: readonly string[] | null;
}

Expand Down
33 changes: 30 additions & 3 deletions src/common/types/pretty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Type } from '@chainner/navi';
/* eslint-disable no-continue */
import { Type, ValueType } from '@chainner/navi';
import { assertNever } from '../util';

export const prettyPrintType = (type: Type): string => {
Expand All @@ -21,10 +22,36 @@ export const prettyPrintType = (type: Type): string => {
if (type.min === 0 && type.max === Infinity) {
return 'uint';
}
if (type.min + 1 === type.max) {
return `${type.min} | ${type.max}`;
}
return type.toString();

case 'union':
return type.items.map(prettyPrintType).join(' | ');
case 'union': {
const literals: number[] = [];
const other: ValueType[] = [];
for (const item of type.items) {
if (item.underlying === 'number') {
if (item.type === 'literal' && Number.isFinite(item.value)) {
literals.push(item.value);
continue;
}
if (item.type === 'int-interval' && item.min + 1 === item.max) {
literals.push(item.min);
literals.push(item.max);
continue;
}
}
other.push(item);
}

const union = [...literals, ...other.map(prettyPrintType)].join(' | ');

// hacky way to detect boolean
if (union === 'false | true') return 'bool';

return union;
}

case 'struct':
if (type.fields.length === 0) return type.name;
Expand Down
5 changes: 4 additions & 1 deletion src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { constants } from 'fs';
import fs from 'fs/promises';
import { LocalStorage } from 'node-localstorage';
import { v4 as uuid4, v5 as uuid5 } from 'uuid';
import type { InputData, InputId, InputValue, NodeSchema, OutputId } from './common-types';
import type { Input, InputData, InputId, InputValue, NodeSchema, OutputId } from './common-types';

export const EMPTY_ARRAY: readonly never[] = [];
export const EMPTY_SET: ReadonlySet<never> = new Set<never>();
Expand Down Expand Up @@ -273,3 +273,6 @@ export const getInputValue = <T extends NonNullable<InputValue>>(
): T | undefined => {
return (inputData[inputId] ?? undefined) as T | undefined;
};

export const isAutoInput = (input: Input): boolean =>
input.kind === 'generic' && input.optional && !input.hasHandle;
Loading

0 comments on commit 4d37c0c

Please sign in to comment.