Skip to content

Commit

Permalink
Merge pull request #123 from wmde/removeCoordFormatter
Browse files Browse the repository at this point in the history
Remove unused globeCoordinate.Formatter
  • Loading branch information
Ladsgroup authored Oct 6, 2017
2 parents 448214c + 424e3b2 commit bf493f3
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 612 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ It contains various JavaScript related to the DataValues library.
## Release notes

### 1.0.0 (dev)
* Removed `globeCoordinate.Formatter`.
* Removed the `globeCoordinate` utility class.
* The library is now a pure JavaScript library.
* Removed MediaWiki ResourceLoader module definitions.

Expand Down
1 change: 0 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = function ( config ) {
'lib/util/util.inherit.js',
'lib/globeCoordinate/globeCoordinate.js',
'lib/globeCoordinate/globeCoordinate.GlobeCoordinate.js',
'lib/globeCoordinate/globeCoordinate.Formatter.js',
'src/dataValues.js',
'src/DataValue.js',
'src/values/*.js',
Expand Down
167 changes: 0 additions & 167 deletions lib/globeCoordinate/globeCoordinate.Formatter.js

This file was deleted.

162 changes: 2 additions & 160 deletions lib/globeCoordinate/globeCoordinate.js
Original file line number Diff line number Diff line change
@@ -1,165 +1,7 @@
/**
* Globe coordinate detection global routines
* Original source: http://simia.net/valueparser/coordinate.js
* VERSION: 0.1
* Globe coordinate module
* @class globeCoordinate
* @singleton
* @license GPL-2.0+
* @author Denny Vrandečić
* @author H. Snater < [email protected] >
*/
this.globeCoordinate = ( function() {
'use strict';

return {
/**
* Return a given decimal value applying a precision.
*
* @param {number} value
* @param {number} precision
* @return {number}
*/
toDecimal: function( value, precision ) {
var logPrecision = Math.max( -9, Math.floor( Math.log( precision ) / Math.LN10 ) ),
factor = Math.pow( 10, -1 * logPrecision );

return Math.round( value * factor ) / factor;
},

/**
* Returns a given decimal value converted to degree taking a precision into account.
*
* @param {number} value
* @param {number} precision
* @return {Object} Returned object has the following structure:
* {
* degree: {number},
* minute: {number|undefined},
* second: {number|undefined}
* }
* "minute" and/or "second" are undefined if not covered by the precision.
*/
toDegree: function( value, precision ) {
var result = {};

value = Math.abs( value );

result.degree = Math.floor( value + 0.00000001 );

if( precision > 0.9999999999 ) {
result.minute = undefined;
} else {
result.minute = Math.abs( Math.floor( ( value - result.degree + 0.000001 ) * 60 ) );
}

if( precision > ( 0.9999999999 / 60 ) ) {
result.second = undefined;
} else {
result.second = ( value - result.degree - result.minute / 60 ) * 3600;

if( precision > ( 0.9999999999 / 3600 ) ) {
result.second = Math.abs( Math.round( result.second ) );
} else if( precision > ( 0.9999999999 / 36000 ) ) {
result.second = Math.abs( Math.round( result.second * 10 ) / 10 );
} else if( precision > ( 0.9999999999 / 360000 ) ) {
result.second = Math.abs( Math.round( result.second * 100 ) / 100 );
} else {
result.second = Math.abs( Math.round( result.second * 1000 ) / 1000 );
}
}

// TODO: precision might be a floating point number and might cause minutes/seconds
// to be "generated".
if( precision > 1 ) {
result.degree = Math.round( result.degree / precision ) * precision;

// JavaScript may cause some disturbance regarding rounding and precision. The
// result should not have a higher floating point number precision than the
// applied precision.
var degreeFloat = String( result.degree ).split( '.' ),
precisionFloat = String( precision ).split( '.' );

if(
degreeFloat[1] && precisionFloat[1]
&& degreeFloat[1].length > precisionFloat[1].length
) {
var trimmedPrecision = degreeFloat[1].substr( 0, precisionFloat[1].length );
result.degree = parseFloat( degreeFloat[0] + '.' + trimmedPrecision );
}
}

return result;
},

/**
* Returns a coordinate's ISO 6709 string representation.
*
* @param {Object} decimalCoordinateDefinition
* Object with the following structure:
* {
* latitude: {number},
* longitude: {number},
* precision: {number}
* }
* @return {string}
*/
iso6709: function( decimalCoordinateDefinition ) {
var latitude = decimalCoordinateDefinition.latitude,
longitude = decimalCoordinateDefinition.longitude,
precision = decimalCoordinateDefinition.precision,
lat = globeCoordinate.toDegree( latitude, precision ),
lon = globeCoordinate.toDegree( longitude, precision ),
latISO,
lonISO;

/**
* Strips a number's sign and fills the number's integer part with zeroes according to a
* given string length.
*
* @param {number} number
* @param {string} length
*/
function pad( number, length ) {
var absolute = Math.abs( number || 0 ),
string = String( absolute ),
exploded = string.split( '.' );

if( exploded[0].length === length ) {
return string;
}

return new Array( length - exploded[0].length + 1 ).join( '0' )
+ exploded[0]
+ ( ( exploded[1] ) ? '.' + exploded[1] : '' );
}

latISO = ( latitude < 0 ? '-' : '+' )
+ pad( lat.degree, 2 )
+ ( precision < 1 ? pad( lat.minute, 2 ) : '' )
+ ( precision < 1 / 60 ? pad( lat.second, 2 ) : '' );

lonISO = ( longitude < 0 ? '-' : '+' )
+ pad( lon.degree, 3 )
+ ( precision < 1 ? pad( lon.minute, 2 ) : '' )
+ ( precision < 1 / 60 ? pad( lon.second, 2 ) : '' );

// Synchronize precision (longitude degree needs to be 1 digit longer):
if( lonISO.indexOf( '.' ) !== -1 && latISO.indexOf( '.' ) === -1 ) {
latISO += '.';
}
while( latISO.length < lonISO.length - 1 ) {
latISO += '0';
}
if( latISO.indexOf( '.' ) !== -1 && lonISO.indexOf( '.' ) === -1 ) {
lonISO += '.';
}
while( lonISO.length < latISO.length + 1 ) {
lonISO += '0';
}

return latISO + lonISO + '/';
}

};

}() );
this.globeCoordinate = {};
Loading

0 comments on commit bf493f3

Please sign in to comment.