### Install knitwork with npm, yarn, or pnpm Source: https://unjs.io/packages/knitwork Install the knitwork package using npm, yarn, or pnpm. Choose the command that matches your package manager. ```bash # npm npm install knitwork # yarn yarn add knitwork # pnpm pnpm install knitwork ``` -------------------------------- ### Serialize JS objects and arrays with genObjectFromRaw, genObjectFromRawEntries, genArrayFromRaw Source: https://unjs.io/packages/knitwork Serialize JavaScript objects and arrays from raw string values using genObjectFromRaw, genObjectFromRawEntries, and genArrayFromRaw. Useful for generating code that includes dynamic expressions. ```javascript import { genObjectFromRaw, genObjectFromRawEntries, genArrayFromRaw } from 'knitwork' // { test: () => import("pkg") } console.log(genObjectFromRaw({ test: '() => import("pkg")' })) // { 0: [ test, () => import("pkg") ] } console.log(genObjectFromRaw([ ['test', '() => import("pkg")'] ])) const entries = Object.entries({ a: 1, b: null, c: '"c"', nest: { hello: '"world"', fn: () => 1 } }) // { a: 1, b: null, c: "c", nest: { hello: "world", fn: () => 1 } } console.log(genObjectFromRawEntries(entries)) // [ 1, 2, () => import("pkg") ] console.log(genArrayFromRaw(['1', '2', '() => import("pkg")'])) ``` -------------------------------- ### Generate ESM import/export statements with genImport and genExport Source: https://unjs.io/packages/knitwork Generate ESM import and export statements using genImport and genExport. Supports named imports, aliases, and import assertions. ```javascript import { genImport, genExport } from 'knitwork' // import foo from "pkg" console.log(genImport('pkg', 'foo')) // import { foo } from "pkg" console.log(genImport('pkg', ['foo'])) // import { a, b } from "pkg" console.log(genImport('pkg', ['a', 'b'])) // import foo as bar from "pkg"; console.log(genImport('pkg', { name: 'foo', as: 'bar' })) // import { foo as bar } from "pkg"; console.log(genImport('pkg', [{ name: 'foo', as: 'bar' }])) // import foo from "pkg" assert { type: "json" }; console.log(genImport('pkg', 'foo', { assert: { type: 'json' } })) // export foo from "pkg" console.log(genExport('pkg', 'foo')) // export { a, b } from "pkg" console.log(genExport('pkg', ['a', 'b'])) // export * as bar from "pkg" console.log(genExport('pkg', { name: '*', as: 'bar' })) // export foo from "pkg" assert { type: "json" }; console.log(genExport('pkg', 'foo', { assert: { type: 'json' } })) ``` -------------------------------- ### Generate TypeScript types with genInterface, genAugmentation, genInlineTypeImport, genTypeImport, genTypeExport Source: https://unjs.io/packages/knitwork Generate TypeScript type declarations: interfaces, module augmentations, inline type imports, type imports, and type exports. ```javascript import { genInterface, genAugmentation, genInlineTypeImport, genTypeImport, genTypeExport } from 'knitwork' // interface FooInterface extends A, B { // name: boolean // optional?: string // } console.log(genInterface('FooInterface', { name: 'boolean', 'optional?': 'string' }, { extends: ['A', 'B'] })) // declare module "my-module" { // interface MyInterface {} // } console.log(genAugmentation('my-module', { MyInterface: {} })) // typeof import("my-module").genString' console.log(genInlineTypeImport('my-module', 'genString')) // typeof import("my-module").default' console.log(genInlineTypeImport('my-module')) // import type { test as value } from "my-module"; console.log(genTypeImport('my-module', [{ name: 'test', as: 'value' }])) // export type { test } from "my-module"; console.log(genTypeExport('my-module', ['test'])) ``` -------------------------------- ### Generate safe variable names with genSafeVariableName Source: https://unjs.io/packages/knitwork Generate safe JavaScript variable names from arbitrary strings using genSafeVariableName. Prefixes with underscore and encodes invalid characters. ```javascript import { genSafeVariableName } from 'knitwork' // _123_32foo genSafeVariableName('123 foo') // _for genSafeVariableName('for') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.