Provides generally useful static functions.

Properties

COMPILE_TIME: any = MPL_BUILD_TIME

Date and time when the code was compiled. This value is set during the esbuild bundling phase with a --define option.

DEBUG: true = true

Flag indicates whether to include debug code, must be true for assertions to work. Must be set as a compiler option, by adding UTIL_DEBUG=true to the compile command. For example: make testviewer UTIL_DEBUG=true. See the shell script compile_js.sh and makefile for details. See Customizing The Build Process.

HEX_DIGITS: "0123456789abcdef" = '0123456789abcdef'

A string listing the the hexadecimal digits '0123456789abcdef'

IMAGES_DIR: string = '.'

Specifies the relative URL of the directory containing images related to the user interface.

LOCALE: string = MPL_LOCALE

Specifies the language to use. The ISO 639-1 language code is a two-letter lowercase code. For example, English is en, and German is de. This value is set during the esbuild bundling phase with a --define option.

MAX_INTEGER: number = ...

Maximum representable integer. Need to avoid having an index ever reach this value because we can then no longer increment by 1. That is: 2^53 + 1 == 2^53 because of how floating point works.

MIN_INTEGER: number = -Math.pow(2, 53)

Minimum representable integer.

MODERN_CLOCK: boolean = ...

Whether running under a modern browser that supports performance.now();

NF: ((num) => string) = Util.nf5

The default number format to use in toString methods.

Type declaration

    • (num): string
    • The default number format to use in toString methods.

      Parameters

      • num: null | number

      Returns string

NOT_IMPLEMENTED: "not implemented" = 'not implemented'

String used to mark functions that have not been implemented.

VERSION: "2.0.0" = '2.0.0'

Returns the current version number for the myphysiclab library, using Semantic Versioning.

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards-compatible manner, and
  • PATCH version when you make backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

maxErrors: 3 = 3

Maximum number of errors to report by setErrorHandler.

numErrors: number = 0

Number of errors reported by setErrorHandler.

