Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behaviour of the 'micro' inc function #2

Closed
Duncank opened this issue Dec 22, 2020 · 17 comments
Closed

Behaviour of the 'micro' inc function #2

Duncank opened this issue Dec 22, 2020 · 17 comments
Labels
enhancement New feature or request

Comments

@Duncank
Copy link

Duncank commented Dec 22, 2020

When I make use of date-based elements in my version tag, I would expect Calver to automatically update these correctly.

Take this example:

const version = new Calver('YYYY.MM.MICRO', '2020.11.3');
version.inc(); // 2020.12.0

const version2 = new Calver('YYYY.MM.MICRO', '2020.12.0');
version2.inc(); // 2020.12.0

The first version number was last created in november (2020-11), since it is december now, it updates to 2020.12.0.
If I want to increase the version number again in december, executing the inc() function returns exactly the same version number. Instead, we'll have to use inc('micro') to get 2020.12.1.

Maybe I'm missing something, but it seems to me that calver-based version numbers always should be adding one to the micro-level in this case, since the year and month levels did not change from the current version number. Calver knows what day it is, so it can compare it with the current version name to determine what should be updated.

I would expect the following behaviour:

const version = new Calver('YYYY.MM.MICRO', '2020.11.3');

// It is november 15th
version.inc(); // 2020.11.4 

// It is november 30th
version.inc(); // 2020.11.5

// It is december 1st
version.inc(); // 2020.12.0

// a later day in december
version.inc(); // 2020.12.1
@muratgozel muratgozel added the enhancement New feature or request label Dec 23, 2020
@muratgozel
Copy link
Owner

Thank you. I also think this is what it should be by default. Fix is on the way...

@muratgozel
Copy link
Owner

Published the fix. Closing the issue now.

@Duncank
Copy link
Author

Duncank commented Dec 23, 2020

Thanks for your help! But I'm not sure it is working as I described above. Can you explain what you expect to happen here:

var version = new calver('YYYY.MM.MICRO', '2020.12.5');
version.inc();
console.log(version.get());

I would assume this should return 2020.12.6, since we are in the same year and month, But instead it returns 2020.12.5.

@muratgozel
Copy link
Owner

I've published another update. Can you confirm that now it's working as expected?

@Duncank
Copy link
Author

Duncank commented Dec 23, 2020

Yeah, this is it! thanks!

var version = new calver('YYYY.MM.MICRO', '2020.11.5');
version.inc(); // 2020.12.0

var version = new calver('YYYY.MM.MICRO', '2020.12.5');
version.inc(); // 2020.12.6

@kherock
Copy link
Contributor

kherock commented Jun 11, 2021

It seems like the API has changed a lot since this issue was opened. Is this is no longer possible with the current versions?

I'm looking for away to do a calendar increment that falls back to incrementing the micro level when the date is the same.

Currently, it just throws an error when the date components aren't updated:

const format = 'YYYY.MM.MICRO';

calver.inc(format, '2021.5.1', 'calendar'); // 2021.6.0
calver.inc(format, '2021.6.0', 'calendar'); // throws with 'There is no change in the version.'
calver.inc(format, '2021.6.0', 'calendar.micro'); // throws with 'Second level should be a modifier or remove it.'

I'd prefer if the API worked this way (adapting from this issue's original comment):

const format = 'YYYY.MM.MICRO';
const version = '2020.11.3';

// It is november 15th
calver.inc(format, version, 'calendar.micro'); // 2020.11.4 

// It is november 30th
calver.inc(format, version, 'calendar.micro'); // 2020.11.5

// It is december 1st
calver.inc(format, version, 'calendar.micro'); // 2020.12.0

// a later day in december
calver.inc(format, version, 'calendar.micro'); // 2020.12.1

@Duncank
Copy link
Author

Duncank commented Jun 11, 2021

That's exactly what I found out this week. I've decided to write my own solution that is only suited for the YYYY.MM.MICRO format, maybe it is of use to you:

function getVersionParts(version) {
    const parts = version.split('.');

    return {
        year: +parts[0],
        month: +parts[1],
        minor: +parts[2],
    };
}

