function encodeQuotedString(value) { return '"' + value + '"' } function encodeSet(values) { if (!values) return '' if (!Array.isArray(values)) values = [values] return values.join(' ') } var encodeValueFns = { anonymous_identity: encodeQuotedString, ca_cert: encodeQuotedString, domain: encodeQuotedString, client_cert: encodeQuotedString, domain_suffix_match: encodeQuotedString, excluded_ssid: encodeQuotedString, imsi: encodeQuotedString, milenage: encodeQuotedString, provisioning_sp: encodeQuotedString, realm: encodeQuotedString, roaming_consortium: encodeQuotedString, roaming_partner: encodeQuotedString, username: encodeQuotedString, engine_id: encodeQuotedString, identity: encodeQuotedString, key_id: encodeQuotedString, password: encodeQuotedString, pcsc: encodeQuotedString, pin: encodeQuotedString, private_key: encodeQuotedString, private_key_passwd: encodeQuotedString, ssid: encodeQuotedString, phase1: encodeQuotedString, phase2: encodeQuotedString, psk: function (value) { if (/[0-9a-f]{40}/i.test(value)) return value else return encodeQuotedString(value) }, } function defaultEncodeValue(value) { switch (typeof value) { case 'object': if (value === null) return '' if (Array.isArray(value)) { return value.join(' ') } var items = [] for (var k in value) { items.push(encodeProperty(k, value[k])) } return items.join(' ') case 'string': return value case 'number': return String(value) case 'boolean': return value ? '1' : '0' case 'undefined': return '' default: throw new Error("weird value: " + value) } } module.exports = function encodeConfig(conf) { if (!conf) return '' var lines = [] for (var k in conf) { var value = conf[k] if (k !== 'networks') { lines.push(encodeProperty(k, value)) } } if (Array.isArray(conf.networks)) { lines.push('') conf.networks.forEach(function (network) { lines.push(encodeNetwork(network), '') }) } return lines.join('\n') } function encodeNetwork(network) { if (!network) return var lines = [] if (network._comment) { lines.push('#' + network._comment.replace(/\n/g, '\n#')) } lines.push('network={') for (var k in network) { if (k !== '_comment') lines.push('\t' + encodeProperty(k, network[k])) } lines.push('}') return lines.join('\n') } function encodeProperty(key, value) { var encodeValue = encodeValueFns[key] || defaultEncodeValue return key + '=' + encodeValue(value) }