Sorted attributes/changes in add/modify requests

This commit is contained in:
Mark Cavage 2011-08-08 10:03:00 -05:00
parent 7706cd5e4e
commit 05be0dd966
4 changed files with 55 additions and 4 deletions

View File

@ -40,6 +40,23 @@ function Attribute(options) {
module.exports = Attribute; module.exports = Attribute;
Attribute.compare = function(a, b) {
if (!(a instanceof Attribute) || !(b instanceof Attribute))
throw new TypeError('can only compare Attributes');
if (a.type < b.type) return -1;
if (a.type > b.type) return 1;
if (a.vals.length < b.vals.length) return -1;
if (a.vals.length > b.vals.length) return 1;
for (var i = 0; i < a.vals.length; i++) {
if (a.vals[i] < b.vals[i]) return -1;
if (a.vals[i] > b.vals[i]) return 1;
}
return 0;
};
Attribute.prototype.addValue = function(val) { Attribute.prototype.addValue = function(val) {
if (typeof(val) !== 'string') if (typeof(val) !== 'string')
throw new TypeError('val (string) required'); throw new TypeError('val (string) required');
@ -57,10 +74,9 @@ Attribute.prototype.parse = function(ber) {
if (ber.readSequence(Protocol.LBER_SET)) { if (ber.readSequence(Protocol.LBER_SET)) {
var end = ber.offset + ber.length; var end = ber.offset + ber.length;
while (ber.offset < end) { while (ber.offset < end)
var val = ber.readString(); this.vals.push(ber.readString().toLowerCase());
this.vals.push(val);
}
} }
return true; return true;

View File

@ -79,6 +79,17 @@ function Change(options) {
module.exports = Change; module.exports = Change;
Change.compare = function(a, b) {
if (!(a instanceof Change) || !(b instanceof Change))
throw new TypeError('can only compare Changes');
if (a.operation < b.operation) return -1;
if (a.operation > b.operation) return 1;
return Attribute.compare(a.modification, b.modification);
};
Change.prototype.parse = function(ber) { Change.prototype.parse = function(ber) {
assert.ok(ber); assert.ok(ber);

View File

@ -66,6 +66,7 @@ AddRequest.prototype._parse = function(ber) {
this.attributes.push(a); this.attributes.push(a);
} }
this.attributes.sort(Attribute.compare);
return true; return true;
}; };
@ -96,3 +97,25 @@ AddRequest.prototype._json = function(j) {
return j; return j;
}; };
AddRequest.prototype.indexOf = function(attr) {
if (!attr || typeof(attr) !== 'string')
throw new TypeError('attr (string) required');
for (var i = 0; i < this.attributes.length; i++)
if (req.attributes[i].type === attr)
return i;
return -1;
};
AddRequest.prototype.attributeNames = function() {
var attrs = [];
for (var i = 0; i < this.attributes.length; i++)
attrs.push[this.attributes[i].type];
return attrs;
};

View File

@ -61,6 +61,7 @@ ModifyRequest.prototype._parse = function(ber) {
this.changes.push(c); this.changes.push(c);
} }
this.changes.sort(Change.compare);
return true; return true;
}; };