Jump to navigation

Namespace: util

Numbas.util

Members

(static) equalityTests

Functions to decide if two tokens of the same type are equal. Dictionary mapping token type name to function.

Source:
See:

(static) numberNotationStyles

Different styles of writing a decimal

Objects of the form {re,format}, where re is a regex recognising numbers in this style, and format(integer,decimal) renders the number in this style.

Each regex matches the integer part in group 1, and the decimal part in group 2 - it should be safe to remove all non-digit characters in these and preserve meaning.

Source:
See:

(static) re_fraction :RegExp

Regular expression recognising a fraction

Type:
Source:

Methods

(static) arraysEqual(a, b) → {Boolean}

Are two arrays equal? True if their elements are all equal

Parameters:
Name Type Description
a Array
b Array
Returns:
Type Description
Boolean
Source:

(static) capitalise(str) → {String}

Make the first letter in the string a capital

Parameters:
Name Type Description
str String
Returns:
Type Description
String
Source:

(static) cartesian_power(l, n) → {Array}

Cartesian product of list, repeated n times

Parameters:
Name Type Description
l Array
n Number
Returns:
Type Description
Array
Source:

(static) cleanNumber(s, styles, strictStyleopt) → {String}

Clean a string potentially representing a number. Remove space, and then try to identify a notation style.

If styles is given, s will be tested against the given styles. If it matches, the string will be rewritten using the matched integer and decimal parts, with punctuation removed and the decimal point changed to a dot.

Parameters:
Name Type Attributes Description
s String

the string potentially representing a number.

styles String | Array.<String>

styles of notation to allow, e.g. ['en','si-en']

strictStyle Boolean <optional>

if false or not given, strings which do not match any of the allowed styles but are valid JavaScript number literals will be allowed. If true, these strings will return 'NaN'.

Returns:
Type Description
String
Source:
See:

(static) combinations(list, r) → {Array.<Array>}

All combinations of r items from given array, without replacement

Parameters:
Name Type Description
list Array
r Number
Returns:
Type Description
Array.<Array>
Source:

(static) combinations_with_replacement(list, r) → {Array.<Array>}

All combinations of r items from given array, with replacement

Parameters:
Name Type Description
list Array
r Number
Returns:
Type Description
Array.<Array>
Source:

(static) contains(list, value) → {Boolean}

Is value in the list?

Parameters:
Name Type Description
list Array
value Numbas.jme.token
Returns:
Type Description
Boolean
Source:

(static) contentsplitbrackets(txt, re_end) → {Array.<String>}

Split a string up by TeX delimiters ($, \[, \])

bits.re_end stores the delimiter if the returned array has unfinished maths at the end

Parameters:
Name Type Description
txt String

string to split up

re_end RegExp

If tex is split across several strings (e.g. text nodes with
in the middle), this can be used to give the end delimiter for unfinished maths

Returns:
Type Description
Array.<String>

bits - stuff outside TeX, left delimiter, TeX, right delimiter, stuff outside TeX, ...

Example
contentsplitbrackets('hello $x+y$ and \[this\] etc') => ['hello ','$','x+y','$',' and ','\[','this','\]']
Source:

(static) copyarray(arr, deep) → {Array}

Clone an array, with array elements copied too. Array.splice() will create a copy of an array, but the elements are the same objects, which can cause fruity bugs. This function clones the array elements as well, so there should be no side-effects when operating on the cloned array.

Parameters:
Name Type Description
arr Array
deep Boolean

if true, do a deep copy of each element

Returns:
Type Description
Array
Source:
See:

(static) copyinto(src, dest)

