* Parse Python dot notation code examples into a JS object. * * @param {string} raw - Raw Python dot notation code examples * @return {object} The parsed data
(raw)
| 102 | * @return {object} The parsed data |
| 103 | */ |
| 104 | function parseCode(raw) { |
| 105 | objects = [] |
| 106 | raw = raw.trim() |
| 107 | raw.split("\n").forEach(envVar => { |
| 108 | let x |
| 109 | x = envVar.split("set({\"") |
| 110 | envVar = x[1] |
| 111 | x = envVar.split("})") |
| 112 | envVar = x[0] |
| 113 | x = envVar.split("\": ") |
| 114 | envVar = x[0] |
| 115 | let namespace = envVar.split(".").map(toKey) |
| 116 | let value = x[1] |
| 117 | while (true) { |
| 118 | if (value === "True") { |
| 119 | value = true |
| 120 | break |
| 121 | } |
| 122 | if (value === "False") { |
| 123 | value = false |
| 124 | break |
| 125 | } |
| 126 | if (value === true || value === false) { |
| 127 | break |
| 128 | } |
| 129 | try { |
| 130 | value = JSON.parse(value) |
| 131 | break |
| 132 | } catch (err) { |
| 133 | break |
| 134 | } |
| 135 | } |
| 136 | let object = value |
| 137 | for (var i = namespace.length - 1; i >= 0; i--) { |
| 138 | new_object = {} |
| 139 | new_object[namespace[i]] = object |
| 140 | object = new_object |
| 141 | } |
| 142 | objects.push(object) |
| 143 | }) |
| 144 | return mergeDeep(...objects) |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Dump a JS config object to a YAML string. |
no test coverage detected