Named exports from ol/string
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
* @module ol/coordinate
|
||||
*/
|
||||
import {modulo} from './math.js';
|
||||
import _ol_string_ from './string.js';
|
||||
import {padNumber} from './string.js';
|
||||
const _ol_coordinate_ = {};
|
||||
|
||||
|
||||
@@ -160,8 +160,8 @@ _ol_coordinate_.degreesToStringHDMS = function(hemispheres, degrees, opt_fractio
|
||||
deg += 1;
|
||||
}
|
||||
|
||||
return deg + '\u00b0 ' + _ol_string_.padNumber(min, 2) + '\u2032 ' +
|
||||
_ol_string_.padNumber(sec, 2, dflPrecision) + '\u2033' +
|
||||
return deg + '\u00b0 ' + padNumber(min, 2) + '\u2032 ' +
|
||||
padNumber(sec, 2, dflPrecision) + '\u2033' +
|
||||
(normalizedDegrees == 0 ? '' : ' ' + hemispheres.charAt(normalizedDegrees < 0 ? 1 : 0));
|
||||
};
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* @module ol/format/XSD
|
||||
*/
|
||||
import _ol_xml_ from '../xml.js';
|
||||
import _ol_string_ from '../string.js';
|
||||
import {padNumber} from '../string.js';
|
||||
const XSD = {};
|
||||
|
||||
|
||||
@@ -124,11 +124,11 @@ XSD.writeCDATASection = function(node, string) {
|
||||
XSD.writeDateTimeTextNode = function(node, dateTime) {
|
||||
const date = new Date(dateTime * 1000);
|
||||
const string = date.getUTCFullYear() + '-' +
|
||||
_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';
|
||||
padNumber(date.getUTCMonth() + 1, 2) + '-' +
|
||||
padNumber(date.getUTCDate(), 2) + 'T' +
|
||||
padNumber(date.getUTCHours(), 2) + ':' +
|
||||
padNumber(date.getUTCMinutes(), 2) + ':' +
|
||||
padNumber(date.getUTCSeconds(), 2) + 'Z';
|
||||
node.appendChild(_ol_xml_.DOCUMENT.createTextNode(string));
|
||||
};
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import {get as getProjection, transform} from '../proj.js';
|
||||
import _ol_reproj_ from '../reproj.js';
|
||||
import ImageSource from '../source/Image.js';
|
||||
import WMSServerType from '../source/WMSServerType.js';
|
||||
import _ol_string_ from '../string.js';
|
||||
import {compareVersions} from '../string.js';
|
||||
import {appendParams} from '../uri.js';
|
||||
|
||||
/**
|
||||
@@ -367,6 +367,6 @@ ImageWMS.prototype.updateParams = function(params) {
|
||||
*/
|
||||
ImageWMS.prototype.updateV13_ = function() {
|
||||
const version = this.params_['VERSION'] || DEFAULT_WMS_VERSION;
|
||||
this.v13_ = _ol_string_.compareVersions(version, '1.3') >= 0;
|
||||
this.v13_ = compareVersions(version, '1.3') >= 0;
|
||||
};
|
||||
export default ImageWMS;
|
||||
|
||||
@@ -14,7 +14,7 @@ import _ol_size_ from '../size.js';
|
||||
import TileImage from '../source/TileImage.js';
|
||||
import WMSServerType from '../source/WMSServerType.js';
|
||||
import _ol_tilecoord_ from '../tilecoord.js';
|
||||
import _ol_string_ from '../string.js';
|
||||
import {compareVersions} from '../string.js';
|
||||
import {appendParams} from '../uri.js';
|
||||
|
||||
/**
|
||||
@@ -340,6 +340,6 @@ TileWMS.prototype.updateParams = function(params) {
|
||||
*/
|
||||
TileWMS.prototype.updateV13_ = function() {
|
||||
const version = this.params_['VERSION'] || DEFAULT_WMS_VERSION;
|
||||
this.v13_ = _ol_string_.compareVersions(version, '1.3') >= 0;
|
||||
this.v13_ = compareVersions(version, '1.3') >= 0;
|
||||
};
|
||||
export default TileWMS;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
/**
|
||||
* @module ol/string
|
||||
*/
|
||||
const _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) {
|
||||
*/
|
||||
export function padNumber(number, width, opt_precision) {
|
||||
const numberString = opt_precision !== undefined ? number.toFixed(opt_precision) : '' + number;
|
||||
let 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
|
||||
@@ -22,7 +22,7 @@ _ol_string_.padNumber = function(number, width, opt_precision) {
|
||||
* @param {string|number} v2 Second version
|
||||
* @returns {number} Value
|
||||
*/
|
||||
_ol_string_.compareVersions = function(v1, v2) {
|
||||
export function compareVersions(v1, v2) {
|
||||
const s1 = ('' + v1).split('.');
|
||||
const s2 = ('' + v2).split('.');
|
||||
|
||||
@@ -39,5 +39,4 @@ _ol_string_.compareVersions = function(v1, v2) {
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
export default _ol_string_;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import _ol_string_ from '../../../src/ol/string.js';
|
||||
import {compareVersions, padNumber} from '../../../src/ol/string.js';
|
||||
|
||||
|
||||
describe('ol.string', function() {
|
||||
@@ -6,67 +6,66 @@ 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');
|
||||
expect(padNumber(6.5, 2)).to.be('06.5');
|
||||
expect(padNumber(6.5, 3)).to.be('006.5');
|
||||
expect(padNumber(1.25, 2)).to.be('01.25');
|
||||
expect(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');
|
||||
expect(padNumber(6.5, 0)).to.be('6.5');
|
||||
expect(padNumber(6.5, 1)).to.be('6.5');
|
||||
expect(padNumber(1.25, 0)).to.be('1.25');
|
||||
expect(padNumber(5, 0)).to.be('5');
|
||||
expect(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');
|
||||
expect(padNumber(6.5, 0, 2)).to.be('6.50');
|
||||
expect(padNumber(6.5, 1, 2)).to.be('6.50');
|
||||
expect(padNumber(6.5, 2, 2)).to.be('06.50');
|
||||
expect(padNumber(1.25, 2, 3)).to.be('01.250');
|
||||
expect(padNumber(1.25, 2, 1)).to.be('01.3');
|
||||
expect(padNumber(9.9, 2, 0)).to.be('10');
|
||||
expect(padNumber(5, 0, 0)).to.be('5');
|
||||
expect(padNumber(5, 1, 1)).to.be('5.0');
|
||||
expect(padNumber(5, 2, 1)).to.be('05.0');
|
||||
expect(padNumber(5, 2, 0)).to.be('05');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('ol.string.compareVersions', function() {
|
||||
const 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);
|
||||
expect(compareVersions(1, 1)).to.be(0);
|
||||
expect(compareVersions(1.0, 1.1)).to.be.below(0);
|
||||
expect(compareVersions(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(compareVersions('1.0', '1.0')).to.be(0);
|
||||
expect(compareVersions('1.0.0.0', '1.0')).to.be(0);
|
||||
expect(compareVersions('1.000', '1.0')).to.be(0);
|
||||
expect(compareVersions('1.0.2.1', '1.1')).to.be.below(0);
|
||||
expect(compareVersions('1.1', '1.0.2.1')).to.be.above(0);
|
||||
expect(compareVersions('1', '1.1')).to.be.below(0);
|
||||
expect(compareVersions('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(compareVersions('9.5', '9.10')).to.be.below(0);
|
||||
expect(compareVersions('9.5', '9.11')).to.be.below(0);
|
||||
expect(compareVersions('9.11', '9.10')).to.be.above(0);
|
||||
expect(compareVersions('9.1', '9.10')).to.be.below(0);
|
||||
expect(compareVersions('9.1.1', '9.10')).to.be.below(0);
|
||||
expect(compareVersions('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);
|
||||
expect(compareVersions(' 7', '6')).to.be.above(0);
|
||||
expect(compareVersions('7 ', '6')).to.be.above(0);
|
||||
expect(compareVersions(' 7 ', '6')).to.be.above(0);
|
||||
expect(compareVersions('7', ' 6')).to.be.above(0);
|
||||
expect(compareVersions('7', '6 ')).to.be.above(0);
|
||||
expect(compareVersions('7', ' 6 ')).to.be.above(0);
|
||||
expect(compareVersions(' 7', ' 6')).to.be.above(0);
|
||||
expect(compareVersions('7 ', '6 ')).to.be.above(0);
|
||||
expect(compareVersions(' 7 ', ' 6 ')).to.be.above(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user