UNPKG

@stencila/schema

Version:

Extensions to schema.org to support semantic, composable, parameterize-able and executable documents

128 lines (127 loc) 5.14 kB
import { Article, Entity, InlineContent, ListItem, Node, Paragraph, TypeMap, TypeMapGeneric, Types } from '../types'; 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> | 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> | TypeMapGeneric<{ type: string; }>>>(typeMap: T) => (node?: Node | 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?: Node | 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 keyof Types>(type: K, node: Node | 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?: Node | 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 keyof Types>(type: K) => (node?: Node | 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?: Node | 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?: Node | 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?: Node | 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?: Node | 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?: Node | 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?: Node | 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?: Node | 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?: Node | 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?: Node | 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?: Node | undefined) => node is ListItem; export {};