Home Reference Source
public class | source

MoodleVersion

A class for parsing and representing version information for the Moodle VLE.

The class can parse both the human-friendly Moodle release strings like 3.3.6 (Build: 20180517), and the underlying raw version numbers like 2017051506.

Note that this class only reliably understands version information from Moodle 2.2 up. The reason for this limitation is that this is the first Moodle version which uses the now standard versioning conventions.

See:

Static Member Summary

Static Public Members
public static get

A regular expression for matching human-friendly Moodle release strings.

public static get

A regular expression for matching under-the-hood version numbers like '2017051506' or '2017051506.00'.

public static get

A regular expression for matching short version strings like '3.5+' (as used on the Moodle download page).

Static Method Summary

Static Public Methods
public static

Convert a branch number into a branch string, i.e.

public static

Convert a branching date number to a branch, e.g.

public static

Convert a branch string into a branch number, i.e.

public static

Convert a branching date number to a branch number, e.g.

public static

Convert a branch to a branching date number, e.g.

public static

Convert a branch number to a branching date number, e.g.

public static

compare(val1: *, val2: *): number

Compare two values to see if they represent the same version, a greater version, or a lesser version.

public static

Convert a date number to a date object.

public static

Convert a date object to a date number.

public static

fromObject(obj: Object): *

A factory method for producing a Moodle Version object given all its properties.

public static

Build a version object from a version string.

public static

isBranch(val: *): boolean

Test if a given value is a branch string, e.g.

public static

isBranchNumber(val: *, strictTypeCheck: boolean): boolean

Test if a given value is a branch number, e.g.

public static

isDateNumber(val: *, strictTypeCheck: boolean): boolean

Test if a given value is a date number, i.e.

public static

isReleaseNumber(val: *, strictTypeCheck: boolean): boolean

Test if a given value is a release number.

public static

Test if a given value is a valid release suffix.

public static

Test if a given value is a valid release type.

public static

Convert a release type to a number.

public static

Convert a release type to a release suffix, e.g.

public static

Convert a relase suffix to a release type, e.g.

Constructor Summary

Public Constructor
public

constructor(versionInfo: string | Object)

By default version objects contain no information.

Member Summary

Public Members
public get

The major version part of the version number, officially known as the branch.

public set

The branch (AKA major version) must be a string consisting of two digits separated by a period, e.g.

public get

The version's branch number, if known. This is the two-digit number used internally within the Moodle code to identify a branch, or major release.

For example, all Moodle 3.5.* releases will have the branch number 35.

public set

The branch number must be a two-digit integer between 10 and 99.

public get

The known mappings between branching date numbers and Moodle branch numbers.

public get

The known mappings between branching date numbers and Moodle branches.

public get

The branching date as a date object.

public set

Setting the branching date will update the branch to match.

public get

The branching date as a DateNumber.

public set

Setting the branching date will update the branch to match.

public get

The known mappings between Moodle braches and branching date numbers.

public get

The known mappings between Moodle brache numberss and branching date numbers.

public get

The build number.

public set

Build numbers must be valid date numbers, i.e.

public get

The long human-friendly form of the version information.

public get

The release number part of the version number.

public set

The release number must be an integer greater than or equal to zero.

public get

The release suffix for the release's type, e.g. '+' for weekly updates to the official releases.

public set

The release suffix must be one of 'dev', an empty string, or '+'.

public get

The release's type, e.g. 'development'.

public set

The release type must be one of 'development', 'official', or 'weekly'.

public get

The short human-friendly form of the version number.

public get

The under-the-hood form of the version number.

Method Summary

Public Methods
public

Determine whether this version is greater than or equal to the given version.

public

Create a new Moodle version object representing the same version information.

public

Compare this version to another.

public

equals(val: *): boolean

Test if a given value is a Moodle Version object representing the same version.

public

Determine whether this version is greater than a given version.

public

Is this version on a branch the library knows about?

public

Determine whether or not this is version is on a long-term support branch.

public

Is this a stable release? I.e.

public

