-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add typescript codegen utils (#3)
* feat: add typescript codegen utils * docs: typo
- Loading branch information
Showing
7 changed files
with
196 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
import { genString } from './string' | ||
import { genObjectKey, wrapInDelimiters } from './utils' | ||
|
||
export type TypeObject = { | ||
[key: string]: string | TypeObject | ||
} | ||
export interface GenInterfaceOptions { | ||
extends?: string | string[] | ||
export?: boolean | ||
} | ||
|
||
export const genTypeObject = (obj: TypeObject, indent = '') => { | ||
const newIndent = indent + ' ' | ||
return wrapInDelimiters(Object.entries(obj).map(([key, value]) => { | ||
const [, k = key, optional = ''] = key.match(/^(.*[^?])(\?)?$/) /* c8 ignore next */ || [] | ||
if (typeof value === 'string') { | ||
return `${newIndent}${genObjectKey(k)}${optional}: ${value}` | ||
} | ||
return `${newIndent}${genObjectKey(k)}${optional}: ${genTypeObject(value, newIndent)}` | ||
}), indent, '{}', false) | ||
} | ||
|
||
export const genInterface = (name: string, contents?: TypeObject, options: GenInterfaceOptions = {}) => { | ||
const result = [ | ||
options.export && 'export', | ||
`interface ${name}`, | ||
options.extends && `extends ${Array.isArray(options.extends) ? options.extends.join(', ') : options.extends}`, | ||
contents ? genTypeObject(contents) : '{}' | ||
].filter(Boolean).join(' ') | ||
return result | ||
} | ||
|
||
export const genAugmentation = (specifier: string, interfaces?: Record<string, TypeObject | [TypeObject, Omit<GenInterfaceOptions, 'export'>]>) => { | ||
return `declare module ${genString(specifier)} ${wrapInDelimiters( | ||
Object.entries(interfaces || {}).map( | ||
([key, entry]) => ' ' + (Array.isArray(entry) ? genInterface(key, ...entry) : genInterface(key, entry)) | ||
) | ||
)}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { genString } from './string' | ||
|
||
export function wrapInDelimiters (lines: string[], indent = '', delimiters = '{}', withComma = true) { | ||
if (!lines.length) { | ||
return delimiters | ||
} | ||
const [start, end] = delimiters | ||
return `${start}\n` + lines.join(withComma ? ',\n' : '\n') + `\n${indent}${end}` | ||
} | ||
|
||
const VALID_IDENTIFIER_RE = /^[$_]?[\w\d]*$/ | ||
|
||
export function genObjectKey (key: string) { | ||
return key.match(VALID_IDENTIFIER_RE) ? key : genString(key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters