From 6036f1359218183a932f24fb625d86cee2383cdb Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 18 May 2013 15:44:28 -0500 Subject: [PATCH] Added unparse to generate .netrc contents from objects. --- index.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/index.js b/index.js index d25d6b8..c0c11a0 100644 --- a/index.js +++ b/index.js @@ -66,3 +66,25 @@ exports.parse = function(content) { return machines }; + +/** + * Generate contents of netrc file from objects. + * @param {Object} machines as returned by `netrc.parse` + * @return {String} text of the netrc file + */ +exports.unparse = function(machines){ + var lines = [], + keys = Object.getOwnPropertyNames(machines).sort(); + keys.forEach(function(key){ + lines.push('machine ' + key); + var machine = machines[key]; + var attrs = Object.getOwnPropertyNames(machine).sort(); + attrs.forEach(function(attr){ + if(typeof(machine[attr]) === 'string'){ + lines.push(' ' + attr + ' ' + machine[attr]); + } + }); + }); + return lines.join('\n'); +}; +