string-infuse

0.0.9 • Public • Published

String-infuse

1. Introduction

This light tool can infuse certain string template with given values by passing an object or just several individual values.

2. Get it

  • Browser: download this repository and include file dist/si.min.js.
  • Node: npm install --save string-infuse.

3. Use it

//------------------- Test Code -------------------//
var si = (typeof global === 'object') ? require('../index') : this.si;
var str = 'Append value: ${name|append(10)}\n' +
    'Repeat value : ${name|repeat(5)}\n' +
    'NickName in lowercase : ${nickName|toLowerCase}\n' +
    'Address in uppercase : ${address|toUpperCase};\n' +
    'object test : ${a.b};';
// json like value
var obj = {
    name: 'Jack',
    nickName: 'Jakie',
    address: 'Las Vegas',
    a: {
        b: "ababab"
    }
};
// add customize function - append
si.inFunction('append', function (x) { return '' + this + x });
 
//object mode
console.log('======= object mode (recommend) =======');
console.log(si.solve(str, obj));
//values mode
console.log('======= values mode =======');
console.log(si.solve(str, 'Ken', 'Ken', 'littleNickName', 'New York', { a: { b: "abcabc" } }));

The output will be

======= object mode (recommend) =======
Append value: Jack10
Repeat value : JackJackJackJackJack
NickName in lowercase : jakie
Address in uppercase : LAS VEGAS;
object test : ababab;
======= values mode =======
Append value: Ken10
Repeat value : KenKenKenKenKen
NickName in lowercase : littlenickname
Address in uppercase : NEW YORK;
object test : abcabc;

4. API

Name Function
inFunction add customize function
unFunction remove customize function
getFunction return function with given name
solve infuse given string with values

If you have any problem, please follow me @lucienlugeek on twitter.

Readme

Keywords

none

Package Sidebar

Install

npm i string-infuse

Weekly Downloads

0

Version

0.0.9

License

MIT

Last publish

Collaborators

  • lucienlugeek