This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use and run standard * standard --fix * standard: unexpected mix of && and || * standard: new Buffer() deprecated * standard: codec already defined * remove Makefile 🔥
- Loading branch information
1 parent
9476e58
commit 6e7a79f
Showing
10 changed files
with
270 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,104 @@ | ||
var encodings = require('./lib/encodings'); | ||
var encodings = require('./lib/encodings') | ||
|
||
module.exports = Codec; | ||
module.exports = Codec | ||
|
||
function Codec(opts){ | ||
this.opts = opts || {}; | ||
this.encodings = encodings; | ||
function Codec (opts) { | ||
this.opts = opts || {} | ||
this.encodings = encodings | ||
} | ||
|
||
Codec.prototype._encoding = function(encoding){ | ||
if (typeof encoding == 'string') encoding = encodings[encoding]; | ||
if (!encoding) encoding = encodings.id; | ||
return encoding; | ||
}; | ||
|
||
Codec.prototype._keyEncoding = function(opts, batchOpts){ | ||
return this._encoding(batchOpts && batchOpts.keyEncoding | ||
|| opts && opts.keyEncoding | ||
|| this.opts.keyEncoding); | ||
}; | ||
|
||
Codec.prototype._valueEncoding = function(opts, batchOpts){ | ||
return this._encoding( | ||
batchOpts && (batchOpts.valueEncoding || batchOpts.encoding) | ||
|| opts && (opts.valueEncoding || opts.encoding) | ||
|| (this.opts.valueEncoding || this.opts.encoding)); | ||
}; | ||
|
||
Codec.prototype.encodeKey = function(key, opts, batchOpts){ | ||
return this._keyEncoding(opts, batchOpts).encode(key); | ||
}; | ||
|
||
Codec.prototype.encodeValue = function(value, opts, batchOpts){ | ||
return this._valueEncoding(opts, batchOpts).encode(value); | ||
}; | ||
|
||
Codec.prototype.decodeKey = function(key, opts){ | ||
return this._keyEncoding(opts).decode(key); | ||
}; | ||
|
||
Codec.prototype.decodeValue = function(value, opts){ | ||
return this._valueEncoding(opts).decode(value); | ||
}; | ||
|
||
Codec.prototype.encodeBatch = function(ops, opts){ | ||
var self = this; | ||
|
||
return ops.map(function(_op){ | ||
Codec.prototype._encoding = function (encoding) { | ||
if (typeof encoding === 'string') encoding = encodings[encoding] | ||
if (!encoding) encoding = encodings.id | ||
return encoding | ||
} | ||
|
||
Codec.prototype._keyEncoding = function (opts, batchOpts) { | ||
return this._encoding((batchOpts && batchOpts.keyEncoding) || | ||
(opts && opts.keyEncoding) || | ||
this.opts.keyEncoding) | ||
} | ||
|
||
Codec.prototype._valueEncoding = function (opts, batchOpts) { | ||
return this._encoding((batchOpts && (batchOpts.valueEncoding || batchOpts.encoding)) || | ||
(opts && (opts.valueEncoding || opts.encoding)) || | ||
(this.opts.valueEncoding || this.opts.encoding)) | ||
} | ||
|
||
Codec.prototype.encodeKey = function (key, opts, batchOpts) { | ||
return this._keyEncoding(opts, batchOpts).encode(key) | ||
} | ||
|
||
Codec.prototype.encodeValue = function (value, opts, batchOpts) { | ||
return this._valueEncoding(opts, batchOpts).encode(value) | ||
} | ||
|
||
Codec.prototype.decodeKey = function (key, opts) { | ||
return this._keyEncoding(opts).decode(key) | ||
} | ||
|
||
Codec.prototype.decodeValue = function (value, opts) { | ||
return this._valueEncoding(opts).decode(value) | ||
} | ||
|
||
Codec.prototype.encodeBatch = function (ops, opts) { | ||
var self = this | ||
|
||
return ops.map(function (_op) { | ||
var op = { | ||
type: _op.type, | ||
key: self.encodeKey(_op.key, opts, _op) | ||
}; | ||
if (self.keyAsBuffer(opts, _op)) op.keyEncoding = 'binary'; | ||
if (_op.prefix) op.prefix = _op.prefix; | ||
} | ||
if (self.keyAsBuffer(opts, _op)) op.keyEncoding = 'binary' | ||
if (_op.prefix) op.prefix = _op.prefix | ||
if ('value' in _op) { | ||
op.value = self.encodeValue(_op.value, opts, _op); | ||
if (self.valueAsBuffer(opts, _op)) op.valueEncoding = 'binary'; | ||
op.value = self.encodeValue(_op.value, opts, _op) | ||
if (self.valueAsBuffer(opts, _op)) op.valueEncoding = 'binary' | ||
} | ||
return op; | ||
}); | ||
}; | ||
return op | ||
}) | ||
} | ||
|
||
var ltgtKeys = ['lt', 'gt', 'lte', 'gte', 'start', 'end']; | ||
var ltgtKeys = ['lt', 'gt', 'lte', 'gte', 'start', 'end'] | ||
|
||
Codec.prototype.encodeLtgt = function(ltgt){ | ||
var self = this; | ||
var ret = {}; | ||
Object.keys(ltgt).forEach(function(key){ | ||
Codec.prototype.encodeLtgt = function (ltgt) { | ||
var self = this | ||
var ret = {} | ||
Object.keys(ltgt).forEach(function (key) { | ||
ret[key] = ltgtKeys.indexOf(key) > -1 | ||
? self.encodeKey(ltgt[key], ltgt) | ||
: ltgt[key] | ||
}); | ||
return ret; | ||
}; | ||
}) | ||
return ret | ||
} | ||
|
||
Codec.prototype.createStreamDecoder = function(opts){ | ||
var self = this; | ||
Codec.prototype.createStreamDecoder = function (opts) { | ||
var self = this | ||
|
||
if (opts.keys && opts.values) { | ||
return function(key, value){ | ||
return function (key, value) { | ||
return { | ||
key: self.decodeKey(key, opts), | ||
value: self.decodeValue(value, opts) | ||
}; | ||
}; | ||
} | ||
} | ||
} else if (opts.keys) { | ||
return function(key) { | ||
return self.decodeKey(key, opts); | ||
}; | ||
return function (key) { | ||
return self.decodeKey(key, opts) | ||
} | ||
} else if (opts.values) { | ||
return function(_, value){ | ||
return self.decodeValue(value, opts); | ||
return function (_, value) { | ||
return self.decodeValue(value, opts) | ||
} | ||
} else { | ||
return function(){}; | ||
return function () {} | ||
} | ||
}; | ||
|
||
Codec.prototype.keyAsBuffer = function(opts){ | ||
return this._keyEncoding(opts).buffer; | ||
}; | ||
} | ||
|
||
Codec.prototype.valueAsBuffer = function(opts){ | ||
return this._valueEncoding(opts).buffer; | ||
}; | ||
Codec.prototype.keyAsBuffer = function (opts) { | ||
return this._keyEncoding(opts).buffer | ||
} | ||
|
||
Codec.prototype.valueAsBuffer = function (opts) { | ||
return this._valueEncoding(opts).buffer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
var test = require('tape'); | ||
var Codec = require('..'); | ||
var test = require('tape') | ||
var Codec = require('..') | ||
|
||
test('key as buffer', function(t){ | ||
var codec = new Codec({ keyEncoding: 'hex' }); | ||
t.ok(codec.keyAsBuffer({})); | ||
t.ok(codec.keyAsBuffer()); | ||
t.notOk(codec.keyAsBuffer({ keyEncoding: 'utf8' })); | ||
t.end(); | ||
}); | ||
|
||
test('value as buffer', function(t){ | ||
var codec = new Codec({ valueEncoding: 'hex' }); | ||
t.ok(codec.valueAsBuffer({})); | ||
t.ok(codec.valueAsBuffer()); | ||
t.notOk(codec.valueAsBuffer({ valueEncoding: 'utf8' })); | ||
t.end(); | ||
}); | ||
test('key as buffer', function (t) { | ||
var codec = new Codec({ keyEncoding: 'hex' }) | ||
t.ok(codec.keyAsBuffer({})) | ||
t.ok(codec.keyAsBuffer()) | ||
t.notOk(codec.keyAsBuffer({ keyEncoding: 'utf8' })) | ||
t.end() | ||
}) | ||
|
||
test('value as buffer', function (t) { | ||
var codec = new Codec({ valueEncoding: 'hex' }) | ||
t.ok(codec.valueAsBuffer({})) | ||
t.ok(codec.valueAsBuffer()) | ||
t.notOk(codec.valueAsBuffer({ valueEncoding: 'utf8' })) | ||
t.end() | ||
}) |
Oops, something went wrong.