create.js 756 B

12345678910111213141516171819202122232425262728
  1. import { List } from '../utils/List.js';
  2. export function createConvertor(walk) {
  3. return {
  4. fromPlainObject(ast) {
  5. walk(ast, {
  6. enter(node) {
  7. if (node.children && node.children instanceof List === false) {
  8. node.children = new List().fromArray(node.children);
  9. }
  10. }
  11. });
  12. return ast;
  13. },
  14. toPlainObject(ast) {
  15. walk(ast, {
  16. leave(node) {
  17. if (node.children && node.children instanceof List) {
  18. node.children = node.children.toArray();
  19. }
  20. }
  21. });
  22. return ast;
  23. }
  24. };
  25. };