stringify.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. function stringifyNode(node, custom) {
  3. var type = node.type;
  4. var value = node.value;
  5. var buf;
  6. var customResult;
  7. if (custom && (customResult = custom(node)) !== undefined) {
  8. return customResult;
  9. } else if (type === "word" || type === "space") {
  10. return value;
  11. } else if (type === "string") {
  12. buf = node.quote || "";
  13. return buf + value + (node.unclosed ? "" : buf);
  14. } else if (type === "comment") {
  15. return "/*" + value + (node.unclosed ? "" : "*/");
  16. } else if (type === "div") {
  17. return (node.before || "") + value + (node.after || "");
  18. } else if (Array.isArray(node.nodes)) {
  19. buf = stringify(node.nodes, custom);
  20. if (type !== "function") {
  21. return buf;
  22. }
  23. return value + "(" + (node.before || "") + buf + (node.after || "") + (node.unclosed ? "" : ")");
  24. }
  25. return value;
  26. }
  27. function stringify(nodes, custom) {
  28. var result, i;
  29. if (Array.isArray(nodes)) {
  30. result = "";
  31. for(i = nodes.length - 1; ~i; i -= 1){
  32. result = stringifyNode(nodes[i], custom) + result;
  33. }
  34. return result;
  35. }
  36. return stringifyNode(nodes, custom);
  37. }
  38. module.exports = stringify;