Comparing version 0.9.5 to 0.9.6
@@ -10,13 +10,11 @@ | ||
function Attribute(options) { | ||
if (options) { | ||
if (typeof (options) !== 'object') throw new TypeError('options must be an object') | ||
if (options.type && typeof (options.type) !== 'string') throw new TypeError('options.type must be a string') | ||
} else options = {} | ||
} else { | ||
options = {} | ||
} | ||
this.type = options.type || '' | ||
this._vals = [] | ||
if (options.vals !== undefined && options.vals !== null) this.vals = options.vals | ||
} | ||
@@ -27,3 +25,2 @@ | ||
Object.defineProperties(Attribute.prototype, { | ||
buffers: { | ||
@@ -35,3 +32,2 @@ get: function getBuffers() { | ||
}, | ||
json: { | ||
@@ -46,3 +42,2 @@ get: function getJson() { | ||
}, | ||
vals: { | ||
@@ -68,3 +63,2 @@ get: function getVals() { | ||
} | ||
}) | ||
@@ -74,3 +68,2 @@ | ||
Attribute.prototype.toString = () => JSON.stringify(this.json) | ||
Attribute.prototype.addValue = function addValue(val) { | ||
@@ -80,3 +73,2 @@ if (Buffer.isBuffer(val)) this._vals.push(val) | ||
} | ||
Attribute.prototype.parse = function parse(ber) { | ||
@@ -95,3 +87,2 @@ assert.ok(ber) | ||
} | ||
Attribute.prototype.toBer = function toBer(ber) { | ||
@@ -114,3 +105,2 @@ assert.ok(ber) | ||
Attribute.toBer = (attr, ber) => Attribute.prototype.toBer.call(attr, ber) | ||
Attribute.isAttribute = function isAttribute(attr) { | ||
@@ -127,3 +117,2 @@ if (!attr || typeof (attr) !== 'object') return false | ||
} | ||
Attribute.compare = function compare(a, b) { | ||
@@ -130,0 +119,0 @@ if (!(Attribute.isAttribute(a)) || !(Attribute.isAttribute(b))) |
const | ||
assert = require('assert-plus'), | ||
assert = require('assert-plus'), | ||
Attribute = require('./attribute'), | ||
Protocol = require('./protocol') | ||
Protocol = require('./protocol') | ||
@@ -10,14 +10,9 @@ //# API | ||
function Change(options) { | ||
if (options) { | ||
assert.object(options); | ||
assert.optionalString(options.operation); | ||
} else { | ||
options = {}; | ||
} | ||
this._modification = false; | ||
this.operation = options.operation || options.type || 'add'; | ||
this.modification = options.modification || {}; | ||
assert.object(options) | ||
assert.optionalString(options.operation) | ||
} else options = {} | ||
this._modification = false | ||
this.operation = options.operation || options.type || 'add' | ||
this.modification = options.modification || {} | ||
} | ||
@@ -29,23 +24,17 @@ | ||
switch (this._operation) { | ||
case 0x00: return 'add'; | ||
case 0x01: return 'delete'; | ||
case 0x02: return 'replace'; | ||
case 0x00: return 'add' | ||
case 0x01: return 'delete' | ||
case 0x02: return 'replace' | ||
default: | ||
throw new Error('0x' + this._operation.toString(16) + ' is invalid'); | ||
throw new Error('0x' + this._operation.toString(16) + ' is invalid') | ||
} | ||
}, | ||
set: function setOperation(val) { | ||
assert.string(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; | ||
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)); | ||
throw new Error('Invalid operation type: 0x' + val.toString(16)) | ||
} | ||
@@ -57,34 +46,26 @@ }, | ||
get: function getModification() { | ||
return this._modification; | ||
return this._modification | ||
}, | ||
set: function setModification(val) { | ||
if (Attribute.isAttribute(val)) { | ||
this._modification = val; | ||
return; | ||
this._modification = val | ||
return | ||
} | ||
if (Object.keys(val).length == 2 && | ||
typeof (val.type) === 'string' && | ||
Array.isArray(val.vals)) { | ||
if (typeof (val.type) === 'string' && | ||
Object.keys(val).length == 2 && | ||
Array.isArray(val.vals)) { | ||
this._modification = new Attribute({ | ||
type: val.type, | ||
vals: val.vals | ||
}); | ||
return; | ||
}) | ||
return | ||
} | ||
var keys = Object.keys(val); | ||
if (keys.length > 1) { | ||
throw new Error('Only one attribute per Change allowed'); | ||
} else if (keys.length === 0) { | ||
return; | ||
} | ||
var k = keys[0]; | ||
var _attr = new Attribute({type: k}); | ||
if (Array.isArray(val[k])) { | ||
val[k].forEach(function (v) { | ||
_attr.addValue(v.toString()); | ||
}); | ||
} else { | ||
_attr.addValue(val[k].toString()); | ||
} | ||
this._modification = _attr; | ||
let keys = Object.keys(val) | ||
if (keys.length > 1) throw new Error('Only one attribute per Change allowed') | ||
else if (keys.length === 0) return | ||
let k = keys[0] | ||
let _attr = new Attribute({type: k}) | ||
if (Array.isArray(val[k])) val[k].forEach((v) => _attr.addValue(v.toString())) | ||
else _attr.addValue(val[k].toString()) | ||
this._modification = _attr | ||
}, | ||
@@ -96,118 +77,81 @@ configurable: false | ||
return { | ||
operation: this.operation, | ||
operation: this.operation, | ||
modification: this._modification ? this._modification.json : {} | ||
}; | ||
} | ||
}, | ||
configurable: false | ||
} | ||
}); | ||
}) | ||
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; | ||
}; | ||
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 | ||
} | ||
Change.compare = (a, b) => { | ||
if (!Change.isChange(a) || !Change.isChange(b)) | ||
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); | ||
}; | ||
if (!Change.isChange(a) || !Change.isChange(b)) 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.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 = []; | ||
} | ||
assert.string(change.operation) | ||
assert.string(change.modification.type) | ||
assert.ok(Array.isArray(change.modification.vals)) | ||
assert.object(obj) | ||
let | ||
type = change.modification.type, | ||
vals = change.modification.vals, | ||
data = obj[type] | ||
if (data !== undefined) if (!Array.isArray(data)) data = [data] | ||
else data = [] | ||
switch (change.operation) { | ||
case 'replace': | ||
if (vals.length === 0) { | ||
delete obj[type]; | ||
return obj; | ||
delete obj[type] | ||
return obj | ||
} else { | ||
data = vals; | ||
data = vals | ||
} | ||
break; | ||
break | ||
case 'add': | ||
var newValues = vals.filter((entry) => { | ||
return (data.indexOf(entry) === -1); | ||
}); | ||
data = data.concat(newValues); | ||
break; | ||
let newValues = vals.filter((entry) => (data.indexOf(entry) === -1)) | ||
data = data.concat(newValues) | ||
break | ||
case 'delete': | ||
data = data.filter((entry) => { | ||
return (vals.indexOf(entry) === -1); | ||
}); | ||
data = data.filter((entry) => (vals.indexOf(entry) === -1)) | ||
if (data.length === 0) { | ||
delete obj[type]; | ||
return obj; | ||
delete obj[type] | ||
return obj | ||
} | ||
break; | ||
default: | ||
break; | ||
break | ||
default: break | ||
} | ||
if (scalar && data.length === 1) obj[type] = data[0] | ||
else obj[type] = data | ||
return obj | ||
} | ||
if (scalar && data.length === 1) { | ||
obj[type] = data[0]; | ||
} else { | ||
obj[type] = data; | ||
} | ||
return obj; | ||
}; | ||
Change.prototype.parse = (ber) => { | ||
assert.ok(ber) | ||
ber.readSequence() | ||
this._operation = ber.readEnumeration() | ||
this._modification = new Attribute() | ||
this._modification.parse(ber) | ||
return true | ||
} | ||
assert.ok(ber); | ||
ber.readSequence(); | ||
this._operation = ber.readEnumeration(); | ||
this._modification = new Attribute(); | ||
this._modification.parse(ber); | ||
return true; | ||
}; | ||
Change.prototype.toBer = (ber) => { | ||
assert.ok(ber); | ||
ber.startSequence(); | ||
ber.writeEnumeration(this._operation); | ||
ber = this._modification.toBer(ber); | ||
ber.endSequence(); | ||
assert.ok(ber) | ||
ber.startSequence() | ||
ber.writeEnumeration(this._operation) | ||
ber = this._modification.toBer(ber) | ||
ber.endSequence() | ||
return ber | ||
} | ||
return ber; | ||
}; | ||
//# Exports | ||
module.exports = Change; | ||
module.exports = Change |
@@ -38,28 +38,19 @@ | ||
RDN.prototype.set = function rdnSet(name, value, opts) { | ||
assert.string(name, 'name (string) required') | ||
assert.string(value, 'value (string) required') | ||
let self = this, lname = name.toLowerCase() | ||
this.attrs[lname] = { value: value, name: name } | ||
if (opts && typeof (opts) === 'object') Object.keys(opts).forEach((k) => { | ||
if (k !== 'value') self.attrs[lname][k] = opts[k] | ||
}) | ||
} | ||
RDN.prototype.equals = function rdnEquals(rdn) { | ||
if (typeof (rdn) !== 'object') return false | ||
let | ||
ourKeys = Object.keys(this.attrs), | ||
theirKeys = Object.keys(rdn.attrs) | ||
if (ourKeys.length !== theirKeys.length) return false | ||
ourKeys.sort() | ||
theirKeys.sort() | ||
for (let i = 0; i < ourKeys.length; i++) { | ||
@@ -69,12 +60,8 @@ if (ourKeys[i] !== theirKeys[i]) return false | ||
} return true | ||
} | ||
RDN.prototype.format = function rdnFormat(options) { | ||
assert.optionalObject(options, 'options must be an object') | ||
options = options || {} | ||
let self = this, str = '' | ||
function escapeValue(val, forceQuote) { | ||
@@ -92,7 +79,5 @@ let out = '', cur = 0, len = val.length, quoted = false | ||
} | ||
function sortParsed(a, b) { | ||
return self.attrs[a].order - self.attrs[b].order | ||
} | ||
function sortStandard(a, b) { | ||
@@ -103,8 +88,5 @@ let nameCompare = a.localeCompare(b) | ||
} | ||
let keys = Object.keys(this.attrs) | ||
if (options.keepOrder) keys.sort(sortParsed) | ||
else keys.sort(sortStandard) | ||
keys.forEach((key) => { | ||
@@ -120,5 +102,3 @@ let attr = self.attrs[key] | ||
}) | ||
return str | ||
} | ||
@@ -131,7 +111,4 @@ | ||
function parse(name) { | ||
if (typeof (name) !== 'string') throw new TypeError('name (string) required') | ||
let cur = 0, len = name.length | ||
function parseRdn() { | ||
@@ -154,3 +131,2 @@ let rdn = new RDN(), order = 0 | ||
} | ||
function trim() { | ||
@@ -164,3 +140,2 @@ let count = 0 | ||
} | ||
function parseAttrType() { | ||
@@ -177,3 +152,2 @@ let beg = cur | ||
} | ||
function parseAttrValue(opts) { | ||
@@ -186,3 +160,5 @@ if (cur < len && name[cur] == '#') { | ||
return parseQuotedAttrValue() | ||
} else return parseStringAttrValue() | ||
} else { | ||
return parseStringAttrValue() | ||
} | ||
} | ||
@@ -206,3 +182,2 @@ | ||
} | ||
function parseStringAttrValue() { | ||
@@ -218,11 +193,8 @@ let beg = cur, str = '', esc = -1 | ||
} | ||
function atTerminator() { | ||
return (cur < len && (name[cur] === ',' || name[cur] === ';' || name[cur] === '+')) | ||
} | ||
let rdns = [] | ||
if (len === 0) return new DN(rdns) | ||
rdns.push(parseRdn()) | ||
while (cur < len) { | ||
@@ -236,5 +208,3 @@ if (name[cur] === ',' || name[cur] === ';') { | ||
} | ||
return new DN(rdns) | ||
} | ||
@@ -280,6 +250,4 @@ | ||
DN.prototype.format = function dnFormat(options) { | ||
assert.optionalObject(options, 'options must be an object') | ||
options = options || this._format | ||
let str = '' | ||
@@ -294,5 +262,3 @@ this.rdns.forEach((rdn) => { | ||
}) | ||
return str | ||
} | ||
@@ -310,8 +276,5 @@ | ||
DN.prototype.parentOf = function parentOf(dn) { | ||
if (typeof (dn) !== 'object') dn = parse(dn) | ||
if (this.rdns.length >= dn.rdns.length) return false | ||
let diff = dn.rdns.length - this.rdns.length | ||
for (let i = this.rdns.length - 1; i >= 0; i--) { | ||
@@ -321,5 +284,3 @@ let myRDN = this.rdns[i], theirRDN = dn.rdns[i + diff] | ||
} | ||
return true | ||
} | ||
@@ -337,15 +298,10 @@ | ||
DN.prototype.equals = function dnEquals(dn) { | ||
if (typeof (dn) !== 'object') dn = parse(dn) | ||
if (this.rdns.length !== dn.rdns.length) return false | ||
for (let i = 0; i < this.rdns.length; i++) | ||
if (!this.rdns[i].equals(dn.rdns[i])) return false | ||
return true | ||
} | ||
DN.prototype.parent = function dnParent() { | ||
if (this.rdns.length !== 0) { | ||
@@ -356,5 +312,3 @@ let save = this.rdns.shift(), dn = new DN(this.rdns) | ||
} | ||
return null | ||
} | ||
@@ -361,0 +315,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"author": "Andrea Leone <git@andrealeone.xyz>", | ||
"version": "0.9.5", | ||
"version": "0.9.6", | ||
"license": "WTFPL", | ||
@@ -8,0 +8,0 @@ "repository": { |
210727
6137