Skip to content

Commit

Permalink
factor out getGameLevelsSchema, use in equality-explorer and fourier-…
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Dec 15, 2021
1 parent e419630 commit 4c350e5
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions js/getGameLevelsSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021, University of Colorado Boulder

/**
* Gets the QueryStringMachine schema for the gameLevels query parameter.
*
* History:
* - The `gameLevels` query parameter was first proposed and discussed in https://github.com/phetsims/vegas/issues/86.
* - The design of the gameLevels query parameter was solidified, and it was first implemented in Equality Explorer,
* see https://github.com/phetsims/equality-explorer/issues/165.
* - When gameLevels was needed in Fourier, the schema was then copied from Fourier to Equality Explorer,
* see https://github.com/phetsims/fourier-making-waves/issues/145.
* - During code review of Number Play in https://github.com/phetsims/number-play/issues/92, yet-another implementation
* was discovered. That motivated factoring out this function, to prevent further duplication and inconsistency.
*
* @author Chris Malley (PixelZoom, Inc.)
*/

import vegas from './vegas.js';

function getGameLevelsSchema( numberOfLevels ) {
return {
public: true,
type: 'array',
elementSchema: {
type: 'number',
isValidValue: Number.isInteger
},
defaultValue: null,
isValidValue: array => {
return ( array === null ) || (
array.length > 0 &&
// unique level numbers
array.length === _.uniq( array ).length &&
// valid level numbers
_.every( array, element => element > 0 && element <= numberOfLevels ) &&
// sorted by ascending level number
_.every( array, ( value, index, array ) => ( index === 0 || array[ index - 1 ] <= value ) )
);
}
};
}

vegas.register( 'getGameLevelsSchema', getGameLevelsSchema );
export default getGameLevelsSchema;

0 comments on commit 4c350e5

Please sign in to comment.