Skip to content

Commit

Permalink
chore: update jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 30, 2024
1 parent 8a8a673 commit 79d3ae0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
55 changes: 32 additions & 23 deletions README.md
Expand Up @@ -110,29 +110,7 @@ genExport("pkg", "foo", { attributes: { type: "json" } });

Generate an ESM `import type` statement.

## Typescript

### `genAugmentation(specifier)`

Generate typescript `declare module` augmentation.

### `genInlineTypeImport(specifier, name, options)`

Generate an typescript `typeof import()` statement for default import.

### `genInterface(name, contents?, options)`

Generate typescript interface.

### `genTypeExport(specifier, imports, options)`

Generate a typescript `export type` statement.

### `genTypeObject(object, indent)`

Generate typescript object type.

### `escapeString(id)`
## Serialization

### `genArrayFromRaw(array, indent, options)`

Expand All @@ -143,6 +121,7 @@ Serialize an array to a string.
```js
genArrayFromRaw([1, 2, 3])
// ~> `[1, 2, 3]`
```

### `genObjectFromRaw(object, indent, options)`

Expand Down Expand Up @@ -170,12 +149,42 @@ genObjectFromValues({ foo: "bar" })
// ~> `{ foo: "bar" }`
```

## String

### `escapeString(id)`

Escape a string for use in a javascript string.

### `genSafeVariableName(name)`

Generate a safe javascript variable name.

### `genString(input, options)`

Generate a string with double or single quotes and handle escapes.

## Typescript

### `genAugmentation(specifier)`

Generate typescript `declare module` augmentation.

### `genInlineTypeImport(specifier, name, options)`

Generate an typescript `typeof import()` statement for default import.

### `genInterface(name, contents?, options)`

Generate typescript interface.

### `genTypeExport(specifier, imports, options)`

Generate a typescript `export type` statement.

### `genTypeObject(object, indent)`

Generate typescript object type.

<!-- /automd -->

## Contribution
Expand Down
9 changes: 9 additions & 0 deletions src/object.ts
Expand Up @@ -14,6 +14,8 @@ export interface GenObjectOptions extends CodegenOptions {
* genObjectFromValues({ foo: "bar", test: '() => import("pkg")' })
* // ~> `{ foo: bar, test: () => import("pkg") }`
* ```
*
* @group serialization
*/
export function genObjectFromRaw(
object: Record<string, any>,
Expand All @@ -32,6 +34,8 @@ export function genObjectFromRaw(
* genObjectFromValues({ foo: "bar" })
* // ~> `{ foo: "bar" }`
* ```
*
* @group serialization
*/
export function genObjectFromValues(
obj: Record<string, any>,
Expand All @@ -49,6 +53,9 @@ export function genObjectFromValues(
* ```js
* genArrayFromRaw([1, 2, 3])
* // ~> `[1, 2, 3]`
* ```
*
* @group serialization
*/
export function genArrayFromRaw(
array: any[],
Expand All @@ -65,6 +72,8 @@ export function genArrayFromRaw(

/**
* Serialize an array of key-value pairs to a string.
*
* @group serialization
*/
export function genObjectFromRawEntries(
array: [key: string, value: any][],
Expand Down
13 changes: 13 additions & 0 deletions src/string.ts
@@ -1,5 +1,10 @@
import type { CodegenOptions } from "./types";

/**
* Generate a string with double or single quotes and handle escapes.
*
* @group string
*/
export function genString(input: string, options: CodegenOptions = {}) {
const str = JSON.stringify(input);
if (!options.singleQuotes) {
Expand All @@ -12,6 +17,12 @@ export function genString(input: string, options: CodegenOptions = {}) {
const NEEDS_ESCAPE_RE = /[\n\r'\\\u2028\u2029]/;
const QUOTE_NEWLINE_RE = /([\n\r'\u2028\u2029])/g;
const BACKSLASH_RE = /\\/g;

/**
* Escape a string for use in a javascript string.
*
* @group string
*/
export function escapeString(id: string): string {
if (!NEEDS_ESCAPE_RE.test(id)) {
return id;
Expand All @@ -21,6 +32,8 @@ export function escapeString(id: string): string {

/**
* Generate a safe javascript variable name.
*
* @group string
*/
export function genSafeVariableName(name: string) {
if (reservedNames.has(name)) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Expand Up @@ -2,6 +2,8 @@ import { genString } from "./string";

/**
* Wrap an array of strings in delimiters.
*
* @group utils
*/
export function wrapInDelimiters(
lines: string[],
Expand All @@ -22,6 +24,8 @@ const VALID_IDENTIFIER_RE = /^[$_]?([A-Z_a-z]\w*|\d)$/;

/**
* Generate a safe javascript variable name for an object key.
*
* @group utils
*/
export function genObjectKey(key: string) {
return VALID_IDENTIFIER_RE.test(key) ? key : genString(key);
Expand Down

0 comments on commit 79d3ae0

Please sign in to comment.