Skip to content

Commit

Permalink
handle stringFactor === 1 properly, improve doc, cleanup, phetsims/ch…
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Jan 25, 2023
1 parent f6c5c74 commit 8d249fe
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions js/DynamicStringTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
*
* @author Sam Reid (PhET Interactive Simulations)
* @author Marla Schulz (PhET Interactive Simulations)
* @author Chris Malley (PixelZoom, Inc.)
*/

import { localizedStrings } from '../../chipper/js/getStringModule.js';
import Utils from '../../dot/js/Utils.js';
import joist from './joist.js';

// key codes
// keyCodes
const LEFT_ARROW = 37;
const UP_ARROW = 38;
const RIGHT_ARROW = 39;
const DOWN_ARROW = 40;
const SPACE_BAR = 32;

// Random words of different lengths that can be cycled through
// Words of different lengths that can be cycled through
const WORD_SOURCE = 'Sometimes when Hippopotomonstrosesquippedaliophobia want lyrics you turn to Shakespeare like ' +
'the following text copied from some work ' +
'To be or not to be that is the question ' +
Expand All @@ -30,10 +31,23 @@ const WORD_SOURCE = 'Sometimes when Hippopotomonstrosesquippedaliophobia want ly
export default class DynamicStringTest {
public static init(): void {

const words = WORD_SOURCE.split( ' ' );

// How much to increase or decrease the length of the string
let stringFactor = 1;

// An integer that assists in indexing into words.
let stride = 0;
const words = WORD_SOURCE.split( ' ' );

function doubleStrings(): void {
stringFactor = stringFactor * 2;
applyToAllStrings( stringFactor );
}

function halveStrings(): void {
stringFactor = Math.max( stringFactor * 0.5, 0.01 );
applyToAllStrings( stringFactor );
}

function setStride( newStride: number ): void {

Expand All @@ -46,29 +60,21 @@ export default class DynamicStringTest {
}

stride = newStride;
console.log( 'stride = ' + stride );
console.log( `stride = ${stride}` );

localizedStrings.forEach( ( localizedString, index ) => {
localizedString.property.value = words[ ( index + stride ) % words.length ];
} );
}

function doubleStrings(): void {
stringFactor = stringFactor * 2;
applyToStrings( stringFactor );
}

function halveStrings(): void {
stringFactor = Math.max( stringFactor * 0.5, 0.01 );
applyToStrings( stringFactor );
}

function reset(): void {

stringFactor = 1;
console.log( `stringFactor = ${stringFactor}` );
applyToAllStrings( stringFactor );

stride = 0;
console.log( `stride = ${stride}` );
localizedStrings.forEach( localizedString => localizedString.restoreInitialValue( 'en' ) );
}

window.addEventListener( 'keydown', event => {
Expand All @@ -92,38 +98,43 @@ export default class DynamicStringTest {
}

/**
* Applies the string factor to all strings.
* Applies stringFactor to all strings.
*/
function applyToStrings( factor: number ): void {
function applyToAllStrings( stringFactor: number ): void {
localizedStrings.forEach( localizedString => {

// Restore the string to its initial value.
localizedString.restoreInitialValue( 'en' );

// Strip out all RTL (U+202A), LTR (U+202B), and PDF (U+202C) characters from string.
const strippedString = localizedString.property.value.replace( /[\u202A\u202B\u202C]/g, '' );
localizedString.property.value = applyToString( strippedString, factor );
if ( stringFactor !== 1 ) {

// Strip out all RTL (U+202A), LTR (U+202B), and PDF (U+202C) characters from string.
const strippedString = localizedString.property.value.replace( /[\u202A\u202B\u202C]/g, '' );
localizedString.property.value = applyToString( stringFactor, strippedString );
}
} );
}

/**
* Applies the string factor to one string.
* Applies stringFactor to one string.
*/
function applyToString( string: string, factor: number ): string {
assert && assert( factor > 0, `factor must be > 0: ${factor}` );
function applyToString( stringFactor: number, string: string ): string {
assert && assert( stringFactor > 0, `stringFactor must be > 0: ${stringFactor}` );

if ( factor > 1 ) {
return doubleString( string, factor );
if ( stringFactor > 1 ) {
return doubleString( string, stringFactor );
}
else {

// Create an array of all placeholders that are present in the string. These are strings surrounded by 2 sets of
// curly braces, like '{{value}}'. This will be an empty array if no match is found.
// Create an array of all placeholders that are present in the string. Each placeholder is a substrings surrounded
// by 2 sets of curly braces, like '{{value}}'. This will be an empty array if no match is found.
const placeholders = string.match( /{{(.+?)}}/g ) || [];

// Remove all placeholders from the string.
const noPlaceholdersString = string.replace( /{{(.+?)}}/g, '' );

// Reduce the length of the string.
const stringLength = Utils.toFixedNumber( noPlaceholdersString.length * factor + 1, 0 );
const stringLength = Utils.toFixedNumber( noPlaceholdersString.length * stringFactor + 1, 0 );
const reducedString = noPlaceholdersString.substring( 0, stringLength );

// Append placeholders to the end of the reduced string. This will add nothing if placeholders is empty.
Expand Down

0 comments on commit 8d249fe

Please sign in to comment.