Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Standard (#29)
Browse files Browse the repository at this point in the history
* use and run standard

* standard --fix

* standard: unexpected mix of && and ||

* standard: new Buffer() deprecated

* standard: codec already defined

* remove Makefile 🔥
  • Loading branch information
ralphtheninja authored May 12, 2018
1 parent 9476e58 commit 6e7a79f
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 291 deletions.
6 changes: 0 additions & 6 deletions Makefile

This file was deleted.

156 changes: 77 additions & 79 deletions index.js
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
}
50 changes: 21 additions & 29 deletions lib/encodings.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
exports.utf8 = exports['utf-8'] = {
encode: function(data){
return isBinary(data)
? data
: String(data);
encode: function (data) {
return isBinary(data) ? data : String(data)
},
decode: identity,
buffer: false,
type: 'utf8'
};
}

exports.json = {
encode: JSON.stringify,
decode: JSON.parse,
buffer: false,
type: 'json'
};
}

exports.binary = {
encode: function(data){
return isBinary(data)
? data
: new Buffer(data);
encode: function (data) {
return isBinary(data) ? data : Buffer.from(data)
},
decode: identity,
buffer: true,
type: 'binary'
};
}

exports.none = {
encode: identity,
decode: identity,
buffer: false,
type: 'id'
};
}

exports.id = exports.none;
exports.id = exports.none

var bufferEncodings = [
'hex',
Expand All @@ -44,29 +40,25 @@ var bufferEncodings = [
'ucs-2',
'utf16le',
'utf-16le'
];
]

bufferEncodings.forEach(function(type){
bufferEncodings.forEach(function (type) {
exports[type] = {
encode: function(data){
return isBinary(data)
? data
: new Buffer(data, type);
encode: function (data) {
return isBinary(data) ? data : Buffer.from(data, type)
},
decode: function(buffer){
return buffer.toString(type);
decode: function (buffer) {
return buffer.toString(type)
},
buffer: true,
type: type
};
});
}
})

function identity(value){
return value;
function identity (value) {
return value
}

function isBinary(data){
return data === undefined
|| data === null
|| Buffer.isBuffer(data);
function isBinary (data) {
return data === undefined || data === null || Buffer.isBuffer(data)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"description": "Levelup's encoding logic",
"license": "MIT",
"devDependencies": {
"standard": "^11.0.1",
"tape": "^4.3.0"
},
"main": "index.js",
"typings": "index.d.ts",
"scripts": {
"test": "make test"
"test": "tape test/*.js && standard"
},
"dependencies": {}
}
33 changes: 16 additions & 17 deletions test/as-buffer.js
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()
})
Loading

0 comments on commit 6e7a79f

Please sign in to comment.