UNPKG

gerber-to-svg

Version:

Gerber and NC drill file to SVG converter

109 lines (102 loc) 2.71 kB
(function() { var CKEY, DTAB, objToXml, repeat; repeat = function(pattern, count) { var result; result = ''; if (count === 0) { return ''; } while (count > 1) { if (count & 1) { result += pattern; } count >>= 1; pattern += pattern; } return result + pattern; }; CKEY = '_'; DTAB = ' '; objToXml = function(obj, op) { var children, dec, decimals, elem, i, ind, key, nl, o, pre, tb, v, val, xml, _i, _len, _ref, _ref1, _ref2; if (op == null) { op = {}; } pre = op.pretty; ind = (_ref = op.indent) != null ? _ref : 0; dec = (_ref1 = op.maxDec) != null ? _ref1 : false; decimals = function(n) { if (typeof n === 'number') { return Number(n.toFixed(dec)); } else { return n; } }; nl = pre ? '\n' : ''; tb = nl ? (typeof pre === 'string' ? pre : DTAB) : ''; tb = repeat(tb, ind); xml = ''; if (typeof obj === 'function') { obj = obj(); } if (Array.isArray(obj)) { for (i = _i = 0, _len = obj.length; _i < _len; i = ++_i) { o = obj[i]; xml += (i !== 0 ? nl : '') + (objToXml(o, op)); } } else if (typeof obj === 'object') { children = false; elem = Object.keys(obj)[0]; if (elem != null) { xml = "" + tb + "<" + elem; if (typeof obj[elem] === 'function') { obj[elem] = obj[elem](); } _ref2 = obj[elem]; for (key in _ref2) { val = _ref2[key]; if (typeof val === 'function') { val = val(); } if (key === CKEY) { children = val; } else { if (Array.isArray(val)) { if (dec) { val = (function() { var _j, _len1, _results; _results = []; for (_j = 0, _len1 = val.length; _j < _len1; _j++) { v = val[_j]; _results.push(decimals(v)); } return _results; })(); } val = val.join(' '); } if (dec) { val = decimals(val); } xml += " " + key + "=\"" + val + "\""; } } if (children) { xml += '>' + nl + objToXml(children, { pretty: pre, indent: ind + 1 }); } if (obj[elem]._ != null) { xml += "" + nl + tb + "</" + elem + ">"; } else { xml += '/>'; } } } else { xml += "" + obj + " "; } return xml; }; module.exports = objToXml; }).call(this);