utils.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. /**
  3. * Merge `b` into `a`.
  4. *
  5. * @param {Object} a
  6. * @param {Object} b
  7. * @return {Object}
  8. * @api public
  9. */
  10. exports.merge = function(a, b) {
  11. for (var key in b) a[key] = b[key];
  12. return a;
  13. };
  14. exports.stringify = function(str) {
  15. return JSON.stringify(str)
  16. .replace(/\u2028/g, '\\u2028')
  17. .replace(/\u2029/g, '\\u2029');
  18. };
  19. exports.walkAST = function walkAST(ast, before, after) {
  20. before && before(ast);
  21. switch (ast.type) {
  22. case 'Block':
  23. ast.nodes.forEach(function (node) {
  24. walkAST(node, before, after);
  25. });
  26. break;
  27. case 'Case':
  28. case 'Each':
  29. case 'Mixin':
  30. case 'Tag':
  31. case 'When':
  32. case 'Code':
  33. ast.block && walkAST(ast.block, before, after);
  34. break;
  35. case 'Attrs':
  36. case 'BlockComment':
  37. case 'Comment':
  38. case 'Doctype':
  39. case 'Filter':
  40. case 'Literal':
  41. case 'MixinBlock':
  42. case 'Text':
  43. break;
  44. default:
  45. throw new Error('Unexpected node type ' + ast.type);
  46. break;
  47. }
  48. after && after(ast);
  49. };