token-replacer
v1.0.1
Published
A library to replace lines and blocks within text (or files) designated by token comments. Useful for removing example code from a template.
Downloads
12
Readme
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