Skip to content

Commit

Permalink
Document number ranges (#641)
Browse files Browse the repository at this point in the history
* Document ranges

* Lint

* Use parentheses for infinite end of range
  • Loading branch information
louwers authored May 2, 2024
1 parent bc59a81 commit e7b5c2b
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions build/generate-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type JsonObject = {
expression?: { interpolated?: boolean; parameters?: string[]};
transition?: boolean;
values?: {[key: string]: { doc: string; 'sdk-support'?: JsonSdkSupport }} | number[];
minimum?: number;
maximum?: number;
}

/**
Expand Down Expand Up @@ -154,6 +156,12 @@ function typeToMakrdownLink(type: string): string {
}
}

function formatRange(minimum?: number, maximum?: number) {
const from = minimum === undefined ? '(-∞' : `[${minimum}`;
const to = maximum === undefined ? '∞)' : `${maximum}]`;
return `${from}, ${to}`;
}

/**
* Converts the property to markdown format - this is a property with example, sdk support, default and other details.
* @param key - the name of the json property
Expand All @@ -169,10 +177,17 @@ function convertPropertyToMarkdown(key: string, value: JsonObject, keyPrefix = '
}
const valueType = typeToMakrdownLink(value.type);
if (value.required) {
markdown += `Required${valueType}. `;
markdown += `Required${valueType}`;
} else {
markdown += `Optional${valueType}. `;
markdown += `Optional${valueType}`;
}

if (value.minimum !== undefined || value.maximum !== undefined) {
markdown += ` in range ${formatRange(value.minimum, value.maximum)}`;
}

markdown += '. ';

const isEnum = value.type === 'enum' && value.values && !Array.isArray(value.values);
if (isEnum) {
markdown += `Possible values: \`${Object.keys(value.values).join('`, `')}\`. `;
Expand All @@ -196,8 +211,8 @@ function convertPropertyToMarkdown(key: string, value: JsonObject, keyPrefix = '
if (value.transition) {
markdown += 'Transitionable. ';
}
// Remove extra space at the end
markdown = `${markdown.substring(0, markdown.length - 2)}*\n\n${value.doc}\n\n`;

markdown = `${markdown.trim()}*\n\n${value.doc}\n\n`;

if (isEnum) {
for (const [enumKey, enumValue] of Object.entries(value.values)) {
Expand Down

0 comments on commit e7b5c2b

Please sign in to comment.