# @littlethings/coder

> A small and simple code generation tool.

Latest version **1.1.5** (published 2022-12-06) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @littlethings/coder
pnpm add @littlethings/coder
yarn add @littlethings/coder
bun add @littlethings/coder
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.5 |
| Published | 2022-12-06 |
| First published | 2022-01-24 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jake Hamilton |
| Maintainers | jakehamilton |
| Keywords | code, generator, littlethings, utility, library |

## Links

- npm: https://www.npmjs.com/package/@littlethings/coder
- Repository: https://github.com/jakehamilton/littlethings
- Issues: https://github.com/jakehamilton/littlethings/issues
- npm.io page: https://npm.io/package/@littlethings/coder

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 1.1.5 (latest) — 2022-12-06
- 1.1.4 — 2022-05-01
- 1.1.3 — 2022-05-01
- 1.1.2 — 2022-04-20
- 1.1.1 — 2022-04-10
- 1.1.0 — 2022-02-10
- 1.0.2 — 2022-01-27
- 1.0.1 — 2022-01-27
- 1.0.0 — 2022-01-24

## README

# LittleCoder

> A small and simple code generation tool.

## Installation

```shell
# With npm
npm install --save @littlethings/coder

# With yarn
yarn add @littlethings/coder
```

## Usage

LittleCoder is a tool that helps you generate code. It is
a simple abstraction around writing text, managing
indentation and blocks.

Here is an example of using LittleCoder.

```ts
import Coder from "@littlethings/coder";

// Create a new instance of Coder.
const coder = new Coder();

// Write a line of code.
coder.line(`console.log("first line of code");`);

// Write an empty line.
coder.line();

// Open a block (takes some text and appends " {").
coder.openBlock("const hello = (name) =>");

// Write some more text.
coder.line("console.log(`Hello, ${name}`);");

// Close the block (*optionally* takes some text and prepends "}").
coder.closeBlock(";");

// Then take the code from the `coder.code` property
console.log(coder.code);
```

<details>
<summary>See Output</summary>

```js
console.log("first line of code");

const hello = (name) => {
	console.log(`Hello, ${name}`);
};
```

</details>

### Constructor

The `Coder` constructor takes an options
argument with two things you can customize:

-   `indentChar`: The character to use for indentation.
    Defaults to tabs (`\t`).
-   `indentAmount`: The amount of `indentChar` characters
    to print for each indent. Defaults to `1`.

These options are both optional and can be specified
together, exclusively, or not at all (to use defaults).

```ts
// Uses defaults.
new Coder();

// Will use the default `indentChar`, but print
// two of them for each indentation.
new Coder({
	indentAmount: 2,
});

// Will use spaces for indentation, printing four
// of them for each indent.
new Coder({
	indentChar: " ",
	indentAmount: 4,
});
```

### `coder.code`

This property contains the code written.

```ts
const coder = new Coder();

coder.line(`console.log("Hello, World");`);

console.log(coder.code);
```

<details>
<summary>See Output</summary>

```js
console.log("Hello, World");
```

</details>

### `coder.line`

This method writes a single line of code, ending
with `\n`.

```ts
const coder = new Coder();

coder.line(`console.log("Hello, World");`);

console.log(coder.code);
```

<details>
<summary>See Output</summary>

```js
console.log("Hello, World");
```

</details>

### `coder.openBlock`

A helper method for writing a single line, ending
in ` {\n` (note the space before the curly brace).
This method also increases the indentation level by
one so all future text is written with an indent.

```ts
const coder = new Coder();

coder.openBlock("function myFunction()");

coder.closeBlock();

console.log(coder.code);
```

<details>
<summary>See Output</summary>

<!--
	Prettier likes to format this, but we want to
	show the real output of the above example.
-->
<!-- prettier-ignore -->
```js
function myFunction() {
}
```

</details>

### `coder.closeBlock`

The complement to `coder.openBlock`. This method
reduces the indentation level, then writes a closing
curly brace (`}`) followed by an optional suffix, then
`\n`.

```ts
const coder = new Coder();

coder.openBlock("const myFunction = () =>");

coder.closeBlock(";");

console.log(coder.code);
```

<details>
<summary>See Output</summary>

<!--
	Prettier likes to format this, but we want to
	show the real output of the above example.
-->
<!-- prettier-ignore -->
```js
const myFunction = () => {
};
```

</details>

### `coder.indent`

A utility method to increase the indentation level.
Typically this method is not called directly and
`coder.openBlock` is used instead.

```ts
const coder = new Coder();

coder.line("const x = {");
coder.indent();
coder.line(`key: "value",`);
coder.dedent();
coder.line("};");

console.log(coder.code);
```

<details>
<summary>See Output</summary>

```js
const x = {
	key: "value",
};
```

</details>

### `coder.dedent`

A utility method to decrease the indentation level.
Typically this method is not called directly and
`coder.closeBlock` is used instead.

```ts
const coder = new Coder();

coder.line("const x = {");
coder.indent();
coder.line(`key: "value",`);
coder.dedent();
coder.line("};");

console.log(coder.code);
```

<details>
<summary>See Output</summary>

```js
const x = {
	key: "value",
};
```

</details>

### `coder.reset`

If you would like to use the same `Coder` instance
multiple times to write different files, you can call
`coder.reset()` to wipe the current code and set the
indentation level back to zero.

```ts
const coder = new Coder();

coder.line(`console.log("a");`);

// Wipes all state, so any previous changes are lost!
coder.reset();

coder.line(`console.log("b");`);

console.log(coder.code);
```

<details>
<summary>See Output</summary>

```js
console.log("b");
```

</details>

---
_Source: https://npm.io/package/@littlethings/coder · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
