https://github.com/unjs/knitwork
🧶 Utilities to generate safe JavaScript code.
https://github.com/unjs/knitwork
code-generation javascript typescript
Last synced: 15 days ago
JSON representation
🧶 Utilities to generate safe JavaScript code.
- Host: GitHub
- URL: https://github.com/unjs/knitwork
- Owner: unjs
- License: mit
- Created: 2022-01-21T20:46:14.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-13T17:08:11.000Z (about 1 year ago)
- Last Synced: 2024-04-13T20:45:37.702Z (about 1 year ago)
- Topics: code-generation, javascript, typescript
- Language: TypeScript
- Homepage:
- Size: 419 KB
- Stars: 245
- Watchers: 7
- Forks: 10
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 🧶 knitwork
[](https://npmjs.com/package/knitwork)
[](https://npm.chart.dev/knitwork)
[](https://codecov.io/gh/unjs/knitwork)Utilities to generate JavaScript code.
## Install
```sh
# ✨ Auto-detect
npx nypm install knitwork# npm
npm install knitwork# yarn
yarn add knitwork# pnpm
pnpm install knitwork# bun
bun install knitwork# deno
deno install knitwork
```**ESM** (Node.js, Bun, Deno)
```js
import {} from "knitwork";
```**CommonJS** (Legacy Node.js)
```js
const {} = require("knitwork");
```**CDN** (Deno, Bun and Browsers)
```js
import {} from "https://esm.sh/knitwork";
```## ESM
### `genDynamicImport(specifier, options)`
Generate an ESM dynamic `import()` statement.
### `genExport(specifier, exports?, options)`
Generate an ESM `export` statement.
### `genImport(specifier, imports?, options)`
Generate an ESM `import` statement.
**Example:**
```js
genImport("pkg", "foo");
// ~> `import foo from "pkg";`genImport("pkg", ["foo"]);
// ~> `import { foo } from "pkg";`genImport("pkg", ["a", "b"]);
// ~> `import { a, b } from "pkg`;genImport("pkg", [{ name: "default", as: "bar" }]);
// ~> `import { default as bar } from "pkg`;genImport("pkg", [{ name: "foo", as: "bar" }]);
// ~> `import { foo as bar } from "pkg`;genImport("pkg", "foo", { attributes: { type: "json" } });
// ~> `import foo from "pkg" with { type: "json" };genExport("pkg", "foo");
// ~> `export foo from "pkg";`genExport("pkg", ["a", "b"]);
// ~> `export { a, b } from "pkg";`// export * as bar from "pkg"
genExport("pkg", { name: "*", as: "bar" });
// ~> `export * as bar from "pkg";`
```### `genTypeImport(specifier, imports, options)`
Generate an ESM `import type` statement.
## Serialization
### `genArrayFromRaw(array, indent, options)`
Serialize an array to a string.
Values are not escaped or quoted.
**Example:**
```js
genArrayFromRaw([1, 2, 3])
// ~> `[1, 2, 3]`
```### `genObjectFromRaw(object, indent, options)`
Serialize an object to a string.
Values are not escaped or quoted.
**Example:**
```js
genObjectFromRaw({ foo: "bar", test: '() => import("pkg")' })
// ~> `{ foo: bar, test: () => import("pkg") }`
```### `genObjectFromRawEntries(array, indent, options)`
Serialize an array of key-value pairs to a string.
Values are not escaped or quoted.
### `genObjectFromValues(obj, indent, options)`
Serialize an object to a string.
Values are escaped and quoted if necessary.
**Example:**
```js
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, indent)`
Generate typescript interface.
### `genTypeExport(specifier, imports, options)`
Generate a typescript `export type` statement.
### `genTypeObject(object, indent)`
Generate typescript object type.
## Utils
### `genObjectKey(key)`
Generate a safe javascript variable name for an object key.
### `wrapInDelimiters(lines, indent, delimiters, withComma)`
Wrap an array of strings in delimiters.
## Contribution
Local development
- Clone this repository
- Install the latest LTS version of [Node.js](https://nodejs.org/en/)
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
- Install dependencies using `bun install`
- Run tests using `bun dev`## License
Published under the [MIT](https://github.com/unjs/knitwork/blob/main/LICENSE) license.
Made by [@pi0](https://github.com/pi0), [@danielroe](https://github.com/danielroe) and [community](https://github.com/unjs/knitwork/graphs/contributors) 💛
![]()
---
_🤖 auto updated with [automd](https://automd.unjs.io)_