Skip to content

Commit

Permalink
address TODOs, #326
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Nov 15, 2022
1 parent 08ba0bd commit 4bcd6dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
28 changes: 4 additions & 24 deletions js/common/NaturalSelectionQueryParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import logGlobal from '../../../phet-core/js/logGlobal.js';
import naturalSelection from '../naturalSelection.js';
import NaturalSelectionUtils from './NaturalSelectionUtils.js';

// The schema that describes the query parameters for this simulation
const SCHEMA = {
const SCHEMA_MAP = {

//------------------------------------------------------------------------------------------------------------------
// Public facing
Expand Down Expand Up @@ -249,29 +248,9 @@ const SCHEMA = {
showTimes: {
type: 'flag'
}
};
} as const;

// @ts-ignore TODO https://github.com/phetsims/natural-selection/issues/326
const NaturalSelectionQueryParameters = QueryStringMachine.getAll( SCHEMA );

/**
* Gets the value for a query parameter.
*/
//TODO https://github.com/phetsims/natural-selection/issues/326
// eslint-disable-next-line @typescript-eslint/no-explicit-any
NaturalSelectionQueryParameters.getValue = function( key: string ): any {
return NaturalSelectionQueryParameters[ key ];
};

/**
* Gets the default value for a query parameter.
*/
//TODO https://github.com/phetsims/natural-selection/issues/326
// eslint-disable-next-line @typescript-eslint/no-explicit-any
NaturalSelectionQueryParameters.getDefaultValue = function( key: string ): any {
// @ts-ignore TODO https://github.com/phetsims/natural-selection/issues/326
return SCHEMA[ key ].defaultValue;
};
const NaturalSelectionQueryParameters = QueryStringMachine.getAll( SCHEMA_MAP );

/**
* Parses a query-parameter value into a Range.
Expand Down Expand Up @@ -304,3 +283,4 @@ logGlobal( 'phet.preloads.phetio.queryParameters' );
logGlobal( 'phet.naturalSelection.NaturalSelectionQueryParameters' );

export default NaturalSelectionQueryParameters;
export { SCHEMA_MAP };
33 changes: 26 additions & 7 deletions js/common/model/parseInitialPopulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import naturalSelection from '../../naturalSelection.js';
import NaturalSelectionQueryParameters from '../NaturalSelectionQueryParameters.js';
import NaturalSelectionQueryParameters, { SCHEMA_MAP } from '../NaturalSelectionQueryParameters.js';
import NaturalSelectionUtils from '../NaturalSelectionUtils.js';
import Allele from './Allele.js';
import BunnyVariety from './BunnyVariety.js';
Expand All @@ -44,8 +44,8 @@ export default function parseInitialPopulation(
genePool: GenePool, mutationsName: string, populationName: string ): BunnyVariety[] {

// Get the query parameter values
const mutationsValue = NaturalSelectionQueryParameters.getValue( mutationsName );
const populationValue = NaturalSelectionQueryParameters.getValue( populationName );
const mutationsValue = getQueryParameterValue( mutationsName );
const populationValue = getQueryParameterValue( populationName );

let initialBunnyVarieties = null; // {BunnyVariety[]}
try {
Expand All @@ -72,10 +72,8 @@ export default function parseInitialPopulation(
} );

// Built the data structure for the default initial population.
const mutationChars = parseMutations( genePool, mutationsName,
NaturalSelectionQueryParameters.getDefaultValue( mutationsName ) );
initialBunnyVarieties = parsePopulation( genePool, mutationChars, populationName,
NaturalSelectionQueryParameters.getDefaultValue( populationName ) );
const mutationChars = parseMutations( genePool, mutationsName, getQueryParameterDefaultValue( mutationsName ) );
initialBunnyVarieties = parsePopulation( genePool, mutationChars, populationName, getQueryParameterDefaultValue( populationName ) );
}
return initialBunnyVarieties;
}
Expand Down Expand Up @@ -298,4 +296,25 @@ function verify( predicate: boolean, message: string ): void {
}
}

/**
* Gets the value for a query parameter.
*/
//TODO https://github.com/phetsims/natural-selection/issues/326
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getQueryParameterValue( key: string ): any {
type Key = keyof typeof NaturalSelectionQueryParameters;
return NaturalSelectionQueryParameters[ key as Key ];
}

/**
* Gets the default value for a query parameter.
*/
//TODO https://github.com/phetsims/natural-selection/issues/326
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getQueryParameterDefaultValue( key: string ): any {
type Key = keyof typeof SCHEMA_MAP;
// @ts-ignore TODO https://github.com/phetsims/natural-selection/issues/326
return SCHEMA_MAP[ key as Key ].defaultValue;
}

naturalSelection.register( 'parseInitialPopulation', parseInitialPopulation );

0 comments on commit 4bcd6dc

Please sign in to comment.