Method for removing cached transforms

This commit is contained in:
Tim Schaub
2013-02-25 11:28:14 -07:00
parent 940ba8afa7
commit ca85c26537
2 changed files with 44 additions and 0 deletions

View File

@@ -266,6 +266,31 @@ ol.Projection.addTransform = function(source, destination, transformFn) {
};
/**
* Unregisters the conversion function to convert coordinates from the source
* projection to the destination projection. This method is used to clean up
* cached transforms during testing.
*
* @param {ol.Projection} source Source projection.
* @param {ol.Projection} destination Destination projection.
* @return {ol.TransformFunction} transformFn The unregistered transform.
*/
ol.Projection.removeTransform = function(source, destination) {
var sourceCode = source.getCode();
var destinationCode = destination.getCode();
var transforms = ol.Projection.transforms_;
goog.asserts.assert(sourceCode in transforms);
goog.asserts.assert(destinationCode in transforms[sourceCode]);
var transform = transforms[sourceCode][destinationCode];
delete transforms[sourceCode][destinationCode];
var keys = goog.object.getKeys(transforms[sourceCode]);
if (keys.length == 0) {
delete transforms[sourceCode];
}
return transform;
};
/**
* @param {string} code Code which is a combination of authority and identifier
* such as “EPSG:4326”.

View File

@@ -131,6 +131,25 @@ describe('ol.Projection', function() {
var transform = ol.Projection.getTransformFromCodes(
'GOOGLE', 'EPSG:4326');
expect(typeof transform).toBe('function');
});
describe('ol.Projection.removeTransform()', function() {
var extent = new ol.Extent(-180, -90, 180, 90);
var units = ol.ProjectionUnits.DEGREES;
it('removes functions cached by addTransform', function() {
var foo = new ol.Projection('foo', units, extent);
var bar = new ol.Projection('bar', units, extent);
var transform = function() {};
ol.Projection.addTransform(foo, bar, transform);
expect(ol.Projection.transforms_).not.toBeUndefined();
expect(ol.Projection.transforms_.foo).not.toBeUndefined();
expect(ol.Projection.transforms_.foo.bar).toBe(transform);
var removed = ol.Projection.removeTransform(foo, bar);
expect(removed).toBe(transform);
expect(ol.Projection.transforms_.foo).toBeUndefined();
});
});