utils.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*!
  2. * Jade - utils
  3. * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Convert interpolation in the given string to JavaScript.
  8. *
  9. * @param {String} str
  10. * @return {String}
  11. * @api private
  12. */
  13. var interpolate = exports.interpolate = function(str){
  14. return str.replace(/(_SLASH_)?([#!]){(.*?)}/g, function(str, escape, flag, code){
  15. code = code
  16. .replace(/\\'/g, "'")
  17. .replace(/_SLASH_/g, '\\');
  18. return escape
  19. ? str.slice(7)
  20. : "' + "
  21. + ('!' == flag ? '' : 'escape')
  22. + "((interp = " + code
  23. + ") == null ? '' : interp) + '";
  24. });
  25. };
  26. /**
  27. * Escape single quotes in `str`.
  28. *
  29. * @param {String} str
  30. * @return {String}
  31. * @api private
  32. */
  33. var escape = exports.escape = function(str) {
  34. return str.replace(/'/g, "\\'");
  35. };
  36. /**
  37. * Interpolate, and escape the given `str`.
  38. *
  39. * @param {String} str
  40. * @return {String}
  41. * @api private
  42. */
  43. exports.text = function(str){
  44. return interpolate(escape(str));
  45. };
  46. /**
  47. * Merge `b` into `a`.
  48. *
  49. * @param {Object} a
  50. * @param {Object} b
  51. * @return {Object}
  52. * @api public
  53. */
  54. exports.merge = function(a, b) {
  55. for (var key in b) a[key] = b[key];
  56. return a;
  57. };