UNPKG

@stencila/schema

Version:

Stencila schema and other specifications

159 lines (158 loc) 8.93 kB
import { Article, Entity, InlineContent, ListItem, Paragraph, Types } from '../types'; import { TypeMap, TypeMapGeneric } from './type-map'; declare type ExtractGeneric<Type> = Type extends TypeMap<infer X> ? X : Type extends TypeMapGeneric<infer Y> ? Y : never; /** * Returns a function which returns true is the type is a member * of the type map. * * @param {TypeMap} typeMap An object containing schema type values */ export declare const typeIs: <T extends Partial<TypeMap<Entity>> | Partial<TypeMapGeneric<{ type: string; }>>>(typeMap: T) => (type: string) => boolean; /** * Returns a type guard to determine whether a node belongs to a set * of types. * Returns a boolean value and narrows the TypeScript inferred type to * the type. * * @template {TypeMap} T * @param {T} typeMap * @param {Node} node A Stencila schema node object */ export declare const nodeIs: <T extends Partial<TypeMap<Entity>> | Partial<TypeMapGeneric<{ type: string; }>>>(typeMap: T) => (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is ExtractGeneric<T>; /** * Returns a type guard to determine whether a node is of a particular type. * Returns a boolean value and narrows the TypeScript inferred type to * the type. * * @param type The type to test for */ export declare const is: <Ts extends Entity>(type: Ts["type"]) => (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is ExtractGeneric<TypeMap<Ts>>; /** * A type guard to determine whether a node is of a specific type. * Returns a boolean value and narrows the TypeScript inferred type to * the type. * * e.g. `isA('Paragraph', node)` * * @param type The name of the type to test for * @param node The node being tested */ export declare const isA: <K extends "Entity" | "ArrayValidator" | "Article" | "AudioObject" | "BooleanValidator" | "Brand" | "Cite" | "CiteGroup" | "Code" | "CodeBlock" | "CodeChunk" | "CodeError" | "CodeExpression" | "CodeFragment" | "Collection" | "ConstantValidator" | "ContactPoint" | "CreativeWork" | "Datatable" | "DatatableColumn" | "Date" | "Delete" | "Emphasis" | "EnumValidator" | "Figure" | "Function" | "Grant" | "Heading" | "ImageObject" | "Include" | "IntegerValidator" | "Link" | "List" | "ListItem" | "Mark" | "Math" | "MathBlock" | "MathFragment" | "MediaObject" | "MonetaryGrant" | "NumberValidator" | "Organization" | "Paragraph" | "Parameter" | "Periodical" | "Person" | "Product" | "PropertyValue" | "PublicationIssue" | "PublicationVolume" | "Quote" | "QuoteBlock" | "SoftwareApplication" | "SoftwareEnvironment" | "SoftwareSession" | "SoftwareSourceCode" | "StringValidator" | "Strong" | "Subscript" | "Superscript" | "Table" | "TableCell" | "TableRow" | "ThematicBreak" | "Thing" | "TupleValidator" | "Variable" | "VideoObject" | "VolumeMount" | "BlockContent" | "CodeBlockTypes" | "CodeFragmentTypes" | "CodeTypes" | "CreativeWorkTypes" | "EntityTypes" | "GrantTypes" | "InlineContent" | "MarkTypes" | "MathTypes" | "MediaObjectTypes" | "Node" | "NumberValidatorTypes" | "ThingTypes" | "ValidatorTypes" | "VariableTypes">(type: K, node: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is Types[K]; export declare const isInstanceOf: <E = never, TM = TypeMap<E extends Entity ? E : never>>(typeMap: E extends never ? never : TM, node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is E; /** * Returns a type guard to determine whether a node is of a specific type. * Returns a boolean value and narrows the TypeScript inferred type to * the type. * * e.g. `article.content.filter(isType('Paragraph'))` * * @param type The type to test for */ export declare const isType: <K extends "Entity" | "ArrayValidator" | "Article" | "AudioObject" | "BooleanValidator" | "Brand" | "Cite" | "CiteGroup" | "Code" | "CodeBlock" | "CodeChunk" | "CodeError" | "CodeExpression" | "CodeFragment" | "Collection" | "ConstantValidator" | "ContactPoint" | "CreativeWork" | "Datatable" | "DatatableColumn" | "Date" | "Delete" | "Emphasis" | "EnumValidator" | "Figure" | "Function" | "Grant" | "Heading" | "ImageObject" | "Include" | "IntegerValidator" | "Link" | "List" | "ListItem" | "Mark" | "Math" | "MathBlock" | "MathFragment" | "MediaObject" | "MonetaryGrant" | "NumberValidator" | "Organization" | "Paragraph" | "Parameter" | "Periodical" | "Person" | "Product" | "PropertyValue" | "PublicationIssue" | "PublicationVolume" | "Quote" | "QuoteBlock" | "SoftwareApplication" | "SoftwareEnvironment" | "SoftwareSession" | "SoftwareSourceCode" | "StringValidator" | "Strong" | "Subscript" | "Superscript" | "Table" | "TableCell" | "TableRow" | "ThematicBreak" | "Thing" | "TupleValidator" | "Variable" | "VideoObject" | "VolumeMount" | "BlockContent" | "CodeBlockTypes" | "CodeFragmentTypes" | "CodeTypes" | "CreativeWorkTypes" | "EntityTypes" | "GrantTypes" | "InlineContent" | "MarkTypes" | "MathTypes" | "MediaObjectTypes" | "Node" | "NumberValidatorTypes" | "ThingTypes" | "ValidatorTypes" | "VariableTypes">(type: K) => (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is Types[K]; /** * Type guard to determine whether a node is a primitive type. * Returns a boolean value and narrows the TypeScript inferred type. * * @param {Node} node The node to get the type for * @returns {(node is null | boolean | number | string)} Returns true if node is one of `null`, `boolean`, `string`, or `number` */ export declare const isPrimitive: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is string | number | boolean | null; /** * Type guard to determine whether a node is an `Entity` * * @param {Node} node The node to get the type for * @returns {(node is Entity)} Returns true if node is an `Entity` or derived type */ export declare const isEntity: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is Entity; /** * Type guard to determine whether a node is both `InlineContent` and * and an `Entity`. * * @param {Node} node The node to get the type for * @returns {(node is InlineContent)} */ export declare const isInlineEntity: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is InlineContent; /** * Type guard to determine whether a node is `InlineContent`. * * @param {Node} node The node to get the type for * @returns {(node is InlineContent)} */ export declare const isInlineContent: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is InlineContent; /** * Type guard to determine whether a node is `BlockContent`. * * @param {Node} node The node to get the type for * @returns {(node is BlockContent)} */ export declare const isBlockContent: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is import("../types").BlockContent; /** * Type guard to determine whether a node is a `CreativeWork`. * * @param {Node} node The node to get the type for * @returns {(node is CreativeWork)} */ export declare const isCreativeWork: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is import("../types").CreativeWorkTypes; /** * Type guard to determine whether a node is a `Code`. * * @param {Node} node The node to get the type for * @returns {(node is Code)} */ export declare const isCode: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is import("../types").CodeTypes; /** * Type guard to determine whether a node is an `Article`. * * @param {Node} node The node to get the type for * @returns {(node is Article)} */ export declare const isArticle: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is Article; /** * Type guard to determine whether a node is an `Paragraph`. * * @param {Node} node The node to get the type for * @returns {(node is Paragraph)} */ export declare const isParagraph: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is Paragraph; /** * Type guard to determine whether a node is an `ListItem`. * * @param {Node} node The node to get the type for * @returns {(node is ListItem)} */ export declare const isListItem: (node?: string | number | boolean | Entity | any[] | { [key: string]: any; } | null | undefined) => node is ListItem; export {};