Fix style/lint issues

This commit is contained in:
Patrick Mooney 2015-10-27 21:21:25 -05:00
parent 05445cb6ec
commit ccfd821700
3 changed files with 42 additions and 67 deletions

1
.gitignore vendored
View File

@ -6,4 +6,3 @@ coverage
*.tar.*
*.tgz
.DS_Store
.idea

View File

@ -9,10 +9,6 @@ var Protocol = require('./protocol');
///--- API
/**
* @constructor
* @param [options={}]
*/
function Attribute(options) {
if (options) {
if (typeof (options) !== 'object')
@ -33,40 +29,33 @@ function Attribute(options) {
module.exports = Attribute;
Object.defineProperties(Attribute.prototype, {
buffers: {
get: function () {
var self = this;
return self._vals;
get: function getBuffers() {
return this._vals;
},
configurable: false
},
json: {
get: function() {
var self = this;
get: function getJson() {
return {
type: self.type,
vals: self.vals
type: this.type,
vals: this.vals
};
},
configurable: false
},
vals: {
get: function() {
var self = this;
var type = self.type;
var _vals = self._vals;
return _vals.map(function(v) {
return v.toString(_getEncodingFromType(type));
get: function getVals() {
var eType = _bufferEncoding(this.type);
return this._vals.map(function (v) {
return v.toString(eType);
});
},
set: function(vals) {
set: function setVals(vals) {
var self = this;
this._vals = [];
if (Array.isArray(vals)) {
vals.forEach(function(v) {
vals.forEach(function (v) {
self.addValue(v);
});
} else {
@ -75,11 +64,15 @@ Object.defineProperties(Attribute.prototype, {
},
configurable: false
}
});
Attribute.prototype.addValue = function (val) {
this._vals.push(_valueToBuffer(val, this.type));
Attribute.prototype.addValue = function addValue(val) {
if (Buffer.isBuffer(val)) {
this._vals.push(val);
} else {
this._vals.push(new Buffer(val + '', _bufferEncoding(this.type)));
}
};
@ -104,7 +97,7 @@ Attribute.compare = function compare(a, b) {
/* END JSSTYLED */
Attribute.prototype.parse = function (ber) {
Attribute.prototype.parse = function parse(ber) {
assert.ok(ber);
ber.readSequence();
@ -122,7 +115,7 @@ Attribute.prototype.parse = function (ber) {
};
Attribute.prototype.toBer = function (ber) {
Attribute.prototype.toBer = function toBer(ber) {
assert.ok(ber);
ber.startSequence();
@ -145,12 +138,17 @@ Attribute.prototype.toBer = function (ber) {
};
Attribute.prototype.toString = function () {
return JSON.stringify(this.json);
};
Attribute.toBer = function (attr, ber) {
return Attribute.prototype.toBer.call(attr, ber);
};
Attribute.isAttribute = function (attr) {
Attribute.isAttribute = function isAttribute(attr) {
if (!attr || typeof (attr) !== 'object') {
return false;
}
@ -170,30 +168,7 @@ Attribute.isAttribute = function (attr) {
};
Attribute.prototype.toString = function () {
return JSON.stringify(this.json);
};
/**
* Gets the encoding to use based on the type of the attribute
* @param {string} type
* @returns {string}
* @private
*/
function _getEncodingFromType(type) {
function _bufferEncoding(type) {
/* JSSTYLED */
return /;binary$/.test(type) ? 'base64' : 'utf8';
}
/**
* Converts a value to a buffer based on the given type
* @param {*} val
* @param {string} type
* @returns {Buffer}
* @private
*/
function _valueToBuffer(val, type) {
return Buffer.isBuffer(val) ?
val :
new Buffer(val + '', _getEncodingFromType(type));
}

View File

@ -4,6 +4,7 @@ var test = require('tape').test;
var asn1 = require('asn1');
///--- Globals
var BerReader = asn1.BerReader;
@ -33,20 +34,20 @@ test('new with args', function (t) {
});
t.ok(attr);
attr.addValue('baz');
t.throws(function() {
new Attribute('not an object');
});
t.throws(function() {
var typeThatIsNotAString = 1;
new Attribute({
type: typeThatIsNotAString
})
});
t.equal(attr.type, 'cn');
t.equal(attr.vals.length, 3);
t.equal(attr.vals[0], 'foo');
t.equal(attr.vals[1], 'bar');
t.equal(attr.vals[2], 'baz');
t.throws(function () {
attr = new Attribute('not an object');
});
t.throws(function () {
var typeThatIsNotAString = 1;
attr = new Attribute({
type: typeThatIsNotAString
});
});
t.end();
});
@ -89,7 +90,7 @@ test('parse', function (t) {
t.end();
});
test('parse - without 0x31', function(t) {
test('parse - without 0x31', function (t) {
var ber = new BerWriter;
ber.startSequence();
ber.writeString('sn');
@ -105,7 +106,7 @@ test('parse - without 0x31', function(t) {
t.end();
});
test('toString', function(t) {
test('toString', function (t) {
var attr = new Attribute({
type: 'foobar',
vals: ['asdf']
@ -154,11 +155,11 @@ test('compare', function (t) {
});
var notAnAttribute = 'this is not an attribute';
t.throws(function() {
t.throws(function () {
comp(a, notAnAttribute);
});
t.throws(function() {
comp(notAnAttribute, b)
t.throws(function () {
comp(notAnAttribute, b);
});
t.equal(comp(a, b), 0);