Skip to content

Commit 718eea9

Browse files
author
Ruben Bridgewater
committed
Implement parser name "auto" and choose that for unkown parsers
1 parent b749f3e commit 718eea9

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ new Parser(options);
3030
* `returnError`: *function*; mandatory
3131
* `returnFatalError`: *function*; optional, defaults to the returnError function
3232
* `returnBuffers`: *boolean*; optional, defaults to false
33-
* `name`: *javascript|hiredis*; optional, defaults to hiredis and falls back to the js parser if not available or if the stringNumbers option is choosen
33+
* `name`: *'javascript'|'hiredis'|'auto'|null*; optional, defaults to hiredis and falls back to the js parser if not available or if the stringNumbers option is choosen. Setting this to 'auto' or null is going to automatically determine what parser is available and chooses that one.
3434
* `stringNumbers`: *boolean*; optional, defaults to false. This is only available for the javascript parser at the moment!
3535

3636
### Example
@@ -55,8 +55,9 @@ var parser = new Parser({
5555
},
5656
returnFatalError: function (err) {
5757
lib.returnFatalError(err);
58-
}
59-
}); // This returns either a hiredis or the js parser instance depending on what's available
58+
},
59+
name: 'auto' // This returns either the hiredis or the js parser instance depending on what's available
60+
});
6061

6162
Library.prototype.streamHandler = function () {
6263
this.stream.on('data', function (buffer) {

changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v.1.3.0 - 27 Mar, 2016
2+
3+
Features
4+
5+
- Added `auto` as parser name option to check what parser is available
6+
- Non existing requested parsers falls back into auto mode instead of always choosing the JS parser
7+
18
## v.1.2.0 - 27 Mar, 2016
29

310
Features

lib/parser.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ function Parser (options) {
2020
throw new Error('Please provide all return functions while initiating the parser');
2121
}
2222

23-
/* istanbul ignore if: hiredis should always be installed while testing */
2423
if (options.name === 'hiredis') {
24+
/* istanbul ignore if: hiredis should always be installed while testing */
2525
if (!parsers.hiredis) {
2626
msg = 'You explicitly required the hiredis parser but hiredis is not installed. The JS parser is going to be returned instead.';
2727
} else if (options.stringNumbers) {
2828
msg = 'You explicitly required the hiredis parser in combination with the stringNumbers option. Only the JS parser can handle that and is choosen instead.';
2929
}
30-
} else if (options.name && !parsers[options.name]) {
31-
msg = 'The requested parser "' + options.name + '" is unkown and the JS parser is choosen instead.';
30+
} else if (options.name && !parsers[options.name] && options.name !== 'auto') {
31+
msg = 'The requested parser "' + options.name + '" is unkown and the default parser is choosen instead.';
3232
}
3333

3434
if (msg) {
3535
console.warn(new Error(msg).stack.replace('Error: ', 'Warning: '));
36-
options.name = 'javascript';
3736
}
3837

3938
options.name = options.name || 'hiredis';

test/parsers.spec.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ describe('parsers', function () {
2323
assert.strictEqual(parser.name, 'hiredis');
2424
});
2525

26+
it('auto parser', function () {
27+
var parser = new Parser({
28+
returnReply: returnReply,
29+
returnError: returnError,
30+
name: 'auto'
31+
});
32+
assert.strictEqual(parser.name, 'hiredis');
33+
});
34+
35+
it('auto parser v2', function () {
36+
var parser = new Parser({
37+
returnReply: returnReply,
38+
returnError: returnError,
39+
name: null
40+
});
41+
assert.strictEqual(parser.name, 'hiredis');
42+
});
43+
2644
it('fail for missing options', function () {
2745
assert.throws(function() {
2846
new Parser({
@@ -42,13 +60,14 @@ describe('parsers', function () {
4260
str += data;
4361
return '';
4462
});
45-
new Parser({
63+
var parser = new Parser({
4664
returnReply: returnReply,
4765
returnError: returnError,
4866
name: 'something_unknown'
4967
});
5068
unhookIntercept();
51-
assert(/^Warning: The requested parser "something_unknown" is unkown and the JS parser is choosen instead\.\n +at new Parser/.test(str), str);
69+
assert.strictEqual(parser.name, 'hiredis');
70+
assert(/^Warning: The requested parser "something_unknown" is unkown and the default parser is choosen instead\.\n +at new Parser/.test(str), str);
5271
});
5372

5473
it('hiredis and stringNumbers', function () {
@@ -57,13 +76,14 @@ describe('parsers', function () {
5776
str += data;
5877
return '';
5978
});
60-
new Parser({
79+
var parser = new Parser({
6180
returnReply: returnReply,
6281
returnError: returnError,
6382
name: 'hiredis',
6483
stringNumbers: true
6584
});
6685
unhookIntercept();
86+
assert.strictEqual(parser.name, 'javascript');
6787
assert(/^Warning: You explicitly required the hiredis parser in combination with the stringNumbers option. .+.\.\n +at new Parser/.test(str), str);
6888
});
6989

0 commit comments

Comments
 (0)