Debian Codegraph @

js/parsers/parser-date-range.js

raw symbols SCIP
1 /*! Parser: date ranges -updated 11/22/2015 (v2.24.6) */
2 /* Include the 'widget-filter-type-insideRange.js' to filter ranges */
3 /*jshint jquery:true */
4 ;(function($) {
5 'use strict';
6
7 var ts = $.tablesorter,
8 getMonthVal,
9
10 regex = {
11 mdy : /(\d{1,2}[-\s]\d{1,2}[-\s]\d{4}(\s+\d{1,2}:\d{2}(:\d{2})?(\s+[AP]M)?)?)/gi,
12
13 dmy : /(\d{1,2}[-\s]\d{1,2}[-\s]\d{4}(\s+\d{1,2}:\d{2}(:\d{2})?(\s+[AP]M)?)?)/gi,
14 dmyreplace : /(\d{1,2})[-\s](\d{1,2})[-\s](\d{4})/,
15
16 ymd : /(\d{4}[-\s]\d{1,2}[-\s]\d{1,2}(\s+\d{1,2}:\d{2}(:\d{2})?(\s+[AP]M)?)?)/gi,
17 ymdreplace : /(\d{4})[-\s](\d{1,2})[-\s](\d{1,2})/,
18
19 // extract out date format (dd MMM yyyy hms) e.g. 13 March 2016 12:55 PM
20 overall_dMMMyyyy : /(\d{1,2}\s+\w+\s+\d{4}(\s+\d{1,2}:\d{2}(:\d{2})?(\s\w+)?)?)/g,
21 matches_dMMMyyyy : /(\d{1,2})\s+(\w+)\s+(\d{4})/
22
23 };
24
25 /*! date-range MMDDYYYY *//* (2/15/2000 - 5/18/2000) */
26 $.tablesorter.addParser({
27 id: 'date-range-mdy',
28 is: function () {
29 return false;
30 },
31 format: function (text) {
32 var date, str, i, len,
33 parsed = [];
34 str = text.replace( /\s+/g, ' ' ).replace( /[\/\-.,]/g, '-' ).match( regex.mdy );
35 len = str && str.length;
36 // work on dates, even if there is no range
37 if ( len ) {
38 for (i = 0; i < len; i++) {
39 date = new Date( str[i] );
40 parsed.push( date instanceof Date && isFinite(date) ? date.getTime() : str[i] );
41 }
42 // sort from min to max
43 return parsed.sort().join( ' - ' );
44 }
45 return text;
46 },
47 type: 'text'
48 });
49
50 /*! date-range DDMMYYYY *//* (15/2/2000 - 18/5/2000) */
51 $.tablesorter.addParser({
52 id: 'date-range-dmy',
53 is: function () {
54 return false;
55 },
56 format: function (text) {
57 var date, str, i, len,
58 parsed = [];
59 str = text.replace( /\s+/g, ' ' ).replace( /[\/\-.,]/g, '-' ).match( regex.dmy );
60 len = str && str.length;
61 if ( len ) {
62 for (i = 0; i < len; i++) {
63 date = new Date( ( '' + str[i] ).replace( regex.dmyreplace, '$2/$1/$3' ) );
64 parsed.push( date instanceof Date && isFinite(date) ? date.getTime() : str[i] );
65 }
66 // sort from min to max
67 return parsed.sort().join( ' - ' );
68 }
69 return text;
70 },
71 type: 'text'
72 });
73
74 /*! date-range DDMMYYYY *//* (2000/2/15 - 2000/5/18) */
75 $.tablesorter.addParser({
76 id: 'date-range-ymd',
77 is: function () {
78 return false;
79 },
80 format: function (text) {
81 var date, str, i, len,
82 parsed = [];
83 str = text.replace( /\s+/g, ' ' ).replace( /[\/\-.,]/g, '-' ).match( regex.ymd );
84 len = str && str.length;
85 if ( len ) {
86 for (i = 0; i < len; i++) {
87 date = new Date( ( '' + str[i] ).replace( regex.ymdreplace, '$2/$3/$1' ) );
88 parsed.push( date instanceof Date && isFinite(date) ? date.getTime() : str[i] );
89 }
90 // sort from min to max
91 return parsed.sort().join( ' - ' );
92 }
93 return text;
94 },
95 type: 'text'
96 });
97
98 if ( !ts.dates ) { ts.dates = {}; }
99 if ( !ts.dates.months ) { ts.dates.months = {}; }
100 ts.dates.months.en = {
101 // See http://mottie.github.io/tablesorter/docs/example-widget-grouping.html
102 // for details on how to use CLDR data for a locale to add data for this parser
103 // CLDR returns an object { 1: "Jan", 2: "Feb", 3: "Mar", ..., 12: "Dec" }
104 1 : 'Jan',
105 2 : 'Feb',
106 3 : 'Mar',
107 4 : 'Apr',
108 5 : 'May',
109 6 : 'Jun',
110 7 : 'Jul',
111 8 : 'Aug',
112 9 : 'Sep',
113 10: 'Oct',
114 11: 'Nov',
115 12: 'Dec'
116 };
117
118 getMonthVal = function( str, c, cellIndex ) {
119 var m, month,
120 // add options to 'config.globalize' for all columns --> globalize : { lang: 'en' }
121 // or per column by using the column index --> globalize : { 0 : { lang: 'fr' } }
122 options = c.globalize && ( c.globalize[ cellIndex ] || c.globalize ) || {},
123 months = ts.dates.months[ options.lang || 'en' ];
124 if ( c.ignoreCase ) {
125 str = str.toLowerCase();
126 }
127 for ( month in months ) {
128 if ( typeof month === 'string' ) {
129 m = months[ month ];
130 if ( c.ignoreCase ) {
131 m = m.toLowerCase();
132 }
133 if ( str.match( m ) ) {
134 return parseInt( month, 10 );
135 }
136 }
137 }
138 return str;
139 };
140
141 /*! date-range "dd MMM yyyy - dd MMM yyyy" *//* ( hms optional )*/
142 ts.addParser({
143 id: 'date-range-dMMMyyyy',
144 is: function () {
145 return false;
146 },
147 format: function( text, table, cell, cellIndex ) {
148 var date, month, matches, i,
149 parsed = [],
150 str = text.replace( /\s+/g, ' ' ).match( regex.overall_dMMMyyyy ),
151 len = str && str.length;
152 if ( len ) {
153 for ( i = 0; i < len; i++ ) {
154 date = '';
155 matches = str[ i ].match( regex.matches_dMMMyyyy );
156 if ( matches && matches.length >= 4 ) {
157 matches.shift();
158 month = getMonthVal( matches[1], table.config, cellIndex );
159 if ( !isNaN( month ) ) {
160 str[i] = str[i].replace( matches[1], month );
161 }
162 date = new Date( ( '' + str[ i ] ).replace( ts.regex.shortDateXXY, '$3/$2/$1' ) );
163 }
164 parsed.push( date instanceof Date && isFinite(date) ? date.getTime() : str[i] );
165 }
166 // sort from min to max
167 return parsed.sort().join( ' - ' );
168 }
169 return text;
170 },
171 type: 'text'
172 });
173
174 })(jQuery);