Skip to content

Commit

Permalink
feat: add a variable to control starting increments from 1 (or other)…
Browse files Browse the repository at this point in the history
… instead of 0
  • Loading branch information
imgrant committed Jul 31, 2023
1 parent 562ac55 commit b1e935c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ calver's default timezone is UTC. If you want to use local timezone, set `useLoc
calver.useLocalTime = true
```

### Counting from 1
For non-date parts of a version (e.g. the `minor` version in the format `YYYY.MM.MINOR`, or the modifier in `2023.7.0-rc.1`), semantic versioning is used, so that the increment begins from `0`. To start skip zero and always start from `1` (or anything else), set `startFrom`:

```js
calver.startFrom = 1
calver.inc('yyyy.mm.minor', '', 'calendar') // 2021.1.1
```

## Tests
```js
npm run test
Expand Down
3 changes: 2 additions & 1 deletion src/ModifierVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ export default class ModifierVersion {

reDigits = /[^0-9\-]/

constructor(obj, parentSeperator, isInitialVersion) {
constructor(obj, parentSeperator, isInitialVersion, startFrom) {
this.DEV = null
this.ALPHA = null
this.BETA = null
this.RC = null

this.isInitialVersion = isInitialVersion
this.parentSeperator = parentSeperator
this.startFrom = startFrom
this.prop = null

this.parse(obj)
Expand Down
11 changes: 6 additions & 5 deletions src/SemanticVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ export default class SemanticVersion {

reDigits = /[^0-9]/

constructor(obj, parentSeperator, isInitialVersion) {
constructor(obj, parentSeperator, isInitialVersion, startFrom) {
this.MAJOR = null
this.MINOR = null
this.PATCH = null

this.isInitialVersion = isInitialVersion
this.parentSeperator = parentSeperator
this.startFrom = startFrom
this.props = []

this.parse(obj)
Expand All @@ -27,7 +28,7 @@ export default class SemanticVersion {
}

reset() {
this.props.map(prop => this[prop] = 0)
this.props.map(prop => this[prop] = this.startFrom)
}

inc(level) {
Expand All @@ -37,13 +38,13 @@ export default class SemanticVersion {

if (level == 'MAJOR') {
this.MAJOR = (parseInt(this.MAJOR) + 1).toString()
if (this.props.indexOf('MINOR') !== -1) this.MINOR = '0'
if (this.props.indexOf('PATCH') !== -1) this.PATCH = '0'
if (this.props.indexOf('MINOR') !== -1) this.MINOR = this.startFrom.toString()
if (this.props.indexOf('PATCH') !== -1) this.PATCH = this.startFrom.toString()
}

if (level == 'MINOR') {
this.MINOR = (parseInt(this.MINOR) + 1).toString()
if (this.props.indexOf('PATCH') !== -1) this.PATCH = '0'
if (this.props.indexOf('PATCH') !== -1) this.PATCH = this.startFrom.toString()
}

if (level == 'PATCH') {
Expand Down
7 changes: 4 additions & 3 deletions src/Version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import SemanticVersion from './SemanticVersion.js'
import ModifierVersion from './ModifierVersion.js'

export default class Version {
constructor(version, seperator, date) {
constructor(version, seperator, date, startFrom) {
this.seperator = seperator
this.startFrom = startFrom
this.versionStringHasModifier = version.versionStringHasModifier
this.isInitialVersion = version.isInitialVersion
this.isCalendarLeading = version.isCalendarLeading
Expand All @@ -22,11 +23,11 @@ export default class Version {
}

if (Object.keys(version.semantic).length > 0) {
this.semanticver = new SemanticVersion(version.semantic, this.seperator, this.isInitialVersion)
this.semanticver = new SemanticVersion(version.semantic, this.seperator, this.isInitialVersion, this.startFrom)
}

if (Object.keys(version.modifier).length > 0) {
this.modifierver = new ModifierVersion(version.modifier, this.seperator, this.isInitialVersion)
this.modifierver = new ModifierVersion(version.modifier, this.seperator, this.isInitialVersion, this.startFrom)
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ class Calver {
this.seperator = '.'
this.levels = ['CALENDAR', 'MAJOR', 'MINOR', 'PATCH', ...ModifierVersion.tags]
this._useLocalTime = false
this.startFrom = 0
}

inc(format, version, levels) {
levels = this.validateLevels(levels)
format = this.validateFormat(format, levels)
const parsedVersion = this.parseVersion(version, format, levels)
const parsedVersion = this.parseVersion(version, format, levels, this.startFrom)
const date = this._useLocalTime ? new LocalDate() : new UtcDate()

const obj = (new Version(parsedVersion, this.seperator, date)).inc(levels).asObject()
const obj = (new Version(parsedVersion, this.seperator, date, this.startFrom)).inc(levels).asObject()

const result = this.asString(format, obj)

Expand Down Expand Up @@ -98,7 +99,7 @@ class Calver {
}

if (ModifierVersion.tags.indexOf(value.toUpperCase()) !== -1) {
if (value.toUpperCase() != tag) value = '-1'
if (value.toUpperCase() != tag) value = (this.startFrom - 1).toString()
else value = version.slice(startIndex + value.length + 1)
}

Expand Down
8 changes: 7 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ calver.useLocalTime = true
assert.strictEqual(calver.inc('yyyy.mm.dd.minor.patch', '', 'calendar'), '2021.1.20.0.0')

assert.strictEqual(calver.inc('yyyy.mm.minor', '2020.12.2', 'calendar.minor.rc'), '2021.1.0-rc.0')
assert.strictEqual(calver.inc('yyyy.mm.minor', '2020.12.2', 'calendar.rc'), '2021.1.0-rc.0')
assert.strictEqual(calver.inc('yyyy.mm.minor', '2020.12.2', 'calendar.rc'), '2021.1.0-rc.0')

calver.startFrom = 1
assert.strictEqual(calver.inc('yyyy.mm.minor', '2020.11.1-rc.3', 'calendar.minor'), '2021.1.1')
assert.strictEqual(calver.inc('yyyy.mm.minor', '2021.1.1-rc.2', 'calendar.minor.dev'), '2021.1.1-dev.1')
assert.strictEqual(calver.inc('yyyy.mm.minor', '2019.8.5-rc.2', 'calendar.minor.rc'), '2021.1.1-rc.3')
calver.startFrom = 0

0 comments on commit b1e935c

Please sign in to comment.