An open API service indexing awesome lists of open source software.

https://github.com/thecreation/inject-string


https://github.com/thecreation/inject-string

nodejs string template

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

          

# inject-string [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]

> Inject a snippet of code or content into a string.

## Installation

Install with [npm](https://www.npmjs.com/)

```sh
$ npm install --save inject-string
```

## Usage

```js
var InjectString = require('inject-string');

var inject = new InjectString('before after');
inject.append('foo');
//=> 'before \nfoo\n after'
```

## Actions
###append

```js
var inject = new InjectString('before foo after');
inject.append('bar');
//=> 'before \nfoobar\n after'
```

### prepend

```js
var inject = new InjectString('before foo after');
inject.prepend('bar');
//=> 'before \nbarfoo\n after'
```

### replace

```js
var inject = new InjectString('before foo after');
inject.replace('bar');
//=> 'before \nbar\n after'
```

### strip

```js
var inject = new InjectString('before foo bar after');
inject.strip('custom');
//=> 'before foo bar after'
```

### stripAll

```js
var inject = new InjectString('before foo bar after');
inject.stripAll();
//=> 'before foo bar after'
```

## Static call

### inject

```js
InjectString.inject('a b', 'foo', {stripTags: true});
//=> 'a foo b'
```

### strip

```js
InjectString.strip('before foo bar after', {tag:'custom'});
//=> 'before foo bar after'
```

### stripAll

```js
InjectString.stripAll('before foo bar after');
//=> 'before foo bar after'
```

## Options
### Keep placeholders

Inject a snippet into a string with placeholders (used for subsequent insertions):

```js
var inject = new InjectString('before after');
inject.append('foo', {
stripTags: false
});
//=> 'before foo after'
```

### Strip placeholders

Inject a snippet into a string without placeholders:

```js
var inject = new InjectString('before after');
inject.append('foo', {
stripTags: true
});
//=> 'before foo after'
```

### Use a custom tag name

Customize the placeholder name:

```js
var inject = new InjectString('before after');
inject.append(str, 'foo', {tag: 'xyz'})
//=> 'before foo after'
```

### Use custom delimiters

Customize the placeholder delimiters:

```js
var str = new InjectString('a {{!snippet}} b', {delimiters: ['{{!', '}}']});
var result = inject.append('foo');
//=> 'a {{! snippet }}foo{{! endsnippet }} b'
```

### Add newlines around snippet

```js
var inject = new InjectString('a b', {newlines: true});
var result = inject.append('foo');
//=> 'a \nfoo\n b'
```

**Specify lines**
```js
var inject = new InjectString('a b', {newlines: 2});
var result = inject.append('foo');
//=> 'a \n\nfoo\n\n b'
```

**Specify lines for before and after separately**
```js
var inject = new InjectString('a b', {newlines: '1,2'});
var result = inject.append('foo');
//=> 'a \nfoo\n\n b'
```

## Running tests

Install dev dependencies:

```sh
$ npm install -d && npm test
```

## Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/amazingSurge/inject-string/issues/new)

## Credits
This library is based on [inject-snippet](https://github.com/jonschlinkert/inject-snippet).

## License

MIT © [amazingSurge](amazingSurge.com)

[npm-image]: https://badge.fury.io/js/inject-string.svg
[npm-url]: https://npmjs.org/package/inject-string
[travis-image]: https://travis-ci.org/amazingSurge/inject-string.svg?branch=master
[travis-url]: https://travis-ci.org/amazingSurge/inject-string
[daviddm-image]: https://david-dm.org/amazingSurge/inject-string.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/amazingSurge/inject-string
[coveralls-image]: https://coveralls.io/repos/amazingSurge/inject-string/badge.svg
[coveralls-url]: https://coveralls.io/r/amazingSurge/inject-string