🧶 knitwork
Utilities to generate JavaScript code.
Install
npx nypm install knitwork
npm install knitwork
yarn add knitwork
pnpm install knitwork
bun install knitwork
deno install knitwork
ESM (Node.js, Bun, Deno)
import {} from "knitwork";
CommonJS (Legacy Node.js)
const {} = require("knitwork");
CDN (Deno, Bun and Browsers)
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:
genImport("pkg", "foo");
genImport("pkg", ["foo"]);
genImport("pkg", ["a", "b"]);
genImport("pkg", [{ name: "default", as: "bar" }]);
genImport("pkg", [{ name: "foo", as: "bar" }]);
genImport("pkg", "foo", { attributes: { type: "json" } });
genExport("pkg", "foo");
genExport("pkg", ["a", "b"]);
genExport("pkg", { name: "*", as: "bar" });
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:
genArrayFromRaw([1, 2, 3])
genObjectFromRaw(object, indent, options)
Serialize an object to a string.
Values are not escaped or quoted.
Example:
genObjectFromRaw({ 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:
genObjectFromValues({ 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
- Enable Corepack using
corepack enable
- Install dependencies using
bun install
- Run tests using
bun dev
License
Published under the MIT license.
Made by @pi0, @danielroe and community 💛
🤖 auto updated with automd