npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@littlethings/coder

v1.1.5

Published

A small and simple code generation tool.

Downloads

7

Readme

LittleCoder

A small and simple code generation tool.

Installation

# 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.

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);
console.log("first line of code");

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

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).

// 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.

const coder = new Coder();

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

console.log(coder.code);
console.log("Hello, World");

coder.line

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

const coder = new Coder();

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

console.log(coder.code);
console.log("Hello, World");

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.

const coder = new Coder();

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

coder.closeBlock();

console.log(coder.code);
function myFunction() {
}

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.

const coder = new Coder();

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

coder.closeBlock(";");

console.log(coder.code);
const myFunction = () => {
};

coder.indent

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

const coder = new Coder();

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

console.log(coder.code);
const x = {
	key: "value",
};

coder.dedent

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

const coder = new Coder();

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

console.log(coder.code);
const x = {
	key: "value",
};

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.

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);
console.log("b");