Determine whether this version is less than a given version.

public

Determine whether this versions is on the same branch as a given version.

public

The version as a plain object indexed by zero or more of:

public

An array containing a SemVer (Semantic Versioning) representation of the version information.

public

toSemVerObject(): {major: number, minor: number, patch: number}

An object containing a SemVer (Semantic Versioning) representation of the version information.

public

Return a string representation of the version.

Static Public Members

public static get releaseRE: RegExp source

A regular expression for matching human-friendly Moodle release strings. This RE is case-insensitive and will allow for the optional pre-fixing of the word Moodle with or whithout a separating space.

See:

public static get versionNumberRE: RegExp source

A regular expression for matching under-the-hood version numbers like '2017051506' or '2017051506.00'.

See:

public static get versionRE: RegExp source

A regular expression for matching short version strings like '3.5+' (as used on the Moodle download page). This RE is case-insensitive and will allow for the optional pre-fixing of the word Moodle with or whithout a separating space.

See:

Static Public Methods

public static branchFromBranchNumber(bn: BranchNumber): BranchString | undefined source

Convert a branch number into a branch string, i.e. 35 to '3.5' and 310 to '3.10'.

Params:

NameTypeAttributeDescription
bn BranchNumber

Return:

BranchString | undefined

If the passed value can't be converted to a branch undefined is returned.

public static branchFromBranchingDateNumber(bdn: DateNumber): BranchString | undefined source

Convert a branching date number to a branch, e.g. 20180517 to '3.5'.

Params:

NameTypeAttributeDescription
bdn DateNumber

public static branchNumberFromBranch(b: BranchString): number | undefined source

Convert a branch string into a branch number, i.e. '3.5' to 35.

Params:

NameTypeAttributeDescription
b BranchString

Return:

number | undefined

If the passed value can't be converted to a branch number undefined is returned.

public static branchNumberFromBranchingDateNumber(bdn: DateNumber): number | undefined source

Convert a branching date number to a branch number, e.g. 20180517 to 35.

Params:

NameTypeAttributeDescription
bdn DateNumber

Return:

number | undefined

public static branchingDateNumberFromBranch(b: BranchString): number | undefined source

Convert a branch to a branching date number, e.g. '3.5' to 20180517.

Params:

NameTypeAttributeDescription
b BranchString

Return:

number | undefined

public static branchingDateNumberFromBranchNumber(bn: BranchNumber): number | undefined source

Convert a branch number to a branching date number, e.g. 35 to 20180517.

Params:

NameTypeAttributeDescription
bn BranchNumber

Return:

number | undefined

public static compare(val1: *, val2: *): number source

Compare two values to see if they represent the same version, a greater version, or a lesser version.

When ranking versions, the branch is given the highest weight, then the release number, then the release type, and finally the build number. When comparing release types, 'development' is considered earlier 'official', which is considered earlier than 'weekly'.

Params:

NameTypeAttributeDescription
val1 *
val2 *

Return:

number

Unless both values are moodle vesion objects, NaN is returned. If val1 represents an earlier version than val2 -1 is returned, if val1 and val2 represent the same version 0 is returned, and if val1 represents a later version than val2 1 is returned.

public static dateFromDateNumber(dn: DateNumber): Date source

Convert a date number to a date object. The date object will represent midnight UTC on the given date.

Params:

NameTypeAttributeDescription
dn DateNumber

Return:

Date

Throw:

TypeError

public static dateNumberFromDate(d: Date): DateNumber source

Convert a date object to a date number. The date will be interpreted as UTC.

Params:

NameTypeAttributeDescription
d Date

Return:

DateNumber

Throw:

TypeError

public static fromObject(obj: Object): * source

A factory method for producing a Moodle Version object given all its properties.

If only one of the branch and branching date are passed, and if a known mapping exists, the other is auto-completed.

This function can be used to create version objects which contain unknown mappings between Moodle branches and branching dates.

Params:

NameTypeAttributeDescription
obj Object

