token-replacer

1.0.1 • Public • Published

token-replacer

A library to replace lines and blocks within text (or files) designated by token comments (e.g. // @template:car next-line). Useful for removing example code from a template.

Installation

Install using NPM:

$ npm i token-replacer

Usage

Replace tokens within a string

import { replaceTokens } from 'token-replacer

const text = `

// @template:car next-line
const car = {}

// @template:house block-start

const house = {
    sqFt: 1000,
    bedrooms: 3,
}

// @template:house block-end
`

const replacedText = replaceTokens(text, {
    car: true,
    house: false,
})

console.log(replacedText)

Since car is set to true, it will be preserved (the token comments will be removed), but house is false, so it will be removed. The resulting replacedText will be:

// @template:car next-line
const car = {}

Replace tokens in files (in-place) within a directory (recursive)

import { replaceTokensInFiles } from 'token-replacer

replaceTokensInFiles('./code', {
    car: true,
    house: false,
})

Token Types

Next Line

Replace the line directly after the next-line comment:

// @template:car next-line

Block

Replace the lines in between a block-start and block-end comment:

// @template:house block-start

const house = {
    sqFt: 1000,
    bedrooms: 3,
}

// @template:house block-end

Readme

Keywords

none

Package Sidebar

Install

npm i token-replacer

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

3.46 kB

Total Files

3

Last publish

Collaborators

  • ngagne