Daniel Werner has submitted this change and it was merged.
Change subject: [coordinate.js] Refactored precision handling
......................................................................
[coordinate.js] Refactored precision handling
Moved the precision levels into the settings object and removed functions
altering
the precision from the Coordinate prototype since a Coordinate object is meant
to
be immutable.
Change-Id: I39753f037a0e6a2eca4264da68f041155ebdf6e1
---
M DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
M DataValues/resources/coordinate.js/src/coordinate.js
M DataValues/resources/coordinate.js/tests/coordinate.Coordinate.tests.js
M DataValues/resources/coordinate.js/tests/coordinate.tests.js
4 files changed, 45 insertions(+), 115 deletions(-)
Approvals:
Daniel Werner: Looks good to me, approved
jenkins-bot: Verified
diff --git a/DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
b/DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
index dd532ba..f12b452 100644
--- a/DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
+++ b/DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
@@ -143,33 +143,6 @@
getPrecision: function() { return this._precision; },
/**
- * Sets the precision.
- *
- * TODO: Make this an immutable object, deprecate this function.
- *
- * @param {number} precision
- */
- setPrecision: function( precision ) { this._precision =
precision; },
-
- /**
- * Increases the precision by one step.
- *
- * TODO: Make this an immutable object, deprecate this function.
- */
- increasePrecision: function() {
- this._precision = coordinate.increasePrecision(
this._precision );
- },
-
- /**
- * Decreases the precision by one step.
- *
- * TODO: Make this an immutable object, deprecate this function.
- */
- decreasePrecision: function() {
- this._precision = coordinate.decreasePrecision(
this._precision );
- },
-
- /**
* Returns the precision text.
*
* @return {string}
diff --git a/DataValues/resources/coordinate.js/src/coordinate.js
b/DataValues/resources/coordinate.js/src/coordinate.js
index b2f30dc..eeaef7b 100644
--- a/DataValues/resources/coordinate.js/src/coordinate.js
+++ b/DataValues/resources/coordinate.js/src/coordinate.js
@@ -30,30 +30,41 @@
degree: '°',
minute: '\'',
second: '"',
- precisionTexts: [
- { precision: 1, text: 'to a degree' },
- { precision: 1 / 60, text: 'to an arcminute' },
- { precision: 1 / 3600, text: 'to an arcsecond'
},
- { precision: 1 / 36000, text: 'to a tenth of an
arcsecond' },
- { precision: 1 / 360000, text: 'to the
hundredth of an arcsecond' },
- { precision: 1 / 3600000, text: 'to the
thousandth of an arcsecond' }
+ precisions: [
+ { level: 10 },
+ { level: 1, text: 'to a degree' },
+ { level: 0.1 },
+ { level: 1 / 60, text: 'to an arcminute' },
+ { level: 0.01 },
+ { level: 1 / 3600, text: 'to an arcsecond' },
+ { level: 0.001 },
+ { level: 1 / 36000, text: 'to 1/10 of an
arcsecond' },
+ { level: 0.0001 },
+ { level: 1 / 360000, text: 'to 1/100 of an
arcsecond' },
+ { level: 0.00001 },
+ { level: 1 / 3600000, text: 'to 1/1000 of an
arcsecond' },
+ { level: 0.000001 }
]
},
/**
- * Default precision levels.
- * @type {number[]}
+ * Returns the index of a precision within the settings array
containing the precisions or
+ * -1 if the precision could not be found.
+ *
+ * @param {number} precision
+ * @return {number}
*/
- precisionLevels: [
- 10,
- 1,
- 0.1, 1/60,
- 0.01, 1/3600,
- 0.001, 1/36000,
- 0.0001, 1/360000,
- 0.00001, 1/3600000,
- 0.000001
- ],
+ getPrecisionIndex: function( precision ) {
+ for( var i in this.settings.precisions ) {
+ if(
+
this.settings.precisions.hasOwnProperty( i )
+ && this.settings.precisions[i].level
=== precision
+ ) {
+ return i;
+ }
+ }
+ return -1;
+ },
/**
* Returns the given precision increased by one step.
@@ -62,14 +73,14 @@
* @return {number}
*/
increasePrecision: function( precision ) {
- var index = this.precisionLevels.indexOf( precision );
+ var index = this.getPrecisionIndex( precision );
- if( index === this.precisionLevels.length - 1 || index
=== -1 ) {
+ if( index === this.settings.precisions.length - 1 ||
index === -1 ) {
var newPrecision = precision / 10;
return ( newPrecision < 1e-9 ) ? 1e-9 :
newPrecision;
}
- return this.precisionLevels[index + 1];
+ return this.settings.precisions[index + 1].level;
},
/**
@@ -83,7 +94,7 @@
return 1e-9;
}
- var index = this.precisionLevels.indexOf( precision );
+ var index = this.getPrecisionIndex( precision );
if( index === 0 ) {
return 180;
@@ -91,11 +102,11 @@
return Math.min( precision * 10, 180 );
}
- return this.precisionLevels[index-1];
+ return this.settings.precisions[index-1].level;
},
/**
- * Returns a given precision as string.
+ * Returns a precision's string representation.
*
* @param {number} precision
* @return {string}
@@ -105,12 +116,12 @@
// Figure out if the precision is very close to a
precision that can be expressed with a
// string:
- for( var i in this.settings.precisionTexts ) {
+ for( var i in this.settings.precisions ) {
if(
-
this.settings.precisionTexts.hasOwnProperty( i )
- && Math.abs( precision -
this.settings.precisionTexts[i].precision ) < 0.0000001
+
this.settings.precisions.hasOwnProperty( i )
+ && Math.abs( precision -
this.settings.precisions[i].level ) < 0.0000001
) {
- precisionText =
this.settings.precisionTexts[i].text;
+ precisionText =
this.settings.precisions[i].text;
}
}
diff --git
a/DataValues/resources/coordinate.js/tests/coordinate.Coordinate.tests.js
b/DataValues/resources/coordinate.js/tests/coordinate.Coordinate.tests.js
index bd06329..6ec7477 100644
--- a/DataValues/resources/coordinate.js/tests/coordinate.Coordinate.tests.js
+++ b/DataValues/resources/coordinate.js/tests/coordinate.Coordinate.tests.js
@@ -136,61 +136,6 @@
} );
- QUnit.test( 'Precision handling', function( assert ) {
- var c = new coordinate.Coordinate( '1.5 1.25' );
-
- assert.equal(
- c.getPrecision(),
- 0.01,
- 'Increased precision'
- );
-
- c.decreasePrecision();
- c.decreasePrecision();
-
- assert.equal(
- c.getPrecision(),
- 0.1,
- 'Decreased precision'
- );
-
- assert.equal(
- c.longitudeDecimal(),
- 1.3,
- 'Verified applied precision'
- );
-
- c.increasePrecision();
- c.increasePrecision();
-
- assert.equal(
- c.getPrecision(),
- 0.01,
- 'Increased precision'
- );
-
- assert.equal(
- c.longitudeDecimal(),
- 1.25,
- 'Verified applied precision'
- );
-
- c.setPrecision( 1 );
-
- assert.equal(
- c.getPrecision(),
- 1,
- 'Set precision'
- );
-
- assert.equal(
- c.longitudeDecimal(),
- 1,
- 'Verified applied precision'
- );
-
- } );
-
QUnit.test( 'iso6709()', function( assert ) {
var c;
diff --git a/DataValues/resources/coordinate.js/tests/coordinate.tests.js
b/DataValues/resources/coordinate.js/tests/coordinate.tests.js
index f26c3c6..ae2a66f 100644
--- a/DataValues/resources/coordinate.js/tests/coordinate.tests.js
+++ b/DataValues/resources/coordinate.js/tests/coordinate.tests.js
@@ -159,13 +159,14 @@
// Look up precision text:
if( typeof expected.tech === 'number' ) {
- $.each( coordinate.settings.precisionTexts,
function( i, textDefinition ) {
- if( textDefinition.precision ===
expected.tech ) {
+ $.each( coordinate.settings.precisions,
function( i, precisionDefinition ) {
+ if( precisionDefinition.level ===
expected.tech ) {
assert.strictEqual(
precisionText,
- textDefinition.text,
- 'Precision text for \''
+ precision + '\' results in text \'' + textDefinition.text + '\'.'
+
precisionDefinition.text,
+ 'Precision text for \''
+ precision + '\' results in text \''
+ +
precisionDefinition.text + '\'.'
);
return false;
--
To view, visit https://gerrit.wikimedia.org/r/66072
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I39753f037a0e6a2eca4264da68f041155ebdf6e1
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits