Remove Goog.string

This commit is contained in:
nicholas
2016-03-26 16:34:36 +13:00
parent cd6494149b
commit c92223d792
6 changed files with 122 additions and 13 deletions

View File

@@ -2,8 +2,8 @@ goog.provide('ol.Coordinate');
goog.provide('ol.CoordinateFormatType');
goog.provide('ol.coordinate');
goog.require('goog.string');
goog.require('ol.math');
goog.require('ol.string');
/**
@@ -133,8 +133,8 @@ ol.coordinate.degreesToStringHDMS_ = function(degrees, hemispheres, opt_fraction
var x = Math.abs(3600 * normalizedDegrees);
var dflPrecision = opt_fractionDigits || 0;
return Math.floor(x / 3600) + '\u00b0 ' +
goog.string.padNumber(Math.floor((x / 60) % 60), 2) + '\u2032 ' +
goog.string.padNumber((x % 60), 2, dflPrecision) + '\u2033 ' +
ol.string.padNumber(Math.floor((x / 60) % 60), 2) + '\u2032 ' +
ol.string.padNumber((x % 60), 2, dflPrecision) + '\u2033 ' +
hemispheres.charAt(normalizedDegrees < 0 ? 1 : 0);
};

View File

@@ -1,9 +1,9 @@
goog.provide('ol.format.XSD');
goog.require('goog.asserts');
goog.require('goog.string');
goog.require('ol');
goog.require('ol.xml');
goog.require('ol.string');
/**
@@ -142,11 +142,11 @@ ol.format.XSD.writeBooleanTextNode = function(node, bool) {
ol.format.XSD.writeDateTimeTextNode = function(node, dateTime) {
var date = new Date(dateTime * 1000);
var string = date.getUTCFullYear() + '-' +
goog.string.padNumber(date.getUTCMonth() + 1, 2) + '-' +
goog.string.padNumber(date.getUTCDate(), 2) + 'T' +
goog.string.padNumber(date.getUTCHours(), 2) + ':' +
goog.string.padNumber(date.getUTCMinutes(), 2) + ':' +
goog.string.padNumber(date.getUTCSeconds(), 2) + 'Z';
ol.string.padNumber(date.getUTCMonth() + 1, 2) + '-' +
ol.string.padNumber(date.getUTCDate(), 2) + 'T' +
ol.string.padNumber(date.getUTCHours(), 2) + ':' +
ol.string.padNumber(date.getUTCMinutes(), 2) + ':' +
ol.string.padNumber(date.getUTCSeconds(), 2) + 'Z';
node.appendChild(ol.xml.DOCUMENT.createTextNode(string));
};

View File

@@ -3,7 +3,6 @@
goog.provide('ol.source.ImageWMS');
goog.require('goog.asserts');
goog.require('goog.string');
goog.require('goog.uri.utils');
goog.require('ol');
goog.require('ol.Image');
@@ -16,6 +15,7 @@ goog.require('ol.proj');
goog.require('ol.source.Image');
goog.require('ol.source.wms');
goog.require('ol.source.wms.ServerType');
goog.require('ol.string');
/**
@@ -372,5 +372,5 @@ ol.source.ImageWMS.prototype.updateParams = function(params) {
*/
ol.source.ImageWMS.prototype.updateV13_ = function() {
var version = this.params_['VERSION'] || ol.DEFAULT_WMS_VERSION;
this.v13_ = goog.string.compareVersions(version, '1.3') >= 0;
this.v13_ = ol.string.compareVersions(version, '1.3') >= 0;
};

View File

@@ -5,7 +5,6 @@
goog.provide('ol.source.TileWMS');
goog.require('goog.asserts');
goog.require('goog.string');
goog.require('goog.uri.utils');
goog.require('ol');
goog.require('ol.TileCoord');
@@ -18,6 +17,7 @@ goog.require('ol.source.TileImage');
goog.require('ol.source.wms');
goog.require('ol.source.wms.ServerType');
goog.require('ol.tilecoord');
goog.require('ol.string');
/**
@@ -390,5 +390,5 @@ ol.source.TileWMS.prototype.updateParams = function(params) {
*/
ol.source.TileWMS.prototype.updateV13_ = function() {
var version = this.params_['VERSION'] || ol.DEFAULT_WMS_VERSION;
this.v13_ = goog.string.compareVersions(version, '1.3') >= 0;
this.v13_ = ol.string.compareVersions(version, '1.3') >= 0;
};

35
src/ol/string.js Normal file
View File

