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

generate-code

v2.3.2

Published

The plugin exports a class that helps generate code and sourcemaps.

Downloads

72

Readme

generate-code

The plugin exports a class that helps generate code and sourcemaps.

Installation

npm install --save generate-code

Usage

const CodeGenerator = require('generate-code');

const code = new CodeGenerator(options);

code.add('var a, b;\n');
code.addWithMapping(
  'var c;',
  { line: 1, column: 10 },
  'c'
);

const generatedCode = code.toString();

// var a, b;
// var c;

const generatedMap = code.generateMap();

// {
//   version: 3,
//   ...
// }

API

CodeGenerator
new CodeGenerator(options: Options)
  • options.filename (required): filename for the sourcemap.
  • options.sourceContent (required): source content for the sourcemap.
  • options.sourceMap (default: true): if it is needed to generate a sourcemap.
  • options.inputSourceMap (default: null): input sourcemap.
CodeGenerator#add
add(chunk: string): this

Adds a chunk of code to the generated code.

Example:

code.add('fun(1, 2);');
CodeGenerator#addWithMap
addWithMap(
  chunk: string,
  map: SourceMap,
  position?: { line: number, column: number } | number
): this

Applies the map to the existing map (shifting it according to the current position in the generated code) and then adds the chunk of code.

Third optional parameter is the position of the mappings relatively to the source. Can be a number or a { line, column } object with 0-indexed line.

Example:

const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: `var a = 10;
var b = {
  c: 1,
  d: 2
};
var c = [];`
});

code.add(`var a = 10;
var b = `);

const {
  code: generated,
  map
} = addUnderscoreToProps(`{
  c: 1,
  d: 2
}`);

code.addWithMap(
  generated,
  map,
  { line: 1, column: 8 } // where '{' is located
);
code.add(`;
var c = [];`);

console.log(code.toString());

// var a = 10;
// var b = {
//   _c: 1,
//   _d: 2,
// };
CodeGenerator#addWithMapping
addWithMapping(
  chunk: string,
  position?: { line: number, column: number } | number,
  name?: string
): this

Applies the mapping to the existing map (shifting it according to the current position in the generated code) and then adds the chunk of code.

Second optional parameter is the position of the mapping relatively to the source. Can be a number or a { line, column } object with 0-indexed line.

Third optional argument describes the mapping name.

Example:

const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: `var a = 10;
var b = {
  c: 1,
  d: 2
};
var c = [];`
});

code.add(`var a = 10;
var b = {
  `);

code.addWithMapping(
  '_c',
  { line: 2, column: 2 }, // where 'c' is located
  'c'
);

code.add(`: 1,
  `);

code.addWithMapping(
  '_d',
  { line: 3, column: 2 }, // where 'd' is located
  'd'
);

code.add(`: 2
};
var c = [];`);

console.log(code.toString());

// var a = 10;
// var b = {
//   _c: 1,
//   _d: 2,
// };
CodeGenerator#getCurrentIndent()
getCurrentIndent(): string

Returns current code indent.

Example:

const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: 'abc'
});

code.add('\na');

code.toString();         // '\na'
code.getCurrentIndent(); // ''

code.add('\n  b');

code.toString();         // '\na\n  b'
code.getCurrentIndent(); // '  '

code.add('\n\tc');

code.toString();         // '\na\n  b\n\tc'
code.getCurrentIndent(); // '\t'
CodeGenerator#toString()
toString(): string

Returns the generated code.

CodeGenerator#generateMap()
generateMap(): SourceMap

Returns the generated sourcemap.