diff --git a/README.md b/README.md index d36af12..7810841 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Which will output: ## Double quotation marks -Normally, all the values will be transformed into array using `split( /\s+/ )`, but string wrapped with `"` will be treated as a continuous string. +Normally, all the values will be transformed into array using `split( /\s+/ )`, but string wrapped with `"` will be treated as a continuous string. For example, the CommandLine below: @@ -82,3 +82,58 @@ will be split into: - `--name="Jack Neekey"` ( `"` is reserved ) - `--sex=male` - `otherargs` + + +## Key/value transformers + +To apply transformations in the keys or values of a parsed table you can use the options parameter. + +```javascript +var FS = require( 'fs' ); +var Parser = require('table-parser'); + +var linux_ps = './ps.log'; + +data = FS.readFileSync( linux_ps ).toString(); +var parsedData = Parser.parse( data, { + keyTransformer: function(key) { + return 'a' + key; + }, + valueTransformer: function(value) { + return value.join(); + } +} ); + +console.log( parsedData ); +``` + +Which will output: + +```bash +[ + { + 'aPID': '692', + 'aTTY': 'ttys000', + 'aTIME': '0:00.06', + 'aCMD': 'login -pfl neekey /bin/bash -c exec -la bash /bin/bash' + }, + { + 'aPID': '49693', + 'aTTY': 'ttys000666', + 'aTIME': '0:00.06', + 'aCMD': '-bash' + }, + { + 'aPID': '', + 'aTTY': 'ttys000', + 'aTIME': '0:47.81', + 'aCMD': '/Users/neekey/Dropbox/nodejs/app/windTest/Redis/mac_linux/src/redis-server' + }, + { + 'aPID': '52300', + 'aTTY': 'ttys001', + 'aTIME': '0:00.05', + 'aCMD': 'login -pfl neekey /bin/bash -c exec -la bash /bin/bash' + } +] +``` diff --git a/lib/index.js b/lib/index.js index 7e1fd82..407a788 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,9 +12,10 @@ var EMPTY_EX = /\s/; /** * The output sting of cmd to parse * @param output + * @param {{ keyTransformer: function(string): string | undefined, valueTransformer: function([*]): * | undefined } | undefined} options * @returns {Array} */ -module.exports.parse = function (output) { +module.exports.parse = function (output, options) { // Split into lines // Basically, the EOL should be: @@ -189,8 +190,18 @@ module.exports.parse = function (output) { var value = null; for (title in titleInfo) { info = titleInfo[title]; - value = line.substring(info.titleBegin, info.titleEnd + 1); - lineItem[title] = splitValue(value.trim()); + value = splitValue(line.substring(info.titleBegin, info.titleEnd + 1).trim()); + + if (options) { + if (options.keyTransformer) { + title = options.keyTransformer(title); + } + if (options.valueTransformer) { + value = options.valueTransformer(value); + } + } + + lineItem[title] = value; } result.push(lineItem); diff --git a/test/spec.js b/test/spec.js index 5187549..fe6f7f8 100644 --- a/test/spec.js +++ b/test/spec.js @@ -132,4 +132,46 @@ describe('tabler-parser', function () { Assert.equal(/^\w+$/.test(ret.USER), true); }); }); + + + it('should apply key and value transformers', function () { + var output = GetOutput('ps.log'); + var result = TableParser.parse(output, { + keyTransformer: function(key) { + return 'a' + key; + }, + valueTransformer: function(value) { + return value.join(' '); + } + }); + + var expectResult = [ + { + 'aPID': '692', + 'aTTY': 'ttys000', + 'aTIME': '0:00.06', + 'aCMD': 'login -pfl neekey /bin/bash -c exec -la bash /bin/bash' + }, + { + 'aPID': '49693', + 'aTTY': 'ttys000666', + 'aTIME': '0:00.06', + 'aCMD': '-bash' + }, + { + 'aPID': '', + 'aTTY': 'ttys000', + 'aTIME': '0:47.81', + 'aCMD': '/Users/neekey/Dropbox/nodejs/app/windTest/Redis/mac_linux/src/redis-server' + }, + { + 'aPID': '52300', + 'aTTY': 'ttys001', + 'aTIME': '0:00.05', + 'aCMD': 'login -pfl neekey /bin/bash -c exec -la bash /bin/bash' + } + ]; + + Assert.deepEqual(result, expectResult); + }) });