diff --git a/src/index.ts b/src/index.ts index 1fc94be..c1b22eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,17 @@ -import { parseAndNormalizeJson, Property, Import, Export, isProperty, isExport, XtpSchema } from "./normalizer"; +import { + Export, + Import, + isExport, + isProperty, + parseAndNormalizeJson, + Property, + XtpSchema, +} from "./normalizer"; import { CodeSample } from "./parser"; -export * from "./normalizer" +export * from "./normalizer"; export function parse(schema: string) { - return parseAndNormalizeJson(schema) + return parseAndNormalizeJson(schema); } export interface XtpProject { @@ -16,48 +24,57 @@ export interface XtpProject { export interface XtpContext { schema: XtpSchema; project: XtpProject; - featureFlags?: { [keyof: string]: any } + featureFlags?: { [keyof: string]: any }; } export function getContext(): XtpContext { - const ctx = JSON.parse(Config.get('ctx') || '{}') - ctx.schema = parse(JSON.stringify(ctx.schema)) - ctx.featureFlags = (ctx.featureFlags || []).reduce((a: any, c: any) => { a[c] = true; return a }, {}) - return ctx + const ctx = JSON.parse(Config.get("ctx") || "{}"); + ctx.schema = parse(JSON.stringify(ctx.schema)); + ctx.featureFlags = (ctx.featureFlags || []).reduce((a: any, c: any) => { + a[c] = true; + return a; + }, {}); + return ctx; } function codeSamples(ex: Export, lang: string): CodeSample[] { - return ex.codeSamples!.filter(s => s.lang.toLowerCase() === lang.toLowerCase())! + if (!ex.codeSamples) { + return []; + } + + return ex.codeSamples.filter((s) => + s.lang.toLowerCase() === lang.toLowerCase() + )!; } -// template helpers +// template helpers function hasComment(p: Property | Export | Import | null | undefined): boolean { - if (!p) return false + if (!p) return false; if (isProperty(p)) { - return !!(p.description || p.$ref) + return !!(p.description || p.$ref); } else if (isExport(p)) { // should cover import and export return !!( - p.description - || hasComment(p.input) - || hasComment(p.output) - ) + p.description || + hasComment(p.input) || + hasComment(p.output) + ); } - return false + return false; } // Formats comment to fit on a single line function formatCommentLine(s: string | null) { - if (!s) return "" - return s.trimEnd().replace(/\n/g, ' ') + if (!s) return ""; + return s.trimEnd().replace(/\n/g, " "); } // Formats comment to a block function formatCommentBlock(s: string | null, prefix?: string) { - if (!s) return "" - if (!prefix) prefix = ' * ' - return s.trimEnd().replace(/\n/g, `\n${prefix}`) + if (!s) return ""; + if (!prefix) prefix = " * "; + return s.trimEnd().replace(/\n/g, `\n${prefix}`); } export const helpers = { @@ -65,4 +82,4 @@ export const helpers = { formatCommentLine, formatCommentBlock, codeSamples, -} +};