16
16
*
17
17
* @param {dataValues.DecimalValue } amount Numeric string or a number.
18
18
* @param {string } unit A unit identifier. Must not be empty, use "1" for unit-less quantities.
19
- * @param {dataValues.DecimalValue } upperBound The upper bound of the quantity, inclusive.
20
- * @param {dataValues.DecimalValue } lowerBound The lower bound of the quantity, inclusive.
19
+ * @param {dataValues.DecimalValue|null } [ upperBound] The upper bound of the quantity, inclusive.
20
+ * @param {dataValues.DecimalValue|null } [ lowerBound] The lower bound of the quantity, inclusive.
21
21
*
22
22
* @throws {Error } if constructor parameters are invalid.
23
23
*/
24
24
var SELF
25
25
= dv . QuantityValue
26
26
= util . inherit ( 'DvQuantityValue' , PARENT , function ( amount , unit , upperBound , lowerBound ) {
27
- if ( ! amount || ! ( amount instanceof dv . DecimalValue ) ) {
27
+ if ( ! amount || ! ( amount instanceof dv . DecimalValue ) ) {
28
28
throw new Error ( 'amount needs to be a DecimalValue object' ) ;
29
29
}
30
30
31
- if ( typeof unit !== 'string' ) {
32
- throw new Error ( 'unit must be of type string' ) ;
33
- } else if ( unit === '' ) {
34
- throw new Error ( 'unit can not be an empty string (use "1" for unit-less quantities)' ) ;
31
+ if ( typeof unit !== 'string' || unit === '' ) {
32
+ throw new Error ( 'unit must be a non-empty string (use "1" for unit-less quantities)' ) ;
35
33
}
36
34
37
- if ( ! lowerBound || ! ( lowerBound instanceof dv . DecimalValue ) ) {
38
- throw new Error ( 'lowerBound needs to be a DecimalValue object' ) ;
39
- }
40
-
41
- if ( ! upperBound || ! ( upperBound instanceof dv . DecimalValue ) ) {
42
- throw new Error ( 'upperBound needs to be a DecimalValue object' ) ;
35
+ // Both can be null/undefined. But if one is set, both must be set.
36
+ if ( upperBound || lowerBound ) {
37
+ if ( ! ( upperBound instanceof dv . DecimalValue )
38
+ || ! ( lowerBound instanceof dv . DecimalValue )
39
+ ) {
40
+ throw new Error ( 'upperBound and lowerBound must both be defined or both undefined' ) ;
41
+ }
43
42
}
44
43
45
44
this . _amount = amount ;
46
45
this . _unit = unit ;
47
- this . _lowerBound = lowerBound ;
48
- this . _upperBound = upperBound ;
46
+ this . _lowerBound = lowerBound || null ;
47
+ this . _upperBound = upperBound || null ;
49
48
} ,
50
49
{
51
50
/**
@@ -61,13 +60,13 @@ var SELF
61
60
_unit : null ,
62
61
63
62
/**
64
- * @property {dataValues.DecimalValue }
63
+ * @property {dataValues.DecimalValue|null }
65
64
* @private
66
65
*/
67
66
_lowerBound : null ,
68
67
69
68
/**
70
- * @property {dataValues.DecimalValue }
69
+ * @property {dataValues.DecimalValue|null }
71
70
* @private
72
71
*/
73
72
_upperBound : null ,
@@ -78,7 +77,7 @@ var SELF
78
77
* @return {string }
79
78
*/
80
79
getSortKey : function ( ) {
81
- return this . getAmount ( ) . getValue ( ) ;
80
+ return this . _amount . getValue ( ) ;
82
81
} ,
83
82
84
83
/**
@@ -134,10 +133,12 @@ var SELF
134
133
return false ;
135
134
}
136
135
137
- return this . getAmount ( ) . equals ( that . getAmount ( ) )
138
- && this . getUnit ( ) === that . getUnit ( )
139
- && this . getLowerBound ( ) . equals ( that . getLowerBound ( ) )
140
- && this . getUpperBound ( ) . equals ( that . getUpperBound ( ) ) ;
136
+ return this . _amount . equals ( that . _amount )
137
+ && this . _unit === that . _unit
138
+ && ( this . _upperBound === that . _upperBound
139
+ || ( this . _upperBound && this . _upperBound . equals ( that . _upperBound ) ) )
140
+ && ( this . _lowerBound === that . _lowerBound
141
+ || ( this . _lowerBound && this . _lowerBound . equals ( that . _lowerBound ) ) ) ;
141
142
} ,
142
143
143
144
/**
@@ -146,12 +147,15 @@ var SELF
146
147
* @return {Object }
147
148
*/
148
149
toJSON : function ( ) {
149
- return {
150
- amount : this . getAmount ( ) . toJSON ( ) ,
151
- unit : this . getUnit ( ) ,
152
- upperBound : this . getUpperBound ( ) . toJSON ( ) ,
153
- lowerBound : this . getLowerBound ( ) . toJSON ( )
150
+ var json = {
151
+ amount : this . _amount . toJSON ( ) ,
152
+ unit : this . _unit
154
153
} ;
154
+ if ( this . _upperBound && this . _lowerBound ) {
155
+ json . upperBound = this . _upperBound . toJSON ( ) ;
156
+ json . lowerBound = this . _lowerBound . toJSON ( ) ;
157
+ }
158
+ return json ;
155
159
}
156
160
} ) ;
157
161
@@ -164,8 +168,8 @@ SELF.newFromJSON = function( json ) {
164
168
return new SELF (
165
169
new dv . DecimalValue ( json . amount ) ,
166
170
json . unit ,
167
- new dv . DecimalValue ( json . upperBound ) ,
168
- new dv . DecimalValue ( json . lowerBound )
171
+ json . upperBound ? new dv . DecimalValue ( json . upperBound ) : null ,
172
+ json . lowerBound ? new dv . DecimalValue ( json . lowerBound ) : null
169
173
) ;
170
174
} ;
171
175
0 commit comments