Skip to content

Commit a804c4f

Browse files
author
Ruben Bridgewater
committed
feat: Add some convenient methods
1 parent 658c79d commit a804c4f

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@ var Parser = require('redis-parser');
2121
var myParser = new Parser(options);
2222
```
2323

24-
### Possible options
24+
### Options
2525

2626
* `returnReply`: *function*; mandatory
2727
* `returnError`: *function*; mandatory
2828
* `returnFatalError`: *function*; optional, defaults to the returnError function
2929
* `returnBuffers`: *boolean*; optional, defaults to false
3030
* `stringNumbers`: *boolean*; optional, defaults to false
3131

32+
### Functions
33+
34+
* `reset()`: reset the parser to it's initial state
35+
* `setReturnBuffers(boolean)`: (JSParser only) set the returnBuffers option on/off without resetting the parser
36+
* `setStringNumbers(boolean)`: (JSParser only) set the stringNumbers option on/off without resetting the parser
37+
3238
### Example
3339

3440
```js

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## v.2.4.0 - xx Jan, 2017
22

3+
Features
4+
5+
- Added `reset` function to reset the parser to it's initial values
6+
- Added `setReturnBuffers` function to reset the returnBuffers option (Only for the JSParser)
7+
- Added `setStringNumbers` function to reset the stringNumbers option (Only for the JSParser)
8+
39
Bugfixes
410

511
- Parsing time for big nested arrays is now linear

lib/hiredis.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,13 @@ HiredisReplyParser.prototype.execute = function (data) {
4949
}
5050
}
5151

52+
/**
53+
* Reset the parser values to the initial state
54+
*
55+
* @returns {undefined}
56+
*/
57+
HiredisReplyParser.prototype.reset = function () {
58+
this.reader = new hiredis.Reader(this.options)
59+
}
60+
5261
module.exports = HiredisReplyParser

lib/parser.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,15 @@ function JavascriptRedisParser (options) {
340340
this.returnFatalError = options.returnFatalError || options.returnError
341341
this.returnReply = options.returnReply
342342
this.name = 'javascript'
343+
this.reset()
344+
}
345+
346+
/**
347+
* Reset the parser values to the initial state
348+
*
349+
* @returns {undefined}
350+
*/
351+
JavascriptRedisParser.prototype.reset = function () {
343352
this.offset = 0
344353
this.buffer = null
345354
this.bigStrSize = 0
@@ -350,6 +359,32 @@ function JavascriptRedisParser (options) {
350359
this.arrayPos = []
351360
}
352361

362+
/**
363+
* Set the returnBuffers option
364+
*
365+
* @param returnBuffers
366+
* @returns {undefined}
367+
*/
368+
JavascriptRedisParser.prototype.setReturnBuffers = function (returnBuffers) {
369+
if (typeof returnBuffers !== 'boolean') {
370+
throw new TypeError('The returnBuffers argument has to be a boolean')
371+
}
372+
this.optionReturnBuffers = returnBuffers
373+
}
374+
375+
/**
376+
* Set the stringNumbers option
377+
*
378+
* @param stringNumbers
379+
* @returns {undefined}
380+
*/
381+
JavascriptRedisParser.prototype.setStringNumbers = function (stringNumbers) {
382+
if (typeof stringNumbers !== 'boolean') {
383+
throw new TypeError('The stringNumbers argument has to be a boolean')
384+
}
385+
this.optionStringNumbers = stringNumbers
386+
}
387+
353388
/**
354389
* Concat a bulk string containing multiple chunks
355390
*

test/parsers.spec.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,79 @@ describe('parsers', function () {
5454
bla: 6
5555
})
5656
})
57+
58+
it('reset returnBuffers option', function () {
59+
var res = 'test'
60+
var replyCount = 0
61+
function checkReply (reply) {
62+
if (replyCount === 0) {
63+
assert.strictEqual(reply, res)
64+
} else {
65+
assert.strictEqual(reply.inspect(), Buffer(res).inspect())
66+
}
67+
replyCount++
68+
}
69+
var parser = JavascriptParser({
70+
returnReply: checkReply,
71+
returnError: returnError
72+
})
73+
parser.execute(new Buffer('+test\r\n'))
74+
parser.execute(new Buffer('+test'))
75+
parser.setReturnBuffers(true)
76+
assert.strictEqual(replyCount, 1)
77+
parser.execute(new Buffer('\r\n'))
78+
assert.strictEqual(replyCount, 2)
79+
})
80+
81+
it('reset returnBuffers option with wrong input', function () {
82+
var parser = JavascriptParser({
83+
returnReply: returnReply,
84+
returnError: returnError
85+
})
86+
assert.throws(function () {
87+
parser.setReturnBuffers(null)
88+
}, function (err) {
89+
assert.strictEqual(err.message, 'The returnBuffers argument has to be a boolean')
90+
assert(err instanceof TypeError)
91+
return true
92+
})
93+
})
94+
95+
it('reset stringNumbers option', function () {
96+
var res = 123
97+
var replyCount = 0
98+
function checkReply (reply) {
99+
if (replyCount === 0) {
100+
assert.strictEqual(reply, res)
101+
} else {
102+
assert.strictEqual(reply, String(res))
103+
}
104+
replyCount++
105+
}
106+
var parser = JavascriptParser({
107+
returnReply: checkReply,
108+
returnError: returnError
109+
})
110+
parser.execute(new Buffer(':123\r\n'))
111+
assert.strictEqual(replyCount, 1)
112+
parser.setStringNumbers(true)
113+
parser.execute(new Buffer(':123\r\n'))
114+
assert.strictEqual(replyCount, 2)
115+
})
116+
117+
it('reset stringNumbers option with wrong input', function () {
118+
var parser = JavascriptParser({
119+
returnReply: returnReply,
120+
returnError: returnError
121+
})
122+
assert.throws(function () {
123+
parser.setStringNumbers(null)
124+
}, function (err) {
125+
assert.strictEqual(err.message, 'The stringNumbers argument has to be a boolean')
126+
assert(err instanceof TypeError)
127+
return true
128+
})
129+
})
57130
})
58131

59132
parsers.forEach(function (Parser) {
@@ -100,6 +173,18 @@ describe('parsers', function () {
100173
replyCount = 0
101174
})
102175

176+
it('reset parser', function () {
177+
function checkReply (reply) {
178+
assert.strictEqual(reply, 'test')
179+
replyCount++
180+
}
181+
var parser = newParser(checkReply)
182+
parser.execute(new Buffer('$123\r\naaa'))
183+
parser.reset()
184+
parser.execute(new Buffer('+test\r\n'))
185+
assert.strictEqual(replyCount, 1)
186+
})
187+
103188
it('should not set the bufferOffset to a negative value', function (done) {
104189
if (Parser.name === 'HiredisReplyParser') {
105190
return this.skip()

0 commit comments

Comments
 (0)