0

I wrote a code and I want to remove undefined or null value from here which I am returning

const parsers = new Map();

parsers._get = parsers.get;
parsers.get = function (key) {
  if (this.has(key)) return this._get(key);
  return () =>
    console.warn(`Parser not implemented for: ${JSON.stringify(key)}`);
};

parsers.set("document", parseDocument);
parsers.set("paragraph", parseParagraph);
parsers.set("text", parseText);
parsers.set("table", parseTable);
parsers.set("tablerow", parseRow);
parsers.set("htr", parseHTR);
parsers.set("tablehc", parseHC);
parsers.set("tbody", parseTBody);
parsers.set("btr", parseBTR);
parsers.set("tablec", parseTableC);

function convert(obj) {
  return [parsers.get(obj.nodeType)(obj)];
}

function parseDocument(obj) {
  let type = "doc";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return { type, children: children.filter((e) => e !== null) };
}

function parseParagraph(obj) {
  let type = "p";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return { type, children: children.filter((e) => e !== null) };
}

function parseText(obj) {
  const result = {};
  result.text = obj.value;
  obj.marks.forEach((e) => (result[e.type] = true));
  return result;
}

function parseTable(obj) {
  let type = "table";
  let children = [];
  children.push(parsers.get("tbody")(obj));
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return { type, children: children.filter((e) => e !== null) };
}

function parseRow(obj) {
  var type = [];
  let children = [];
  
  let check=[];
  obj.content.forEach((e) => {
  function add(item) {
    if(check.indexOf(item) === -1) {
      check.push(item);
    }
  }
    add(e.nodeType);
  });
  
  if (check.join() === "tablehc") {
      type.push("thead");
      children.push(parsers.get("htr")(obj));
    } else {
   // getting null and undefined from here
    }
  type = [...new Set(type)].join();
  
  if(children.length>0)
  return { type, children: children.filter((e) => e !== null) };
}

function parseHTR(obj) {
  let type = "tr";
  let children = [];
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return { type, children: children.filter((e) => e !== null) };
}

function parseHC(obj) {
  let type = "th";
  let children = [];
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return { type, children: children.filter((e) => e !== null) };
}

function parseTBody(obj) {
  let type = "tbody";
  let children = [];
  children.push(parsers.get("btr")(obj));
  return { type, children: children.filter((e) => e !== null) };
}

function parseBTR(obj) {
  let type = "tr";
  let children = [];
  obj.content.forEach((e) => {
  if (e.nodeType === "tablec")
    children.push(parsers.get(e.nodeType)(e));
  });
  
  if (children.length > 0) return { type, children: children.filter((e) => e !== null) };
}

function parseTableC(obj) {
  let type = "td";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return { type, children: children.filter((e) => e !== null) };
}

let result = convert(getSrcData());
console.log("Converted object: ", result);

