Tobias Gritschacher has submitted this change and it was merged.
Change subject: (bug 48145) Splits time.js in several files (added
"time.Time.js" and "time.Time.parse.js")
......................................................................
(bug 48145) Splits time.js in several files (added "time.Time.js" and
"time.Time.parse.js")
This change doesn't add any improvements, it just moves the code for clear
separation of different
tasks. This also adds some comments and TODOs. The different fields in the
time's "settings" field
are now exposed as simple fields, not functions as before.
Change-Id: I96df9a7dc7ff5202418ea29efb294004ce4a48b9
---
M DataValues/DataValues.resources.php
A DataValues/resources/time.js/time.Time.js
A DataValues/resources/time.js/time.Time.parse.js
M DataValues/resources/time.js/time.js
4 files changed, 462 insertions(+), 517 deletions(-)
Approvals:
Tobias Gritschacher: Looks good to me, approved
jenkins-bot: Verified
diff --git a/DataValues/DataValues.resources.php
b/DataValues/DataValues.resources.php
index 3177971..70eff85 100644
--- a/DataValues/DataValues.resources.php
+++ b/DataValues/DataValues.resources.php
@@ -81,6 +81,15 @@
),
),
+ // time.js
+ 'time.js' => $moduleTemplate + array(
+ 'scripts' => array(
+ 'time.js/time.js',
+ 'time.js/time.Time.js',
+ 'time.js/time.Time.parse.js',
+ )
+ ),
+
// qunit-parameterize from
https://github.com/AStepaniuk/qunit-parameterize
'qunit.parameterize' => $moduleTemplate + array(
'scripts' => array(
diff --git a/DataValues/resources/time.js/time.Time.js
b/DataValues/resources/time.js/time.Time.js
new file mode 100644
index 0000000..cf393cd
--- /dev/null
+++ b/DataValues/resources/time.js/time.Time.js
@@ -0,0 +1,156 @@
+/**
+ * time.js's Time constructor for parsing and representing a time.
+ *
+ * @author Denny Vrandečić
+ * @author Daniel Werner < [email protected] >
+ */
+time.Time = ( function( time ) {
+ 'use strict';
+
+ /**
+ * Constructor for object representing a point in time with a certain
precision.
+ *
+ * @param {string} inputtext Text to be interpreted as time.
+ * @param {number} explicitPrecision Precission which will overrule the
precision
+ */
+ function Time( inputtext, explicitPrecision ) {
+ this.getInputtext = function() {
+ return inputtext;
+ };
+
+ // TODO: doing parsing here is bad practice, constructor should
instead take parser output.
+ // This way we never get an invalid Time object and can throw
errors if wrong arguments
+ // are provided to the constructor.
+ var result = time.Time.parse( inputtext );
+ if( result === null ) {
+ result = {};
+ }
+
+ var bce = (result.bce !== undefined) ? result.bce : false,
+ year = (result.year !== undefined) ? result.year : null,
+ month = (result.month !== undefined) ? result.month : 1,
+ day = (result.day !== undefined) ? result.day : 1,
+ hour = (result.hour !== undefined) ? result.hour : 0,
+ minute = (result.minute !== undefined) ? result.minute
: 0,
+ second = (result.second !== undefined) ? result.second
: 0,
+ utcoffset = '+00:00',
+ calendarname = (result.calendarname !== undefined) ?
result.calendarname : 'Gregorian';
+
+ this.year = function() {
+ return year;
+ };
+
+ this.month = function() {
+ return month;
+ };
+
+ this.day = function() {
+ return day;
+ };
+
+ this.utcoffset = function() {
+ return utcoffset;
+ };
+
+ var precision = explicitPrecision !== undefined ?
explicitPrecision : result.precision;
+ this.precision = function() {
+ return precision;
+ };
+
+ this.precisionText = function() {
+ return time.precisionText( precision );
+ };
+
+ var before = 0,
+ after = 0;
+
+ this.before = function() {
+ return before;
+ };
+
+ this.after = function() {
+ return after;
+ };
+
+ this.gregorian = function() {
+ if( calendarname === 'Gregorian' ) {
+ return {
+ 'year': year,
+ 'month': month,
+ 'day': day
+ };
+ } else if( calendarname === 'Julian' ) {
+ return time.julianToGregorian( year, month, day
);
+ }
+ };
+
+ this.julian = function() {
+ if( calendarname === 'Julian' ) {
+ return {
+ 'year': year,
+ 'month': month,
+ 'day': day
+ };
+ } else if( calendarname === 'Gregorian' ) {
+ if( year !== null ) {
+ return time.gregorianToJulian( year,
month, day );
+ }
+ }
+ return null;
+ };
+
+ this.jdn = function() {
+ if( year === null ) {
+ return null;
+ }
+ if( calendarname === 'Gregorian' ) {
+ return time.gregorianToJulianDay( year, month,
day );
+ } else {
+ return time.julianToJulianDay( year, month, day
);
+ }
+ };
+
+ this.calendarText = function() {
+ return calendarname;
+ };
+
+ this.calendarURI = function() {
+ if( calendarname === 'Gregorian' ) {
+ return 'http://wikidata.org/id/Q1985727';
+ } else if( calendarname === 'Julian' ) {
+ return 'http://wikidata.org/id/Q1985786';
+ }
+ };
+
+ this.iso8601 = function() {
+ var g = this.gregorian();
+ return ( ( g.year < 0 ) ? '-' : '+' ) + pad( g.year, 11
) + '-' + pad( g.month, 2 )
+ + '-' + pad( g.day, 2 ) + 'T' + pad( hour, 2 )
+ ':' + pad( minute, 2 )
+ + ':' + pad( second, 2 ) + 'Z';
+ };
+
+ this.text = function() {
+ return time.getTextFromDate( precision, year, month,
day );
+ };
+
+ this.gregorianText = function() {
+ var result = this.gregorian();
+ return time.getTextFromDate( precision, result.year,
result.month, result.day );
+ };
+
+ this.julianText = function() {
+ var result = this.julian();
+ if( result === null ) {
+ return '';
+ }
+ return time.getTextFromDate( precision, result.year,
result.month, result.day );
+ };
+ }
+
+ function pad( number, digits ) {
+ return ( 1e12 + Math.abs( number ) + '' ).slice( -digits );
+ }
+
+ return Time; // expose time.Time
+
+}( time ) );
diff --git a/DataValues/resources/time.js/time.Time.parse.js
b/DataValues/resources/time.js/time.Time.parse.js
new file mode 100644
index 0000000..921ccf9
--- /dev/null
+++ b/DataValues/resources/time.js/time.Time.parse.js
@@ -0,0 +1,288 @@
+/**
+ * time.js's Time parser.
+ *
+ * @author Denny Vrandečić
+ * @author Daniel Werner < [email protected] >
+ */
+time.Time.parse = ( function( time ) {
+ 'use strict';
+
+ var settings = time.settings;
+
+ function parse( text ) {
+ var tokens = tokenize( text ),
+ retval = {},
+ result = matchGrammars( [
+ 'y', '-y', 'my', 'm-y', 'yb', 'myb', 'mdy',
'md-y', 'dmy', 'dm-y',
+ 'mdyb', 'dmyb', 'mdyc', ',md-yc', 'dmyc',
'dm-yc',
+ 'mdybc', 'dmybc', 'ymd', '-ymd', 'ym', '-ym'
+ ], tokens );
+
+ if( result === null ) {
+ return null;
+ }
+
+ if( result.minus !== undefined ) {
+ result.year = result.year * -1;
+ }
+ if( result.bce !== undefined ) {
+ if( result.year < 1 ) {
+ return;
+ }
+ retval.bce = result.bce;
+ if( result.bce ) {
+ result.year = -1 * (result.year - 1);
+ }
+ }
+ if( result.year !== undefined ) {
+ retval.year = result.year;
+ var temp = result.year;
+ if( retval.bce ) {
+ temp -= 1;
+ }
+ if( result.year < 1 ) {
+ retval.bce = true;
+ }
+ retval.precision = 9;
+ if( (temp < -1500) || (temp > 5000) ) {
+ while( temp % 10 === 0 ) {
+ temp /= 10;
+ retval.precision -= 1;
+ }
+ }
+ }
+ if( result.month !== undefined ) {
+ retval.month = result.month;
+ retval.precision = 10;
+ }
+ if( result.day !== undefined ) {
+ retval.day = result.day;
+ retval.precision = 11;
+ }
+ if( result.calendar !== undefined ) {
+ retval.calendarname = result.calendar;
+ } else if( (result.year < 1583) && (retval.precision > 10) ) {
+ retval.calendarname = 'Julian';
+ } else {
+ retval.calendarname = 'Gregorian';
+ }
+
+ return retval;
+ }
+
+ function matchGrammars( grammars, tokens ) {
+ var result = null;
+ for( var i = 0; i < grammars.length; i++ ) {
+ result = matchGrammar( grammars[i], tokens );
+ if( result !== null ) {
+ return result;
+ }
+ }
+ return null;
+ }
+
+ function matchGrammar( grammar, tokens ) {
+ var result = {};
+ if( grammar.length !== tokens.length ) {
+ return null;
+ }
+
+ for( var i = 0; i < grammar.length; i++ ) {
+ if( tokens[i] === null ) {
+ return null;
+ }
+ if( grammar[i] === 'y' ) {
+ if( (tokens[i].type === 'number') ||
(tokens[i].type === 'year') ) {
+ result.year = tokens[i].val;
+ continue;
+ } else {
+ return null;
+ }
+ }
+ if( grammar[i] === 'm' ) {
+ if( ( ( tokens[i].type === 'number' ) ||
(tokens[i].type === 'month') )
+ && tokens[i].month
+ ) {
+ result.month = tokens[i].val;
+ continue;
+ } else {
+ return null;
+ }
+ }
+ if( grammar[i] === 'd' ) {
+ if( ((tokens[i].type === 'number') ||
(tokens[i].type === 'day')) && tokens[i].day ) {
+ result.day = tokens[i].val;
+ continue;
+ } else {
+ return null;
+ }
+ }
+ if( grammar[i] === 'c' ) {
+ if( tokens[i].type === 'calendar' ) {
+ result.calendar = tokens[i].val;
+ continue;
+ } else {
+ return null;
+ }
+ }
+ if( grammar[i] === 'b' ) {
+ if( tokens[i].type === 'bce' ) {
+ result.bce = tokens[i].val;
+ continue;
+ } else {
+ return null;
+ }
+ }
+ if( grammar[i] === '-' ) {
+ if( tokens[i].type === 'minus' ) {
+ if( grammar[ i + 1 ] === 'y' ) {
+ result.minus = true;
+ }
+ continue;
+ } else {
+ return null;
+ }
+ }
+ return null;
+ }
+ return result;
+ }
+
+ function tokenize( s ) {
+ var result = [],
+ token = '',
+ minus = {
+ 'type': 'minus',
+ 'val': '-'
+ };
+
+ for( var i = 0; i < s.length; i++ ) {
+ if( /[\s,\.\/-]/.test( s[i] ) ) {
+ if( token === '' ) {
+ if( s[i] === '-' ) {
+ result.push( minus );
+ }
+ continue;
+ }
+ var analysis = analyze( token );
+ if( analysis !== null ) {
+ result.push( analysis );
+ token = '';
+ continue;
+ }
+ if( s[i] === '-' ) {
+ result.push( analysis );
+ result.push( minus );
+ token = '';
+ continue;
+ }
+ token += s[i];
+ continue;
+ }
+ if( fullMatch( token, /\d+/ ) && !/\d/.test( s[i] ) ) {
+ if( token !== '' ) {
+ result.push( analyze( token ) );
+ }
+ token = '';
+ }
+ token += s[i];
+ }
+ if( token !== '' ) {
+ result.push( analyze( token ) );
+ }
+ return result;
+ }
+
+ function analyze( t ) {
+ if( !fullMatch( t, /\d{1,11}/ ) ) {
+ return testString( t );
+ }
+ var v = parseInt( t, 10 ),
+ day = (t > 0) && (t < 32),
+ month = (t > 0) && (t < 13),
+ type = 'number';
+
+ if( !day && !month ) {
+ type = 'year';
+ }
+ return {
+ 'val': v,
+ 'type': type,
+ 'month': month,
+ 'day': day
+ };
+ }
+
+ function testString( s ) {
+ var v = readAsMonth( s );
+ if( v !== null ) {
+ return {
+ 'val': v,
+ 'type': 'month',
+ 'month': true
+ };
+ }
+ v = readAsBCE( s );
+ if( v !== null ) {
+ return {
+ 'val': v,
+ 'type': 'bce'
+ };
+ }
+ v = readAsCalendar( s );
+ if( v !== null ) {
+ return {
+ 'val': v,
+ 'type': 'calendar'
+ };
+ }
+ return null;
+ }
+
+ function readAsMonth( word ) {
+ for( var i = 0; i < settings.monthnames.length; i++ ) {
+ for( var j = 0; j < settings.monthnames[i].length; j++
) {
+ if( settings.monthnames[i][j].toLowerCase() ===
word.toLowerCase() ) {
+ return i + 1;
+ }
+ }
+ }
+ return null;
+ };
+
+ function readAsBCE( word ) {
+ for( var i = 0; i < settings.bce.length; i++ ) {
+ if( settings.bce[i].toLowerCase() ===
word.toLowerCase() ) {
+ return true;
+ }
+ }
+ for( var i = 0; i < settings.ace.length; i++ ) {
+ if( settings.ace[i].toLowerCase() ===
word.toLowerCase() ) {
+ return false;
+ }
+ }
+ return null;
+ };
+
+ function readAsCalendar( word ) {
+ for( var i = 0; i < settings.calendarnames.length; i++ ) {
+ for( var j = 0; j < settings.calendarnames[i].length;
j++ ) {
+ if( settings.calendarnames[i][j].toLowerCase()
=== word.toLowerCase() ) {
+ return settings.calendarnames[i][0];
+ }
+ }
+ }
+ return null;
+ };
+
+ function fullMatch( str, reg ) {
+ var matches = reg.exec( str );
+ if( matches === null ) {
+ return false;
+ }
+ return str === matches[0];
+ }
+
+ return parse; // expose time.parse
+
+}( time ) );
diff --git a/DataValues/resources/time.js/time.js
b/DataValues/resources/time.js/time.js
index 6a57b4d..f5f0122 100644
--- a/DataValues/resources/time.js/time.js
+++ b/DataValues/resources/time.js/time.js
@@ -5,15 +5,15 @@
* @licence GNU GPL v2+
* @author Denny Vrandečić
*/
-( function( window ) {
+( function( global ) {
var time = {},
- _oldTime = window.time;
+ _oldTime = global.time;
- window.time = time;
+ global.time = time;
time.noConflict = function() {
- window.time = _oldTime;
+ global.time = _oldTime;
return time;
};
@@ -30,6 +30,7 @@
settings.calendarnames[1] = [
'Julian', 'J', 'JD', 'JC', 'OS', 'O.S.', 'Old Style', 'Julian
calendar', 'Julian date'
];
+
settings.monthnames = [];
settings.monthnames[0] = ['January', 'Jan'];
settings.monthnames[1] = ['February', 'Feb'];
@@ -43,6 +44,7 @@
settings.monthnames[9] = ['October', 'Oct'];
settings.monthnames[10] = ['November', 'Nov'];
settings.monthnames[11] = ['December', 'Dec'];
+
settings.precisiontexts = [];
settings.precisiontexts[0] = 'billion years';
settings.precisiontexts[1] = 'hundred million years';
@@ -59,6 +61,7 @@
settings.precisiontexts[12] = 'hour';
settings.precisiontexts[13] = 'minute';
settings.precisiontexts[14] = 'second';
+
settings.outputprecision = [];
settings.outputprecision[0] = '% billion years';
settings.outputprecision[1] = '%00 million years';
@@ -72,232 +75,6 @@
var maxPrecision = function() {
return 14;
- };
-
- var isSane = function( text ) {
- if( text.indexOf( '<' ) > -1 ) {
- return false;
- }
- if( text.indexOf( '>' ) > -1 ) {
- return false;
- }
- return true;
- };
-
- var settingArray = function( name, texts ) {
- if( texts === undefined ) {
- return settings[name].slice( 0 );
- }
- if( !Array.isArray( texts ) ) {
- throw 'Parameter to set should be an array of strings';
- }
- if( texts.length === 0 ) {
- throw 'Parameter to set should be an array of strings';
- }
- for( var i = 0; i < texts.length; i++ ) {
- if( typeof texts[i] !== 'string' ) {
- throw 'Parameter to set should be an array of
strings';
- }
- if( !isSane( texts[i] ) ) {
- throw 'String ' + texts[i] + ' is not
considered sane';
- }
- }
- settings[name] = texts.slice( 0 );
- return settings[name].slice( 0 );
- };
- var bce = function( texts ) {
- return settingArray( 'bce', texts );
- };
- var ace = function( texts ) {
- return settingArray( 'ace', texts );
- };
- var precisionTexts = function( texts ) {
- if( texts.length !== 1 + maxPrecision() ) {
- throw 'Incorrect number of strings';
- }
- return settingArray( 'precisiontexts', texts );
- };
- var outputPrecision = function( texts ) {
- if( texts.length !== settins.outputprecision.length ) {
- throw 'Incorrect number of strings';
- }
- return settingArray( 'outputprecision', texts );
- };
-
- var settingText = function( name, text ) {
- if( text === undefined ) {
- return settings[name];
- }
- if( typeof text !== 'string' ) {
- throw 'Parameter to set should be a string';
- }
- if( !isSane( text ) ) {
- throw 'String ' + text + ' is not considered sane';
- }
- settings[name] = text;
- return settings[name];
- };
- var pastText = function( texts ) {
- return settingText( 'pasttext', texts );
- };
- var futureText = function( texts ) {
- return settingText( 'futuretext', texts );
- };
-
- var settingArrayOfArrays = function( name, index, maxindex, texts ) {
- if( !((index >= 0) && (index <= maxindex)) ) {
- throw 'Index out of range';
- }
- if( texts === undefined ) {
- return settings[name][index].slice( 0 );
- }
- if( !Array.isArray( texts ) ) {
- throw 'Parameter to set should be an array of strings';
- }
- if( texts.length === 0 ) {
- throw 'Parameter to set should be an array of strings';
- }
- for( var i = 0; i < texts.length; i++ ) {
- if( typeof texts[i] !== 'string' ) {
- throw 'Parameter to set should be an array of
strings';
- }
- if( !isSane( texts[i] ) ) {
- throw 'String ' + texts[i] + ' is not
considered sane';
- }
- }
- settings[name][index] = texts.slice( 0 );
- return settings[name][index].slice( 0 );
- };
- var calendarNames = function( index, texts ) {
- return settingArrayOfArrays( 'calendarnames', index, 1, texts );
- };
- var monthNames = function( index, texts ) {
- return settingArrayOfArrays( 'monthnames', index, 11, texts );
- };
-
- var pad = function( number, digits ) {
- return ( 1e12 + Math.abs( number ) + '' ).slice( -digits );
- };
-
- var Time = function( inputtext, precision ) {
- var inputprecision = precision;
-
- this.getInputtext = function() {
- return inputtext;
- };
-
- var result = parse( inputtext );
- if( result === null ) {
- result = {};
- }
-
- var bce = (result.bce !== undefined) ? result.bce : false,
- year = (result.year !== undefined) ? result.year : null,
- month = (result.month !== undefined) ? result.month : 1,
- day = (result.day !== undefined) ? result.day : 1,
- hour = (result.hour !== undefined) ? result.hour : 0,
- minute = (result.minute !== undefined) ? result.minute
: 0,
- second = (result.second !== undefined) ? result.second
: 0,
- utcoffset = '+00:00',
- calendarname = (result.calendarname !== undefined) ?
result.calendarname : 'Gregorian';
-
- this.year = function() {
- return year;
- };
- this.month = function() {
- return month;
- };
- this.day = function() {
- return day;
- };
- this.utcoffset = function() {
- return utcoffset;
- };
-
- var precision = (inputprecision !== undefined) ? inputprecision
: result.precision;
- this.precision = function() {
- return precision;
- };
- this.precisionText = function() {
- return precisionText( precision );
- };
- var before = 0,
- after = 0;
- this.before = function() {
- return before;
- };
- this.after = function() {
- return after;
- };
-
- this.gregorian = function() {
- if( calendarname === 'Gregorian' ) {
- return {
- 'year': year,
- 'month': month,
- 'day': day
- };
- } else if( calendarname === 'Julian' ) {
- return julianToGregorian( year, month, day );
- }
- };
- this.julian = function() {
- if( calendarname === 'Julian' ) {
- return {
- 'year': year,
- 'month': month,
- 'day': day
- };
- } else if( calendarname === 'Gregorian' ) {
- if( year !== null ) {
- return gregorianToJulian( year, month,
day );
- }
- }
- return null;
- };
- this.jdn = function() {
- if( year === null ) {
- return null;
- }
- if( calendarname === 'Gregorian' ) {
- return gregorianToJulianDay( year, month, day );
- } else {
- return julianToJulianDay( year, month, day );
- }
- };
-
- this.calendarText = function() {
- return calendarname;
- };
- this.calendarURI = function() {
- if( calendarname === 'Gregorian' ) {
- return 'http://wikidata.org/id/Q1985727';
- } else if( calendarname === 'Julian' ) {
- return 'http://wikidata.org/id/Q1985786';
- }
- }
-
- this.iso8601 = function() {
- var g = this.gregorian();
- return ( ( g.year < 0 ) ? '-' : '+' ) + pad( g.year, 11
) + '-' + pad( g.month, 2 )
- + '-' + pad( g.day, 2 ) + 'T' + pad( hour, 2 )
+ ':' + pad( minute, 2 )
- + ':' + pad( second, 2 ) + 'Z';
- };
-
- this.text = function() {
- return getTextFromDate( precision, year, month, day );
- };
- this.gregorianText = function() {
- var result = this.gregorian();
- return getTextFromDate( precision, result.year,
result.month, result.day );
- };
- this.julianText = function() {
- var result = this.julian();
- if( result === null ) {
- return '';
- }
- return getTextFromDate( precision, result.year,
result.month, result.day );
- };
};
var julianToJulianDay = function( year, month, day ) {
@@ -358,281 +135,6 @@
var gregorianToJulian = function( year, month, day ) {
var julianday = gregorianToJulianDay( year, month, day );
return julianDayToJulian( julianday );
- };
-
- var readAsMonth = function( word ) {
- for( var i = 0; i < settings.monthnames.length; i++ ) {
- for( var j = 0; j < settings.monthnames[i].length; j++
) {
- if( settings.monthnames[i][j].toLowerCase() ===
word.toLowerCase() ) {
- return i + 1;
- }
- }
- }
- return null;
- };
-
- var readAsBCE = function( word ) {
- for( var i = 0; i < settings.bce.length; i++ ) {
- if( settings.bce[i].toLowerCase() ===
word.toLowerCase() ) {
- return true;
- }
- }
- for( var i = 0; i < settings.ace.length; i++ ) {
- if( settings.ace[i].toLowerCase() ===
word.toLowerCase() ) {
- return false;
- }
- }
- return null;
- };
-
- var readAsCalendar = function( word ) {
- for( var i = 0; i < settings.calendarnames.length; i++ ) {
- for( var j = 0; j < settings.calendarnames[i].length;
j++ ) {
- if( settings.calendarnames[i][j].toLowerCase()
=== word.toLowerCase() ) {
- return settings.calendarnames[i][0];
- }
- }
- }
- return null;
- };
-
- var testString = function( s ) {
- var v = readAsMonth( s );
- if( v !== null ) {
- return {
- 'val': v,
- 'type': 'month',
- 'month': true
- };
- }
- v = readAsBCE( s );
- if( v !== null ) {
- return {
- 'val': v,
- 'type': 'bce'
- };
- }
- v = readAsCalendar( s );
- if( v !== null ) {
- return {
- 'val': v,
- 'type': 'calendar'
- };
- }
- return null;
- };
-
- var fullMatch = function( str, reg ) {
- var matches = reg.exec( str );
- if( matches === null ) {
- return false;
- }
- return str === matches[0];
- };
-
- var analyze = function( t ) {
- if( fullMatch( t, /\d{1,11}/ ) ) {
- var v = parseInt( t ),
- day = (t > 0) && (t < 32),
- month = (t > 0) && (t < 13),
- type = 'number';
-
- if( !day && !month ) {
- type = 'year';
- }
- return {
- 'val': v,
- 'type': type,
- 'month': month,
- 'day': day
- };
- } else {
- return testString( t );
- }
- };
-
- var tokenize = function( s ) {
- var result = [],
- token = '',
- minus = {
- 'type': 'minus',
- 'val': '-'
- };
-
- for( var i = 0; i < s.length; i++ ) {
- if( /[\s,\.\/-]/.test( s[i] ) ) {
- if( token === '' ) {
- if( s[i] === '-' ) {
- result.push( minus );
- }
- continue;
- }
- var analysis = analyze( token );
- if( analysis !== null ) {
- result.push( analysis );
- token = '';
- continue;
- }
- if( s[i] === '-' ) {
- result.push( analysis );
- result.push( minus );
- token = '';
- continue;
- }
- token += s[i];
- continue;
- }
- if( fullMatch( token, /\d+/ ) && !/\d/.test( s[i] ) ) {
- if( token !== '' ) {
- result.push( analyze( token ) );
- }
- token = '';
- }
- token += s[i];
- }
- if( token !== '' ) {
- result.push( analyze( token ) );
- }
- return result;
- };
-
- var matchGrammar = function( grammar, tokens ) {
- var result = {};
- if( grammar.length !== tokens.length ) {
- return null;
- }
-
- for( var i = 0; i < grammar.length; i++ ) {
- if( tokens[i] === null ) {
- return null;
- }
- if( grammar[i] === 'y' ) {
- if( (tokens[i].type === 'number') ||
(tokens[i].type === 'year') ) {
- result.year = tokens[i].val;
- continue;
- } else {
- return null;
- }
- }
- if( grammar[i] === 'm' ) {
- if( ( ( tokens[i].type === 'number' ) ||
(tokens[i].type === 'month') )
- && tokens[i].month
- ) {
- result.month = tokens[i].val;
- continue;
- } else {
- return null;
- }
- }
- if( grammar[i] === 'd' ) {
- if( ((tokens[i].type === 'number') ||
(tokens[i].type === 'day')) && tokens[i].day ) {
- result.day = tokens[i].val;
- continue;
- } else {
- return null;
- }
- }
- if( grammar[i] === 'c' ) {
- if( tokens[i].type === 'calendar' ) {
- result.calendar = tokens[i].val;
- continue;
- } else {
- return null;
- }
- }
- if( grammar[i] === 'b' ) {
- if( tokens[i].type === 'bce' ) {
- result.bce = tokens[i].val;
- continue;
- } else {
- return null;
- }
- }
- if( grammar[i] === '-' ) {
- if( tokens[i].type === 'minus' ) {
- if( grammar[ i + 1 ] === 'y' ) {
- result.minus = true;
- }
- continue;
- } else {
- return null;
- }
- }
- return null;
- }
- return result;
- };
-
- var matchGrammars = function( grammars, tokens ) {
- var result = null;
- for( var i = 0; i < grammars.length; i++ ) {
- result = matchGrammar( grammars[i], tokens );
- if( result !== null ) {
- return result;
- }
- }
- return null;
- };
-
- var parse = function( text ) {
- var tokens = tokenize( text ),
- retval = {},
- result = matchGrammars( [
- 'y', '-y', 'my', 'm-y', 'yb', 'myb', 'mdy',
'md-y', 'dmy', 'dm-y',
- 'mdyb', 'dmyb', 'mdyc', ',md-yc', 'dmyc',
'dm-yc',
- 'mdybc', 'dmybc', 'ymd', '-ymd', 'ym', '-ym'
- ], tokens );
-
- if( result === null ) {
- return null;
- }
-
- if( result.minus !== undefined ) {
- result.year = result.year * -1;
- }
- if( result.bce !== undefined ) {
- if( result.year < 1 ) {
- return;
- }
- retval.bce = result.bce;
- if( result.bce ) {
- result.year = -1 * (result.year - 1);
- }
- }
- if( result.year !== undefined ) {
- retval.year = result.year;
- var temp = result.year;
- if( retval.bce ) {
- temp -= 1;
- }
- if( result.year < 1 ) {
- retval.bce = true;
- }
- retval.precision = 9;
- if( (temp < -1500) || (temp > 5000) ) {
- while( temp % 10 === 0 ) {
- temp /= 10;
- retval.precision -= 1;
- }
- }
- }
- if( result.month !== undefined ) {
- retval.month = result.month;
- retval.precision = 10;
- }
- if( result.day !== undefined ) {
- retval.day = result.day;
- retval.precision = 11;
- }
- if( result.calendar !== undefined ) {
- retval.calendarname = result.calendar;
- } else if( (result.year < 1583) && (retval.precision > 10) ) {
- retval.calendarname = 'Julian';
- } else {
- retval.calendarname = 'Gregorian';
- }
-
- return retval;
};
var writeApproximateYear = function( year, precision ) {
@@ -698,8 +200,6 @@
return settings.precisiontexts[acc];
};
- time.Time = Time;
-
time.julianToGregorian = julianToGregorian;
time.gregorianToJulian = gregorianToJulian;
time.julianToJulianDay = julianToJulianDay;
@@ -715,14 +215,6 @@
time.precisionText = precisionText;
time.maxPrecision = maxPrecision;
- time.settings = {};
- time.settings.bce = bce;
- time.settings.ace = ace;
- time.settings.pastText = pastText;
- time.settings.futureText = futureText;
- time.settings.calendarNames = calendarNames;
- time.settings.monthNames = monthNames;
- time.settings.precisionTexts = precisionTexts;
- time.settings.outputPrecision = outputPrecision;
+ time.settings = settings;
-} )( window );
+}( this ) ); // 'this' is global scope, i.e. 'window' in the browser and
'global' on the server
--
To view, visit https://gerrit.wikimedia.org/r/61524
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I96df9a7dc7ff5202418ea29efb294004ce4a48b9
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits