<commit_before>// Copyright 2021-2022, University of Colorado Boulder

/**
 * Gets the QueryStringMachine schema for the gameLevels query parameter.
 * Note that game levels are numbered starting from 1.
 *
 * 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 <NAME> (PixelZoom, Inc.)
 */

import vegas from './vegas.js';

function getGameLevelsSchema( numberOfLevels: number ) {

  assert && assert( Number.isInteger( numberOfLevels ) && numberOfLevels > 0,
    `numberOfLevels must be a positive integer: ${numberOfLevels}` );

  return {
    public: true,
    type: 'array',
    elementSchema: {
      type: 'number',
      isValidValue: ( value: number ) => ( Number.isInteger( value ) && value > 0 )
    },
    defaultValue: Array.from( { length: numberOfLevels }, ( _, i ) => i + 1 ), // [ 1, 2,...,numberOfLevels]
    isValidValue: ( array: number[] ) => {
      return ( array === null ) || (
        // at least 1 level must be visible
        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;<commit_msg>clean up validation<commit_after>// Copyright 2021-2022, University of Colorado Boulder

/**
 * Gets the QueryStringMachine schema for the gameLevels query parameter.
 * Note that game levels are numbered starting from 1.
 *
 * 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 <NAME> (PixelZoom, Inc.)
 */

import vegas from './vegas.js';

function getGameLevelsSchema( numberOfLevels: number ) {

  assert && assert( Number.isInteger( numberOfLevels ) && numberOfLevels > 0,
    `numberOfLevels must be a positive integer: ${numberOfLevels}` );

  return {
    public: true,
    type: 'array',
    elementSchema: {
      type: 'number',

      // validation for each level number in the array
      isValidValue: ( value: number ) => ( Number.isInteger( value ) && value > 0 && value <= numberOfLevels )
    },
    defaultValue: Array.from( { length: numberOfLevels }, ( _, i ) => i + 1 ), // [ 1, 2,...,numberOfLevels]

    // validation for the array as a whole
    isValidValue: ( array: number[] ) => {
      return ( array === null ) || (
        // at least 1 level must be visible
        array.length > 0 &&
        // unique level numbers
        array.length === _.uniq( array ).length &&
        // sorted by ascending level number
        _.every( array, ( value, index, array ) => ( index === 0 || array[ index - 1 ] <= value ) )
      );
    }
  };
}

vegas.register( 'getGameLevelsSchema', getGameLevelsSchema );
export default getGameLevelsSchema;
{'fraction_non_alphanumeric': 0.11930889563390147, 'fraction_numerical': 0.013308428671491944, 'mean_word_length': 3.1431334622823983, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '325431', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1194, 'n_words': 410}