Skip to content

Commit

Permalink
Merge pull request #1 from dylibso/forgive-me-for-i-have-omitted-code…
Browse files Browse the repository at this point in the history
…-samples

chore: take pity on thy code which hath not samples
  • Loading branch information
nilslice authored Jul 17, 2024
2 parents 70c20e0 + c57648d commit 4786e72
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,53 +24,62 @@ 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 = {
hasComment,
formatCommentLine,
formatCommentBlock,
codeSamples,
}
};

0 comments on commit 4786e72

Please sign in to comment.