Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit ccfd821

Browse files
committed
Fix style/lint issues
1 parent 05445cb commit ccfd821

File tree

3 files changed

+42
-67
lines changed

3 files changed

+42
-67
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ coverage
66
*.tar.*
77
*.tgz
88
.DS_Store
9-
.idea

lib/attribute.js

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ var Protocol = require('./protocol');
99

1010
///--- API
1111

12-
/**
13-
* @constructor
14-
* @param [options={}]
15-
*/
1612
function Attribute(options) {
1713
if (options) {
1814
if (typeof (options) !== 'object')
@@ -33,40 +29,33 @@ function Attribute(options) {
3329
module.exports = Attribute;
3430

3531
Object.defineProperties(Attribute.prototype, {
36-
3732
buffers: {
38-
get: function () {
39-
var self = this;
40-
return self._vals;
33+
get: function getBuffers() {
34+
return this._vals;
4135
},
4236
configurable: false
4337
},
44-
4538
json: {
46-
get: function() {
47-
var self = this;
39+
get: function getJson() {
4840
return {
49-
type: self.type,
50-
vals: self.vals
41+
type: this.type,
42+
vals: this.vals
5143
};
5244
},
5345
configurable: false
5446
},
55-
5647
vals: {
57-
get: function() {
58-
var self = this;
59-
var type = self.type;
60-
var _vals = self._vals;
61-
return _vals.map(function(v) {
62-
return v.toString(_getEncodingFromType(type));
48+
get: function getVals() {
49+
var eType = _bufferEncoding(this.type);
50+
return this._vals.map(function (v) {
51+
return v.toString(eType);
6352
});
6453
},
65-
set: function(vals) {
54+
set: function setVals(vals) {
6655
var self = this;
6756
this._vals = [];
6857
if (Array.isArray(vals)) {
69-
vals.forEach(function(v) {
58+
vals.forEach(function (v) {
7059
self.addValue(v);
7160
});
7261
} else {
@@ -75,11 +64,15 @@ Object.defineProperties(Attribute.prototype, {
7564
},
7665
configurable: false
7766
}
78-
7967
});
8068

81-
Attribute.prototype.addValue = function (val) {
82-
this._vals.push(_valueToBuffer(val, this.type));
69+
70+
Attribute.prototype.addValue = function addValue(val) {
71+
if (Buffer.isBuffer(val)) {
72+
this._vals.push(val);
73+
} else {
74+
this._vals.push(new Buffer(val + '', _bufferEncoding(this.type)));
75+
}
8376
};
8477

8578

@@ -104,7 +97,7 @@ Attribute.compare = function compare(a, b) {
10497
/* END JSSTYLED */
10598

10699

107-
Attribute.prototype.parse = function (ber) {
100+
Attribute.prototype.parse = function parse(ber) {
108101
assert.ok(ber);
109102

110103
ber.readSequence();
@@ -122,7 +115,7 @@ Attribute.prototype.parse = function (ber) {
122115
};
123116

124117

125-
Attribute.prototype.toBer = function (ber) {
118+
Attribute.prototype.toBer = function toBer(ber) {
126119
assert.ok(ber);
127120

128121
ber.startSequence();
@@ -145,12 +138,17 @@ Attribute.prototype.toBer = function (ber) {
145138
};
146139

147140

141+
Attribute.prototype.toString = function () {
142+
return JSON.stringify(this.json);
143+
};
144+
145+
148146
Attribute.toBer = function (attr, ber) {
149147
return Attribute.prototype.toBer.call(attr, ber);
150148
};
151149

152150

153-
Attribute.isAttribute = function (attr) {
151+
Attribute.isAttribute = function isAttribute(attr) {
154152
if (!attr || typeof (attr) !== 'object') {
155153
return false;
156154
}
@@ -170,30 +168,7 @@ Attribute.isAttribute = function (attr) {
170168
};
171169

172170

173-
Attribute.prototype.toString = function () {
174-
return JSON.stringify(this.json);
175-
};
176-
177-
/**
178-
* Gets the encoding to use based on the type of the attribute
179-
* @param {string} type
180-
* @returns {string}
181-
* @private
182-
*/
183-
function _getEncodingFromType(type) {
171+
function _bufferEncoding(type) {
184172
/* JSSTYLED */
185173
return /;binary$/.test(type) ? 'base64' : 'utf8';
186174
}
187-
188-
/**
189-
* Converts a value to a buffer based on the given type
190-
* @param {*} val
191-
* @param {string} type
192-
* @returns {Buffer}
193-
* @private
194-
*/
195-
function _valueToBuffer(val, type) {
196-
return Buffer.isBuffer(val) ?
197-
val :
198-
new Buffer(val + '', _getEncodingFromType(type));
199-
}

test/attribute.test.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var test = require('tape').test;
44

55
var asn1 = require('asn1');
66

7+
78
///--- Globals
89

910
var BerReader = asn1.BerReader;
@@ -33,20 +34,20 @@ test('new with args', function (t) {
3334
});
3435
t.ok(attr);
3536
attr.addValue('baz');
36-
t.throws(function() {
37-
new Attribute('not an object');
38-
});
39-
t.throws(function() {
40-
var typeThatIsNotAString = 1;
41-
new Attribute({
42-
type: typeThatIsNotAString
43-
})
44-
});
4537
t.equal(attr.type, 'cn');
4638
t.equal(attr.vals.length, 3);
4739
t.equal(attr.vals[0], 'foo');
4840
t.equal(attr.vals[1], 'bar');
4941
t.equal(attr.vals[2], 'baz');
42+
t.throws(function () {
43+
attr = new Attribute('not an object');
44+
});
45+
t.throws(function () {
46+
var typeThatIsNotAString = 1;
47+
attr = new Attribute({
48+
type: typeThatIsNotAString
49+
});
50+
});
5051
t.end();
5152
});
5253

@@ -89,7 +90,7 @@ test('parse', function (t) {
8990
t.end();
9091
});
9192

92-
test('parse - without 0x31', function(t) {
93+
test('parse - without 0x31', function (t) {
9394
var ber = new BerWriter;
9495
ber.startSequence();
9596
ber.writeString('sn');
@@ -105,7 +106,7 @@ test('parse - without 0x31', function(t) {
105106
t.end();
106107
});
107108

108-
test('toString', function(t) {
109+
test('toString', function (t) {
109110
var attr = new Attribute({
110111
type: 'foobar',
111112
vals: ['asdf']
@@ -154,11 +155,11 @@ test('compare', function (t) {
154155
});
155156
var notAnAttribute = 'this is not an attribute';
156157

157-
t.throws(function() {
158+
t.throws(function () {
158159
comp(a, notAnAttribute);
159160
});
160-
t.throws(function() {
161-
comp(notAnAttribute, b)
161+
t.throws(function () {
162+
comp(notAnAttribute, b);
162163
});
163164

164165
t.equal(comp(a, b), 0);

0 commit comments

Comments
 (0)