@@ -9,10 +9,6 @@ var Protocol = require('./protocol');
9
9
10
10
///--- API
11
11
12
- /**
13
- * @constructor
14
- * @param [options={ }]
15
- */
16
12
function Attribute ( options ) {
17
13
if ( options ) {
18
14
if ( typeof ( options ) !== 'object' )
@@ -33,40 +29,33 @@ function Attribute(options) {
33
29
module . exports = Attribute ;
34
30
35
31
Object . defineProperties ( Attribute . prototype , {
36
-
37
32
buffers : {
38
- get : function ( ) {
39
- var self = this ;
40
- return self . _vals ;
33
+ get : function getBuffers ( ) {
34
+ return this . _vals ;
41
35
} ,
42
36
configurable : false
43
37
} ,
44
-
45
38
json : {
46
- get : function ( ) {
47
- var self = this ;
39
+ get : function getJson ( ) {
48
40
return {
49
- type : self . type ,
50
- vals : self . vals
41
+ type : this . type ,
42
+ vals : this . vals
51
43
} ;
52
44
} ,
53
45
configurable : false
54
46
} ,
55
-
56
47
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 ) ;
63
52
} ) ;
64
53
} ,
65
- set : function ( vals ) {
54
+ set : function setVals ( vals ) {
66
55
var self = this ;
67
56
this . _vals = [ ] ;
68
57
if ( Array . isArray ( vals ) ) {
69
- vals . forEach ( function ( v ) {
58
+ vals . forEach ( function ( v ) {
70
59
self . addValue ( v ) ;
71
60
} ) ;
72
61
} else {
@@ -75,11 +64,15 @@ Object.defineProperties(Attribute.prototype, {
75
64
} ,
76
65
configurable : false
77
66
}
78
-
79
67
} ) ;
80
68
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
+ }
83
76
} ;
84
77
85
78
@@ -104,7 +97,7 @@ Attribute.compare = function compare(a, b) {
104
97
/* END JSSTYLED */
105
98
106
99
107
- Attribute . prototype . parse = function ( ber ) {
100
+ Attribute . prototype . parse = function parse ( ber ) {
108
101
assert . ok ( ber ) ;
109
102
110
103
ber . readSequence ( ) ;
@@ -122,7 +115,7 @@ Attribute.prototype.parse = function (ber) {
122
115
} ;
123
116
124
117
125
- Attribute . prototype . toBer = function ( ber ) {
118
+ Attribute . prototype . toBer = function toBer ( ber ) {
126
119
assert . ok ( ber ) ;
127
120
128
121
ber . startSequence ( ) ;
@@ -145,12 +138,17 @@ Attribute.prototype.toBer = function (ber) {
145
138
} ;
146
139
147
140
141
+ Attribute . prototype . toString = function ( ) {
142
+ return JSON . stringify ( this . json ) ;
143
+ } ;
144
+
145
+
148
146
Attribute . toBer = function ( attr , ber ) {
149
147
return Attribute . prototype . toBer . call ( attr , ber ) ;
150
148
} ;
151
149
152
150
153
- Attribute . isAttribute = function ( attr ) {
151
+ Attribute . isAttribute = function isAttribute ( attr ) {
154
152
if ( ! attr || typeof ( attr ) !== 'object' ) {
155
153
return false ;
156
154
}
@@ -170,30 +168,7 @@ Attribute.isAttribute = function (attr) {
170
168
} ;
171
169
172
170
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 ) {
184
172
/* JSSTYLED */
185
173
return / ; b i n a r y $ / . test ( type ) ? 'base64' : 'utf8' ;
186
174
}
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
- }
0 commit comments