Skip to content

Commit e51be3e

Browse files
authored
Merge pull request #102 from wmde/nullBounds
Allow QuantityValue without bounds
2 parents 34a78ba + fadb3ab commit e51be3e

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

src/values/QuantityValue.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,35 @@
1616
*
1717
* @param {dataValues.DecimalValue} amount Numeric string or a number.
1818
* @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.
2121
*
2222
* @throws {Error} if constructor parameters are invalid.
2323
*/
2424
var SELF
2525
= dv.QuantityValue
2626
= util.inherit( 'DvQuantityValue', PARENT, function( amount, unit, upperBound, lowerBound ) {
27-
if( !amount || !( amount instanceof dv.DecimalValue ) ) {
27+
if ( !amount || !( amount instanceof dv.DecimalValue ) ) {
2828
throw new Error( 'amount needs to be a DecimalValue object' );
2929
}
3030

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)' );
3533
}
3634

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+
}
4342
}
4443

4544
this._amount = amount;
4645
this._unit = unit;
47-
this._lowerBound = lowerBound;
48-
this._upperBound = upperBound;
46+
this._lowerBound = lowerBound || null;
47+
this._upperBound = upperBound || null;
4948
},
5049
{
5150
/**
@@ -61,13 +60,13 @@ var SELF
6160
_unit: null,
6261

6362
/**
64-
* @property {dataValues.DecimalValue}
63+
* @property {dataValues.DecimalValue|null}
6564
* @private
6665
*/
6766
_lowerBound: null,
6867

6968
/**
70-
* @property {dataValues.DecimalValue}
69+
* @property {dataValues.DecimalValue|null}
7170
* @private
7271
*/
7372
_upperBound: null,
@@ -78,7 +77,7 @@ var SELF
7877
* @return {string}
7978
*/
8079
getSortKey: function() {
81-
return this.getAmount().getValue();
80+
return this._amount.getValue();
8281
},
8382

8483
/**
@@ -134,10 +133,12 @@ var SELF
134133
return false;
135134
}
136135

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 ) ) );
141142
},
142143

143144
/**
@@ -146,12 +147,15 @@ var SELF
146147
* @return {Object}
147148
*/
148149
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
154153
};
154+
if ( this._upperBound && this._lowerBound ) {
155+
json.upperBound = this._upperBound.toJSON();
156+
json.lowerBound = this._lowerBound.toJSON();
157+
}
158+
return json;
155159
}
156160
} );
157161

@@ -164,8 +168,8 @@ SELF.newFromJSON = function( json ) {
164168
return new SELF(
165169
new dv.DecimalValue( json.amount ),
166170
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
169173
);
170174
};
171175

tests/src/values/QuantityValue.tests.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ define( [
3636
return [
3737
[new dv.DecimalValue( 0 ), 'some unit', new dv.DecimalValue( 0 ), new dv.DecimalValue( 0 )],
3838
[new dv.DecimalValue( 0 ), 'some unit', new dv.DecimalValue( -1 ), new dv.DecimalValue( 1 )],
39-
[new dv.DecimalValue( 5 ), 'some unit', new dv.DecimalValue( 4 ), new dv.DecimalValue( 6 )]
39+
[new dv.DecimalValue( 5 ), 'some unit', new dv.DecimalValue( 4 ), new dv.DecimalValue( 6 )],
40+
[new dv.DecimalValue( 6 ), 'some unit', null, null],
41+
[new dv.DecimalValue( 7 ), 'some unit']
4042
];
4143
}
4244

0 commit comments

Comments
 (0)