User:Ohconfucius/test/MOSNUM utils.js: Difference between revisions

Content deleted Content added
add code documentation, tweaked docs on regex method
use 'token' terminology consistently
Line 44:
* The tokens can be either capturing or non-capturing. The capturing
* tokens make their output available for later processing. Most of the
* capturing magic stringstokens can be used in the output with equivalent meaning.
* meaning.
*
* Internally, the routine maintains information about several dates, so
Line 61 ⟶ 60:
* provided as alias to @XX for convenience.
*
* AVAILABLE REGEX MAGIC STRINGSTOKENS
* =============================
* Note: The capturing tokens start with an uppercase letter whereas the
* equivalent non-capturing tokens start with a lowercase letter.
Line 93 ⟶ 92:
* • @@ : Matches a literal @.
*
* AVAILABLE REPLACEMENT STRING MAGIC STRINGSTOKENS
* ==========================================
* Days:
* • @SD : Outputs a day in numeric format without leading zero (1-31).
Line 131 ⟶ 130:
* of which is an object defining the nth date as parsed by the routine. Each
* object contains numeric values of days, months and years as 'd', 'm' and 'y'
* properties respectively. Each value can be -1 if a magic stringtoken for that date
* date value was not specified in the regex, or an error occurs. The function
* should return true if the replacement should be done, false otherwise.
*
Line 180 ⟶ 179:
var Formats = [
{ type : ParamType.SD, group : Group.DAY, magictoken : "@SD", match : /([1-9]|[1-2][0-9]|30|31)/ },
{ type : ParamType.ZD, group : Group.DAY, magictoken : "@ZD", match : /(0[1-9]|[1-2][0-9]|30|31)/ },
{ type : ParamType.DD, group : Group.DAY, magictoken : "@DD", match : /(0?[1-9]|[1-2][0-9]|30|31)/ },
{ type : ParamType.DAY,group : Group.DAY, magictoken : "@Day",match : /((?:[012]?[1-9]|10|20|30|31)(?:st|nd|rd|th|)?)/ },
{ type : ParamType.SM, group : Group.MONTH,magictoken : "@SM", match : /([1-9]|10|11|12)/ },
{ type : ParamType.ZM, group : Group.MONTH,magictoken : "@ZM", match : /(0[1-9]|10|11|12)/ },
{ type : ParamType.MM, group : Group.MONTH,magictoken : "@MM", match : /(0?[1-9]|10|11|12)/ },
{
type : ParamType.FMONTH, group : Group.MONTH, magictoken : "@FullMonth",
match : /(January|February|March|April|May|June|July|August|September|October|November|December)/
},
{
type : ParamType.MONTH, group : Group.MONTH, magictoken : "@Month",
match : /(January|February|March|April|May|June|July|August|September|October|November|December|Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ //must be after month entry
type : ParamType.MON, group : Group.MONTH, magictoken : "@Mon",
match : /(Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ type : ParamType.YYYY, group : Group.YEAR, magictoken : "@YYYY", match : "([1-2][0-9]{3})" },
{ type : ParamType.YYNN, group : Group.YEAR, magictoken : "@YYNN", match : "([1-2][0-9]{3}|[0-9]{2})" },
{ type : ParamType.YY, group : Group.YEAR, magictoken : "@YY", match : "([0-9]{2})" }, //must be after yyyy and yy24 entries
{ type : ParamType.YEAR, group : Group.YEAR, magictoken : "@Year", match : "([1-2][0-9]{3}|[1-9][0-9]{0,2})" }
];
var NCFormats = [
{ magictoken : "@sd", match : /(?:[1-9]|[1-2][0-9]|30|31)/ },
{ magictoken : "@zd", match : /(?:0[1-9]|[1-2][0-9]|30|31)/ },
{ magictoken : "@dd", match : /(?:0?[1-9]|[1-2][0-9]|30|31)/ },
{ magictoken : "@day",match : /(?:(?:[012]?[1-9]|10|20|30|31)(?:st|nd|rd|th|)?)/ },
{ magictoken : "@sm", match : /(?:[1-9]|10|11|12)/ },
{ magictoken : "@zm", match : /(?:0[1-9]|10|11|12)/ },
{ magictoken : "@mm", match : /(?:0?[1-9]|10|11|12)/ },
{
magictoken : "@fullmonth",
match : /(?:January|February|March|April|May|June|July|August|September|October|November|December)/
},
{
magictoken : "@month",
match : /(?:January|February|March|April|May|June|July|August|September|October|November|December|Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ //must be after month entry
magictoken : "@mon",
match : /(?:Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ magictoken : "@yyyy", match : /(?:[1-2][0-9]{3})/ },
{ magictoken : "@yynn", match : "(?:[1-2][0-9]{3}|[0-9]{2})" },
{ magictoken : "@yy", match : "(?:[0-9]{2})" }, //must be after yyyy and yy24 entries
{ magictoken : "@year", match : "(?:[1-2][0-9]{3}|[1-9][0-9]{0,2})" },
// misc
{ magictoken : "@th", match : "(?:th|st|nd|rd)" }
];
Line 248 ⟶ 247:
}
// get positions of all capturing magic stringstokens in the regex
var magic_per_grouptoken_per_group = [0,0,0];
for (var i = 0; i < Formats.length; i++) {
var index = -1;
while (1) {
index = reg.indexOf(Formats[i].magictoken, index+1);
if (index == -1)
break;
if (params_by_index[index] === undefined) {
if (magic_per_grouptoken_per_group[index] > MAX_DATE) {
alert("DATE SCRIPT: unsupported number of dates from the same group");
return;
Line 265 ⟶ 264:
param.index = index;
param.type = Formats[i].type;
param.num = magic_per_grouptoken_per_group[Formats[i].group];
params_by_index[index] = param;
magic_per_grouptoken_per_group[Formats[i].group]++;
}
}
Line 279 ⟶ 278:
param_desc.sort(function(a,b) {return a.index - b.index;});
//replace magic stringstokens with proper matches
for (var i = 0; i < Formats.length; i++) {
reg = reg.split(Formats[i].magictoken).join(ohc.dateutil.regex_to_string(Formats[i].match));
}
for (var i = 0; i < NCFormats.length; i++) {
reg = reg.split(NCFormats[i].magictoken).join(ohc.dateutil.regex_to_string(NCFormats[i].match));
}
reg = reg.split("@@").join("@");