1.0.0 • Published 9 years ago
superterp v1.0.0
superterp
A super lightweight simple string inTERPolation library for javascript and node.js
Install
Node.js
$ npm install --save superterp
Vanilla JavaScript
Copy the distribution superterp.js file into your project files, and include it as per usual:
<script src="superterp.min.js"></script>
Usage
> var terp = require('superterp'); // Node.js
> terp('Hi ${name}! Eat a ${type} sandwich!', { name: 'asshole', type: 'turd' });
'Hi asshole! Eat a turd sandwich!'
terp(str, vars, context)
Parameters:
- str - The source String to be inTERPolated. Optional.
- vars - An Object defining variables that will be available to expressions embedded in the source
str
. Optional. - context - The Object that will become
this
in embedded expressions in the sourcestr
. Optional.
terp
returns the String result of the inerpolation. If the str
parameter isn't a String,
the value of str
is returned without modification.
Examples
Use the full power of javascript expressions in substitutions:
> terp("${name} smokes ${joints + 10} joints a day.", { name: "Ted", joints: 4 });
'Ted smokes 14 joins a day.'
Evaluate interpolations against a context object:
> var ted = { name: "Ted" };
> var turd = { name: "Turd" };
> var template = "Who likes ${food}? ${this.name} does.";
> terp(template, { food: "liver" }, ted);
'Who likes liver? Ted does.'
> terp(template, { food: "bees" }, turd);
'Who likes bees? Turd does.'
Escape a conflicting opening interpolation delimiter:
> terp("Emoticons ${'$'}{ and moustaches and shit.")
'Emoticons ${ and moustaches and shit.'
Limitations
superterp aims to be lighweight. It doesn't make attempts to support nested occurrences of the expression delimiters.
Specifically, you can't put a }
anywhere inside a ${ ... }
. Deal with it.
Testing
gulp test
License
MIT © Troy Kinsella