mtcute/packages/tl-utils/src/codegen/utils.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-08-29 14:33:11 +03:00
/**
* Transform snake_case string to camelCase string
* @param s Snake_case string
*/
export function snakeToCamel(s: string): string {
return s.replace(/(?<!^|_)_[a-z0-9]/gi, ($1) => {
return $1.substring(1).toUpperCase()
})
}
2022-08-29 14:33:11 +03:00
/**
* Transform camelCase string to PascalCase string
* @param s camelCase string
*/
2023-09-24 01:32:22 +03:00
export const camelToPascal = (s: string): string => s[0].toUpperCase() + s.substring(1)
2022-08-29 14:33:11 +03:00
/**
* Format a string as a JS documentation comment
* @param s Comment to format
*/
export function jsComment(s: string): string {
return (
`/**${
2022-08-12 17:13:24 +03:00
// awesome hack not to break up {@link} links and <a href
s
2022-08-12 17:22:40 +03:00
.replace(/<br\/?>/g, '\n\n')
.replace(/\{@link (.*?)\}/g, '{@linkƒ$1}')
2022-08-12 17:13:24 +03:00
.replace(/<a href/g, '<aƒhref')
.replace(/(?![^\n]{1,60}$)([^\n]{1,60})\s/g, '$1\n')
2022-08-12 17:00:02 +03:00
.replace(/\n|^/g, '\n * ')
.replace(/\{@linkƒ(.*)\}/g, '{@link $1}')
.replace(/<aƒhref/g, '<a href')
}\n */`
)
}
2022-08-29 14:33:11 +03:00
/**
* Indent the string with the given amount of spaces
*
* @param size Number of spaces to indent with
* @param s String to indent
*/
export function indent(size: number, s: string): string {
let prefix = ''
while (size--) prefix += ' '
return prefix + s.replace(/\n/g, `\n${prefix}`)
}