Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ident case conversion funcs #6

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
Import,
isExport,
isProperty,
Parameter,
parseAndNormalizeJson,
Property,
Parameter,
XtpSchema,
} from "./normalizer";
import { CodeSample } from "./parser";
Expand Down Expand Up @@ -55,7 +55,9 @@ function codeSamples(ex: Export, lang: string): CodeSample[] {
}

// template helpers
function hasComment(p: Parameter | Property | Export | Import | null | undefined): boolean {
function hasComment(
p: Parameter | Property | Export | Import | null | undefined,
): boolean {
if (!p) return false;

if (isProperty(p)) {
Expand Down Expand Up @@ -85,25 +87,40 @@ function formatCommentBlock(s: string | null, prefix?: string) {
}

function isJsonEncoded(p: Parameter | null): boolean {
if (!p) return false
return p.contentType === 'application/json'
if (!p) return false;
return p.contentType === "application/json";
}

function isUtf8Encoded(p: Parameter | null): boolean {
if (!p) return false
return p.contentType === 'text/plain; charset=utf-8'
if (!p) return false;
return p.contentType === "text/plain; charset=utf-8";
}

function isPrimitive(p: Property | Parameter): boolean {
if (!p.$ref) return true
if (!p.$ref) return true;
// enums are currently primitive (strings)
// schemas with props are not (needs to be encoded)
return !!p.$ref.enum || !p.$ref.properties
return !!p.$ref.enum || !p.$ref.properties;
}

function isDateTime(p: Property | Parameter | null): boolean {
if (!p) return false
return p.type === 'string' && p.format === 'date-time'
if (!p) return false;
return p.type === "string" && p.format === "date-time";
}

function capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1);
}

function camelToSnakeCase(s: string) {
return s.split(/(?=[A-Z])/).join("_").toLowerCase();
}

function snakeToCamelCase(s: string) {
return s.split("_").map((part, index) => {
if (index === 0) return part;
return part.charAt(0).toUpperCase() + part.slice(1);
}).join("");
}

export const helpers = {
Expand All @@ -115,4 +132,7 @@ export const helpers = {
isPrimitive,
isJsonEncoded,
isUtf8Encoded,
capitalize,
camelToSnakeCase,
snakeToCamelCase,
};
Loading