function getNewVersion(input) {
    const previousVersion = getVersionParts(input);

    const now = new Date();
    const year = now.getFullYear();
    const month = now.getMonth() + 1;
    let minor = 0;

    if (year === previousVersion.year && month === previousVersion.month) {
        minor = previousVersion.minor + 1;
    }

    return `${year}.${month}.${minor}`;
}

Use it like this:

// in may 2021
getNewVersion('2021.5.3'); // 2021.5.4

// in june 2021
getNewVersion('2021.5.3'); // 2021.6.0

@muratgozel
Copy link
Owner

Hi @kherock

If you try calver.inc(format, '2021.6.0', 'micro'); instead of calver.inc(format, '2021.6.0', 'calendar.micro'); it will work. Perhaps I should document this better in the readme.

@muratgozel
Copy link
Owner

Hi @Duncank

Noo I wrote this library to not to write our own functions. Just specify which tag you would like to update in the inc function. Look at the previous message I sent in this issue. Is it still not satisfying you?

@kherock
Copy link
Contributor

kherock commented Jun 11, 2021

@muratgozel My problem isn't that I can't get it to work, this is functionality that used to be possible for incrementing micro versions. I have an automated script that should increment a version number where

  • If the previous version falls on an older date, move it to the current date and reset the micro level to .0
  • If the previous version falls on the same date, bump the micro level up by one

You should notice that this is similar to how calendar.<modifier> increments work (though it starts counting from .1 rather than .0). Currently, I'm stuck with writing my own function that looks like this:

function incrementCalendarMicro(format, version) {
  try {
    return calver.inc(format, version, 'calendar');
  } catch (err) {
    if (!err.message.startsWith('There is no change in the version')) throw err;
    return calver.inc(format, version, 'micro');
  }
}

@muratgozel
Copy link
Owner

A reasonable suggestion for calendar.<semver> tags. Please wait I will implement that shortly.

@muratgozel
Copy link
Owner

muratgozel commented Jun 19, 2021

Calendar + semantic tags available in the .inc method as of version 21.1.4

// assuming current date is Jan 2021.
expect(calver.inc('yy.mm.micro.modifier', '20.4.1-dev.3', 'calendar.micro')).toBe('21.1.0')
expect(calver.inc('yy.mm.micro', '21.1.0', 'calendar.micro')).toBe('21.1.1')

Please do npm update.

@kherock @Duncank

@daorren
Copy link

daorren commented Aug 18, 2021

I believe there's still a problem

const format = 'YY.MM.MICRO';
console.log(calver.inc(format, '21.1.4', 'micro')) // current package version
// 21.1.5

It's June when you published, why there's an 1 for MM.

@muratgozel
Copy link
Owner

@daorren The Date.now() function manipulated in test script. It returns a constant date time which is 2021, January.

@daorren
Copy link

daorren commented Aug 28, 2021

I believe there's still a problem

const format = 'YY.MM.MICRO';
console.log(calver.inc(format, '21.1.4', 'micro')) // current package version
// 21.1.5

It's June when you published, why there's an 1 for MM.

@muratgozel
Now I notice spec/helpers/calver.js, but it should only influence test related things, right?
The snippet above is not for test. I mean, if I just require this package and use it, and it should give current month like August, right?

Besides, when I mentiont It's June when you published, why there's an 1 for MM., I was trying to say, when you published this package, you may use dev/commit.js, and it gave you an 1, and that's wrong already if I understood this package correctly

@muratgozel
Copy link
Owner

@daorren Yes, it is only for tests.

I specified "micro" as a tag therefore it didn't update the calendar portion of the current version. If I were to specify "calendar.micro" then the current version would be 21.8.0.

@daorren
Copy link

daorren commented Aug 29, 2021

@daorren Yes, it is only for tests.

I specified "micro" as a tag therefore it didn't update the calendar portion of the current version. If I were to specify "calendar.micro" then the current version would be 21.8.0.

Thank you for your reply, now I understand I must use 'calendar' to update to current date

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants