alanshaw/david — pull request #28 by shahata (2013-09-14 22:30:24+00:00)
Title: fixed issues with latest/stable version detection

This PR fixes two issues:

1)
'latest' and 'stable' versions weren't detected correctly. For example, if you looked at karma you got stable 0.11.0 and latest 0.10.2, when in reality it is exactly the opposite (stable 0.10.2 and latest 0.11.0). The reason for this is that you assumed that the 'latest' version you get from npm is the 'newest', while the truth is that the 'latest' version you get from npm is simply the version tagged 'latest'. I think that karma is one of the few node modules using this versioning system correctly. 0.10.2 is tagged as 'latest' and the newest (0.11.0) is tagged as 'canary'.

The revised implementation assumes that 'latest' version returned from npm is the 'stable' one, while the 'unstable' is the last version in the version array. Only if the 'stable' version doesn't look like a stable version number, I use the old logic of scanning the version array backwards until I find a version that look like a stable version number.

I also don't like the fact that in david's code 'latest' is used as synonym to 'unstable', I find it confusing especially since in my opinion npm regards 'latest' actually as the 'stable' version.

2)
The code assumes that the version array is sorted from oldest to newest, while actually the array is sorted using semver.compare(), which for example will put 0.0.1-alpha10 *before* 0.0.1-alpha9 (see for example 'npm view zombie versions'), so in order to fix this I also get the 'time' attribute from 'npm view' and then sort the version array by time