Skip to content

Commit 9913fb7

Browse files
committed
Added docstrings for functions versioning.js
1 parent b5015cc commit 9913fb7

File tree

1 file changed

+106
-2
lines changed

1 file changed

+106
-2
lines changed

web/versioning.js

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ var DOC_VERSIONS = {};
33
var DOC_SORTED_VERSIONS = [];
44
var DOC_LATEST_VERSION = null;
55

6+
/**
7+
* Takes a string of the form <code>x.y.z[.suffix]</code> and returns a version
8+
* object with the following fields:
9+
* - <code>version</code>: a string containing the <code>x.y.z</code> part
10+
* - <code>nparts</code>: an array with x, y, and z in numeric form
11+
* - <code>suffix</code>: the <code>suffix</code> part or an empty string
12+
* - <code>raw</code>: the original string passed to this function
13+
*
14+
* @param {String} v A string in the form <code>x.y.z[.suffix]</code>
15+
* @return {Object} A version object with information about the input
16+
* version
17+
*/
618
function versionSplit(v) {
719
var parts = v.split(".", 4);
820

@@ -29,6 +41,20 @@ function versionSplit(v) {
2941
return {version: version, nparts: nparts, suffix: suffix, raw: v};
3042
}
3143

44+
/**
45+
* Compares the numeric parts of two version objects. Specifically,
46+
* it successively compares <code>v1.nparts[i]</code> with
47+
* <code>v2.nparts[i]</code> until one is larger than the other.
48+
* Sorting a list of version objects using this comparator results
49+
* in the latest version being the first.
50+
*
51+
* @param {Object} v1 The first version object to compare
52+
* @param {Object} v2 The second version object to compare
53+
* @return {Number} A negative number if v2 represents a version
54+
* smaller than v1 (ignoring the suffix), 0 if
55+
* equal (again, ignoring the suffix), and a
56+
* positive number otheriwse.
57+
*/
3258
function compareVN(v1, v2) {
3359
for (var i = 0; i < 3; i++) {
3460
var d = v1.nparts[i] - v2.nparts[i];
@@ -39,6 +65,16 @@ function compareVN(v1, v2) {
3965
return 0;
4066
}
4167

68+
/**
69+
* Compares two version objects. First, their numeric parts are compared
70+
* and the result returned if the numeric parts are not equal. If the
71+
* numeric parts are equal, their suffixes are compared.
72+
*
73+
* @param {Object} v1 The first version object to compare.
74+
* @param {Object} v2 The second version object to compare.
75+
* @return {Number} A negative number if v2 < v1, zero if
76+
* v2 == v1, and a positive number otherwise.
77+
*/
4278
function compareV(v1, v2) {
4379
var nd = compareVN(v1, v2);
4480
if (nd != 0) {
@@ -49,14 +85,41 @@ function compareV(v1, v2) {
4985
}
5086
}
5187

88+
89+
/**
90+
* Compares two version strings. It first parses them into version
91+
* objects then uses <code>compareV()</code>.
92+
*
93+
* @param {String} sv1 The first version string to compare
94+
* @param {String} sv2 The second version string to compare
95+
* @return {Number} A negative number if v2 < v1, zero if
96+
* v2 == v1, or a positive number if v2 > v1.
97+
*/
5298
function compareSV(sv1, sv2) {
5399
return compareV(versionSplit(sv1), versionSplit(sv2));
54100
}
55101

102+
103+
/**
104+
* Initializes various version related global variables.
105+
* The build process produces a <code>versions.js</code> file
106+
* which contains a list of raw version strings. These versions
107+
* are then parsed and the versions with the same numbers (but different
108+
* suffixes) are coalesced into a single object with a pointer to the
109+
* version with the latest suffix. The idea is that we only present the
110+
* numeric version to the users, but always point to the latest version
111+
* with that numeric prefix. For example, the latest of "0.1.0",
112+
* "0.1.0.post1", and "0.1.0.post2" is "0.1.0.post2", but the user only
113+
* sees "0.1.0".
114+
*
115+
* This function also selects the latest version which is to be displayed
116+
* by default.
117+
*
118+
*/
56119
function initVersions() {
57-
/* We need to sort them, coalesce 0.1.0* to 0.1.0 and make sure 0.1.0
120+
/* We need to sort them, coalesce 0.1.0* to 0.1.0 and make sure 0.1.0
58121
points to the latest in the set of 0.1.0'es. We also set DOC_LATEST_VERSION.
59-
The order is numeric for the numeric parts and lexicographic for the
122+
The order is numeric for the numeric parts and lexicographic for the
60123
trailing part. */
61124
var vs = {};
62125

@@ -81,14 +144,40 @@ function initVersions() {
81144
DOC_VERSIONS = vs;
82145
}
83146

147+
/**
148+
* Returns the numeric part of the latest version that can be selected.
149+
* For example, if all versions are "0.1.0", "0.1.0.post1", "0.9.0", and,
150+
* "0.9.0.post1", this function will return "0.9.0" since it is the numeric
151+
* part of the latest version, which is "0.9.0.post1".
152+
*
153+
* @return {String} A string representing the latest version that can
154+
* be selected by a user.
155+
*/
84156
function getLatestVersion() {
85157
return DOC_LATEST_VERSION;
86158
}
87159

160+
/**
161+
* Returns a list with the numeric parts of all versions, with the most recent
162+
* version occupying the first position in the list. For example, if all
163+
* versions are "0.1.0", "0.1.0.post1", "0.9.0", and, "0.9.0.post1" then this
164+
* function will return <code>["0.9.0", "0.1.0"]</code>.
165+
*
166+
* @return {Array} A list of version numbers.
167+
*/
88168
function getSortedVersionLabels() {
89169
return DOC_SORTED_VERSIONS.map(v => v.version);
90170
}
91171

172+
/**
173+
* Splits the current URL around the documentation version. It returns
174+
* an array with three elements, the first being the part before the version,
175+
* the second being the version, and the third being the part after the
176+
* version. For example, the url will be
177+
* split into <code>["abc.com/docs/v", "0.1.0", "path"]</code>.
178+
*
179+
* @return {Array} A list of URL parts as described above.
180+
*/
92181
function splitLocation() {
93182
var url = window.location.href;
94183

@@ -108,12 +197,27 @@ function splitLocation() {
108197
}
109198
}
110199

200+
/**
201+
* Replaces the current version in the URL with the version selected in the
202+
* version selector (<code>this.version</code>). For example, if the current
203+
* URL is <code>"abc.com/docs/v/0.1.0/path"</code> and
204+
* <code>this.version == "0.9.0"</code>, then this function will redirect
205+
* the browser to <code>"abc.com/docs/v/0.9.0/path"</code>.
206+
*/
111207
function setVersion() {
112208
var s = splitLocation();
113209

114210
window.location = s[0] + "/" + DOC_VERSIONS[this.version].latest.raw + "/" + s[2];
115211
}
116212

213+
/**
214+
* Returns the numeric part of the current version being displayed.
215+
* For example, if the current URL is "abc.com/docs/v/0.1.0.post2/path",
216+
* this method will return "0.1.0".
217+
*
218+
* @return {String} A string representing the numeric part of the
219+
* currently displayed documentation version.
220+
*/
117221
function getCurrentVersion() {
118222
v = splitLocation()[1];
119223
if (v == "") {

0 commit comments

Comments
 (0)