-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (57 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// const exampleRequest = require("./request-example.json")
const StringToNumber = (value) => {
if (!isNaN(value)) {
return Number(value)
} else {
let temp = Number(value)
if (isNaN(temp)) {
return value
}
return temp
}
}
const stringObjectManipulation = (item) => {
if (item === "true") {
item = true
} else if (item === "false") {
item = false
} else if (item === "null") {
item = null
} else if (item === "undefined") {
item = "undefined"
} else {
let temp = StringToNumber(item)
item = temp
}
return item
}
const ConvertStringValues = (dataObject) => {
try {
const emptyValues = new Set([null, undefined, [], {}])
Object.keys(dataObject).forEach((key) => {
if (emptyValues.has(dataObject[key])) {
return dataObject[key]
} if (typeof dataObject[key] === "string") {
dataObject[key] = stringObjectManipulation(dataObject[key])
} else if (typeof dataObject[key] === "object") {
return ConvertStringValues(dataObject[key]);
} else if (Array.isArray(dataObject[key])) {
if (emptyValues.has(dataObject[key])) {
return dataObject[key]
} else if (typeof dataObject[key] === "object") {
return ConvertStringValues(dataObject[key]);
} else if (typeof dataObject[key] === "string") {
dataObject[key] = stringObjectManipulation(dataObject[key])
}
} else {
return dataObject[key]
}
})
return dataObject
} catch (error) {
return error
}
}
module.exports = ConvertStringValues
//if case if you want to test ..
// console.log(ConvertStringValues(exampleRequest))