Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.
/ codec Public archive

Commit 039e512

Browse files
committed
Merge pull request #4 from Level/add/encode-ltgt
add .encodeLtgt
2 parents 8afbe2e + 3671b7c commit 039e512

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ var codec = new Codec(db.options);
3131

3232
Encode `batch` ops with given `opts`.
3333

34+
### #encodeLtgt(ltgt[, opts])
35+
36+
Encode the ltgt values of option object `ltgt` with given `opts`.
37+
3438
### #decodeKey(key[, opts])
3539

3640
Decode `key` with given `opts`.

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ Codec.prototype.encodeBatch = function(ops, opts){
5959
});
6060
};
6161

62+
var ltgtKeys = ['lt', 'gt', 'lte', 'gte', 'start', 'end'];
63+
64+
Codec.prototype.encodeLtgt = function(ltgt, opts){
65+
var self = this;
66+
var ret = {};
67+
Object.keys(ltgt).forEach(function(key){
68+
ret[key] = ltgtKeys.indexOf(key) > -1
69+
? self.encodeKey(ltgt[key], opts)
70+
: ltgt[key]
71+
});
72+
return ret;
73+
};
74+
6275
Codec.prototype.keyAsBuffer = function(opts){
6376
return this._keyEncoding(opts).buffer;
6477
};

test/ltgt.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var test = require('tape');
2+
var Codec = require('..');
3+
4+
test('encode ltgt', function(t){
5+
var ltgt = {
6+
start: '686579',
7+
lte: '686579'
8+
};
9+
var codec = new Codec({ keyEncoding: 'hex' });
10+
11+
var encoded = codec.encodeLtgt(ltgt);
12+
t.equal(encoded.start.toString(), 'hey');
13+
t.equal(encoded.lte.toString(), 'hey');
14+
15+
var encoded = codec.encodeLtgt(ltgt, { keyEncoding: 'json' });
16+
t.equal(encoded.start, '"686579"');
17+
t.equal(encoded.lte, '"686579"');
18+
19+
t.end();
20+
});
21+

0 commit comments

Comments
 (0)