Improve DN formatting flexibility

- Add 'spaced' function to DN objects allowing toggle of inter-RDN when
  rendering to a string.  ('dc=test,dc=tld' vs 'dc=test, dc=tld')

- Detect RDN spacing when parsing DN.
This commit is contained in:
Patrick Mooney 2013-10-24 18:27:42 -05:00
parent e808aee99d
commit 0d12eef3ff
7 changed files with 33 additions and 9 deletions

View File

@ -53,11 +53,13 @@ function parse(name) {
var cur = 0; var cur = 0;
var len = name.length; var len = name.length;
var rdnSpaced = false;
function parseRdn() { function parseRdn() {
var rdn = new RDN(); var rdn = new RDN();
while (cur < len) { while (cur < len) {
trim(); var leading = trim();
rdnSpaced = rdnSpaced || (leading > 0);
var attr = parseAttrType(); var attr = parseAttrType();
trim(); trim();
if (cur >= len || name[cur++] !== '=') if (cur >= len || name[cur++] !== '=')
@ -77,8 +79,12 @@ function parse(name) {
function trim() { function trim() {
while ((cur < len) && isWhitespace(name[cur])) var count = 0;
while ((cur < len) && isWhitespace(name[cur])) {
++cur; ++cur;
count++;
}
return count;
} }
function parseAttrType() { function parseAttrType() {
@ -179,7 +185,7 @@ function parse(name) {
} }
} }
return new DN(rdns); return new DN(rdns).spaced(rdnSpaced);
} }
@ -196,6 +202,7 @@ function DN(rdns) {
}); });
this.rdns = rdns.slice(); this.rdns = rdns.slice();
this.rdnSpaced = true;
this.__defineGetter__('length', function () { this.__defineGetter__('length', function () {
return this.rdns.length; return this.rdns.length;
@ -208,7 +215,12 @@ DN.prototype.toString = function () {
this.rdns.forEach(function (rdn) { this.rdns.forEach(function (rdn) {
_dn.push(rdn.toString()); _dn.push(rdn.toString());
}); });
return _dn.join(', '); return _dn.join(this.rdnSpaced ? ', ' : ',');
};
DN.prototype.spaced = function (spaces) {
this.rdnSpaced = (spaces === false) ? false : true;
return this;
}; };

View File

@ -131,3 +131,15 @@ test('case insensitive attribute names GH-20', function (t) {
t.ok(dn1.equals(dn.parse('cn=foo, DC=bar'))); t.ok(dn1.equals(dn.parse('cn=foo, DC=bar')));
t.end(); t.end();
}); });
test('rdn spacing', function (t) {
var dn1 = dn.parse('cn=foo,dc=bar');
var dn2 = dn.parse('cn=foo, dc=bar');
t.ok(dn1.equals(dn2));
t.equals(dn1.toString(), 'cn=foo,dc=bar');
t.equals(dn2.toString(), 'cn=foo, dc=bar');
t.equals(dn1.spaced().toString(), 'cn=foo, dc=bar');
t.equals(dn2.spaced(false).toString(), 'cn=foo,dc=bar');
t.end();
});

View File

@ -51,7 +51,7 @@ test('new with args', function (t) {
test('parse', function (t) { test('parse', function (t) {
var ber = new BerWriter(); var ber = new BerWriter();
ber.writeString('cn=foo,o=test'); ber.writeString('cn=foo, o=test');
ber.startSequence(); ber.startSequence();

View File

@ -45,7 +45,7 @@ test('new with args', function (t) {
test('parse', function (t) { test('parse', function (t) {
var ber = new BerWriter(); var ber = new BerWriter();
ber.writeString('cn=foo,o=test'); ber.writeString('cn=foo, o=test');
ber.startSequence(); ber.startSequence();
ber.writeString('sn'); ber.writeString('sn');

View File

@ -45,7 +45,7 @@ test('new with args', function (t) {
test('parse', function (t) { test('parse', function (t) {
var ber = new BerWriter(); var ber = new BerWriter();
ber.writeString('cn=foo,o=test'); ber.writeString('cn=foo, o=test');
ber.writeString('cn=foo2'); ber.writeString('cn=foo2');
ber.writeBoolean(true); ber.writeBoolean(true);

View File

@ -55,7 +55,7 @@ test('new with args', function (t) {
test('parse', function (t) { test('parse', function (t) {
var ber = new BerWriter(); var ber = new BerWriter();
ber.writeString('cn=foo,o=test'); ber.writeString('cn=foo, o=test');
ber.startSequence(); ber.startSequence();
ber.startSequence(); ber.startSequence();

View File

@ -58,7 +58,7 @@ test('parse', function (t) {
}); });
var ber = new BerWriter(); var ber = new BerWriter();
ber.writeString('cn=foo,o=test'); ber.writeString('cn=foo, o=test');
ber.writeEnumeration(0); ber.writeEnumeration(0);
ber.writeEnumeration(0); ber.writeEnumeration(0);
ber.writeInt(1); ber.writeInt(1);