UNPKG

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