@@ -0,0 +1,35 @@
goog.provide('ol.string');
/**
* @param {number} number Number to be formatted
* @param {number} width The desired width
* @param {number=} opt_precision Precision of the output string (i.e. number of decimal places)
* @returns {string} Formatted string
*/
ol.string.padNumber = function(number, width, opt_precision) {
var numberString = opt_precision !== undefined ? number.toFixed(opt_precision) : '' + number;
var decimal = numberString.indexOf('.');
decimal = decimal === -1 ? numberString.length : decimal;
return decimal > width ? numberString : new Array(1 + width - decimal).join('0') + numberString;
};
/**
* Adapted from https://github.com/omichelsen/compare-versions/blob/master/index.js
* @param {string|number} v1 First version
* @param {string|number} v2 Second version
* @returns {number} Value
*/
ol.string.compareVersions = function(v1, v2) {
var s1 = ('' + v1).split('.');
var s2 = ('' + v2).split('.');
for (var i = 0; i < Math.max(s1.length, s2.length); i++) {
var n1 = parseInt(s1[i] || '0', 10);
var n2 = parseInt(s2[i] || '0', 10);
if (n1 > n2) return 1;
if (n2 > n1) return -1;
}
return 0;
};

View File

@@ -0,0 +1,74 @@
goog.provide('ol.test.string');
describe('ol.string', function() {
describe('ol.string.padNumber', function() {
it('returns the correct padding without precision', function() {
expect(ol.string.padNumber(6.5, 2)).to.be('06.5');
expect(ol.string.padNumber(6.5, 3)).to.be('006.5');
expect(ol.string.padNumber(1.25, 2)).to.be('01.25');
expect(ol.string.padNumber(5, 3)).to.be('005');
});
it('returns the same string when padding is less than length', function() {
expect(ol.string.padNumber(6.5, 0)).to.be('6.5');
expect(ol.string.padNumber(6.5, 1)).to.be('6.5');
expect(ol.string.padNumber(1.25, 0)).to.be('1.25');
expect(ol.string.padNumber(5, 0)).to.be('5');
expect(ol.string.padNumber(5, 1)).to.be('5');
});
it('returns the correct string precision is given', function() {
expect(ol.string.padNumber(6.5, 0, 2)).to.be('6.50');
expect(ol.string.padNumber(6.5, 1, 2)).to.be('6.50');
expect(ol.string.padNumber(6.5, 2, 2)).to.be('06.50');
expect(ol.string.padNumber(1.25, 2, 3)).to.be('01.250');
expect(ol.string.padNumber(1.25, 2, 1)).to.be('01.3');
expect(ol.string.padNumber(9.9, 2, 0)).to.be('10');
expect(ol.string.padNumber(5, 0, 0)).to.be('5');
expect(ol.string.padNumber(5, 1, 1)).to.be('5.0');
expect(ol.string.padNumber(5, 2, 1)).to.be('05.0');
expect(ol.string.padNumber(5, 2, 0)).to.be('05');
});
});
describe('ol.string.compareVersions', function() {
var f = ol.string.compareVersions;
it('should return the correct value for number input', function() {
expect(f(1, 1)).to.be(0);
expect(f(1.0, 1.1)).to.be.below(0);
expect(f(2.0, 1.1)).to.be.above(0);
});
it('should return the correct value for string input', function() {
expect(f('1.0', '1.0')).to.be(0);
expect(f('1.0.0.0', '1.0')).to.be(0);
expect(f('1.000', '1.0')).to.be(0);
expect(f('1.0.2.1', '1.1')).to.be.below(0);
expect(f('1.1', '1.0.2.1')).to.be.above(0);
expect(f('1', '1.1')).to.be.below(0);
expect(f('2.2', '2')).to.be.above(0);
expect(f('9.5', '9.10')).to.be.below(0);
expect(f('9.5', '9.11')).to.be.below(0);
expect(f('9.11', '9.10')).to.be.above(0);
expect(f('9.1', '9.10')).to.be.below(0);
expect(f('9.1.1', '9.10')).to.be.below(0);
expect(f('9.1.1', '9.11')).to.be.below(0);
expect(f(' 7', '6')).to.be.above(0);
expect(f('7 ', '6')).to.be.above(0);
expect(f(' 7 ', '6')).to.be.above(0);
expect(f('7', ' 6')).to.be.above(0);
expect(f('7', '6 ')).to.be.above(0);
expect(f('7', ' 6 ')).to.be.above(0);
expect(f(' 7', ' 6')).to.be.above(0);
expect(f('7 ', '6 ')).to.be.above(0);
expect(f(' 7 ', ' 6 ')).to.be.above(0);
});
});
});
goog.require('ol.string');