From 424e3b20ca6ca66f46e2bfe53b885ce640afe283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiemo=20M=C3=A4ttig?= Date: Fri, 11 Aug 2017 10:45:17 -0400 Subject: [PATCH] Remove unused globeCoordinate.Formatter --- README.md | 2 + karma.conf.js | 1 - .../globeCoordinate.Formatter.js | 167 -------------- lib/globeCoordinate/globeCoordinate.js | 162 +------------ .../globeCoordinate.Formatter.tests.js | 72 ------ .../globeCoordinate/globeCoordinate.tests.js | 212 ------------------ 6 files changed, 4 insertions(+), 612 deletions(-) delete mode 100644 lib/globeCoordinate/globeCoordinate.Formatter.js delete mode 100644 tests/lib/globeCoordinate/globeCoordinate.Formatter.tests.js delete mode 100644 tests/lib/globeCoordinate/globeCoordinate.tests.js diff --git a/README.md b/README.md index c6871a0..8cb4066 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/karma.conf.js b/karma.conf.js index b18270c..d471843 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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', diff --git a/lib/globeCoordinate/globeCoordinate.Formatter.js b/lib/globeCoordinate/globeCoordinate.Formatter.js deleted file mode 100644 index 51a1fd1..0000000 --- a/lib/globeCoordinate/globeCoordinate.Formatter.js +++ /dev/null @@ -1,167 +0,0 @@ -( function( globeCoordinate ) { - 'use strict'; - - var defaultOptions = { - north: 'N', - east: 'E', - south: 'S', - west: 'W', - dot: '.', - latLongCombinator: ', ', - degree: '°', - minute: '\'', - second: '"', - precisionTexts: [ - { precision: 1 / 60, text: 'to an arcminute' }, - { precision: 1 / 3600, text: 'to an arcsecond' }, - { precision: 1 / 36000, text: 'to 1/10 of an arcsecond' }, - { precision: 1 / 360000, text: 'to 1/100 of an arcsecond' }, - { precision: 1 / 3600000, text: 'to 1/1000 of an arcsecond' }, - { precision: 1 / 36000000, text: '1/10000\'' } - ], - format: 'decimal' - }; - - /** - * Globe coordinate formatter. - * @class globeCoordinate.Formatter - * @license GPL-2.0+ - * @author H. Snater < mediawiki@snater.com > - * - * @constructor - * - * @param {Object} [options={}] - */ - var SELF = globeCoordinate.Formatter = function Formatter( options ) { - options = options || {}; - - this._options = defaultOptions; - - for( var key in options ) { - if( options.hasOwnProperty( key ) && this._options[key] ) { - this._options[key] = options[key]; - } - } - }; - - SELF.prototype = { - // Don't forget about "constructor" since we are overwriting the whole prototype here: - constructor: SELF, - - /** - * Options - * @property {Object} - * @private - */ - _options: null, - - /** - * Returns a text representation of a GlobeCoordinate object formatted according to the - * "format" option. - * - * @param {globeCoordinate.GlobeCoordinate} gc - * @return {string} - */ - format: function( gc ) { - return this[this._options.format]( gc ); - }, - - /** - * Returns the precision's string representation. - * - * @return {string} - */ - precisionText: function( precision ) { - return SELF.PRECISIONTEXT( precision, this._options ); - }, - - /** - * Returns the decimal coordinate as text. - * - * @param {globeCoordinate.GlobeCoordinate} gc - * @return {string} - */ - decimal: function( gc ) { - var latitude = gc.getLatitude(); - var longitude = gc.getLongitude(); - var precision = gc.getPrecision(); - - if( gc.getPrecision() ) { - latitude = globeCoordinate.toDecimal( latitude, precision ); - longitude = globeCoordinate.toDecimal( longitude, precision ); - } - - return String( latitude ) - + this._options.latLongCombinator - + longitude; - }, - - /** - * Returns the coordinate as text in degree. - * - * @param {globeCoordinate.GlobeCoordinate} gc - * @return {string} - */ - degree: function( gc ) { - var lat = gc.getLatitude(), - lon = gc.getLongitude(), - precision = gc.getPrecision(); - - var text = function( number, sign ) { - if( number === undefined ) { - return ''; - } - return String( number ) + sign; - }; - - var latDeg = globeCoordinate.toDegree( lat, precision ), - longDeg = globeCoordinate.toDegree( lon, precision ); - - return text( Math.abs( latDeg.degree ), this._options.degree ) - + text( latDeg.minute, this._options.minute ) - + text( latDeg.second, this._options.second ) - + ( ( lat < 0 ) ? this._options.south : this._options.north ) - + this._options.latLongCombinator - + text( Math.abs( longDeg.degree ), this._options.degree ) - + text( longDeg.minute, this._options.minute ) - + text( longDeg.second, this._options.second ) - + ( ( lon < 0 ) ? this._options.west : this._options.east ); - } - }; - - /** - * Returns a precision's string representation. - * @property {Function} - * @static - * - * @param {number} precision - * @param {Object} [options={}] - * @return {string} - */ - SELF.PRECISIONTEXT = function( precision, options ) { - var precisionText, - combinedOptions = {}; - - options = options || {}; - - for( var key in defaultOptions ) { - combinedOptions[key] = ( options[key] ) ? options[key] : defaultOptions[key]; - } - - // Figure out if the precision is very close to a precision that can be expressed with a - // string: - for( var i in combinedOptions.precisionTexts ) { - if ( Math.abs( precision - combinedOptions.precisionTexts[i].precision ) < 0.000000000001 ) { - precisionText = combinedOptions.precisionTexts[i].text; - break; - } - } - - if( !precisionText ) { - precisionText = '±' + precision + combinedOptions.degree; - } - - return precisionText; - }; - -}( globeCoordinate ) ); diff --git a/lib/globeCoordinate/globeCoordinate.js b/lib/globeCoordinate/globeCoordinate.js index 00cf90e..f230e2a 100644 --- a/lib/globeCoordinate/globeCoordinate.js +++ b/lib/globeCoordinate/globeCoordinate.js @@ -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 < mediawiki@snater.com > */ -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 = {}; diff --git a/tests/lib/globeCoordinate/globeCoordinate.Formatter.tests.js b/tests/lib/globeCoordinate/globeCoordinate.Formatter.tests.js deleted file mode 100644 index f5f441f..0000000 --- a/tests/lib/globeCoordinate/globeCoordinate.Formatter.tests.js +++ /dev/null @@ -1,72 +0,0 @@ -/** - * @license GPL-2.0+ - * @author H. Snater < mediawiki@snater.com > - */ -( function( GlobeCoordinate, Formatter, $, QUnit ) { - 'use strict'; - - QUnit.module( 'globeCoordinate.Formatter.js' ); - - QUnit.test( 'Formatting', function( assert ) { - assert.expect( 8 ); - var decimalTexts = { - '1, 1': new GlobeCoordinate( { latitude: 1, longitude: 1, precision: 1 } ), - '-10, -1.5': new GlobeCoordinate( { latitude: -10, longitude: -1.5, precision: 0.1 } ), - '20, 0': new GlobeCoordinate( { latitude: 24, longitude: -1.5, precision: 10 } ), - '15, 20': new GlobeCoordinate( { latitude: 15, longitude: 20 } ) - }, - degreeTexts= { - '1°N, 1°E': new GlobeCoordinate( { latitude: 1, longitude: 1, precision: 1 } ), - '10°0\'S, 2°30\'W': new GlobeCoordinate( { latitude: -10, longitude: -2.5, precision: 0.1 } ), - '20°N, 0°W': new GlobeCoordinate( { latitude: 24, longitude: -1.5, precision: 10 } ), - '1°0\'0"N, 1°0\'0"E': new GlobeCoordinate( { latitude: 1, longitude: 1 } ) - }; - - var formatter = new Formatter( { format: 'decimal' } ); - - // Just some output sanity checking: - - $.each( decimalTexts, function( expected, gc ) { - assert.equal( - formatter.format( gc ), - expected, - 'Verified formatter\'s decimal output: ' + expected - ); - } ); - - formatter = new Formatter( { format: 'degree' } ); - - $.each( degreeTexts, function( expected, gc ) { - assert.equal( - formatter.format( gc ), - expected, - 'Verified formatter\'s degree output: ' + expected - ); - } ); - - } ); - - QUnit.test( 'precisionText()', function( assert ) { - assert.expect( 4 ); - var precisions = { - 1: '±1°', - 0.016666666666666666: 'to an arcminute', - 2.7777777777777776e-7: 'to 1/1000 of an arcsecond', - 10: '±10°' - }, - formatter = new Formatter(); - - $.each( precisions, function( testPrecision, expected ) { - var precisionText = formatter.precisionText( testPrecision ); - - // Look up precision text: - assert.strictEqual( - precisionText, - expected, - 'Precision text for \'' + testPrecision + '\' results in text \'' + expected + '\'.' - ); - } ); - - } ); - -}( globeCoordinate.GlobeCoordinate, globeCoordinate.Formatter, jQuery, QUnit ) ); diff --git a/tests/lib/globeCoordinate/globeCoordinate.tests.js b/tests/lib/globeCoordinate/globeCoordinate.tests.js deleted file mode 100644 index 7e8cc14..0000000 --- a/tests/lib/globeCoordinate/globeCoordinate.tests.js +++ /dev/null @@ -1,212 +0,0 @@ -/** - * @license GPL-2.0+ - * @author H. Snater < mediawiki@snater.com > - */ -( function( globeCoordinate, $, QUnit ) { - 'use strict'; - - /** - * Values that are used in combination with the "precisions" object. - * @property {number[]} - */ - var values = [ 0, 0.06, 0.4, 0.5, 1, 10, 17 ]; - - /** - * Keyed by the precision to apply, this object contains the results for the values specified in - * the "values" array. - * @property {Object} - */ - var precisions = { - 0: { - toDecimal: [ 0, 0.06, 0.4, 0.5, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: 0, second: 0 }, - { degree: 0, minute: 3, second: 36 }, - { degree: 0, minute: 24, second: 0 }, - { degree: 0, minute: 30, second: 0 }, - { degree: 1, minute: 0, second: 0 }, - { degree: 10, minute: 0, second: 0 }, - { degree: 17, minute: 0, second: 0 } - ] - }, - 1: { - toDecimal: [ 0, 0, 0, 1, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 1, minute: undefined, second: undefined }, - { degree: 10, minute: undefined, second: undefined }, - { degree: 17, minute: undefined, second: undefined } - ] - }, - 2: { - toDecimal: [ 0, 0, 0, 1, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 2, minute: undefined, second: undefined }, - { degree: 10, minute: undefined, second: undefined }, - { degree: 18, minute: undefined, second: undefined } - ] - }, - 1.00000001: { - toDecimal: [ 0, 0, 0, 1, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 1.00000001, minute: undefined, second: undefined }, - { degree: 10.0000001, minute: undefined, second: undefined }, - { degree: 17.00000017, minute: undefined, second: undefined } - ] - }, - 0.016666666666666666: { - toDecimal: [ 0, 0.06, 0.4, 0.5, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: 0, second: undefined }, - { degree: 0, minute: 3, second: undefined }, - { degree: 0, minute: 24, second: undefined }, - { degree: 0, minute: 30, second: undefined }, - { degree: 1, minute: 0, second: undefined }, - { degree: 10, minute: 0, second: undefined }, - { degree: 17, minute: 0, second: undefined } - ] - }, - 2.7777777777777776e-7: { - toDecimal: [ 0, 0.06, 0.4, 0.5, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: 0, second: 0 }, - { degree: 0, minute: 3, second: 36 }, - { degree: 0, minute: 24, second: 0 }, - { degree: 0, minute: 30, second: 0 }, - { degree: 1, minute: 0, second: 0 }, - { degree: 10, minute: 0, second: 0 }, - { degree: 17, minute: 0, second: 0 } - ] - }, - 1.0000000001e-10: { - toDecimal: [ 0, 0.06, 0.4, 0.5, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: 0, second: 0 }, - { degree: 0, minute: 3, second: 36 }, - { degree: 0, minute: 24, second: 0 }, - { degree: 0, minute: 30, second: 0 }, - { degree: 1, minute: 0, second: 0 }, - { degree: 10, minute: 0, second: 0 }, - { degree: 17, minute: 0, second: 0 } - ] - }, - 1.0000001: { - toDecimal: [ 0, 0, 0, 1, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 1.0000001, minute: undefined, second: undefined }, - { degree: 10.000001, minute: undefined, second: undefined }, - { degree: 17.0000017, minute: undefined, second: undefined } - ] - }, - 1.1: { - toDecimal: [ 0, 0, 0, 1, 1, 10, 17 ], - toDegree: [ - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 1.1, minute: undefined, second: undefined }, - { degree: 9.9, minute: undefined, second: undefined }, - { degree: 16.5, minute: undefined, second: undefined } - ] - }, - 10: { - toDecimal: [ 0, 0, 0, 0, 0, 10, 20 ], - toDegree: [ - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 0, minute: undefined, second: undefined }, - { degree: 10, minute: undefined, second: undefined }, - { degree: 20, minute: undefined, second: undefined } - ] - } - }; - - /** - * ISO 6709 representations keyed by the input string used to generate a GlobeCoordinate object. - * @property {Object} - */ - var iso6709representations = { - '+00+000/': { latitude: 0, longitude: 0, precision: 1 }, - '-03+002/': { latitude: -3, longitude: 2, precision: 1 }, - '+0106+00200/': { latitude: 1.1, longitude: 2, precision: 0.1 }, - '+900000+0300600/': { latitude: 90, longitude: 30.1, precision: 0.01 }, - '+000600+0000027/': { latitude: 0.1, longitude: 0.0075, precision: 1 / 3600 }, - '-0006+00000/': { latitude: -0.1, longitude: 0, precision: 1 / 60 }, - '+010001+0000000/': { latitude: 1.00028, longitude: 0, precision: 1 / 3600 }, - '+010001.8+0000000.0/': { latitude: 1.0005, longitude: 0, precision: 1 / 36000 }, - '+895400.000-0000001.116/': { latitude: 89.9, longitude: -0.00031, precision: 1 / 3600000 }, - '+050000.0-0000010.5/': { latitude: 5, longitude: -0.00292, precision: 1 / 36000 } - }; - - QUnit.module( 'globeCoordinate.js' ); - - QUnit.test( 'Applying precision', function( assert ) { - assert.expect( 70 ); - - $.each( precisions, function( precision, expected ) { - $.each( values, function( i, value ) { - - assert.strictEqual( - globeCoordinate.toDecimal( value, precision ), - expected.toDecimal[i], - 'Applied precision \'' + precision + '\' to \'' + value + '\' resulting in \'' + expected.toDecimal[i] + '\'.' - ); - - } ); - } ); - - } ); - - QUnit.test( 'Converting to degree', function( assert ) { - assert.expect( 70 ); - - $.each( precisions, function( precision, expected ) { - $.each( values, function( i, value ) { - - assert.deepEqual( - globeCoordinate.toDegree( value, precision ), - expected.toDegree[i], - 'Converted \'' + value + '\' to degree applying precision \'' + precision - + '\' resulting in: ' - + expected.toDegree[i].degree + '° ' - + expected.toDegree[i].minute + '" ' - + expected.toDegree[i].second + '\'.' - ); - - } ); - } ); - - } ); - - QUnit.test( 'iso6709()', function( assert ) { - assert.expect( 10 ); - - $.each( iso6709representations, function( iso6709string, gcDef ) { - assert.equal( - globeCoordinate.iso6709( gcDef ), - iso6709string, - 'Generated ISO 6709 string: \'' + iso6709string + '\'.' - ); - } ); - - } ); - -}( globeCoordinate, jQuery, QUnit ) );