function getSrcData() {
  return {
    nodeType: "document",
    content: [
      {
        nodeType: "paragraph",
        content: [
          {
            nodeType: "text",
            value: "dummy testing bold",
            marks: [
              {
                type: "bold",
              },
            ],
          },
        ],
      },
      {
        nodeType: "table",
        content: [
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "hey",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "code text",
                        marks: [
                          {
                            type: "code",
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "text",
                        marks: [
                          {
                            type: "bold",
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
      {
        nodeType: "paragraph",
        content: [
          {
            nodeType: "text",
            value: "",
            marks: [],
          },
        ],
      },
      {
        nodeType: "table",
        content: [
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "hey",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "code text",
                        marks: [
                          {
                            type: "code",
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "bold text",
                        marks: [
                          {
                            type: "bold",
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  };
}

Is there any possible way I can get input value without this undefined and null value

I am getting undefined here but in my system I am getting null value in place of undefined is there any possible if I can return with out the null value and undefined in my actual output

as my expected value is like this

expected output:-

Converted object: [
  {
    "type": "doc",
    "children": [
      {
        "type": "p",
        "children": [
          {
            "text": "dummy testing bold",
            "bold": true
          }
        ]
      },
      {
        "type": "table",
        "children": [
          {
            "type": "head",
            "children": [
              {
                "type": "tr",
                "children": [
                  {
                    "type": "th",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "hey" }]
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "body",
            "children": [
              {
                "type": "tr",
                "children": [
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "code text", "code": true }]
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tr",
                "children": [
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "bold text", "bold": true }]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "p",
        "children": [
          {
            "text": ""
          }
        ]
      },

// table should look like this
      {
        "type": "table",
        "children": [
          {
// head part contains only the head part and its supporting children
            "type": "head",
            "children": [
              {
                "type": "tr",
                "children": [
                  {
                    "type": "th",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "hey" }]
                      }
                    ]
                  },
                  {
                    "type": "th",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "" }]
                      }
                    ]
                  }
                ]
              }
            ]
          },
// body part contains only the body part and its supporting children
          {
            "type": "body",
            "children": [
              {
                "type": "tr",
                "children": [
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "code text", "code": true }]
                      }
                    ]
                  },
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "" }]
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tr",
                "children": [
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "bold text", "bold": true }]
                      }
                    ]
                  },
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "" }]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

5
  • That's like a lot of code, could you please shorten the snippet maybe and be more clear of what you are trying to achieve? Commented Nov 7, 2022 at 19:14
  • You are conditionally returning { type, children } or undefined in your functions, you should return something else than undefined or handle it somehow Commented Nov 7, 2022 at 19:15
  • but I don't want to return it if their children.length is 0 is there any possible way I cannot return this? @KonradLinkowski Commented Nov 7, 2022 at 19:19
  • return { type, children: children.filter(e => e !== undefined) } everywhere should fix the issue Commented Nov 7, 2022 at 19:23
  • not working @KonradLinkowski I tried here and in my code too with null still i am getting null in my program and undefined here. See I have updated the code snippet Commented Nov 7, 2022 at 19:42

1 Answer 1

1

I don't see any nulls but it removes every undefined

const parsers = new Map();

parsers._get = parsers.get;
parsers.get = function(key) {
  if (this.has(key)) return this._get(key);
  return () =>
    console.warn(`Parser not implemented for: ${JSON.stringify(key)}`);
};

parsers.set("document", parseDocument);
parsers.set("paragraph", parseParagraph);
parsers.set("text", parseText);
parsers.set("table", parseTable);
parsers.set("tablerow", parseRow);
parsers.set("htr", parseHTR);
parsers.set("tablehc", parseHC);
parsers.set("tbody", parseTBody);
parsers.set("btr", parseBTR);
parsers.set("tablec", parseTableC);

function convert(obj) {
  return [parsers.get(obj.nodeType)(obj)];
}

function parseDocument(obj) {
  let type = "doc";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}

function parseParagraph(obj) {
  let type = "p";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}

function parseText(obj) {
  const result = {};
  result.text = obj.value;
  obj.marks.forEach((e) => (result[e.type] = true));
  return result;
}

function parseTable(obj) {
  let type = "table";
  let children = [];
  children.push(parsers.get("tbody")(obj));
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}

function parseRow(obj) {
  var type = [];
  let children = [];

  let check = [];
  obj.content.forEach((e) => {
    function add(item) {
      if (check.indexOf(item) === -1) {
        check.push(item);
      }
    }
    add(e.nodeType);
  });

  if (check.join() === "tablehc") {
    type.push("thead");
    children.push(parsers.get("htr")(obj));
  } else {
    // getting null and undefined from here
  }
  type = [...new Set(type)].join();

  if (children.length > 0)
    return {
      type,
      children: children.filter((e) => e !== undefined)
    };
}

function parseHTR(obj) {
  let type = "tr";
  let children = [];
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}

function parseHC(obj) {
  let type = "th";
  let children = [];
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}

function parseTBody(obj) {
  let type = "tbody";
  let children = [];
  children.push(parsers.get("btr")(obj));
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}

function parseBTR(obj) {
  let type = "tr";
  let children = [];
  obj.content.forEach((e) => {
    if (e.nodeType === "tablec")
      children.push(parsers.get(e.nodeType)(e));
  });

  if (children.length > 0) return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}

function parseTableC(obj) {
  let type = "td";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}

let result = convert(getSrcData());
console.log("Converted object: ", result);

function getSrcData() {
  return {
    nodeType: "document",
    content: [{
        nodeType: "paragraph",
        content: [{
          nodeType: "text",
          value: "dummy testing bold",
          marks: [{
            type: "bold",
          }, ],
        }, ],
      },
      {
        nodeType: "table",
        content: [{
            nodeType: "tablerow",
            content: [{
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "hey",
                    marks: [],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [{
              nodeType: "tablec",
              content: [{
                nodeType: "paragraph",
                content: [{
                  nodeType: "text",
                  value: "code text",
                  marks: [{
                    type: "code",
                  }, ],
                }, ],
              }, ],
            }, ],
          },
          {
            nodeType: "tablerow",
            content: [{
              nodeType: "tablec",
              content: [{
                nodeType: "paragraph",
                content: [{
                  nodeType: "text",
                  value: "text",
                  marks: [{
                    type: "bold",
                  }, ],
                }, ],
              }, ],
            }, ],
          },
        ],
      },
      {
        nodeType: "paragraph",
        content: [{
          nodeType: "text",
          value: "",
          marks: [],
        }, ],
      },
      {
        nodeType: "table",
        content: [{
            nodeType: "tablerow",
            content: [{
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "hey",
                    marks: [],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [{
                nodeType: "tablec",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "code text",
                    marks: [{
                      type: "code",
                    }, ],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablec",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [{
                nodeType: "tablec",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "bold text",
                    marks: [{
                      type: "bold",
                    }, ],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablec",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
            ],
          },
        ],
      },
    ],
  };
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.