stuppid_bot

форматирование строк

Sep 8th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // возвращает форматированную строку, аналогичную той, что выводит console.log
  2. function sprtinf(s) {
  3.   var a = Array.prototype.slice.call(arguments, 1)
  4.     , v;
  5.   return s.replace(/%(?:%|\d+([sdf]))/g, function (m, t) {
  6.     if (m == '%%') return '%';
  7.     if (!a.length) return m;
  8.     v = a.shift();
  9.     if (t == 'd') return parseInt(v);
  10.     if (t == 'f') return parseFloat(v);
  11.     return v;
  12.   });
  13. }
  14.  
  15. // как в C#, Python
  16. function format(s) {
  17.   var a = Array.prototype.slice.call(arguments, 1);
  18.   return s.replace(/\{(\d+)\}/g, function (m, i) {
  19.     if (i >= a.length) return m;
  20.     return a[i];
  21.   });
  22. }
Advertisement
Add Comment
Please, Sign In to add comment