an object defining zero or more of the following keys:

  • branch (e.g. '3.5') or branchNumber (e.g. 35) - if both are specified branchNumber takes precedence.
  • branchingDate or branchingDateNumber - if both are specified branchingDateNumber takes precedence.
  • releaseNumber
  • releaseType (e.g. 'weekly') or releaseSuffix (e.g. '+') - if both are specified, releaseSuffix takes precedence
  • buildNumber

Return:

*

Throw:

TypeError

A type error is thrown if an object is not passed, or, if any of the keys within that object map to an invalid value.

public static fromString(verStr: string): MoodleVersion source

Build a version object from a version string. The vesion string can be in one of the following formats:

  • A human-friendly full release string (ReleaseString), e.g. 'Moodle 3.5+ (Build: 20180614)' (will be accepted with or without the 'Moodle' prefix).
  • A human-friendly short version string (VersionString), e.g. 'Moodle 3.3.6+' (will be accepted with or without the 'Moodle' prefix).
  • An under-the-hood version number (VersionNumber), e.g.
  • '2017051506' or '2017051506.00'.
  • A string as returned by calling .toString() on an instance of this class.

Params:

NameTypeAttributeDescription
verStr string

the version string to parse.

Return:

MoodleVersion

Throw:

TypeError

RangeError

See:

public static isBranch(val: *): boolean source

Test if a given value is a branch string, e.g. '3.5' or '3.10'.

Params:

NameTypeAttributeDescription
val *

the value to test.

Return:

boolean

public static isBranchNumber(val: *, strictTypeCheck: boolean): boolean source

