Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit cefd6f4

Browse files
committed
Move Change getters/setters to prototype
1 parent 5b8cbb7 commit cefd6f4

File tree

2 files changed

+107
-81
lines changed

2 files changed

+107
-81
lines changed

lib/change.js

Lines changed: 83 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,95 +6,100 @@ var Attribute = require('./attribute');
66
var Protocol = require('./protocol');
77

88

9-
109
///--- API
1110

1211
function Change(options) {
1312
if (options) {
14-
if (typeof (options) !== 'object')
15-
throw new TypeError('options must be an object');
16-
if (options.operation && typeof (options.operation) !== 'string')
17-
throw new TypeError('options.operation must be a string');
13+
assert.object(options);
14+
assert.optionalString(options.operation);
1815
} else {
1916
options = {};
2017
}
2118

22-
var self = this;
2319
this._modification = false;
20+
this.operation = options.operation || options.type || 'add';
21+
this.modification = options.modification || {};
22+
}
23+
Object.defineProperties(Change.prototype, {
24+
operation: {
25+
get: function getOperation() {
26+
switch (this._operation) {
27+
case 0x00: return 'add';
28+
case 0x01: return 'delete';
29+
case 0x02: return 'replace';
30+
default:
31+
throw new Error('0x' + this._operation.toString(16) + ' is invalid');
32+
}
33+
},
34+
set: function setOperation(val) {
35+
assert.string(val);
36+
switch (val.toLowerCase()) {
37+
case 'add':
38+
this._operation = 0x00;
39+
break;
40+
case 'delete':
41+
this._operation = 0x01;
42+
break;
43+
case 'replace':
44+
this._operation = 0x02;
45+
break;
46+
default:
47+
throw new Error('Invalid operation type: 0x' + val.toString(16));
48+
}
49+
},
50+
configurable: false
51+
},
52+
modification: {
53+
get: function getModification() {
54+
return this._modification;
55+
},
56+
set: function setModification(val) {
57+
if (Attribute.isAttribute(val)) {
58+
this._modification = val;
59+
return;
60+
}
61+
// Does it have an attribute-like structure
62+
if (Object.keys(val).length == 2 &&
63+
typeof (val.type) === 'string' &&
64+
Array.isArray(val.vals)) {
65+
this._modification = new Attribute({
66+
type: val.type,
67+
vals: val.vals
68+
});
69+
return;
70+
}
2471

25-
this.__defineGetter__('operation', function () {
26-
switch (self._operation) {
27-
case 0x00: return 'add';
28-
case 0x01: return 'delete';
29-
case 0x02: return 'replace';
30-
default:
31-
throw new Error('0x' + self._operation.toString(16) + ' is invalid');
32-
}
33-
});
34-
this.__defineSetter__('operation', function (val) {
35-
if (typeof (val) !== 'string')
36-
throw new TypeError('operation must be a string');
37-
38-
switch (val.toLowerCase()) {
39-
case 'add':
40-
self._operation = 0x00;
41-
break;
42-
case 'delete':
43-
self._operation = 0x01;
44-
break;
45-
case 'replace':
46-
self._operation = 0x02;
47-
break;
48-
default:
49-
throw new Error('Invalid operation type: 0x' + val.toString(16));
50-
}
51-
});
52-
this.__defineGetter__('modification', function () {
53-
return self._modification;
54-
});
55-
this.__defineSetter__('modification', function (attr) {
56-
if (Attribute.isAttribute(attr)) {
57-
self._modification = attr;
58-
return;
59-
}
60-
// Does it have an attribute-like structure
61-
if (Object.keys(attr).length == 2 &&
62-
typeof (attr.type) === 'string' &&
63-
Array.isArray(attr.vals)) {
64-
self._modification = new Attribute({
65-
type: attr.type,
66-
vals: attr.vals
67-
});
68-
return;
69-
}
70-
71-
var keys = Object.keys(attr);
72-
if (keys.length > 1)
73-
throw new Error('Only one attribute per Change allowed');
72+
var keys = Object.keys(val);
73+
if (keys.length > 1) {
74+
throw new Error('Only one attribute per Change allowed');
75+
} else if (keys.length === 0) {
76+
return;
77+
}
7478

75-
keys.forEach(function (k) {
79+
var k = keys[0];
80+
console.log(keys);
7681
var _attr = new Attribute({type: k});
77-
if (Array.isArray(attr[k])) {
78-
attr[k].forEach(function (v) {
82+
if (Array.isArray(val[k])) {
83+
val[k].forEach(function (v) {
7984
_attr.addValue(v.toString());
8085
});
8186
} else {
82-
_attr.addValue(attr[k].toString());
87+
_attr.addValue(val[k].toString());
8388
}
84-
self._modification = _attr;
85-
});
86-
});
87-
this.__defineGetter__('json', function () {
88-
return {
89-
operation: self.operation,
90-
modification: self._modification ? self._modification.json : {}
91-
};
92-
});
93-
94-
this.operation = options.operation || options.type || 'add';
95-
this.modification = options.modification || {};
96-
}
97-
module.exports = Change;
89+
this._modification = _attr;
90+
},
91+
configurable: false
92+
},
93+
json: {
94+
get: function getJSON() {
95+
return {
96+
operation: this.operation,
97+
modification: this._modification ? this._modification.json : {}
98+
};
99+
},
100+
configurable: false
101+
}
102+
});
98103

99104
Change.isChange = function isChange(change) {
100105
if (!change || typeof (change) !== 'object') {
@@ -206,3 +211,8 @@ Change.prototype.toBer = function (ber) {
206211

207212
return ber;
208213
};
214+
215+
216+
///--- Exports
217+
218+
module.exports = Change;

test/change.test.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test('new no args', function (t) {
3232

3333
test('new with args', function (t) {
3434
var change = new Change({
35-
operation: 0x00,
35+
operation: 'add',
3636
modification: new Attribute({
3737
type: 'cn',
3838
vals: ['foo', 'bar']
@@ -50,19 +50,35 @@ test('new with args', function (t) {
5050
});
5151

5252

53+
test('validate fields', function (t) {
54+
var c = new Change();
55+
t.ok(c);
56+
t.throws(function () {
57+
c.operation = 'bogus';
58+
});
59+
t.throws(function () {
60+
c.modification = {too: 'many', fields: 'here'};
61+
});
62+
c.modification = {
63+
foo: ['bar', 'baz']
64+
};
65+
t.ok(c.modification);
66+
t.end();
67+
});
68+
69+
5370
test('GH-31 (multiple attributes per Change)', function (t) {
54-
try {
55-
t.notOk(new Change({
71+
t.throws(function () {
72+
var c = new Change({
5673
operation: 'replace',
5774
modification: {
5875
cn: 'foo',
5976
sn: 'bar'
6077
}
61-
}), 'should have thrown');
62-
} catch (e) {
63-
t.ok(e);
64-
t.end();
65-
}
78+
});
79+
t.notOk(c);
80+
});
81+
t.end();
6682
});
6783

6884

0 commit comments

Comments
 (0)