@@ -346,10 +346,71 @@ angular.module('cesium.http.services', ['cesium.cache.services'])
346346 return Math . floor ( moment ( ) . utc ( ) . valueOf ( ) / 1000 ) ;
347347 }
348348
349+ function isPositiveInteger ( x ) {
350+ // http://stackoverflow.com/a/1019526/11236
351+ return / ^ \d + $ / . test ( x ) ;
352+ }
353+
354+ /**
355+ * Compare two software version numbers (e.g. 1.7.1)
356+ * Returns:
357+ *
358+ * 0 if they're identical
359+ * negative if v1 < v2
360+ * positive if v1 > v2
361+ * Nan if they in the wrong format
362+ *
363+ * E.g.:
364+ *
365+ * assert(version_number_compare("1.7.1", "1.6.10") > 0);
366+ * assert(version_number_compare("1.7.1", "1.7.10") < 0);
367+ *
368+ * "Unit tests": http://jsfiddle.net/ripper234/Xv9WL/28/
369+ *
370+ * Taken from http://stackoverflow.com/a/6832721/11236
371+ */
372+ function compareVersionNumbers ( v1 , v2 ) {
373+ var v1parts = v1 . split ( '.' ) ;
374+ var v2parts = v2 . split ( '.' ) ;
375+
376+ // First, validate both numbers are true version numbers
377+ function validateParts ( parts ) {
378+ for ( var i = 0 ; i < parts . length ; ++ i ) {
379+ if ( ! isPositiveInteger ( parts [ i ] ) ) {
380+ return false ;
381+ }
382+ }
383+ return true ;
384+ }
385+ if ( ! validateParts ( v1parts ) || ! validateParts ( v2parts ) ) {
386+ return NaN ;
387+ }
388+
389+ for ( var i = 0 ; i < v1parts . length ; ++ i ) {
390+ if ( v2parts . length === i ) {
391+ return 1 ;
392+ }
393+
394+ if ( v1parts [ i ] === v2parts [ i ] ) {
395+ continue ;
396+ }
397+ if ( v1parts [ i ] > v2parts [ i ] ) {
398+ return 1 ;
399+ }
400+ return - 1 ;
401+ }
402+
403+ if ( v1parts . length != v2parts . length ) {
404+ return - 1 ;
405+ }
406+
407+ return 0 ;
408+ }
409+
349410 function isVersionCompatible ( minVersion , actualVersion ) {
350411 // TODO: add implementation
351412 console . debug ( '[http] TODO: implement check version [{0}] compatible with [{1}]' . format ( actualVersion , minVersion ) ) ;
352- return true ;
413+ return compareVersionNumbers ( minVersion , actualVersion ) <= 0 ;
353414 }
354415
355416 var cache = angular . copy ( csCache . constants ) ;
@@ -374,6 +435,7 @@ angular.module('cesium.http.services', ['cesium.cache.services'])
374435 now : getDateNow
375436 } ,
376437 version : {
438+ compare : compareVersionNumbers ,
377439 isCompatible : isVersionCompatible
378440 } ,
379441 cache : cache
0 commit comments