-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (42 loc) · 1.18 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
'use strict'
module.exports = function doIt(inputObject, spaceAmount, useTabs) {
function arrayToType (array, indentationLevel) {
if (array.length > 0) {
return anyToType(array[0], indentationLevel) + "[]"
}
else {
return "[]"
}
}
function generateIndentation (amount) {
let indentation = ""
for (let i = 0; i < (useTabs ? amount : amount * spaceAmount); i++) {
indentation += (useTabs ? "\t" : " ")
}
return indentation
}
function objectToType (object, indentationLevel) {
let type = "{\n"
const indentation = generateIndentation(indentationLevel + 1)
for (const key in object) {
const value = object[key]
type += `${indentation}${key}: ${anyToType(value, indentationLevel + 1)},\n`
}
return `${type}${generateIndentation(indentationLevel)}}`
}
function anyToType (any, indentationLevel) {
if (Array.isArray(any)) {
return arrayToType(any, indentationLevel)
}
else if (any == null) {
return "void"
}
else if (typeof any === "object") {
return objectToType(any, indentationLevel)
}
else {
return typeof any
}
}
return anyToType(inputObject, 0)
}