hey-api-builders
Version:
A custom plugin for @hey-api/openapi-ts that generates mock data builders with a lightweight custom runtime, Zod integration, or static mock generation.
58 lines • 1.72 kB
JavaScript
;
/**
* String utilities for code generation
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.toPascal = toPascal;
exports.safeTypeName = safeTypeName;
exports.normalizeTypeName = normalizeTypeName;
exports.isValidIdentifier = isValidIdentifier;
exports.safePropName = safePropName;
/**
* Converts a string to PascalCase
* @param str - Input string
* @returns PascalCase string
*/
function toPascal(str) {
return str
.replace(/([_-]+|\s+)([a-zA-Z0-9])/g, (_, __, c) => c.toUpperCase())
.replace(/^[a-z]/, (c) => c.toUpperCase());
}
/**
* Creates a safe TypeScript type name by removing invalid characters
* @param name - Input name
* @returns Safe type name
*/
function safeTypeName(name) {
return name.replace(/[^a-zA-Z0-9_]/g, '_');
}
/**
* Normalizes common type name prefixes (UI -> Ui, API -> Api, etc.)
* @param typeName - Input type name
* @returns Normalized type name
*/
function normalizeTypeName(typeName) {
return typeName
.replace(/^UI([A-Z])/g, 'Ui$1')
.replace(/^API([A-Z])/g, 'Api$1')
.replace(/^HTTP([A-Z])/g, 'Http$1')
.replace(/^URL([A-Z])/g, 'Url$1')
.replace(/^ID([A-Z])/g, 'Id$1');
}
/**
* Checks if a property name is a valid JavaScript identifier
* @param key - Property name to check
* @returns True if valid identifier
*/
function isValidIdentifier(key) {
return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key);
}
/**
* Makes a property name safe for code generation
* @param key - Property name
* @returns Quoted key if needed, otherwise raw key
*/
function safePropName(key) {
return isValidIdentifier(key) ? key : `"${key}"`;
}
//# sourceMappingURL=string-utils.js.map