As I have 4 key/value in map , I'm trying to store two keys in string format and rest two in an array but it's not taking array's multiple value.
right now what output I'm getting :
{ url: 'account/43',
status: '200',
headers: [ '\'content-type\' = \'application/json\'' ],
body: [ '{ "name": "Fatma Zaman" }' ]}
Expected output:
{ url: 'account/43',
status: '200' ,
headers:
[ 'content-type = application/json',
'content-type = application/text' ],
body: [ '{ name: Fatma Zaman }' ] }
below are the code which is returning either multiple headers value but all keys are in array format or url/status in string & headers/body in array.
function processFile(content) {
content.forEach(function(node) {
if (node.startsWith("//")) {
key = node.substring(2, node.length-2).toLowerCase().trim()
return
} else {
value = node
}
if (key in map) {
map[key].push(value)
} else {
map[key]= [value]
}
map[key] = ["headers", "body"].includes(key)? [value] : value
})
return map
}
if I add below code , it's giving me multiple value but not url/body not in string
if (key in map) {
map[key].push(value)
} else {
map[key]= [value]
}
In short , I'm not able to achieve both at the same time. like multiple header value with url/status in string format. Any help and suggestion would be appreciate it
Here is the full code
const fs = require("fs")
const path = require("path")
let key
let value
let filePath
function parseFile(filePath) {
filePath = path.join(__dirname, "../resources/FileData1.txt")
fs.readFile(filePath, function(err, data) {
if (err) throw err
content = data.toString().split(/(?:\r\n|\r|\n)/g).map(function(line) {
return line.trim()
}).filter(Boolean)
console.log(processFile(content))
})
}
function processFile(content) {
content.forEach(function(node) {
if (node.startsWith("//")) {
key = node.substring(2, node.length-2).toLowerCase().trim()
return
} else {
value = node
}
if (key in map) {
map[key].push(value)
} else {
map[key] = value
}
// map[key] = ["headers", "body"].includes(key)? [value] : value
})
return map
}
parseFile(filePath)
module.exports = {parseFile, processFile}
And input are in below format:
//Status//
200
//HEADERS//
content-type = application/json
//BODY//
{ name: Fatma Zaman }
//URL//
account/43
//HEADERS//
content-type = application/text