Methods

  • Formats a number with 0 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 0 decimal places; or null or undefined

  • Formats a number with 18 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 18 decimal places; or null or undefined

  • Formats a number with 1 decimal place and a plus sign if positive.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 1 decimal place and a plus sign if positive; or null or undefined

  • Formats a number with 2 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 2 decimal places; or null or undefined

  • Formats a number with 3 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 3 decimal places; or null or undefined

  • Formats a number with 4 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 4 decimal places; or null or undefined

  • Formats a number with 5 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 5 decimal places; or null or undefined

  • Formats a number with 5 decimal places, but if too small then switch to exponential.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 5 decimal places; or null or undefined

  • Formats a number with 7 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 7 decimal places; or null or undefined

  • Formats a number with 7 decimal places, but if too small then switch to exponential.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 7 decimal places; or null or undefined

  • Formats a number with 9 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with 9 decimal places; or null or undefined

  • Formats a number with 7 digit exponential notation.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number in 7 digit exponential notation; or null or undefined

  • Formats a number with 17 digit exponential notation.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number in 17 digit exponential notation; or null or undefined

  • Converts an array of numbers to a string, with a separator string between each number.

    Parameters

    • r: number[]

      the array to print

    • Optional nf: ((n) => string)

      number format function to use

        • (n): string
        • Parameters

          • n: number

          Returns string

    • Optional separator: string

      the text to insert between each value; default is a comma and space ,

    Returns string

    the array of numbers converted to a string

  • Converts an array of booleans to string, with commas between each boolean.

    Parameters

    • r: boolean[]

      the array to print

    • Optional trueString: string

      the string that indicates a true value; default true

    • Optional falseString: string

      the string that indicates a false value; default false

    Returns string

    the array of booleans converted to a string

  • assert throws an error if the test is not true.

    TO DO: The idea of assertions is that they disappear when you turn off the ASSERTS flag. Here they would be disabled with ASSERTS flag false, but the code doesn't disappear.

    Parameters

    • test: boolean

      whether the assertion is true or false

    • Optional opt_message: string

      optional message to print to console if assertion is false

    Returns void

  • Returns a CSS3 color string composed of a # followed by 3 hex digits corresponding to given red, green, blue proportions.

    Parameters

    • red: number

      proportion of red from 0.0 to 1.0

    • green: number

      proportion of green from 0.0 to 1.0

    • blue: number

      proportion of blue from 0.0 to 1.0

    Returns string

    the corresponding CSS3 color string with 3 hex digits

  • Returns a CSS3 color string composed of a # followed by 6 hex digits corresponding to given red, green, blue proportions.

    Parameters

    • red: number

      proportion of red from 0.0 to 1.0

    • green: number

      proportion of green from 0.0 to 1.0

    • blue: number

      proportion of blue from 0.0 to 1.0

    Returns string

    the corresponding CSS3 color string with 6 hex digits

  • Creates an HTMLImageElement from the given URL.

    Parameters

    • url: string

      location of the image as a URL

    • width: number

      width of the image in pixels

    • Optional opt_height: number

      optional height of image in pixels

    Returns HTMLImageElement

    an HTMLImageElement

  • Define a global name with a value.

    Parameters

    • n: string

      name of the global

    • v: any

      value of the global

    • Optional opt_write: boolean

      whether the global is writable

    Returns void

  • Returns text with specified number of characters removed from start or end of string.

    Parameters

    • text: string
    • n: number

      number of characters to drop; if positive the characters are removed from front of string; if negative then from end of string

    Returns string

  • Returns whether the arrays are equal.

    Type Parameters

    • T

    Parameters

    • a1: T[]

      first array

    • a2: T[]

      second array

    Returns boolean

    true if arrays are same length and have same elements in each position

  • Executes the function on each member of the array, starting at end of array. Note: use arrow function syntax => which which binds current context implicitly so you don't need to worry about "this".

    Type Parameters

    • T

    • K

    Parameters

    • a: T[]

      array of arguments

    • fnc: ((a, idx, arr) => void)

      function that takes 3 arguments: an element of the array, the index number, and the array

        • (a, idx, arr): void
        • Parameters

          • a: T
          • idx: number
          • arr: T[]

          Returns void

    • Optional opt_this: K

      the "this" argument when executing the function

    Returns void

  • Finds the specified element in the HTML Document; throws an error if element is not found.

    Parameters

    • elementId: string

      element id of the element in the HTML Document

    Returns HTMLElement

    the element from the current HTML Document

    Throws

    if element is not found.

  • Returns the dimensions of the browser viewport.

    Returns [number, number]

    the width and height of the browser viewport

  • Returns the length of hypotenuse of right triangle.

    Parameters

    • a: number

      length of a side of the right triangle

    • b: number

      length of other side of the right triangle

    Returns number

    length of hypotenuse = sqrt(a^2 + b^2)

  • Returns true if running under Chrome browser.

    Returns boolean

    true if running under Chrome browser

  • Returns true if running on iPhone.

    Returns boolean

    true if running on iPhone

  • Returns whether the object is an object.

    Parameters

    • obj: any

    Returns boolean

    Whether variable is an object.

  • Returns the given angle limited to the range -pi to +pi.

    Parameters

    • angle: number

      the angle in radians

    Returns number

    the equivalent angle in the range -pi to +pi

  • Finds the specified element in the HTML Document; returns null if element is not found.

    Parameters

    • elementId: string

      element id of the element in the HTML Document

    Returns null | HTMLElement

    the element from the current HTML Document, or null if not found

  • Returns list of names of methods (functions) defined on the given object.

    Parameters

    • obj: object

      object to examine

    Returns string[]

    list of names of functions defined on the given object

  • Returns the name of the property with the given value, within the given object.

    Parameters

    • obj: object

      the object whose values are examined

    • value: object

      the value of interest

    Returns string

    the name of the property with the given value, within the object; or the empty string if value not found.

  • Returns a boolean array with all entries initialized to false.

    Parameters

    • n: number

      length of array

    Returns boolean[]

    array of booleans with all entries initialized to false.

  • Returns an array of numbers with all entries initialized to zero.

    Parameters

    • n: number

      length of array

    Returns number[]

    an array of numbers with all entries initialized to zero.

  • Formats a number with from zero to 5 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with zero to 5 decimal places; or null or undefined

  • Formats a number with from zero to 7 decimal places.

    Parameters

    • num: null | number

      the number to format

    Returns string

    the number with zero to 7 decimal places; or null or undefined

  • Maps a number in range 0 to 1 to a single hexadecimal character from 0 to f.

    Parameters

    • n: number

      number between 0 and 1

    Returns string

    the number converted to a single hexadecimal character

  • Maps a number in range 0 to 1 to a two-digit hexadecimal number.

    Parameters

    • n: number

      number between 0 and 1

    Returns string

    the number converted to a two-digit hexadecimal number

  • Formats the toString represention of an object to be more readable. Adds newlines and spaces so that each property of an object appears on a separate line, and is indented according to the "level depth" of objects being formatted.

    Assumes that the object's toString is formatted according to Javascript conventions like this:

    ClassName{property1: value1, property2: value2}
    

    Semi-colons or commas are equivalent for separating properties. Assumes that arrays are formatted like standard JavaScript as [object1, object2, object3].

    The level depth works as follows: Level 1 means that the each property of the object appears on a separate line preceded a single indent string. For example:

    ClassName{
      property1: value1,
      property2: value2,
    }
    

    Level 2 is like level 1, but additionally any object that appears as the value of a level 1 property is also expanded:

    ClassName{
      property1: ClassName2{
        property3: value3,
        property4: value4
      },
      property2: value2,
    }
    

    Level 3 adds another level of expansion for objects found at Level 2. And so on for higher levels.

    ClassName{
      property1: ClassName2{
        property3: value3,
        property4: ClassName3{
          property5: value5,
          property6: value6
        }
      },
      property2: value2,
    }
    

    The "property detection" is done by looking for commas or semicolons. A new level is begun whenever an opening brace { or square bracket [ is seen. Anything in quotes is ignored. Works for arrays also.

    TO DO escaped quotes in strings should be ignored.

    Parameters

    • input: string | object

      the string to reformat. Typically this is the toString representation of an object.

    • Optional level: number

      how much nesting of the object to pay attention to. Nesting occurs whenever opening braces { or brackets [ are seen in the input string. Default is 2.

    • Optional indent: string

      String to use for indenting each new level. Default is two spaces.

    Returns string

    the input string formatted to be more readable

  • Prints array of numbers to console.log on multiple lines so that each line is no longer than lineLength.

    Parameters

    • array: number[]

      the array to print

    • Optional lineLength: number

      maximum length of a line, default 80

    • Optional format: ((n) => string)

      formatting function, default is NF5E

        • (n): string
        • Parameters

          • n: number

          Returns string

    Returns void

  • Prints to console.log a variable set of numbers using NF5 format.

    Parameters

    • s: string

      prefix to print

    • Rest ...nums: number[]

      numbers to print, variable number of arguments

    Returns void

  • Returns list of names of (non-function) properties defined on the given object, and optionally also shows the values of the properties.

    Parameters

    • obj: null | object

      the object to examine, or null

    • Optional showValues: boolean

      whether to show values of the properties (default is false)

    Returns string[]

    array of names of properties of the object (and possibly their values)

  • returns array with numbers 0, 1, 2, 3, ..., n-1.

    Parameters

    • n: number

    Returns number[]

  • removes the first occurence of the object from the array.

    Type Parameters

    • T

    Parameters

    • arr: T[]

      the array to remove from

    • myobj: T

      the object to remove

    Returns boolean

    true if the object was removed

  • removes all copies of the object from the array.

    Type Parameters

    • T

    Parameters

    • arr: T[]

      the array to remove from

    • myobj: T

      the object to remove

    Returns boolean

    true if the object was removed

  • remove duplicates from a sorted array.

    TO DO: add an assert that the array is sorted

    Type Parameters

    • T

    Parameters

    • a: T[]

    Returns void

  • Returns an array of the specified length with the given value repeated.

    Type Parameters

    • T

    Parameters

    • value: T

      the value to repeat

    • len: number

      length of array to create

    Returns T[]

    array with the given value repeated

  • Sets a global error handler function in window.onerror to alert user.

    Returns void

  • Specifies the relative URL of the directory containing images related to the user interface. The value is accessible via IMAGES_DIR.

    Parameters

    • Optional images_dir: string

      the relative URL of the images directory; if undefined IMAGES_DIR is not changed.

    Returns void

  • Shows an error alert to user, but only if not too many error alerts have been shown already. This avoids user being stuck in infinite loop of error alerts.

    Parameters

    • s: string

      text to show in alert

    Returns void

  • Returns the current time as given by the system clock, in seconds.

    Returns number

    the current time as given by the system clock, in seconds

  • Returns specified number of characters from start or end of string.

    Parameters

    • text: string
    • n: number

      number of characters to take; if positive the characters are taken from front of string; if negative then from end of string

    Returns string

  • Throws an error if the argument is not a finite number.

    Parameters

    • value: number

      the number to test

    Returns number

    the value that passed the test

    Throws

    if the argument is not a finite number

  • Throws an error if the argument is not a number.

    Parameters

    • value: number

      the number to test

    Returns number

    the value that passed the test

    Throws

    if the argument is not a number

  • Returns the language independent form of the given string by changing to uppercase and replacing spaces and dashes with underscores.

    Parameters

    • text: string

      the text to convert

    Returns string

    the text upper-cased and with spaces and dashes replaced by underscores

  • Whether all elements of the array are unique with no duplicates.

    Parameters

    • arr: string[]

      the array to examine

    Returns boolean

    Whether all elements of the array are unique with no duplicates.

  • Ensures the given text consists of only uppercase letters, numbers and underscore and first character is a letter or underscore. This is required for language independent names.

    Parameters

    • text: string

      the text to validate

    Returns string

    the validated text

    Throws

    if text does not qualify as a name

  • Returns true if the numbers are significantly different to a certain tolerance level, adjusting the tolerance for larger numbers.

    For numbers with absolute value smaller than magnitude the numbers are compared using a fixed tolerance of magnitude*epsilon.

    For numbers with absolute value larger than magnitude, the tolerance is epsilon times the larger of the absolute values of the numbers being compared.

    Unless specified, the default for magnitude is 1.0 and epsilon is 1E-14. These settings return true if the numbers are significantly different to approximately 14 decimal digits when their magnitude is near 1.0.

    The goal is to have a test that is immune to the inaccuracy of double arithmetic. Doubles have 15 to 17 significant decimal digits of accuracy, so comparing 14 significant digits should be fairly safe from the inaccuracy in double arithmetic.

    This method takes into account the size of the numbers being compared, so it is safer than code such as

    if (Math.abs(a - b) > 1E-16)  // do something
    

    Doubles have 15 to 17 significant decimal digits of accuracy. When the numbers being compared are much bigger in magnitude than 1.0, then this test is too strict -- it effectively is comparing to zero, meaning exact equality.

    See Comparing Floating Point Numbers, 2012 Edition by Bruce Dawson.

    See StackOverflow: How dangerous is it to compare floating point values?

    Parameters

    • arg1: number

      the first number to compare

    • arg2: number

      the second number to compare

    • Optional epsilon: number

      the small value used with magnitude to calculate the tolerance for deciding when the numbers are different, default is 1E-14.

    • Optional magnitude: number

      the approximate magnitude of the numbers being compared, default is 1.0.

    Returns boolean

    true if the doubles are different to 14 significant decimal digits

    Throws

    if magnitude or epsilon is negative or zero

  • Sets all values of array to zero

    Parameters

    • arr: number[]

      the array to modify

    Returns void

Generated using TypeDoc