/
options.ts
107 lines (99 loc) · 2.79 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { valid as semverValid } from 'semver'
import type {
Options,
FinalTemplatesOptions,
FinalOptions,
FinalContext,
CommitKnownProps,
SortBy,
PickStrings
} from './types/index.js'
import {
formatDate,
createComparator
} from './utils.js'
const HASH_SHORT_LENGTH = 7
const HEADER_MAX_LENGTH = 100
/**
* Default commit transform function.
* @param commit
* @param _context
* @param options
* @param options.formatDate - Date formatter function.
* @returns Patch object for commit.
*/
export function defaultCommitTransform<Commit extends CommitKnownProps = CommitKnownProps>(
commit: Commit,
_context: unknown,
options: Pick<FinalOptions<Commit>, 'formatDate'>
) {
const {
hash,
header,
committerDate
} = commit
return {
hash: typeof hash === 'string'
? hash.substring(0, HASH_SHORT_LENGTH)
: hash,
header: typeof header === 'string'
? header.substring(0, HEADER_MAX_LENGTH)
: header,
committerDate: committerDate
? options.formatDate(committerDate)
: committerDate
} as Partial<Commit>
}
/**
* Get final options object.
* @param options
* @param templates
* @returns Final options object.
*/
export function getFinalOptions<Commit extends CommitKnownProps = CommitKnownProps>(
options: Options<Commit>,
templates: FinalTemplatesOptions
) {
const prefinalOptions = {
groupBy: 'type' as const,
commitsSort: 'header' as const,
noteGroupsSort: 'title' as const,
notesSort: 'text' as const,
transform: defaultCommitTransform,
generateOn: (commit: Commit) => Boolean(semverValid(commit.version)),
finalizeContext: (context: FinalContext<Commit>) => context,
debug: () => { /* noop */ },
formatDate,
reverse: false,
ignoreReverted: true,
doFlush: true,
...templates,
...options
}
const finalOptions = {
...prefinalOptions,
commitGroupsSort: createComparator(prefinalOptions.commitGroupsSort),
commitsSort: createComparator(prefinalOptions.commitsSort as SortBy<PickStrings<Commit>>),
noteGroupsSort: createComparator(prefinalOptions.noteGroupsSort),
notesSort: createComparator(prefinalOptions.notesSort)
} as FinalOptions<Commit>
return finalOptions
}
/**
* Get final context object.
* @param context
* @param options
* @returns Final context object.
*/
export function getGenerateOnFunction<Commit extends CommitKnownProps = CommitKnownProps>(
context: FinalContext<Commit>,
options: FinalOptions<Commit>
) {
const { generateOn } = options
if (typeof generateOn === 'string') {
return (commit: Commit) => typeof commit[generateOn] !== 'undefined'
} else if (typeof generateOn !== 'function') {
return () => false
}
return (keyCommit: Commit, commitsGroup: Commit[]) => generateOn(keyCommit, commitsGroup, context, options)
}