Skip to content

Commit 730e3ff

Browse files
committed
feat: prefix, suffix and clean methods added
1 parent c5da463 commit 730e3ff

File tree

10 files changed

+190
-7
lines changed

10 files changed

+190
-7
lines changed

Diff for: README.md

+22
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ calver nt 2024-32 2024-30 --cycle week
183183

184184
Outputs the exact version string or exits with error.
185185

186+
### Prefix, suffix and clean
187+
188+
Simple helper methods that might be useful in your versioning processes.
189+
190+
```ts
191+
calver.prefix('2024-4.123') // outputs v2024-4.123
192+
calver.prefix('2024-4.123', 'v') // outputs v2024-4.123
193+
194+
calver.suffix('2024-4.123', '-something') // outputs 2024-4.123-something
195+
196+
calver.clean(' =v2024-4.123-something ') // 2024-4.123
197+
```
198+
199+
They work same as in the module api:
200+
201+
```sh
202+
calver prefix 2024-4.123
203+
calver prefix 2024-4.123 --prefix v
204+
calver suffix 2024-4.123 --suffix something
205+
calver clean " =v2024-4.123-something "
206+
```
207+
186208
## Contributing
187209

188210
If you're interested in contributing, read the [CONTRIBUTING.md](https://github.com/muratgozel/muratgozel/blob/main/CONTRIBUTING.md) first, please.

Diff for: dist/cli.cjs

+21-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var commander = require('commander')
44
var index = require('./index.cjs')
55

