-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
56 lines (49 loc) · 1.1 KB
/
index.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
const Mtj = require('../lib/index')
const fs = require('fs')
type schema = {
[key: string]: any
}
new Mtj({
token: fs.readFileSync('./samples/default.md', 'utf8'),
dest: './samples/nested/default.json',
extraHeader: {
errno: 0,
to: 'samples/nested/default'
},
contentKey: 'content',
filter (schema: schema): object | void {
schema.date = formatDate(schema.date)
}
})
function formatDate (date: Date | string): string {
const convert = [
null,
'JAN',
'FEB',
'MAR',
'APR',
'MAY',
'JUN',
'JUL',
'AUG',
'SEP',
'OCT',
'NOV',
'DEC'
]
if (date instanceof Date) {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate().toLocaleString('zh', { minimumIntegerDigits: 2, useGrouping: false })
return `${year} ${convert[month]} ${day}` // 2018 AUG 1
}
// string type
const reg = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/
const format = reg.exec(date)
if (format[0]) {
return format[0] // 2018-8-1
} else {
console.warn('[Format date]: Formatting failed !')
return date
}
}