https://github.com/b4dnewz/string-template
A utility to format strings with many options
https://github.com/b4dnewz/string-template
replacement string string-formatter string-manipulation string-template string-utility template utility
Last synced: 12 months ago
JSON representation
A utility to format strings with many options
- Host: GitHub
- URL: https://github.com/b4dnewz/string-template
- Owner: b4dnewz
- License: mit
- Created: 2019-11-14T13:22:08.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T00:51:22.000Z (about 3 years ago)
- Last Synced: 2025-02-12T17:15:33.604Z (12 months ago)
- Topics: replacement, string, string-formatter, string-manipulation, string-template, string-utility, template, utility
- Language: TypeScript
- Size: 644 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# string-template
> A utility to format strings with many options
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage percentage][coveralls-image]][coveralls-url]
## Getting started
Download it using your favourite package manager.
```
npm i @b4dnewz/string-template
```
Then import it in your code and __have fun__ formatting strings.
```js
import template from "@b4dnewz/string-template";
const str = `
That's an awfully hot {item},
did {person} kill himself?
Probably {answer}.
`;
template(str, {
item: "coffee pot",
person: "Jeffrey Epstein",
answer: "not"
});
```
By default the function will fail if unknown properties are found, but can be disabled using options.
## Using custom pattern
The string template function will use this pattern `{%s}`, for finding variables in the string, where __%s__ will be replaced with the built-in word match pattern which is not customizable.
If you want to use a different template pattern you can add the __pattern__ option as third parameter.
```js
const str = `Using a <:adjective:> pattern`;
const out = template(str, {
adjective: "different"
}, {
pattern: "<:%s:>"
});
console.log(out);
```
## Options
#### pattern
Type: `string`
The string template pattern to use for finding replacements, it __must use__ the string replacer `%s` to know where to put the word pattern.
```js
template(input, replacements, {
pattern: ":%s:"
});
```
#### ignoreErrors
Type: `boolean`
When `true` unknown properties in the string will not raise an error and will simply ignored.
```js
template(input, replacements, {
ignoreErrors: true
});
```
---
## License
This package is released under [MIT License](./LICENSE) © [Filippo Conti](https://b4dnewz.github.io/)
[npm-image]: https://badge.fury.io/js/%40b4dnewz%2Fstring-template.svg
[npm-url]: https://npmjs.org/package/@b4dnewz/string-template
[travis-image]: https://travis-ci.org/b4dnewz/string-template.svg?branch=master
[travis-url]: https://travis-ci.org/b4dnewz/string-template
[coveralls-image]: https://coveralls.io/repos/b4dnewz/string-template/badge.svg
[coveralls-url]: https://coveralls.io/r/b4dnewz/string-template