66
var name = 'calver'
7-
var version = '24.0.0'
7+
var version = '24.0.2'
88
var description =
99
'Calendar based software versioning library as node.js module and with cli support.'
1010
var type = 'module'
@@ -172,6 +172,26 @@ program
172172
}
173173
console.log(str)
174174
})
175+
program
176+
.command('prefix')
177+
.argument('<string>', 'version string')
178+
.option('--prefix <string>', 'The prefix.', 'v')
179+
.action((str, options) => {
180+
console.log(index.prefix(str, options.prefix))
181+
})
182+
program
183+
.command('suffix')
184+
.argument('<string>', 'version string')
185+
.option('--suffix <string>', 'The suffix.')
186+
.action((str, options) => {
187+
console.log(index.suffix(str, options.suffix))
188+
})
189+
program
190+
.command('clean')
191+
.argument('<string>', 'version string')
192+
.action((str) => {
193+
console.log(index.clean(str))
194+
})
175195
program.parse()
176196
function parseCycleArg(value) {
177197
if (!index.isCycleValid(value)) {

Diff for: dist/cli.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import {
77
valid,
88
nt,
99
ot,
10+
prefix,
11+
suffix,
12+
clean,
1013
isCycleValid,
1114
} from './index.js'
1215

1316
var name = 'calver'
14-
var version = '24.0.0'
17+
var version = '24.0.2'
1518
var description =
1619
'Calendar based software versioning library as node.js module and with cli support.'
1720
var type = 'module'
@@ -179,6 +182,26 @@ program
179182
}
180183
console.log(str)
181184
})
185+
program
186+
.command('prefix')
187+
.argument('<string>', 'version string')
188+
.option('--prefix <string>', 'The prefix.', 'v')
189+
.action((str, options) => {
190+
console.log(prefix(str, options.prefix))
191+
})
192+
program
193+
.command('suffix')
194+
.argument('<string>', 'version string')
195+
.option('--suffix <string>', 'The suffix.')
196+
.action((str, options) => {
197+
console.log(suffix(str, options.suffix))
198+
})
199+
program
200+
.command('clean')
201+
.argument('<string>', 'version string')
202+
.action((str) => {
203+
console.log(clean(str))
204+
})
182205
program.parse()
183206
function parseCycleArg(value) {
184207
if (!isCycleValid(value)) {

Diff for: dist/index.cjs

+20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
'use strict'
22

33
const CALVER_RE_SYNTAX = /^[0-9]{4}(-[0-9]{1,2}(-[0-9]{1,2})?)?(\.[0-9]+)?$/
4+
const CALVER_SEARCH_RE_SYNTAX =
5+
/[0-9]{4}(-[0-9]{1,2}(-[0-9]{1,2})?)?(\.[0-9]+)?/
46
const CALVER_CALENDAR_PORTION_SEPARATOR = '-'
57
const CALVER_MINOR_PORTION_SEPARATOR = '.'
68
const CALVER_NUMBER_OF_WEEKS_IN_A_YEAR = 54
79
const CALVER_NUMBER_OF_MONTHS_IN_A_YEAR = 12
810
const CALVER_NUMBER_OF_DAYS_IN_A_MONTH = 31
911
const CALVER_CYCLES = ['auto', 'year', 'month', 'week', 'day']
12+
function clean(str) {
13+
const result = str.match(CALVER_SEARCH_RE_SYNTAX)
14+
if (!result) {
15+
throw new Error(
16+
'Failed to clean the text that was supposed to contain a calver version.',
17+
)
18+
}
19+
return result[0]
20+
}
21+
function suffix(str, suffix2) {
22+
return str + (suffix2 ?? '')
23+
}
24+
function prefix(str, prefix2 = 'v') {
25+
return (prefix2 ?? '') + str
26+
}
1027
function initial(settings) {
1128
if (!isCycleValid(settings.cycle, false)) {
1229
throw new Error('Invalid release cycle')
@@ -232,11 +249,14 @@ function isCycleValid(str, allowAuto = true) {
232249
}
233250

234251
exports.CALVER_CYCLES = CALVER_CYCLES
252+
exports.clean = clean
235253
exports.cycle = cycle
236254
exports.initial = initial
237255
exports.isCycleValid = isCycleValid
238256
exports.nt = nt
239257
exports.ot = ot
240258
exports.parse = parse
259+
exports.prefix = prefix
260+
exports.suffix = suffix
241261
exports.toString = toString
242262
exports.valid = valid

Diff for: dist/index.d.cts

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
declare const CALVER_CYCLES: CalVerCycle[]
2+
declare function clean(str: string): string
3+
declare function suffix(str: string, suffix: string): string
4+
declare function prefix(str: string, prefix?: string): string
25
declare function initial(settings: CalVerCycleSettings): string
36
declare function nt(
47
newer: string,
@@ -49,12 +52,15 @@ export {
4952
type CalVerCycleSettings,
5053
type CalVerObject,
5154
type CalVerValidSettings,
55+
clean,
5256
cycle,
5357
initial,
5458
isCycleValid,
5559
nt,
5660
ot,
5761
parse,
62+
prefix,
63+
suffix,
5864
toString,
5965
valid,
6066
}

Diff for: dist/index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
declare const CALVER_CYCLES: CalVerCycle[]
2+
declare function clean(str: string): string
3+
declare function suffix(str: string, suffix: string): string
4+
declare function prefix(str: string, prefix?: string): string
25
declare function initial(settings: CalVerCycleSettings): string
36
declare function nt(
47
newer: string,
@@ -49,12 +52,15 @@ export {
4952
type CalVerCycleSettings,
5053
type CalVerObject,
5154
type CalVerValidSettings,
55+
clean,
5256
cycle,
5357
initial,
5458
isCycleValid,
5559
nt,
5660
ot,
5761
parse,
62+
prefix,
63+
suffix,
5864
toString,
5965
valid,
6066
}

Diff for: dist/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
const CALVER_RE_SYNTAX = /^[0-9]{4}(-[0-9]{1,2}(-[0-9]{1,2})?)?(\.[0-9]+)?$/
2+
const CALVER_SEARCH_RE_SYNTAX =
3+
/[0-9]{4}(-[0-9]{1,2}(-[0-9]{1,2})?)?(\.[0-9]+)?/
24
const CALVER_CALENDAR_PORTION_SEPARATOR = '-'
35
const CALVER_MINOR_PORTION_SEPARATOR = '.'
46
const CALVER_NUMBER_OF_WEEKS_IN_A_YEAR = 54
57
const CALVER_NUMBER_OF_MONTHS_IN_A_YEAR = 12
68
const CALVER_NUMBER_OF_DAYS_IN_A_MONTH = 31
79
const CALVER_CYCLES = ['auto', 'year', 'month', 'week', 'day']
10+
function clean(str) {
11+
const result = str.match(CALVER_SEARCH_RE_SYNTAX)
12+
if (!result) {
13+
throw new Error(
14+
'Failed to clean the text that was supposed to contain a calver version.',
15+
)
16+
}
17+
return result[0]
18+
}
19+
function suffix(str, suffix2) {
20+
return str + (suffix2 ?? '')
21+
}
22+
function prefix(str, prefix2 = 'v') {
23+
return (prefix2 ?? '') + str
24+
}
825
function initial(settings) {
926
if (!isCycleValid(settings.cycle, false)) {
1027
throw new Error('Invalid release cycle')
@@ -231,12 +248,15 @@ function isCycleValid(str, allowAuto = true) {
231248

232249
export {
233250
CALVER_CYCLES,
251+
clean,
234252
cycle,
235253
initial,
236254
isCycleValid,
237255
nt,
238256
ot,
239257
parse,
258+
prefix,
259+
suffix,
240260
toString,
241261
valid,
242262
}

Diff for: src/cli.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
initial,
99
nt,
1010
ot,
11+
prefix,
12+
suffix,
13+
clean,
14+
type CalVerCycle,
1115
} from './index.js'
1216

1317
const program = new Command()
@@ -23,7 +27,7 @@ program
2327
parseCycleArg,
2428
'auto',
2529
)
26-
.action((str, options) => {
30+
.action((str: string, options: { cycle: CalVerCycle }) => {
2731
const next = cycle(str, { cycle: options.cycle })
2832
console.log(next)
2933
})
@@ -35,7 +39,7 @@ program
3539
'release cycle. one of ' + CALVER_CYCLES.join(', '),
3640
parseCycleArgStrict,
3741
)
38-
.action((options) => {
42+
.action((options: { cycle: CalVerCycle }) => {
3943
const initialVersion = initial({ cycle: options.cycle })
4044
console.log(initialVersion)
4145
})
@@ -49,7 +53,7 @@ program
4953
parseCycleArg,
5054
'auto',
5155
)
52-
.action((str, options) => {
56+
.action((str: string, options: { cycle: CalVerCycle }) => {
5357
const validVersion = valid(str, { cycle: options.cycle })
5458
console.log(validVersion)
5559
})
@@ -64,7 +68,7 @@ program
6468
parseCycleArg,
6569
'auto',
6670
)
67-
.action((str, str2, options) => {
71+
.action((str: string, str2: string, options: { cycle: CalVerCycle }) => {
6872
const isNewer = nt(str, str2, { cycle: options.cycle })
6973
if (!isNewer) {
7074
throw new Error(
@@ -84,7 +88,7 @@ program
8488
parseCycleArg,
8589
'auto',
8690
)
87-
.action((str, str2, options) => {
91+
.action((str: string, str2: string, options: { cycle: CalVerCycle }) => {
8892
const isNewer = ot(str, str2, { cycle: options.cycle })
8993
if (!isNewer) {
9094
throw new Error(
@@ -94,6 +98,29 @@ program
9498
console.log(str)
9599
})
96100

101+
program
102+
.command('prefix')
103+
.argument('<string>', 'version string')
104+
.option('--prefix <string>', 'The prefix.', 'v')
105+
.action((str: string, options: { prefix: string }) => {
106+
console.log(prefix(str, options.prefix))
107+
})
108+
109+
program
110+
.command('suffix')
111+
.argument('<string>', 'version string')
112+
.option('--suffix <string>', 'The suffix.')
113+
.action((str: string, options: { suffix: string }) => {
114+
console.log(suffix(str, options.suffix))
115+
})
116+
117+
program
118+
.command('clean')
119+
.argument('<string>', 'version string')
120+
.action((str: string) => {
121+
console.log(clean(str))
122+
})
123+
97124
program.parse()
98125

99126
function parseCycleArg(value: string) {

Diff for: src/index.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
valid,
99
nt,
1010
ot,
11+
prefix,
12+
suffix,
13+
clean,
1114
} from './index.js'
1215

1316
const samples: [string, CalVerObject][] = [
@@ -176,3 +179,17 @@ test('older than', () => {
176179
expect(ot('2020', '2019')).toBe(false)
177180
expect(ot('2020', '2021')).toBe(true)
178181
})
182+
183+
test('prefix', () => {
184+
expect(prefix('2024-4.123')).toBe('v2024-4.123')
185+
expect(prefix('2024-4.123', 'ver')).toBe('ver2024-4.123')
186+
expect(prefix('2024-4.123', 'ver-')).toBe('ver-2024-4.123')
187+
})
188+
189+
test('suffix', () => {
190+
expect(suffix('2024-4.123', '-something')).toBe('2024-4.123-something')
191+
})
192+
193+
test('clean', () => {
194+
expect(clean(' a=2024-4.123-something ')).toBe('2024-4.123')
195+
})

Diff for: src/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const CALVER_RE_SYNTAX = /^[0-9]{4}(-[0-9]{1,2}(-[0-9]{1,2})?)?(\.[0-9]+)?$/
2+
const CALVER_SEARCH_RE_SYNTAX =
3+
/[0-9]{4}(-[0-9]{1,2}(-[0-9]{1,2})?)?(\.[0-9]+)?/
24
const CALVER_CALENDAR_PORTION_SEPARATOR = '-'
35
const CALVER_MINOR_PORTION_SEPARATOR = '.'
46
const CALVER_NUMBER_OF_WEEKS_IN_A_YEAR = 54
@@ -12,6 +14,26 @@ export const CALVER_CYCLES: CalVerCycle[] = [
1214
'day',
1315
]
1416

17+
export function clean(str: string) {
18+
const result = str.match(CALVER_SEARCH_RE_SYNTAX)
19+
20+
if (!result) {
21+
throw new Error(
22+
'Failed to clean the text that was supposed to contain a calver version.',
23+
)
24+
}
25+
26+
return result[0]
27+
}
28+
29+
export function suffix(str: string, suffix: string) {
30+
return str + (suffix ?? '')
31+
}
32+
33+
export function prefix(str: string, prefix: string = 'v') {
34+
return (prefix ?? '') + str
35+
}
36+
1537
export function initial(settings: CalVerCycleSettings) {
1638
if (!isCycleValid(settings.cycle, false)) {
1739
throw new Error('Invalid release cycle')

0 commit comments

Comments
 (0)