Test if a given value is a branch number, e.g. 35, '35', 310 or `'310'.

Params:

NameTypeAttributeDescription
val *

the value to test.

strictTypeCheck boolean
  • optional
  • default: false

whether or not to enable strict type checking. With strict type cheking enabled, string representation of otherwise valid values will return false.

Return:

boolean

public static isDateNumber(val: *, strictTypeCheck: boolean): boolean source

Test if a given value is a date number, i.e. an 8-digit number of the form YYYYMMDD.

Params:

NameTypeAttributeDescription
val *

the value to test.

strictTypeCheck boolean
  • optional
  • default: false

whether or not to enable strict type checking. With strict type cheking enabled, string representation of otherwise valid values will return false.

Return:

boolean

public static isReleaseNumber(val: *, strictTypeCheck: boolean): boolean source

Test if a given value is a release number.

Note that if strict type checking is not enabled, the empty string is considered a valid release number, being synonymous with zero in Moodle release strings.

Params:

NameTypeAttributeDescription
val *

the value to test.

strictTypeCheck boolean
  • optional
  • default: false

whether or not to enable strict type checking. With strict type cheking enabled, string representation of otherwise valid values will return false.

Return:

boolean

public static isReleaseSuffix(val: *): boolean source

Test if a given value is a valid release suffix.

Params:

NameTypeAttributeDescription
val *

the value to test.

Return:

boolean

See:

public static isReleaseType(val: *): boolean source

Test if a given value is a valid release type.

Params:

NameTypeAttributeDescription
val *

the value to test.

Return:

boolean

See:

public static numberFromReleaseType(rt: *): number source

Convert a release type to a number. Useful for version comparisons.

All invalid values convert to 0, 'development' to 1, 'official' to 2, and 'weekly' to 3.

Params:

NameTypeAttributeDescription
rt *

Return:

number

public static releaseSuffixFromReleaseType(rt: ReleaseType): ReleaseSuffix | undefined source

Convert a release type to a release suffix, e.g. 'weekly' to '+'.

Params:

NameTypeAttributeDescription
rt ReleaseType

public static releaseTypeFromReleaseSuffix(rs: ReleaseSuffix): ReleaseType | undefined source

Convert a relase suffix to a release type, e.g. '+' to 'weekly'.

Params:

NameTypeAttributeDescription
rs ReleaseSuffix

Public Constructors

public constructor(versionInfo: string | Object) source

By default version objects contain no information.

If a string is passed, the object is initialised using MoodleVersion.fromString, and if an object is passed then MoodleVersion.fromObject is used instead.

Params:

NameTypeAttributeDescription
versionInfo string | Object

Throw:

*

TypeError

*

RangeError

Public Members

public get branch: BranchString | undefined source

The major version part of the version number, officially known as the branch.

For example, the branch for each of the 3.4, 3.4+, 3.4.1, and 3.4.1+ releases is '3.4'.

public set branch: BranchString source

The branch (AKA major version) must be a string consisting of two digits separated by a period, e.g. '3.5'.

Setting the branch will also update the branching date to match.

To create an object with an un-known combination of branch and branching date, use the MoodleVersion.fromObject factory method.

Throw:

TypeError

RangeError

A range error is thrown if the branch does not have a known mapping to a branching date.

public get branchNumber: number | undefined source

The version's branch number, if known. This is the two-digit number used internally within the Moodle code to identify a branch, or major release.

For example, all Moodle 3.5.* releases will have the branch number 35.

public set branchNumber: BranchNumber | undefined source

The branch number must be a two-digit integer between 10 and 99.

Setting the branch number will also update the branching date to match.

To create an object with an un-known combination of branch and branching date, use the MoodleVersion.fromObject factory method.

Throw:

TypeError

RangeError

A range error is thrown if the branch does not have a known mapping to a branching date.

public get brancheNumbersByBranchingDateNumber: * source

The known mappings between branching date numbers and Moodle branch numbers.

@type{Map<DateNumber, BranchNumber>}

public get branchesByBranchingDateNumber: * source

The known mappings between branching date numbers and Moodle branches.

@type{Map<DateNumber, Branch>}

public get branchingDate: Date | undefined source

The branching date as a date object.

public set branchingDate source

Setting the branching date will update the branch to match.

To create an object with an un-known combination of branch and branching date, use the MoodleVersion.fromObject factory method.

@type{Date|undefined}

Throw:

TypeError

RangeError

A range error is thrown if the branching date does not have a known mapping to a branch.

public get branchingDateNumber: DateNumber | undefined source

The branching date as a DateNumber.

public set branchingDateNumber source

Setting the branching date will update the branch to match.

To create an object with an un-known combination of branch and branching date, use the MoodleVersion.fromObject factory method.

@type{DateNumber|undefined}

Throw:

TypeError

RangeError

A range error is thrown if the branching date does not have a known mapping to a branch.

public get branchingDateNumbersByBranch: * source

The known mappings between Moodle braches and branching date numbers.

@type{Map<Branch, DateNumber>}

public get branchingDateNumbersByBranchNumber: * source

The known mappings between Moodle brache numberss and branching date numbers.

@type{Map<BranchNumber, DateNumber>}

public get buildNumber: BuildNumber | undefined source

The build number.

public set buildNumber: ReleaseSuffix | undefined source

Build numbers must be valid date numbers, i.e. of the form YYYYMMDD.

Throw:

TypeError

public get release: ReleaseString source

The long human-friendly form of the version information.

In keeping with how Moodle presents version strings, release numbers of zero are omitted. If the release type is unknown no suffix is appended. If the branch is unknown it is represented as '??.??', if the release number is unknown it's represented as '.??', and if the build number is unknown it's represented as '????????'.

public get releaseNumber: ReleaseNumber | undefined source

The release number part of the version number.

public set releaseNumber: ReleaseNumber | undefined source

The release number must be an integer greater than or equal to zero.

Throw:

TypeError

public get releaseSuffix: ReleaseSuffix | undefined source

The release suffix for the release's type, e.g. '+' for weekly updates to the official releases.

public set releaseSuffix: ReleaseSuffix | undefined source

The release suffix must be one of 'dev', an empty string, or '+'. The value will get automatically cast to lower case before validation is applied.

Throw:

TypeError

public get releaseType: ReleaseType | undefined source

The release's type, e.g. 'development'.

public set releaseType: ReleaseType | undefined source

The release type must be one of 'development', 'official', or 'weekly'. The value will get automatically cast to lower case before validation is applied.

Throw:

TypeError

public get version: VersionString source

The short human-friendly form of the version number.

In keeping with how Moodle presents version strings, release numbers of zero are omitted. If the release type is unknown no suffix is appended. If the branch is unknown it is represented as '??.??', and if the release number is unknown it's represented as '.??'.

public get versionNumber: VersionNumber source

The under-the-hood form of the version number.

If the branch is unknown its replaced with eight question marks, and if the release number is unknown it's replaced with two.

Public Methods

public atLeast(mv: MoodleVersion): boolean | undefined source

Determine whether this version is greater than or equal to the given version.

Params:

NameTypeAttributeDescription
mv MoodleVersion

Return:

boolean | undefined

If the version is definitely greater or equal then true is returned, and if the version is definitely less than then false is returned. If the value is not a Moodle version object then undefined is returned.

public clone(): MoodleVersion source

Create a new Moodle version object representing the same version information.

Return:

MoodleVersion

public compareTo(mv: MoodleVersion): number source

Compare this version to another.

Params:

NameTypeAttributeDescription
mv MoodleVersion

Return:

number

-1 returned if passed version is lesser, 0 if the passed version is the same, and 1 if the passed version is greater. If the passed value is not a Moodle version object, NaN will be returned.

public equals(val: *): boolean source

Test if a given value is a Moodle Version object representing the same version.

Params:

NameTypeAttributeDescription
val *

Return:

boolean

public greaterThan(mv: MoodleVersion): boolean | undefined source

Determine whether this version is greater than a given version.

Params:

NameTypeAttributeDescription
mv MoodleVersion

Return:

boolean | undefined

If the version is definitely greater then true is returned, and if the version is equal or definitely less than then false is returned. If the value is not a Moodle version object then undefined is returned.

public isKnownBranch(): boolean source

Is this version on a branch the library knows about?

Return:

boolean

public isLTS(): boolean | undefined source

Determine whether or not this is version is on a long-term support branch. If the branch is not defined or unknown, undefined is returned.

Return:

boolean | undefined

public isStable(): boolean | undefined source

Is this a stable release? I.e. is the release type official or weekly?

Return:

boolean | undefined

Both official and weekly releases are considered stable, while development releases are not. If the release type is not defined, undefined is returned.

public lessThan(mv: MoodleVersion): boolean | undefined source

Determine whether this version is less than a given version.

Params:

NameTypeAttributeDescription
mv MoodleVersion

Return:

boolean | undefined

If the version is definitely lesser then true is returned, and if the version is equal or definitely greater then false is returned. If the value is not a Moodle version object then undefined is returned.

public sameBranch(mv: MoodleVersion): boolean | undefined source

Determine whether this versions is on the same branch as a given version.

Params:

NameTypeAttributeDescription
mv MoodleVersion

Return:

boolean | undefined

If the two versions share a branch then true is returned, if the branch numbers differ, false is returned. If the value passed is not a Moodle version object, or, the branch is undefined in both versions, undefined is returned.

public toObject(): Object source

The version as a plain object indexed by zero or more of:

  • version
  • versionNumber
  • release
  • branch
  • branchNumber
  • branchingDateNumber
  • branchingDate
  • releaseNumber
  • releaseType
  • releaseSuffix
  • buildNumber

Return:

Object

public toSemVerArray(): Array<number> source

An array containing a SemVer (Semantic Versioning) representation of the version information. The first element will be the major version number, the second the minor, and the third the patch.

Return:

Array<number>

An array of three integers.

public toSemVerObject(): {major: number, minor: number, patch: number} source

An object containing a SemVer (Semantic Versioning) representation of the version information.

Return:

{major: number, minor: number, patch: number}

public toString(): string source

Return a string representation of the version. The output will be of the form B.B.R[S] (type: T, branching date: BD & build: BN), e.g. 3.3.6 (type: official, branching date: 20170515 & build: 20180517). Undefined components will be rendered as ??.

Return:

string