Utilities to generate JavaScript code.
npm install knitwork


Utilities to generate JavaScript code.
``shโจ Auto-detect
npx nypm install knitwork
ESM (Node.js, Bun, Deno)
`js
import {} from "knitwork";
`CommonJS (Legacy Node.js)
`js
const {} = require("knitwork");
`CDN (Deno and Browsers)
`js
import {} from "https://esm.sh/knitwork";
`ESM
$3
Generate an ESM dynamic
import() statement.$3
Generate an ESM type
import() statement.$3
Generate an ESM
export statement.$3
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";
``
Generate an ESM import type statement.
Serialize an array to a string.
Values are not escaped or quoted.
Example:
`js[1, 2, 3]
genArrayFromRaw([1, 2, 3])
// ~> `
Serialize an object to a string.
Values are not escaped or quoted.
Example:
`js{ foo: bar, test: () => import("pkg") }
genObjectFromRaw({ foo: "bar", test: '() => import("pkg")' })
// ~> `
Serialize an array of key-value pairs to a string.
Values are not escaped or quoted.
Serialize an object to a string.
Values are escaped and quoted if necessary.
Example:
`js{ foo: "bar" }
genObjectFromValues({ foo: "bar" })
// ~> `
Escape a string for use in a javascript string.
Generate a safe javascript variable name.
Generate a string with double or single quotes and handle escapes.
Generate typescript declare module augmentation.
Generate an typescript typeof import() statement for default import.
Generate typescript interface.
Generate a typescript export type statement.
Generate typescript object type.
Generate a safe javascript variable name for an object key.
Wrap an array of strings in delimiters.
Local development
- Clone this repository
- Install the latest LTS version of Node.js
- Enable Corepack using corepack enablebun install
- Install dependencies using bun dev`
- Run tests using
Published under the MIT license.
Made by @pi0, @danielroe and community ๐
---
_๐ค auto updated with automd_