daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var regex = /\{([a-zA-Z\.0-9]*)\}/mg;
  2.  
  3. var getObject = function (obj, str) {
  4.     var parts = str.split('.');
  5.     for (var i = 0, l = parts.length; i < l; ++i) {
  6.         if (!obj[parts[i]] || obj[parts[i]] === null) {
  7.             return null;
  8.         }
  9.         obj = obj[parts[i]];
  10.     }
  11.     return obj;
  12. };
  13.  
  14. module.exports = function (template, obj) {
  15.     if (arguments.length === 1) {
  16.         return template;
  17.     } else if (arguments.length === 2) {
  18.         if (arguments[1] == null) {
  19.             return template;
  20.         } else if (typeof arguments[1] === 'object') {
  21.             var obj = arguments[1];
  22.         } else if (typeof arguments[1] === 'Array') {
  23.             var obj = arguments[1];
  24.         } else {
  25.             var obj = [arguments[1]];
  26.         }
  27.     } else if (arguments.length > 1) {
  28.         var obj = Array.slice.call(arguments, 1);
  29.     } else {
  30.         return template;
  31.     }
  32.  
  33.     return template.replace(regex, function (match, name) {
  34.         return getObject(obj, name) || match;
  35.     });
  36. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top