Skip to content

Commit

Permalink
Made TNumber nonparametric and moved range/units to NumberProperty, see
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Sep 11, 2017
1 parent 9f6c643 commit d7388b3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
68 changes: 65 additions & 3 deletions js/NumberProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ define( function( require ) {
'use strict';

// modules
var inherit = require( 'PHET_CORE/inherit' );
var Property = require( 'AXON/Property' );
var axon = require( 'AXON/axon' );
var Property = require( 'AXON/Property' );
var TNumber = require( 'ifphetio!PHET_IO/types/TNumber' );
var inherit = require( 'PHET_CORE/inherit' );

// constants
/**
Expand All @@ -22,6 +23,58 @@ define( function( require ) {
return ( typeof value === 'number' );
};

// valid values for options.units // TODO: Should this be an enum?
var VALID_UNITS = [
'amperes',
'milliamperes',
'becquerels',
'centimeters',
'centimeters-squared',
'coulombs',
'degrees Celsius',
'farads',
'kilograms',
'grams',
'gray',
'henrys',
'henries',
'hertz',
'joules',
'katals',
'kelvins',
'liters',
'liters/second',
'lumens',
'lux',
'meters',
'meters/second',
'meters/second/second',
'moles',
'moles/liter',
'nanometers',
'newtons',
'newtons/meters',
'newtons-second/meters',
'ohms',
'ohm-centimeters',
'pascals',
'percent',
'radians',
'radians/second',
'seconds',
'siemens',
'sieverts',
'steradians',
'tesla',
'view-coordinates/second',
'volts',
'watts',
'webers'
];

// values for options.type
var VALID_TYPE_VALUES = [ 'FloatingPoint', 'Integer' ];

/**
* @param {number} value - initial value
* @param {Object} [options]
Expand All @@ -30,12 +83,21 @@ define( function( require ) {
function NumberProperty( value, options ) {

options = _.extend( {
range: null // {null|Range|{min:number, max:number}} range of the value
range: null, // {null|Range|{min:number, max:number}} range of the value
phetioValueType: TNumber,
valueType: 'FloatingPoint', // 'FloatingPoint' | 'Integer'
units: null
}, options );

assert && assert( !options.validValues, 'NumberProperty cannot use validValues' );
assert && assert( !options.isValidValue, 'NumberProperty implements its own isValidValue' );

options.units && assert && assert( _.includes( VALID_UNITS, options.units ), 'Invalid units: ' + options.units );
assert && assert( _.includes( VALID_TYPE_VALUES, options.valueType ), 'invalid type: ' + options.valueType );

// @public (read-only) {string} units from above
this.units = options.units;

if ( options.range ) {
options.isValidValue = function( value ) {
return IS_NUMBER( value ) && ( value >= options.range.min ) && ( value <= options.range.max );
Expand Down
11 changes: 4 additions & 7 deletions js/ObservableArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ define( function( require ) {
'use strict';

// modules
var Property = require( 'AXON/Property' );
var axon = require( 'AXON/axon' );
var inherit = require( 'PHET_CORE/inherit' );
var Emitter = require( 'AXON/Emitter' );
var NumberProperty = require( 'AXON/NumberProperty' );
var TObservableArray = require( 'AXON/TObservableArray' );
var inherit = require( 'PHET_CORE/inherit' );
var Tandem = require( 'TANDEM/Tandem' );

// phet-io modules
var TNumber = require( 'ifphetio!PHET_IO/types/TNumber' );

/**
* @param {Object[]} [array]
* @param {Object} [options]
Expand Down Expand Up @@ -54,9 +51,9 @@ define( function( require ) {
this._removedListeners = []; // @private listeners called when an item is removed

// @public (read-only) observe this, but don't set it
this.lengthProperty = new Property( this._array.length, {
this.lengthProperty = new NumberProperty( this._array.length, {
tandem: options.tandem && options.tandem.createTandem( 'lengthProperty' ),
phetioValueType: TNumber( { type: 'Integer' } )
valueType: 'Integer'
} );

// @private Store the initial array, if any, for resetting, see #4
Expand Down
2 changes: 1 addition & 1 deletion js/TObservableArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ define( function( require ) {
* Get the number of electrons currently in the array.
*/
getLength: {
returnType: TNumber( { type: 'Integer' } ),
returnType: TNumber,
parameterTypes: [],
implementation: function() {
return this.instance.length;
Expand Down

0 comments on commit d7388b3

Please sign in to comment.