-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-to-sqm.js
121 lines (105 loc) · 3.47 KB
/
json-to-sqm.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(function (global) {
'use strict'
var utils = {
isObject: function (obj) { return Object.prototype.toString.call(obj) === '[object Object]' },
isString: function (str) { return typeof str === 'string' },
isNumber: function (nr) { return (Number(nr) === nr && nr % 1 === 0) || (Number(nr) === nr && nr % 1 !== 0) },
isArray: Array.isArray
}
var chars = {
indent: function (nr) {
var ret = ''
for (var i = 0; i < nr; i++) {
ret += '\t'
}
return ret
},
REG_ADDONS: /^addOns/,
LF: '\r\n',
lineBreakIndent: function (nr) { return chars.LF + chars.indent(nr) }
}
function beginClass (className, indentLevel) {
return chars.indent(indentLevel) + 'class ' + className + chars.lineBreakIndent(indentLevel) + '{' + chars.LF
}
function endClass (indentLevel) {
return chars.indent(indentLevel) + '};'
}
function convertToSqmString (obj) {
var indentLevel = 0
var ret = ''
var parseObject = function parseObject (obj) {
iterateObject(obj, function (key, value) {
if (utils.isObject(value)) {
ret += beginClass(key, indentLevel++)
parseObject(value)
ret += endClass(--indentLevel)
} else {
ret += format(key, value, indentLevel)
}
ret += chars.LF
})
}
parseObject(obj)
return ret
}
function iterateObject (obj, cb) {
Object.keys(obj).forEach(function (k) {
cb(k, obj[k])
})
}
/* Formats a single value, including array */
function format (key, value, indentLevel) {
var ret = chars.indent(indentLevel) + key
switch (true) {
case utils.isString(value): ret += '="' + value.replace(/"/g, '""') + '"'
break
case utils.isNumber(value): ret += '=' + value
break
case utils.isArray(value):
var lineBreakAfterValue = !value.every(function (v) {
return utils.isNumber(v)
})
ret += '[]=' + (!lineBreakAfterValue ? '{' : (chars.lineBreakIndent(indentLevel) + '{') + chars.LF)
if (lineBreakAfterValue) indentLevel++
ret += formatArrayValues(value, indentLevel, lineBreakAfterValue)
if (lineBreakAfterValue) indentLevel--
ret += (!lineBreakAfterValue ? '}' : (chars.LF + chars.indent(indentLevel) + '}'))
break
default: throw new Error('Unexpected type: key' + key + ', value: ' + value)
}
ret += ';'
return ret
}
/* Format array values */
function formatArrayValues (arr, indentLevel, indentEveryNewLine) {
var ret = ''
for (var i = 0, l = arr.length; i < l; i++) {
var v = arr[i]
if (indentEveryNewLine) ret += chars.indent(indentLevel)
if (utils.isString(v)) ret += '"' + v.replace(/"/g, '""') + '"'
else ret += v
if (i !== l - 1) ret += ',' + (indentEveryNewLine ? chars.LF : '')
}
return ret
}
/* Export */
function JsonToSqm (jsonSqm) {
if (utils.isString(jsonSqm)) jsonSqm = JSON.parse(jsonSqm)
if (!jsonSqm || !utils.isObject(jsonSqm.Mission)) {
throw new Error('Missing "Mission" in JSON object')
}
return convertToSqmString(jsonSqm)
}
/*eslint-disable*/
if (typeof define === 'function' && define.amd) {
define(function () { return JsonToSqm })
} else if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = JsonToSqm
}
exports.JsonToSqm = JsonToSqm
} else {
global.JsonToSqm = JsonToSqm
}
/*eslint-enable*/
})(this)