Guest User

Untitled

a guest
Nov 27th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. // This is the Interpol, I use it to make Text Interpolation in Javascript
  2. // using #{this_format} ...Ruby's Way
  3.  
  4. var Interpol = function (text, par, fun) {
  5. text = text || "";
  6. par = par || {};
  7. fun = fun || function(x){ return x };
  8. return text.replace(/#{[^\}]+}/g, function(m, p, ft){
  9. var rt = m.substr(2);
  10. rt = rt.substr(0, rt.length - 1);
  11. rt = par[rt].toString() || "";
  12. return fun(rt);
  13. })
  14. };
  15.  
  16.  
  17. // And this is how I use it
  18. // Parameters: [Text], [Things to interpolate], [cool function]
  19.  
  20. var text1 = Interpol("Hello, I'm #{name} #{last}", {name: "Herson", last: "salinas"});
  21. var text2 = Interpol("Hello, I'm #{name} #{last}", {name: "Herson", last: "salinas"}, function(x){
  22. return x.toUpperCase();
  23. });
  24. console.log(text1);
  25. console.log(text2);
  26.  
  27. var sqlParse = function (str) {
  28. return str.replace(/\\/g, "\\\\").replace(/'/g, "''");
  29. }
  30.  
  31. var query = "select * from users where username = '#{name}' and password = md5('#{pass}')";
  32. var sql = Interpol(query, {name: "Bobby", pass: "I'm the Fisher"}, sqlParse);
  33.  
  34. console.log(sql);
Add Comment
Please, Sign In to add comment