Skip to content

Commit

Permalink
fix(Schema generation): Sort children and descendants for more determ…
Browse files Browse the repository at this point in the history
…inistic output
  • Loading branch information
nokome committed Jul 25, 2019
1 parent 8638638 commit d04a423
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,22 @@ const processSchema = (schemas: Map<string, Schema>, schema: Schema): void => {

// Add to parent's children
parent.children =
parent.children === undefined ? [title] : [...parent.children, title]
parent.children === undefined ? [title] : [...parent.children, title].sort()

// Add to all ancestors' descendants and type enum
let ancestor: Schema | null = parent
while (ancestor !== null) {
ancestor.descendants =
ancestor.descendants === undefined
? [title]
: [...ancestor.descendants, title]
: [...ancestor.descendants, title].sort()
if (
ancestor.title !== undefined &&
ancestor.properties !== undefined &&
ancestor.properties.type !== undefined &&
ancestor.properties.type.enum !== undefined
) {
ancestor.properties.type.enum = [
...ancestor.properties.type.enum,
title
]
ancestor.properties.type.enum = [ancestor.title, ...ancestor.descendants]
}
ancestor = parentSchema(schemas, ancestor)
}
Expand Down
6 changes: 2 additions & 4 deletions tests/__file_snapshots__/Thing.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/**
* The most generic type of item https://schema.org/Thing.
*/
export interface Thing {
type: 'Thing' | 'CreativeWork' | 'Article' | 'MediaObject' | 'AudioObject' | 'Brand' | 'Code' | 'CodeBlock' | 'SoftwareSourceCode' | 'CodeChunk' | 'CodeExpr' | 'Collection' | 'ContactPoint' | 'Datatable' | 'DatatableColumn' | 'DatatableColumnSchema' | 'Mark' | 'Delete' | 'Emphasis' | 'Environment' | 'Heading' | 'ImageObject' | 'Include' | 'Link' | 'List' | 'ListItem' | 'Mount' | 'Organization' | 'Paragraph' | 'Person' | 'Product' | 'Quote' | 'QuoteBlock' | 'ResourceParameters' | 'SoftwareApplication' | 'SoftwareSession' | 'Strong' | 'Subscript' | 'Superscript' | 'Table' | 'TableCell' | 'TableRow' | 'ThematicBreak' | 'VideoObject'
export interface Thing extends Entity {
type: 'Thing' | 'Article' | 'AudioObject' | 'Brand' | 'Code' | 'CodeBlock' | 'CodeChunk' | 'CodeExpr' | 'Collection' | 'ContactPoint' | 'CreativeWork' | 'Datatable' | 'Environment' | 'ImageObject' | 'MediaObject' | 'Mount' | 'Organization' | 'Person' | 'Product' | 'ResourceParameters' | 'SoftwareApplication' | 'SoftwareSession' | 'SoftwareSourceCode' | 'Table' | 'VideoObject'
alternateNames?: Array<string>
description?: string
id?: string
meta?: {[key: string]: any}
name?: string
url?: string
}
Expand Down

0 comments on commit d04a423

Please sign in to comment.