diff --git a/README.md b/README.md index 5a8acb0..0b9c865 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,9 @@ var logfmt = require('logfmt'); logfmt.stringify({foo: 'bar'}); // 'foo=bar' +logfmt.stringify({foo: 'hello kitty', bar: {a: 'hi', b: 'there'}}); +// 'foo="hello kitty" bar.a=hi bar.b=there' + logfmt.parse('foo=bar'); // {foo: 'bar'} ``` diff --git a/lib/stringify.js b/lib/stringify.js index da4bd43..df3fc73 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -1,4 +1,4 @@ -exports.stringify = function(data){ +exports.stringify = function stringify(data, prefix){ var line = ''; for(var key in data) { @@ -8,6 +8,11 @@ exports.stringify = function(data){ is_null = true; value = ''; } + else if(typeof value === "object") { + var elem = stringify(value, key) + ' '; + line += elem; + continue; + } else value = value.toString(); var needs_quoting = value.indexOf(' ') > -1 || value.indexOf('=') > -1; @@ -17,7 +22,8 @@ exports.stringify = function(data){ if(needs_quoting) value = '"' + value + '"'; if(value === '' && !is_null) value = '""'; - line += key + '=' + value + ' '; + var elem = (prefix ? `${prefix}.${key}` : key) + '=' + value + ' '; + line += elem; } //trim traling space diff --git a/package-lock.json b/package-lock.json index d915fbd..bcfde1c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -190,9 +190,9 @@ "dev": true }, "csv-parse": { - "version": "4.4.3", - "resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-4.4.3.tgz", - "integrity": "sha512-TiLGAy14FPJ7/yB+Gn6RgSxoZLpf6pJTRkGqmCt9t/SGVwubrXjbUWtEw39RlKB6hDHzbdjLyBZaysQ0Ji6p/w==", + "version": "4.16.0", + "resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-4.16.0.tgz", + "integrity": "sha512-Zb4tGPANH4SW0LgC9+s9Mnequs9aqn7N3/pCqNbVjs2XhEF6yWNU2Vm4OGl1v2Go9nw8rXt87Cm2QN/o6Vpqgg==", "dev": true }, "csv-stringify": { @@ -601,9 +601,9 @@ } }, "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, "lodash.get": { @@ -682,13 +682,22 @@ "dev": true }, "mkdirp": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", - "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", "dev": true, "optional": true, "requires": { - "minimist": "0.0.8" + "minimist": "^1.2.5" + }, + "dependencies": { + "minimist": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "dev": true, + "optional": true + } } }, "mocha": { diff --git a/test/stringify_test.js b/test/stringify_test.js index 73bbf44..928b999 100644 --- a/test/stringify_test.js +++ b/test/stringify_test.js @@ -50,4 +50,9 @@ suite('logfmt.stringify', function() { options.foo = 13 assert.equal('foo=13 bar=abc', logfmt.stringify(options)); }) + + test("nested object", function(){ + var data = {foo: {a: 1, b: "hello world!"}, bar: "hello kitty"} + assert.equal("foo.a=1 foo.b=\"hello world!\" bar=\"hello kitty\"", logfmt.stringify(data)) + }) })