Shallow copy an object into an already existing object (add all src's properties to dest)

Parameters:
Name Type Description
src Object
dest Object
Source:

(static) copyobj(obj, deep) → {Object}

Clone an object.

Parameters:
Name Type Description
obj Object
deep Boolean

if true, each property is cloned as well (recursively) so there should be no side-effects when operating on the cloned object.

Returns:
Type Description
Object
Source:

(static) currency(n, prefix, suffix) → {String}

Format an amount of currency

Parameters:
Name Type Description
n Number
prefix String

symbol to use in front of currency if abs(n) >= 1

suffix String

symbol to use after currency if abs(n) <= 1

Returns:
Type Description
String
Example
currency(5.3,'£','p') => £5.30
Source:

(static) distinct(list) → {Array}

Return a copy of the input list with duplicates removed

Parameters:
Name Type Description
list Array
Returns:
Type Description
Array
Source:
See:

(static) eq(a, b) → {Boolean}

Generic equality test on Numbas.jme.tokens

Parameters:
Name Type Description
a Numbas.jme.token
b Numbas.jme.token
Returns:
Type Description
Boolean
Source:
See:

(static) escapeHTML(str) → {String}

Because XML doesn't like having ampersands hanging about, replace them with escape codes

Parameters:
Name Type Description
str String

XML string

Returns:
Type Description
String
Source:

(static) except(list, exclude) → {Array}

Filter out values in exclude from list

Parameters:
Name Type Description
list Numbas.jme.types.TList
exclude Numbas.jme.types.TList
Returns:
Type Description
Array
Source:

(static) extend(a, b, extendMethods) → {function}

Derive type B from A (class inheritance, really)

B's prototype supercedes A's.

Parameters:
Name Type Description
a function

the constructor for the parent class

b function

a constructor to be called after a's constructor is done.

extendMethods Boolean

if true, the methods of the new type are constructed so that the method from type A is applied, then the method from type B. Nothing is returned.

Returns:
Type Description
function

a constructor for the derived class

Source:

(static) extend_object(destination, others*) → {Object}

Extend destination with all the properties from subsequent arguments. undefined values are not copied over. Replacement for jQuery.extend. Modified from https://stackoverflow.com/a/11197343 Object.assign doesn't behave the same way - it copies over undefined.

Parameters:
Name Type Description
destination Object
others* Object
Returns:
Type Description
Object
Source:

(static) formatString(str, …value) → {String}

Replace occurences of %s with the extra arguments of the function

Parameters:
Name Type Attributes Description
str String
value String <repeatable>

string to substitute

Returns:
Type Description
String
Example
formatString('hello %s %s','Mr.','Perfect') => 'hello Mr. Perfect'
Source:

(static) formatTime(t) → {String}

String representation of a time, in the format HH:MM:SS

Parameters:
Name Type Description
t Date
Returns:
Type Description
String
Source:

(static) hashCode(str) → {String}

Parameters:
Name Type Description
str String
Returns:
Type Description
String
Source:

(static) isBool(b) → {Boolean}

Test if parameter is a boolean - that is: a boolean literal, or any of the strings 'false','true','yes','no', case-insensitive.

Parameters:
Name Type Description
b Object
Returns:
Type Description
Boolean
Source:

(static) isFloat(f) → {Boolean}

Test if parameter is a float

Parameters:
Name Type Description
f Object
Returns:
Type Description
Boolean
Source:

(static) isFraction(s) → {Boolean}

Test if parameter is a fraction

Parameters:
Name Type Description
s String
Returns:
Type Description
Boolean
Source:

(static) isInt(i) → {Boolean}

Test if parameter is an integer

Parameters:
Name Type Description
i Object
Returns:
Type Description
Boolean
Source:

(static) isNonemptyHTML(html) → {Boolean}

Parse a string as HTML, and return true only if it contains non-whitespace text

Parameters:
Name Type Description
html String
Returns:
Type Description
Boolean
Source:

(static) isNumber(n, allowFractions, styles, strictStyle) → {Boolean}

Is na number? i.e. !isNaN(n), or is n "infinity", or if allowFractions is true, is n a fraction?

If styles is given, try to put the number in standard form if it matches any of the given styles.

Parameters:
Name Type Description
n Number | String
allowFractions Boolean
styles String | Array.<String>

styles of notation to allow.

strictStyle Boolean

if false or not given, strings which do not match any of the allowed styles but are valid JavaScript number literals will be allowed. If true, these strings will return false.

Returns:
Type Description
Boolean
Source:
See:

(static) letterOrdinal(n) → {String}

Get the letter format of an ordinal e.g. the Nth element in the sequence a,b,c,...z,aa,ab,..,az,ba,...

Parameters:
Name Type Description
n Number
Returns:
Type Description
String
Source:

(static) lpad(s, n, p) → {String}

Pad string s on the left with a character p until it is n characters long.

Parameters:
Name Type Description
s String
n Number
p String
Returns:
Type Description
String
Source:

(static) matchNotationStyle(s, styles, strictStyleopt, mustMatchAllopt) → {Object|null}

Try to match a string representing a number in any of the given styles at the start of the given string, and return both the matched text and a JavaScript number literal equivalent.

Parameters:
Name Type Attributes Description
s String

the string potentially representing a number.

styles String | Array.<String>

styles of notation to allow, e.g. ['en','si-en']

strictStyle Boolean <optional>

if false or not given, strings which do not match any of the allowed styles but are valid JavaScript number literals will be allowed. If true, these strings will return 'NaN'.

mustMatchAll Boolean <optional>

if true, then the string must contain only the matched number.

Returns:
Type Description
Object | null
  • {matched, cleaned} or null
Source:
See:

(static) neq(a, b) → {Boolean}

Generic inequality test on Numbas.jme.tokens

Parameters:
Name Type Description
a Numbas.jme.token
b Numbas.jme.token
Returns:
Type Description
Boolean
Source:
See:

(static) nicePartName(path) → {String}

Get a human-sensible name of a part, given its path

Parameters:
Name Type Description
path String
Returns:
Type Description
String
Source:

(static) objects_equal(a, b) → {Boolean}

Are the given objects equal? False if they're of different types. If they're both arrays, uses Numbas.util.arraysEqual. If they're both objects, true if every key in b is also in a, and a[k] is equal to b[k] for every k in a. Otherwise, uses JavaScript's equality test.

Parameters:
Name Type Description
a *
b *
Returns:
Type Description
Boolean
Source:

(static) parseBool(b) → {Boolean}

Parse parameter as a boolean. The boolean value true and the strings 'true' and 'yes' are parsed as the value true, everything else is false.

Parameters:
Name Type Description
b Object
Returns:
Type Description
Boolean
Source:

(static) parseDecimal(s, allowFractions, styles, strictStyle) → {Decimal}

Parse a number - either as a Decimal, or parse a fraction.

Parameters:
Name Type Description
s String
allowFractions Boolean

are fractions of the form a/b (a and b integers without punctuation) allowed?

styles String | Array.<String>

styles of notation to allow.

strictStyle Boolean

if false or not given, strings which do not match any of the allowed styles but are valid JavaScript number literals will be allowed. If true, these strings will return NaN.

Returns:
Type Description
Decimal
Source:
See:

(static) parseFraction(s) → {fraction}

Parse a string representing an integer or fraction

Parameters:
Name Type Description
s String
Returns:
Type Description
fraction
Source:
See:

(static) parseNumber(s, allowFractions, styles, strictStyle) → {Number}

Parse a number - either parseFloat, or parse a fraction.

Parameters:
Name Type Description
s String
allowFractions Boolean

are fractions of the form a/b (a and b integers without punctuation) allowed?

styles String | Array.<String>

styles of notation to allow.

strictStyle Boolean

if false or not given, strings which do not match any of the allowed styles but are valid JavaScript number literals will be allowed. If true, these strings will return NaN.

Returns:
Type Description
Number
Source:
See:

(static) permutations(list, r) → {Array.<Array>}

All permutations of all choices of r elements from list

Inspired by the algorithm in Python's itertools library

Parameters:
Name Type Description
list Array

elements to choose and permute

r Number

number of elements to choose

Returns:
Type Description
Array.<Array>
Source:

(static) pluralise(n, singular, plural) → {String}

Pluralise a word

If n is not unity, return plural, else return singular

Parameters:
Name Type Description
n Number
singular String

string to return if n is +1 or -1

plural String

string to returns if n is not +1 or -1

Returns:
Type Description
String
Source:

(static) product(lists) → {Array}

Cartesian product of one or more lists

Parameters:
Name Type Description
lists Array

list of arrays

Returns:
Type Description
Array
Source:

(static) rpad(s, n, p) → {String}

Pad string s on the right with a character p until it is n characters long.

Parameters:
Name Type Description
s String
n Number
p String
Returns:
Type Description
String
Source:

(static) separateThousands(n, separator) → {String}

Write a number with every three digits separated by the given separator character

Parameters:
Name Type Description
n Number
separator String
Returns:
Type Description
String
Example
separateThousands(1234567.1234,',') => '1,234,567.1234'
Source:

(static) sortBy(props) → {function}

Create a comparison function which sorts objects by a particular property

Parameters:
Name Type Description
props Array.<String> | String

name of the property (or list of names of properties) to sort by

Returns:
Type Description
function
Source:

(static) splitbrackets(str, lb, rb) → {Array.<String>}

Split a string up according to brackets

Strips out nested brackets

Parameters:
Name Type Description
str String

string to split

lb String

left bracket string

rb String

right bracket string

Returns:
Type Description
Array.<String>
  • alternating strings in brackets and strings outside: odd-numbered indices are inside brackets.
Example
splitbrackets('a{{b}}c','{','}') => ['a','b','c']
Source:

(static) standardNumberFormatter(thousands, decimal_mark, separate_decimal) → {function}

Create a function (integer,decimal) -> string which formats a number according to the given punctuation.

Parameters:
Name Type Default Description
thousands String

the string used to separate powers of 1000

decimal_mark String

the decimal mark character

separate_decimal Boolean false

should the thousands separator be used to separate negative powers of 1000 (that is, groups of 3 digits after the decimal point)?

Returns:
Type Description
function
Source:

(static) unPercent(s) → {Number}

Get rid of the % on the end of percentages and parse as float, then divide by 100

Parameters:
Name Type Description
s String
Returns:
Type Description
Number
Examples
unPercent('50%') => 0.5
unPercent('50') => 0.5
Source:

(static) wrapListIndex(n, size) → {Number}

Wrap a list index so -1 maps to length-1

Parameters:
Name Type Description
n Number
size Number
Returns:
Type Description
Number
Source:

(static) zip(lists) → {Array}

Zip lists together: given lists [a,b,c,...], [x,y,z,...], return [[a,x],[b,y],[c,z], ...]

Parameters:
Name Type Description
lists Array

list of arrays

Returns:
Type Description
Array
Source: