fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. const solution = async (strChunk) => {
  5. const lines = strChunk.split(/\n+/g);
  6. const splitLineToParams = (line) => {
  7. const [keyPath, value] = line.split("=").map((item) => item.trim());
  8. return { keyPath, value };
  9. };
  10. const splitKeyPath = (keyPath) => {
  11. return keyPath.split(".");
  12. };
  13. const assembleNestedObj = (obj, keysPath, value) => {
  14. const [currentKey, ...restKeys] = keysPath;
  15. if (!restKeys.length) {
  16. return {
  17. ...obj,
  18. [currentKey]: value
  19. };
  20. }
  21. return {
  22. ...obj,
  23. [currentKey]: {
  24. ...assembleNestedObj(obj, restKeys, value),
  25. },
  26. };
  27. };
  28. let objArray = [];
  29. for (const line of lines) {
  30. const { keyPath, value } = splitLineToParams(line);
  31. const keyPathSplited = splitKeyPath(keyPath);
  32. objArray.push(assembleNestedObj({}, keyPathSplited, value));
  33. }
  34. process.stdout.write(JSON.stringify(objArray.reduce((acc, resultObj) => {
  35. return { ...acc, ...resultObj };
  36. }, {}), null, 2));
  37. };
  38. process.stdin.on('data', async (chunk) => {
  39. await solution(chunk.toString('utf-8'));
  40. process.exit(0);
  41. });
  42.  
Success #stdin #stdout 0.07s 35700KB
stdin
fizz = 3
buzz.one = item1
buzz.one = item2
buzz.one.two = 123
fizzbuzz.root.a.b.c = 13
stdout
{
  "fizz": "3",
  "buzz": {
    "one": {
      "two": "123"
    }
  },
  "fizzbuzz": {
    "root": {
      "a": {
        "b": {
          "c": "13"
        }
      }
    }
  }
}