Package detail

knitwork

unjs4mMIT1.2.0

Utilities to generate JavaScript code.

readme

🧶 knitwork

npm version npm downloads codecov

Utilities to generate JavaScript code.

Install

# ✨ 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)

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");
// ~> `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:

genArrayFromRaw([1, 2, 3])
// ~> `[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")' })
// ~> `{ 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" })
// ~> `{ 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

<summary>Local development</summary> - 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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v1.2.0

compare changes

🚀 Enhancements

  • Expose genObjectKey and wrapInDelimiters utils (227ec06)

📖 Documentation

  • Correct jsdocs for genObjectFromRaw (#97)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.1.0

compare changes

🚀 Enhancements

  • Add genObjectFromValues (#9)
  • esm: Support new import attributes syntax (#89)

🩹 Fixes

  • Stringify key identifier starting with number literal (#86)
  • genString: Respect singleQuotes option (#88)
  • typescript: Indent generated augmentation interfaces (#4)

💅 Refactors

  • object: Use namespace for options (ba67797)

📖 Documentation

  • Fix genImport example with default name (#87)
  • Add jsdocs (8a8a673)

🏡 Chore

❤️ Contributors

1.0.0 (2022-11-14)

0.1.3 (2022-11-11)

Features

  • import assertions for genImport and genDynamicImport (#34) (71c4ad2)

0.1.2 (2022-06-10)

Features

0.1.1 (2022-03-08)

Features

  • add typescript codegen utils (#3) (b74fbe2)

0.0.2 (2022-01-25)

Features

  • generate objects and arrays from raw source (beb4297)

0.0.1 (2022-01-21)

Features

Bug Fixes