Friday 29 April 2022

return multiple function from parse function in node js / javascript

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("hr", parseHR);
parsers.set("heading-4", parseH4);
parsers.set("heading-6", parseH6);
parsers.set("block", parseBlock);

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

function parseDocument(obj) {
  let type = "doc";
  let attrs = { dirty: true };
  let children = [];

  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return { type, children, attrs };
}

function parseParagraph(obj) {
  let type = "p";
  let attrs = { dirty: true };
  let children = [];

  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return { type, children, attrs };
}

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

function parseHR(obj) {
  let type = "hr";
  let attrs = { dirty: true };
  let children = [];
  return { type, attrs, children };
}

function parseH4(obj) {
  let type = "h4";
  let attrs = { dirty: true };
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  children = children.map((c) => c.children).flat();
  return { type, attrs, children };
}

function parseH6(obj) {
  let type = "h6";
  let attrs = { dirty: true };
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  children = children.map((c) => c.children).flat();
  return { type, attrs, children };
}

function parseBlock(obj) {
  let jsonObject = {
    MVJlVePWr6: {
      e_id: "MVJlVePWr6",
      name_placeholder: "news_item",
      alternative_tags: ["da", "es"],
    },
    gevhdjsx: {
      e_id: "gevhdjsx",
      name_placeholder: "changes_item",
      alternative_tags: ["da"],
    },
  };
  let type, attrs, children;
  for (
    i = 0;
    jsonObject[obj.data.target.sys.id].alternative_tags.length > i;
    i++
  ) {
    for (const alt of jsonObject[obj.data.target.sys.id].alternative_tags) {
      type = "block";
      attrs = { dirty: true };
      children = {};
      if (obj.data.target.sys.id in jsonObject) {
        children = {
          type: "primary",
          name: `${alt}_name_we_got`,
          other_name: alt,
          employees: "intern",
        };
      }
      return { type, attrs, children };
    }
  }
}

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

function getSrcData() {
  return {
    nodeType: "document",
    data: {},
    content: [
      {
        nodeType: "paragraph",
        content: [
          {
            nodeType: "text",
            value: "dummy testing bold",
            marks: [
              {
                type: "bold",
              },
            ],
            data: {},
          },
        ],
        data: {},
      },
      {
        nodeType: "hr",
        content: [],
        data: {},
      },
      {
        nodeType: "paragraph",
        content: [
          {
            nodeType: "text",
            value: "",
            marks: [],
            data: {},
          },
        ],
        data: {},
      },
      {
        nodeType: "heading-4",
        content: [
          {
            nodeType: "text",
            value: "heading 4",
            marks: [
              {
                type: "bold",
              },
            ],
            data: {},
          },
        ],
        data: {},
      },
      {
        nodeType: "heading-6",
        content: [
          {
            nodeType: "text",
            value: "heading 6",
            marks: [
              {
                type: "italic",
              },
            ],
            data: {},
          },
        ],
        data: {},
      },
      {
        nodeType: "hr",
        content: [],
        data: {},
      },
      {
        nodeType: "block",
        content: [],
        data: {
          target: {
            sys: {
              id: "MVJlVePWr6",
              type: "Link",
              linkType: "Block",
            },
          },
        },
      },
    ],
  };
}

I have wrote this code and I am getting expected output the problem is I want to send multiple data from one function that is from parseBlock but I can only retrun one value which is first da but I can't return other en value

as I check and printed console.log() so I am getting multiple values but when I am returning the value I am getting the single value

as my expected output is like this from parseBlock but in above output I am getting single value from parseBlock

      {
        "type": "block",
        "attrs": {
          "dirty": true
        },
        "children": {
          "type": "primary",
          "name": "da_name_we_got",
          "other_name": "da",
          "employees": "intern"
        }
      },
      {
        "type": "block",
        "attrs": {
          "dirty": true
        },
        "children": {
          "type": "primary",
          "name": "es_name_we_got",
          "other_name": "es",
          "employees": "intern"
        }
      }

but this is not I am getting and I don't know where I am doing wrong to return the multiple values from one function



from return multiple function from parse function in node js / javascript

No comments:

Post a Comment