Format strings easily
- βοΈ Simple
- βοΈ Expandable/Configurable
- βοΈ Type Checking
- βοΈ No Dependencies
const formatter = strif.create({
transformers: {
date: s => new Date(s),
lds: d => d.toLocaleString()
},
plugins: [
'.tests/plugins/strif-color.js'
]
});
const template =
formatter
.template('{time} {user} {message}')
.prop('time', { transformers: [`date`, `lds`, `blue`] })
.prop('user', { accessor: 'user.name', transformers: [`gray`] })
.prop('message', { type: 'string' });
console.log(template.compile(data));
// will output: <%b1970-1-1 04:07:03> <%grBob> This is a super long message
Install from npm:
$ npm install strif
With require:
const strif = require('strif');
With ES6 import:
import strif from 'strif';
In the browser:
<script src="node_modules/strif/dist/strif.dist.js"></script>
! NOTICE: Plugins currently don't work in browser, woking on it. PRs Welcome
Using strif is actually pretty easy, you can use the default formatter under strif
let template = strif.template('{time} {user} {message}');
template.compile(data);
or create a custom one by using strif.create(opts)
, you can pass a set of transformers and plugins and other options
const formatter = strif.create({
transformers: {
date: s => new Date(s),
lds: d => d.toLocaleString()
}
});
let template = formatter
.template('{time} {user} {message}')
.prop('time', { transformers: [`date`] });
template.compile({
time: 11223322,
message: 'This is a super long message ',
user: { name: 'Bob' }
});
Using strif in the browser is as simple as in node, just import the script strif/dist/strif.dist.js
<html lang="en">
<head>
<script src="node_modules/strif/dist/strif.dist.js"></script>
</head>
<body>
<script>
strif.create(); // strif is available
</script>
</body>
</html>
! NOTICE: Plugins currently don't work in browser, woking on it. PRs Welcome
Exported members from strif
.
interface strif {
create(opts: strif.StrifOptions): void;
Formatter: strif.Formatter;
}
interface strif.Formatter {
constructor(opts: strif.FormatterOptions);
template(template: string, options: strif.TemplateOptions): strif.Template;
fromFile(path: string, options: strif.TemplateOptions): strif.Template;
}
interface strif.Template {
constructor(template: string, transformers: { [key: string]: (v) => v }, options: strif.TemplateOptions);
prop(name: string, options: strif.PropOptions): this;
print(): void;
compile(data: object, options: { ignoreTransformers: string[] }): string;
}
interface strif.Prop {
constructor(name, opts: strif.PropOptions);
getFromObject(obj: object): any;
}
interface strif.PropOptions {
accessor: string;
type: string;
transformers: string[];
}
interface strif.TemplateOptions {
props: strif.StrifProp[];
}
interface strif.FormatterOptions {
transformers: { [key: string]: (v) => v };
plugins: string[];
}
Transformers are functions that are be used to process some segment of the template,
they will receive a value and they must also return a value, here are some example:
{
transformers: {
date: s => new Date(s),
lds: d => d.toLocaleString()
}
}
I added a little bit of plugin support, what a plugin actually is, is an object (for now) wich contains transformers (also for now), and will be attached to any template generated by that transformer. Here are some example:
const chalk = require('chalk');
module.exports = {
transformers: {
blue: s => chalk.blue(s),
gray: s => chalk.gray(s),
green: s => chalk.green(s),
}
};
Check this demo for another example.
If you found a bug or have a feature request please dont hesitate on leaving a issue
If you would like to collaborate please check CONTRIBUTING.md for more details.
This project was in some way inspired by @davidchambers/string-format, at least in the sense of the transformers concept.