From a36d862d0ee021c1290630272cad613db4f01c54 Mon Sep 17 00:00:00 2001 From: zepumph Date: Thu, 29 Jul 2021 10:17:55 -0400 Subject: [PATCH] factor out isValidValue, https://github.com/phetsims/tandem/issues/244 --- js/types/MapIO.js | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/js/types/MapIO.js b/js/types/MapIO.js index 0680fbb..b5c0554 100644 --- a/js/types/MapIO.js +++ b/js/types/MapIO.js @@ -32,19 +32,23 @@ const MapIO = ( keyType, valueType ) => { const cacheKey = keyType.typeName + ',' + valueType.typeName; if ( !cache.has( cacheKey ) ) { + + // parameterized valid value function + const mapIsValidValue = map => { + for ( const [ key, value ] of map ) { + if ( !ValidatorDef.isValueValid( key, keyType.validator ) ) { + return false; + } + if ( !ValidatorDef.isValueValid( value, valueType.validator ) ) { + return false; + } + } + return true; + }; + cache.set( cacheKey, new IOType( `MapIO<${keyType.typeName},${valueType.typeName}>`, { valueType: Map, - isValidValue: map => { - for ( const [ key, value ] of map ) { - if ( !ValidatorDef.isValueValid( key, keyType.validator ) ) { - return false; - } - if ( !ValidatorDef.isValueValid( value, valueType.validator ) ) { - return false; - } - } - return true; - }, + isValidValue: mapIsValidValue, parameterTypes: [ keyType, valueType ], toStateObject: map => { const array = []; @@ -61,19 +65,7 @@ const MapIO = ( keyType, valueType ) => { }, documentation: 'IO Type for the built-in JS Map type, with the key and value types specified.', stateSchema: StateSchema.asValue( `Map<${keyType.typeName},${valueType.typeName}>`, { - - // TODO: https://github.com/phetsims/tandem/issues/244 duplicate with above - isValidValue: map => { - for ( const [ key, value ] of map ) { - if ( !ValidatorDef.isValueValid( key, keyType.validator ) ) { - return false; - } - if ( !ValidatorDef.isValueValid( value, valueType.validator ) ) { - return false; - } - } - return true; - } + isValidValue: mapIsValidValue } ) } ) ); }