1
- " use strict" ;
1
+ ' use strict' ;
2
2
3
- var _typeof2 = require ( "babel-runtime/helpers/typeof" ) ;
3
+ const ParseDates = require ( "@datagica/parse-dates" ) . ParseDates ;
4
+ const XRegExp = require ( "xregexp" ) . XRegExp ;
4
5
5
- var _typeof3 = _interopRequireDefault ( _typeof2 ) ;
6
+ class ParseDateIntervals extends ParseDates {
7
+ constructor ( opts ) {
6
8
7
- var _promise = require ( "babel-runtime/core-js/promise" ) ;
8
-
9
- var _promise2 = _interopRequireDefault ( _promise ) ;
10
-
11
- var _classCallCheck2 = require ( "babel-runtime/helpers/classCallCheck" ) ;
12
-
13
- var _classCallCheck3 = _interopRequireDefault ( _classCallCheck2 ) ;
14
-
15
- var _createClass2 = require ( "babel-runtime/helpers/createClass" ) ;
16
-
17
- var _createClass3 = _interopRequireDefault ( _createClass2 ) ;
18
-
19
- var _possibleConstructorReturn2 = require ( "babel-runtime/helpers/possibleConstructorReturn" ) ;
20
-
21
- var _possibleConstructorReturn3 = _interopRequireDefault ( _possibleConstructorReturn2 ) ;
22
-
23
- var _inherits2 = require ( "babel-runtime/helpers/inherits" ) ;
24
-
25
- var _inherits3 = _interopRequireDefault ( _inherits2 ) ;
26
-
27
- var _parseDates = require ( "@datagica/parse-dates" ) ;
28
-
29
- var _xregexp = require ( "xregexp" ) ;
30
-
31
- var _xregexp2 = _interopRequireDefault ( _xregexp ) ;
32
-
33
- function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
34
-
35
- var ParseDateIntervals = function ( _ParseDates ) {
36
- ( 0 , _inherits3 . default ) ( ParseDateIntervals , _ParseDates ) ;
37
-
38
- function ParseDateIntervals ( ) {
39
- var opts = arguments . length <= 0 || arguments [ 0 ] === undefined ? { } : arguments [ 0 ] ;
40
- ( 0 , _classCallCheck3 . default ) ( this , ParseDateIntervals ) ;
41
-
42
- var _this = ( 0 , _possibleConstructorReturn3 . default ) ( this , Object . getPrototypeOf ( ParseDateIntervals ) . call ( this , opts ) ) ;
9
+ if ( typeof opts === 'undefined' ) {
10
+ opts = { } ;
11
+ }
43
12
44
- _this . intervalPrefix = "(?:(?:de|from)?\\s+)?" ;
45
- _this . intervalSeparator = "\\s*(?:-|,|until|au|to|à)\\s*" ;
46
- _this . dateIntervalStrPatterns = [ ] ;
47
- for ( var i = 1 ; i < 10 ; i ++ ) {
48
- if ( typeof _this [ "datePattern" + i ] === "function" ) {
49
- _this . dateIntervalStrPatterns . push ( "" + _this . intervalPrefix + _this [ 'datePattern' + i ] ( 'from' ) + ( "" + _this . intervalSeparator + _this [ 'datePattern' + i ] ( 'to' ) ) ) ;
13
+ super ( opts ) ;
14
+
15
+ this . intervalPrefix = `(?:(?:de|from)?\\s+)?` ;
16
+ this . intervalSeparator = `\\s*(?:-|,|until|au|to|à)\\s*` ;
17
+ this . dateIntervalStrPatterns = [ ] ;
18
+ for ( let i = 1 ; i < 10 ; i ++ ) {
19
+ if ( typeof this [ `datePattern${ i } ` ] === "function" ) {
20
+ this . dateIntervalStrPatterns . push (
21
+ `${ this . intervalPrefix } ${ this [ 'datePattern' + i ] ( 'from' ) } ` +
22
+ `${ this . intervalSeparator } ${ this [ 'datePattern' + i ] ( 'to' ) } `
23
+ )
50
24
}
51
25
}
52
26
53
- _this . dateIntervalsPatterns = new _xregexp2 . default ( _this . dateIntervalStrPatterns . join ( '|' ) , 'i' ) ;
54
- return _this ;
27
+ this . dateIntervalsPatterns = new XRegExp ( this . dateIntervalStrPatterns . join ( '|' ) , 'i' )
55
28
}
56
29
57
- ( 0 , _createClass3 . default ) ( ParseDateIntervals , [ {
58
- key : "matchToDateInterval" ,
59
- value : function matchToDateInterval ( match ) {
60
-
61
- for ( var i = 1 ; i < 10 ; i ++ ) {
62
- if ( match [ "year" + i + "from" ] && match [ "month" + i + "from" ] && match [ "year" + i + "from" ] && match [ "month" + i + "to" ] ) {
63
- var dateFrom = new Date ( this . matchToYears ( match , i , 'from' ) , this . matchToMonths ( match , i , 'from' ) - 1 , this . matchToDays ( match , i , 'from' ) ) ;
64
- var dateTo = new Date ( this . matchToYears ( match , i , 'to' ) , this . matchToMonths ( match , i , 'to' ) - 1 , this . matchToDays ( match , i , 'to' ) ) ;
65
- return {
66
- from : {
67
- str : dateFrom . toString ( ) ,
68
- timestamp : + dateFrom ,
69
- month : dateFrom . getMonth ( ) + 1 ,
70
- date : dateFrom . getDate ( ) ,
71
- year : dateFrom . getFullYear ( )
72
- } ,
73
- to : {
74
- str : dateTo . toString ( ) ,
75
- timestamp : + dateTo ,
76
- month : dateTo . getMonth ( ) + 1 ,
77
- date : dateTo . getDate ( ) ,
78
- year : dateTo . getFullYear ( )
79
- }
80
- } ;
30
+ matchToDateInterval ( match ) {
31
+
32
+ for ( let i = 1 ; i < 10 ; i ++ ) {
33
+ if ( match [ `year${ i } from` ] && match [ `month${ i } from` ] &&
34
+ match [ `year${ i } from` ] && match [ `month${ i } to` ] ) {
35
+ const dateFrom = new Date (
36
+ this . matchToYears ( match , i , 'from' ) ,
37
+ this . matchToMonths ( match , i , 'from' ) - 1 ,
38
+ this . matchToDays ( match , i , 'from' )
39
+ ) ;
40
+ const dateTo = new Date (
41
+ this . matchToYears ( match , i , 'to' ) ,
42
+ this . matchToMonths ( match , i , 'to' ) - 1 ,
43
+ this . matchToDays ( match , i , 'to' )
44
+ ) ;
45
+ return {
46
+ from : {
47
+ str : dateFrom . toString ( ) ,
48
+ timestamp : + dateFrom ,
49
+ month : dateFrom . getMonth ( ) + 1 ,
50
+ date : dateFrom . getDate ( ) ,
51
+ year : dateFrom . getFullYear ( )
52
+ } ,
53
+ to : {
54
+ str : dateTo . toString ( ) ,
55
+ timestamp : + dateTo ,
56
+ month : dateTo . getMonth ( ) + 1 ,
57
+ date : dateTo . getDate ( ) ,
58
+ year : dateTo . getFullYear ( )
59
+ }
81
60
}
82
61
}
83
- throw new Error ( "couldn't find patterns" ) ;
84
62
}
85
- } , {
86
- key : "parseDateIntervals" ,
87
- value : function parseDateIntervals ( input ) {
88
- var opts = arguments . length <= 1 || arguments [ 1 ] === undefined ? { } : arguments [ 1 ] ;
63
+ throw new Error ( "couldn't find patterns" ) ;
64
+ }
65
+ parseDateIntervals ( input , opts ) {
66
+
67
+ if ( typeof opts === 'undefined' ) {
68
+ opts = { } ;
69
+ }
89
70
90
- var text = "" ;
91
- if ( typeof input === 'string' ) {
92
- text = input ;
93
- } else if ( typeof input . text === 'string' ) {
94
- text = input . text ;
95
- } else {
96
- return _promise2 . default . reject ( new Error ( " input is not text but " + ( typeof input === "undefined" ? "undefined" : ( 0 , _typeof3 . default ) ( input ) ) ) ) ;
97
- }
98
- var match = _xregexp2 . default . exec ( text , this . dateIntervalsPatterns ) ;
71
+ let text = "" ;
72
+ if ( typeof input === 'string' ) {
73
+ text = input
74
+ } else if ( typeof input . text === 'string' ) {
75
+ text = input . text
76
+ } else {
77
+ return Promise . reject ( new Error ( ` input is not text but ${ typeof input } ` ) )
78
+ }
79
+ const match = XRegExp . exec ( text , this . dateIntervalsPatterns ) ;
99
80
100
- //console.log(JSON.stringify(match));
81
+ //console.log(JSON.stringify(match));
101
82
102
- try {
103
- return _promise2 . default . resolve ( this . matchToDateInterval ( match ) ) ;
104
- } catch ( err ) {
105
- console . log ( "error: " + err ) ;
106
- return _promise2 . default . resolve ( null ) ;
107
- }
83
+ try {
84
+ return Promise . resolve ( this . matchToDateInterval ( match ) ) ;
85
+ } catch ( err ) {
86
+ console . log ( "error: " + err ) ;
87
+ return Promise . resolve ( null ) ;
108
88
}
109
- } ] ) ;
110
- return ParseDateIntervals ;
111
- } ( _parseDates . ParseDates ) ;
89
+ }
90
+ }
112
91
113
- var singletonInstance = new ParseDateIntervals ( ) ;
114
- var singletonMethod = function singletonMethod ( ) {
92
+ const singletonInstance = new ParseDateIntervals ( )
93
+ const singletonMethod = function ( ) {
115
94
return singletonInstance . parseDateIntervals . apply ( singletonInstance , arguments ) ;
116
- } ;
95
+ }
117
96
118
- module . exports = singletonMethod ;
119
- module . exports . default = singletonMethod ;
120
- module . exports . parseDateIntervals = singletonInstance ;
121
- module . exports . ParseDateIntervals = ParseDateIntervals ;
97
+ module . exports = singletonMethod
98
+ module . exports . default = singletonMethod
99
+ module . exports . parseDateIntervals = singletonInstance
100
+ module . exports . ParseDateIntervals = ParseDateIntervals
0 commit comments