Skip to content

Commit f03f757

Browse files
committed
Merge pull request #69 from wmde/calUris
Simplify calendar model configuration structure
2 parents 5d37e62 + 164f44c commit f03f757

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/values/TimeValue.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var PARENT = dv.DataValue;
1515
*
1616
* @param {string} iso8601
1717
* @param {Object} [options]
18-
* @param {string} [options.calendarModel=dataValues.TimeValue.CALENDARS.GREGORIAN.uri]
18+
* @param {string} [options.calendarModel=dataValues.TimeValue.CALENDARS.GREGORIAN]
1919
* Wikidata URL of the calendar model.
2020
* @param {number} [options.precision=dataValues.TimeValue.PRECISIONS.DAY]
2121
* @param {number} [options.before=0]
@@ -40,7 +40,7 @@ var SELF = dv.TimeValue = util.inherit( 'DvTimeValue', PARENT, function( iso8601
4040
}
4141

4242
this._options = {
43-
calendarModel: SELF.CALENDARS.GREGORIAN.uri,
43+
calendarModel: SELF.CALENDARS.GREGORIAN,
4444
precision: SELF.getPrecisionById( 'DAY' ),
4545
before: 0,
4646
after: 0,
@@ -74,7 +74,7 @@ var SELF = dv.TimeValue = util.inherit( 'DvTimeValue', PARENT, function( iso8601
7474
* @throws {Error} if a value to set is not specified properly.
7575
*/
7676
_setOption: function( key, value ) {
77-
if( key === 'calendarModel' && SELF.getCalendarModelTextByUri( value ) === null ) {
77+
if( key === 'calendarModel' && !SELF.getCalendarModelKeyByUri( value ) ) {
7878
throw new Error( 'Setting ' + key + ': No valid calendar model URI provided' );
7979
}
8080
if( $.inArray( key, ['precision', 'before', 'after', 'timezone'] ) !== -1
@@ -250,41 +250,37 @@ SELF.TYPE = 'time';
250250

251251
// TODO: Inject configurations...
252252
/**
253-
* Calendar configuration.
253+
* Known calendar model URIs.
254254
* @property {Object}
255255
* @static
256256
* @since 0.7
257257
*/
258258
SELF.CALENDARS = {
259-
GREGORIAN: {
260-
text: 'Gregorian',
261-
uri: 'http://www.wikidata.org/entity/Q1985727'
262-
},
263-
JULIAN: {
264-
text: 'Julian',
265-
uri: 'http://www.wikidata.org/entity/Q1985786'
266-
}
259+
GREGORIAN: 'http://www.wikidata.org/entity/Q1985727',
260+
JULIAN: 'http://www.wikidata.org/entity/Q1985786'
267261
};
268262

269263
/**
270-
* Retrieves a calendar model text by its URI.
264+
* Retrieves a lower-cased calendar model key string, e.g. "gregorian", by its URI.
271265
* @static
272266
* @since 0.7
273267
*
274268
* @param {string} uri
275269
* @return {string|null}
276270
*/
277-
SELF.getCalendarModelTextByUri = function( uri ) {
278-
var text = null;
271+
SELF.getCalendarModelKeyByUri = function( uri ) {
272+
var key = null;
279273

280-
$.each( SELF.CALENDARS, function( id, calendar ) {
281-
if( calendar.uri === uri ) {
282-
text = calendar.text;
274+
$.each( SELF.CALENDARS, function( knownKey, knownUri ) {
275+
if ( uri === knownUri ) {
276+
key = knownKey.toLowerCase();
277+
return false;
283278
}
284-
return text === null;
279+
280+
return true;
285281
} );
286282

287-
return text;
283+
return key;
288284
};
289285

290286
/**

0 commit comments

Comments
 (0)