Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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'
}
]
```
17 changes: 14 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});