2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2014-07-16 19:53:13 +00:00
|
|
|
var assert = require('assert-plus');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
var Attribute = require('./attribute');
|
|
|
|
var Protocol = require('./protocol');
|
|
|
|
|
|
|
|
|
|
|
|
///--- API
|
|
|
|
|
|
|
|
function Change(options) {
|
|
|
|
if (options) {
|
2015-10-31 14:37:58 +00:00
|
|
|
assert.object(options);
|
|
|
|
assert.optionalString(options.operation);
|
2011-08-04 20:32:01 +00:00
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this._modification = false;
|
2015-10-31 14:37:58 +00:00
|
|
|
this.operation = options.operation || options.type || 'add';
|
|
|
|
this.modification = options.modification || {};
|
|
|
|
}
|
|
|
|
Object.defineProperties(Change.prototype, {
|
|
|
|
operation: {
|
|
|
|
get: function getOperation() {
|
|
|
|
switch (this._operation) {
|
|
|
|
case 0x00: return 'add';
|
|
|
|
case 0x01: return 'delete';
|
|
|
|
case 0x02: return 'replace';
|
|
|
|
default:
|
|
|
|
throw new Error('0x' + this._operation.toString(16) + ' is invalid');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set: function setOperation(val) {
|
|
|
|
assert.string(val);
|
|
|
|
switch (val.toLowerCase()) {
|
|
|
|
case 'add':
|
|
|
|
this._operation = 0x00;
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
this._operation = 0x01;
|
|
|
|
break;
|
|
|
|
case 'replace':
|
|
|
|
this._operation = 0x02;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Invalid operation type: 0x' + val.toString(16));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
modification: {
|
|
|
|
get: function getModification() {
|
|
|
|
return this._modification;
|
|
|
|
},
|
|
|
|
set: function setModification(val) {
|
|
|
|
if (Attribute.isAttribute(val)) {
|
|
|
|
this._modification = val;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Does it have an attribute-like structure
|
|
|
|
if (Object.keys(val).length == 2 &&
|
|
|
|
typeof (val.type) === 'string' &&
|
|
|
|
Array.isArray(val.vals)) {
|
|
|
|
this._modification = new Attribute({
|
|
|
|
type: val.type,
|
|
|
|
vals: val.vals
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2012-02-18 08:15:52 +00:00
|
|
|
|
2015-10-31 14:37:58 +00:00
|
|
|
var keys = Object.keys(val);
|
|
|
|
if (keys.length > 1) {
|
|
|
|
throw new Error('Only one attribute per Change allowed');
|
|
|
|
} else if (keys.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-08 21:31:00 +00:00
|
|
|
|
2015-10-31 14:37:58 +00:00
|
|
|
var k = keys[0];
|
|
|
|
console.log(keys);
|
2011-08-04 21:25:12 +00:00
|
|
|
var _attr = new Attribute({type: k});
|
2015-10-31 14:37:58 +00:00
|
|
|
if (Array.isArray(val[k])) {
|
|
|
|
val[k].forEach(function (v) {
|
2011-08-04 21:25:12 +00:00
|
|
|
_attr.addValue(v.toString());
|
|
|
|
});
|
|
|
|
} else {
|
2015-10-31 14:37:58 +00:00
|
|
|
_attr.addValue(val[k].toString());
|
2011-08-04 21:25:12 +00:00
|
|
|
}
|
2015-10-31 14:37:58 +00:00
|
|
|
this._modification = _attr;
|
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
json: {
|
|
|
|
get: function getJSON() {
|
|
|
|
return {
|
|
|
|
operation: this.operation,
|
|
|
|
modification: this._modification ? this._modification.json : {}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
}
|
|
|
|
});
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2014-06-24 19:14:17 +00:00
|
|
|
Change.isChange = function isChange(change) {
|
|
|
|
if (!change || typeof (change) !== 'object') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((change instanceof Change) ||
|
|
|
|
((typeof (change.toBer) === 'function') &&
|
|
|
|
(change.modification !== undefined) &&
|
|
|
|
(change.operation !== undefined))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Change.compare = function (a, b) {
|
2014-06-24 19:14:17 +00:00
|
|
|
if (!Change.isChange(a) || !Change.isChange(b))
|
2011-08-08 15:03:00 +00:00
|
|
|
throw new TypeError('can only compare Changes');
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
if (a.operation < b.operation)
|
|
|
|
return -1;
|
|
|
|
if (a.operation > b.operation)
|
|
|
|
return 1;
|
2011-08-08 15:03:00 +00:00
|
|
|
|
|
|
|
return Attribute.compare(a.modification, b.modification);
|
|
|
|
};
|
|
|
|
|
2014-07-16 19:53:13 +00:00
|
|
|
/**
|
|
|
|
* Apply a Change to properties of an object.
|
|
|
|
*
|
|
|
|
* @param {Object} change the change to apply.
|
|
|
|
* @param {Object} obj the object to apply it to.
|
|
|
|
* @param {Boolean} scalar convert single-item arrays to scalars. Default: false
|
|
|
|
*/
|
|
|
|
Change.apply = function apply(change, obj, scalar) {
|
|
|
|
assert.string(change.operation);
|
|
|
|
assert.string(change.modification.type);
|
|
|
|
assert.ok(Array.isArray(change.modification.vals));
|
|
|
|
assert.object(obj);
|
|
|
|
|
|
|
|
var type = change.modification.type;
|
|
|
|
var vals = change.modification.vals;
|
|
|
|
var data = obj[type];
|
|
|
|
if (data !== undefined) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
data = [data];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data = [];
|
|
|
|
}
|
|
|
|
switch (change.operation) {
|
|
|
|
case 'replace':
|
|
|
|
if (vals.length === 0) {
|
|
|
|
// replace empty is a delete
|
|
|
|
delete obj[type];
|
|
|
|
return obj;
|
|
|
|
} else {
|
|
|
|
data = vals;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'add':
|
|
|
|
// add only new unique entries
|
|
|
|
var newValues = vals.filter(function (entry) {
|
|
|
|
return (data.indexOf(entry) === -1);
|
|
|
|
});
|
|
|
|
data = data.concat(newValues);
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
data = data.filter(function (entry) {
|
|
|
|
return (vals.indexOf(entry) === -1);
|
|
|
|
});
|
|
|
|
if (data.length === 0) {
|
|
|
|
// Erase the attribute if empty
|
|
|
|
delete obj[type];
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (scalar && data.length === 1) {
|
|
|
|
// store single-value outputs as scalars, if requested
|
|
|
|
obj[type] = data[0];
|
|
|
|
} else {
|
|
|
|
obj[type] = data;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2011-08-08 15:03:00 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Change.prototype.parse = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.readSequence();
|
|
|
|
this._operation = ber.readEnumeration();
|
2011-08-04 21:25:12 +00:00
|
|
|
this._modification = new Attribute();
|
|
|
|
this._modification.parse(ber);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Change.prototype.toBer = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.startSequence();
|
|
|
|
ber.writeEnumeration(this._operation);
|
2011-08-04 21:25:12 +00:00
|
|
|
ber = this._modification.toBer(ber);
|
2011-08-04 20:32:01 +00:00
|
|
|
ber.endSequence();
|
|
|
|
|
|
|
|
return ber;
|
|
|
|
};
|
2015-10-31 14:37:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- Exports
|
|
|
|
|
|
|
|
module.exports = Change;
|