@@ -6,95 +6,100 @@ var Attribute = require('./attribute');
6
6
var Protocol = require ( './protocol' ) ;
7
7
8
8
9
-
10
9
///--- API
11
10
12
11
function Change ( options ) {
13
12
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 ) ;
18
15
} else {
19
16
options = { } ;
20
17
}
21
18
22
- var self = this ;
23
19
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
+ }
24
71
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
+ }
74
78
75
- keys . forEach ( function ( k ) {
79
+ var k = keys [ 0 ] ;
80
+ console . log ( keys ) ;
76
81
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 ) {
79
84
_attr . addValue ( v . toString ( ) ) ;
80
85
} ) ;
81
86
} else {
82
- _attr . addValue ( attr [ k ] . toString ( ) ) ;
87
+ _attr . addValue ( val [ k ] . toString ( ) ) ;
83
88
}
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
+ } ) ;
98
103
99
104
Change . isChange = function isChange ( change ) {
100
105
if ( ! change || typeof ( change ) !== 'object' ) {
@@ -206,3 +211,8 @@ Change.prototype.toBer = function (ber) {
206
211
207
212
return ber ;
208
213
} ;
214
+
215
+
216
+ ///--- Exports
217
+
218
+ module . exports = Change ;
0 commit comments