File tree 3 files changed +46
-2
lines changed
3 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ class Attribute {
28
28
if ( options . type && typeof ( options . type ) !== 'string' ) {
29
29
throw TypeError ( 'options.type must be a string' )
30
30
}
31
- this . # type = options . type || ''
31
+ this . type = options . type || ''
32
32
33
33
const values = options . values || options . vals || [ ]
34
34
if ( options . vals ) {
@@ -261,6 +261,35 @@ class Attribute {
261
261
return result
262
262
}
263
263
264
+ /**
265
+ * Given an object of attribute types mapping to attribute values, construct
266
+ * a set of Attributes.
267
+ *
268
+ * @param {object } obj Each key is an attribute type, and each value is an
269
+ * attribute value or set of values.
270
+ *
271
+ * @returns {Attribute[] }
272
+ *
273
+ * @throws If an attribute cannot be constructed correctly.
274
+ */
275
+ static fromObject ( obj ) {
276
+ const attributes = [ ]
277
+ for ( const [ key , value ] of Object . entries ( obj ) ) {
278
+ if ( Array . isArray ( value ) === true ) {
279
+ attributes . push ( new Attribute ( {
280
+ type : key ,
281
+ values : value
282
+ } ) )
283
+ } else {
284
+ attributes . push ( new Attribute ( {
285
+ type : key ,
286
+ values : [ value ]
287
+ } ) )
288
+ }
289
+ }
290
+ return attributes
291
+ }
292
+
264
293
/**
265
294
* Determine if an object represents an {@link Attribute}.
266
295
*
Original file line number Diff line number Diff line change @@ -301,6 +301,21 @@ tap.test('#fromBer', t => {
301
301
t . end ( )
302
302
} )
303
303
304
+ tap . test ( '#fromObject' , t => {
305
+ t . test ( 'handles basic object' , async t => {
306
+ const attrs = Attribute . fromObject ( {
307
+ foo : [ 'foo' ] ,
308
+ bar : 'bar' ,
309
+ 'baz;binary' : Buffer . from ( [ 0x00 ] )
310
+ } )
311
+ for ( const attr of attrs ) {
312
+ t . equal ( Object . prototype . toString . call ( attr ) , '[object LdapAttribute]' )
313
+ }
314
+ } )
315
+
316
+ t . end ( )
317
+ } )
318
+
304
319
tap . test ( '#isAttribute' , t => {
305
320
t . test ( 'rejects non-object' , async t => {
306
321
t . equal ( Attribute . isAttribute ( 42 ) , false )
Original file line number Diff line number Diff line change 7
7
"name" : " @ldapjs/attribute" ,
8
8
"homepage" : " https://github.com/ldapjs/attribute" ,
9
9
"description" : " API for handling LDAP entry attributes" ,
10
- "version" : " 1.0.0-rc.4 " ,
10
+ "version" : " 1.0.0-rc.5 " ,
11
11
"license" : " MIT" ,
12
12
"repository" : {
13
13
"type" : " git" ,
You can’t perform that action at this time.
0 commit comments