forked from zetavg/date-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate-parser.coffee
64 lines (52 loc) · 1.81 KB
/
date-parser.coffee
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
DateParser =
LOCALES:
'zh-TW': require "./lib/zh-TW"
DEFAULT_LOCALE: 'zh-TW'
DEFAULT_TIMEZONE: 'Asia/Taipei'
locale: (locale) ->
if @LOCALES[locale]
@DEFAULT_LOCALE = locale
@LOCALES[locale].testStrings?.forEach (s) =>
@parse s
else
console.error "No such locale: #{locale}"
timezone: (timezone) ->
@DEFAULT_TIMEZONE = timezone
parse: (text, timezone, locale) ->
text = '' unless text
locale = @DEFAULT_LOCALE unless locale
timezone = @DEFAULT_TIMEZONE unless timezone
expressions = @LOCALES[locale]?.expressions
text = text.replace(RegExp(@LOCALES[locale]?.words?.interjections, 'g'), '') if @LOCALES[locale]?.words?.interjections
if not expressions
console.error "No such locale: #{locale}"
return null
for expression in @LOCALES[locale].expressions
try
result = expression(text, timezone)
return result if result
catch error
console.error 'error', error
return null
number2integer: (text, locale) ->
text = '' unless text
locale = @DEFAULT_LOCALE unless locale
return @LOCALES[locale]?.number2integer?(text)
time2object: (text, locale) ->
text = '' unless text
locale = @DEFAULT_LOCALE unless locale
return @LOCALES[locale]?.time2object?(text)
dayTime2moment: (text, locale) ->
text = '' unless text
locale = @DEFAULT_LOCALE unless locale
return @LOCALES[locale]?.dayTime2moment?(text)
date2object: (text, locale) ->
text = '' unless text
locale = @DEFAULT_LOCALE unless locale
return @LOCALES[locale]?.date2object?(text)
dateExpression2moment: (text, locale) ->
text = '' unless text
locale = @DEFAULT_LOCALE unless locale
return @LOCALES[locale]?.dateExpression2moment?(text)
DateParser.locale('zh-TW')
module.exports = DateParser