1
1
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
2
3
3
4
+ var assert = require ( 'assert-plus' ) ;
5
+
6
+
7
+ ///--- Helpers
4
8
5
9
function invalidDN ( name ) {
6
10
var e = new Error ( ) ;
@@ -9,18 +13,26 @@ function invalidDN(name) {
9
13
return e ;
10
14
}
11
15
12
-
13
16
function isAlphaNumeric ( c ) {
14
17
var re = / [ A - Z a - z 0 - 9 ] / ;
15
18
return re . test ( c ) ;
16
19
}
17
20
18
-
19
21
function isWhitespace ( c ) {
20
22
var re = / \s / ;
21
23
return re . test ( c ) ;
22
24
}
23
25
26
+ function repeatChar ( c , n ) {
27
+ var out = '' ;
28
+ var max = n ? n : 0 ;
29
+ for ( var i = 0 ; i < max ; i ++ )
30
+ out += c ;
31
+ return out ;
32
+ }
33
+
34
+ ///--- API
35
+
24
36
function RDN ( obj ) {
25
37
var self = this ;
26
38
this . attrs = { } ;
@@ -32,12 +44,10 @@ function RDN(obj) {
32
44
}
33
45
}
34
46
47
+ RDN . prototype . set = function rdnSet ( name , value , opts ) {
48
+ assert . string ( name , 'name (string) required' ) ;
49
+ assert . string ( value , 'value (string) required' ) ;
35
50
36
- RDN . prototype . set = function set ( name , value , opts ) {
37
- if ( typeof ( name ) !== 'string' )
38
- throw new TypeError ( 'name (string) required' ) ;
39
- if ( typeof ( value ) !== 'string' )
40
- throw new TypeError ( 'value (string) required' ) ;
41
51
var self = this ;
42
52
var lname = name . toLowerCase ( ) ;
43
53
this . attrs [ lname ] = {
@@ -52,8 +62,7 @@ RDN.prototype.set = function set(name, value, opts) {
52
62
}
53
63
} ;
54
64
55
-
56
- RDN . prototype . equals = function equals ( rdn ) {
65
+ RDN . prototype . equals = function rdnEquals ( rdn ) {
57
66
if ( typeof ( rdn ) !== 'object' )
58
67
return false ;
59
68
@@ -79,13 +88,10 @@ RDN.prototype.equals = function equals(rdn) {
79
88
* Convert RDN to string according to specified formatting options.
80
89
* (see: DN.format for option details)
81
90
*/
82
- RDN . prototype . format = function format ( options ) {
83
- if ( options ) {
84
- if ( typeof ( options ) !== 'object' )
85
- throw new TypeError ( 'options must be an object' ) ;
86
- } else {
87
- options = { } ;
88
- }
91
+ RDN . prototype . format = function rdnFormat ( options ) {
92
+ assert . optionalObject ( options , 'options must be an object' ) ;
93
+ options = options || { } ;
94
+
89
95
var self = this ;
90
96
var str = '' ;
91
97
@@ -154,8 +160,7 @@ RDN.prototype.format = function format(options) {
154
160
return str ;
155
161
} ;
156
162
157
-
158
- RDN . prototype . toString = function toString ( ) {
163
+ RDN . prototype . toString = function rdnToString ( ) {
159
164
return this . format ( ) ;
160
165
} ;
161
166
@@ -314,26 +319,18 @@ function parse(name) {
314
319
}
315
320
316
321
317
-
318
- ///--- API
319
-
320
-
321
322
function DN ( rdns ) {
322
- if ( ! Array . isArray ( rdns ) )
323
- throw new TypeError ( 'rdns ([object]) required' ) ;
324
- rdns . forEach ( function ( rdn ) {
325
- if ( typeof ( rdn ) !== 'object' )
326
- throw new TypeError ( 'rdns ([object]) required' ) ;
327
- } ) ;
323
+ assert . optionalArrayOfObject ( rdns , '[object] required' ) ;
328
324
329
- this . rdns = rdns . slice ( ) ;
325
+ this . rdns = rdns ? rdns . slice ( ) : [ ] ;
330
326
this . _format = { } ;
331
-
332
- this . __defineGetter__ ( 'length' , function ( ) {
333
- return this . rdns . length ;
334
- } ) ;
335
327
}
336
-
328
+ Object . defineProperties ( DN . prototype , {
329
+ length : {
330
+ get : function getLength ( ) { return this . rdns . length ; } ,
331
+ configurable : false
332
+ }
333
+ } ) ;
337
334
338
335
/**
339
336
* Convert DN to string according to specified formatting options.
@@ -358,21 +355,11 @@ function DN(rdns) {
358
355
* - upperName: RDN names will be uppercased instead of lowercased.
359
356
* - skipSpace: Disable trailing space after RDN separators
360
357
*/
361
- DN . prototype . format = function ( options ) {
362
- if ( options ) {
363
- if ( typeof ( options ) !== 'object' )
364
- throw new TypeError ( 'options must be an object' ) ;
365
- } else {
366
- options = this . _format ;
367
- }
358
+ DN . prototype . format = function dnFormat ( options ) {
359
+ assert . optionalObject ( options , 'options must be an object' ) ;
360
+ options = options || this . _format ;
361
+
368
362
var str = '' ;
369
- function repeatChar ( c , n ) {
370
- var out = '' ;
371
- var max = n ? n : 0 ;
372
- for ( var i = 0 ; i < max ; i ++ )
373
- out += c ;
374
- return out ;
375
- }
376
363
this . rdns . forEach ( function ( rdn ) {
377
364
var rdnString = rdn . format ( options ) ;
378
365
if ( str . length !== 0 ) {
@@ -390,22 +377,19 @@ DN.prototype.format = function (options) {
390
377
return str ;
391
378
} ;
392
379
393
-
394
380
/**
395
381
* Set default string formatting options.
396
382
*/
397
383
DN . prototype . setFormat = function setFormat ( options ) {
398
- if ( typeof ( options ) !== ' object')
399
- throw new TypeError ( 'options must be an object' ) ;
384
+ assert . object ( options , ' options must be an object') ;
385
+
400
386
this . _format = options ;
401
387
} ;
402
388
403
-
404
- DN . prototype . toString = function ( ) {
389
+ DN . prototype . toString = function dnToString ( ) {
405
390
return this . format ( ) ;
406
391
} ;
407
392
408
-
409
393
DN . prototype . parentOf = function parentOf ( dn ) {
410
394
if ( typeof ( dn ) !== 'object' )
411
395
dn = parse ( dn ) ;
@@ -425,20 +409,17 @@ DN.prototype.parentOf = function parentOf(dn) {
425
409
return true ;
426
410
} ;
427
411
428
-
429
412
DN . prototype . childOf = function childOf ( dn ) {
430
413
if ( typeof ( dn ) !== 'object' )
431
414
dn = parse ( dn ) ;
432
415
return dn . parentOf ( this ) ;
433
416
} ;
434
417
435
-
436
418
DN . prototype . isEmpty = function isEmpty ( ) {
437
419
return ( this . rdns . length === 0 ) ;
438
420
} ;
439
421
440
-
441
- DN . prototype . equals = function ( dn ) {
422
+ DN . prototype . equals = function dnEquals ( dn ) {
442
423
if ( typeof ( dn ) !== 'object' )
443
424
dn = parse ( dn ) ;
444
425
@@ -453,8 +434,7 @@ DN.prototype.equals = function (dn) {
453
434
return true ;
454
435
} ;
455
436
456
-
457
- DN . prototype . parent = function ( ) {
437
+ DN . prototype . parent = function dnParent ( ) {
458
438
if ( this . rdns . length !== 0 ) {
459
439
var save = this . rdns . shift ( ) ;
460
440
var dn = new DN ( this . rdns ) ;
@@ -465,47 +445,38 @@ DN.prototype.parent = function () {
465
445
return null ;
466
446
} ;
467
447
468
-
469
- DN . prototype . clone = function ( ) {
448
+ DN . prototype . clone = function dnClone ( ) {
470
449
var dn = new DN ( this . rdns ) ;
471
450
dn . _format = this . _format ;
472
451
return dn ;
473
452
} ;
474
453
475
-
476
- DN . prototype . reverse = function ( ) {
454
+ DN . prototype . reverse = function dnReverse ( ) {
477
455
this . rdns . reverse ( ) ;
478
456
return this ;
479
457
} ;
480
458
481
-
482
- DN . prototype . pop = function ( ) {
459
+ DN . prototype . pop = function dnPop ( ) {
483
460
return this . rdns . pop ( ) ;
484
461
} ;
485
462
486
-
487
- DN . prototype . push = function ( rdn ) {
488
- if ( typeof ( rdn ) !== 'object' )
489
- throw new TypeError ( 'rdn (RDN) required' ) ;
463
+ DN . prototype . push = function dnPush ( rdn ) {
464
+ assert . object ( rdn , 'rdn (RDN) required' ) ;
490
465
491
466
return this . rdns . push ( rdn ) ;
492
467
} ;
493
468
494
-
495
- DN . prototype . shift = function ( ) {
469
+ DN . prototype . shift = function dnShift ( ) {
496
470
return this . rdns . shift ( ) ;
497
471
} ;
498
472
499
-
500
- DN . prototype . unshift = function ( rdn ) {
501
- if ( typeof ( rdn ) !== 'object' )
502
- throw new TypeError ( 'rdn (RDN) required' ) ;
473
+ DN . prototype . unshift = function dnUnshift ( rdn ) {
474
+ assert . object ( rdn , 'rdn (RDN) required' ) ;
503
475
504
476
return this . rdns . unshift ( rdn ) ;
505
477
} ;
506
478
507
-
508
- DN . isDN = function ( dn ) {
479
+ DN . isDN = function isDN ( dn ) {
509
480
if ( ! dn || typeof ( dn ) !== 'object' ) {
510
481
return false ;
511
482
}
@@ -523,11 +494,7 @@ DN.isDN = function (dn) {
523
494
///--- Exports
524
495
525
496
module . exports = {
526
-
527
497
parse : parse ,
528
-
529
498
DN : DN ,
530
-
531
499
RDN : RDN
532
-
533
500
} ;
0 commit comments