Skip to content

Commit

Permalink
Parsers: globalization tweaks & new parser
Browse files Browse the repository at this point in the history
Make month & weekday Globalize ready.
globalization parser now caches the Globalize object in a "Globalize" option
Add "weekday-index" parser
Update demos
  • Loading branch information
Mottie committed Nov 2, 2015
1 parent dbcd306 commit 4fc923e
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 76 deletions.
16 changes: 9 additions & 7 deletions dist/js/parsers/parser-date-month.min.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*! Parser: Month - updated 10/26/2014 (v2.18.0) */
/*! Parser: Month - updated 11/2/2015 (v2.24.1) */
/* Demo: http://jsfiddle.net/Mottie/abkNM/4169/ */
/*jshint jquery:true */
!function(a){"use strict";var b=a.tablesorter;b.dates=a.extend({},b.dates,{
// *** modify this array to match the desired language ***
monthCased:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]}),b.dates.monthLower=b.dates.monthCased.join(",").toLocaleLowerCase().split(","),b.addParser({id:"month",is:function(){return!1},format:function(c,d){if(c){var e=-1,f=d.config,g=f.ignoreCase?c.toLocaleLowerCase():c;
// return s (original string) if there isn't a match
// (non-weekdays will sort separately and empty cells will sort as expected)
return a.each(b.dates["month"+(f.ignoreCase?"Lower":"Cased")],function(a,b){return 0>e&&g.match(b)?(e=a,!1):void 0}),0>e?c:e}return c},type:"numeric"})}(jQuery);
!function(a){"use strict";var b=a.tablesorter;b.dates=a.extend({},{
// See http://mottie.github.io/tablesorter/docs/example-widget-grouping.html
// for details on how to use CLDR data for a locale to add data for this parser
// CLDR returns an object { 1: "Jan", 2: "Feb", 3: "Mar", ..., 12: "Dec" }
months:{en:{1:"Jan",2:"Feb",3:"Mar",4:"Apr",5:"May",6:"Jun",7:"Jul",8:"Aug",9:"Sep",10:"Oct",11:"Nov",12:"Dec"}}},b.dates),b.addParser({id:"month",is:function(){return!1},format:function(a,c,d,e){if(a){var f,g,h=c.config,
// add options to 'config.globalize' for all columns --> globalize : { lang: 'en' }
// or per column by using the column index --> globalize : { 0 : { lang: 'fr' } }
i=h.globalize&&(h.globalize[e]||h.globalize)||{},j=b.dates.months[i.lang||"en"];h.ignoreCase&&(a=a.toLowerCase());for(g in j)if("string"==typeof g&&(f=j[g],h.ignoreCase&&(f=f.toLowerCase()),a.match(f)))return parseInt(g,10)}return a},type:"numeric"})}(jQuery);
31 changes: 24 additions & 7 deletions dist/js/parsers/parser-date-weekday.min.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
/*! Parser: weekday - updated 10/26/2014 (v2.18.0) */
/*! Parser: weekday - updated 11/2/2015 (v2.24.1) */
/* Demo: http://jsfiddle.net/Mottie/abkNM/4169/ */
/*jshint jquery:true */
!function(a){"use strict";var b=a.tablesorter;b.dates=a.extend({},b.dates,{
// *** modify this array to change match the language ***
weekdayCased:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]}),b.dates.weekdayLower=b.dates.weekdayCased.join(",").toLocaleLowerCase().split(","),b.addParser({id:"weekday",is:function(){return!1},format:function(c,d){if(c){var e=-1,f=d.config;
// return s (original string) if there isn't a match
// (non-weekdays will sort separately and empty cells will sort as expected)
return c=f.ignoreCase?c.toLocaleLowerCase():c,a.each(b.dates["weekday"+(f.ignoreCase?"Lower":"Cased")],function(a,b){return 0>e&&c.match(b)?(e=a,!1):void 0}),0>e?c:e}return c},type:"numeric"})}(jQuery);
!function(a){"use strict";var b=a.tablesorter;b.dates=a.extend(!0,{},{
// See http://mottie.github.io/tablesorter/docs/example-widget-grouping.html
// for details on how to use CLDR data for a locale to add data for this parser
// CLDR returns { sun: "Sun", mon: "Mon", tue: "Tue", wed: "Wed", thu: "Thu", ... }
weekdays:{en:{sun:"Sun",mon:"Mon",tue:"Tue",wed:"Wed",thu:"Thu",fri:"Fri",sat:"Sat"}},
// set table.config.weekStarts to change weekday start date for your locale
// cross-reference of a date on which the week starts on a...
// https://github.com/unicode-cldr/cldr-core/blob/master/supplemental/weekData.json
weekStartList:{sun:"1995",// Sun 1/1/1995
mon:"1996",// Mon 1/1/1996
fri:"1999",// Friday 1/1/1999
sat:"2000"},
// do not modify this array; it is used for cross referencing
weekdaysXref:["sun","mon","tue","wed","thu","fri","sat"]},b.dates),b.addParser({id:"weekday",is:function(){return!1},format:function(c,d,e,f){if(c){var g,h,i,j=d.config,
// add options to 'config.globalize' for all columns --> globalize : { lang: 'en' }
// or per column by using the column index --> globalize : { 0 : { lang: 'fr' } }
k=j.globalize&&(j.globalize[f]||j.globalize)||{},l=b.dates.weekdays[k.lang||"en"],m=b.dates.weekdaysXref;j.ignoreCase&&(c=c.toLowerCase());for(h in l)if("string"==typeof h&&(g=l[h],j.ignoreCase&&(g=g.toLowerCase()),c.match(g)))return i=a.inArray(h,m),i>-1?i:c}return c},type:"numeric"}),
// useful when a group widget date column is set to "group-date-week"
// and you want to only sort on the day of the week ignore the actual date, month and year
b.addParser({id:"weekday-index",is:function(){return!1},format:function(a,c){if(a){var d=c.config,e=new Date(a);if(e instanceof Date&&isFinite(e))
// use a specific date that started with that weekday so sorting is only going to be
// based on the day of the week and not the date, month or year
return new Date("1/"+(e.getDay()+1)+"/"+b.dates.weekStartList[d.weekStarts||"sun"])}return a},type:"numeric"})}(jQuery);
14 changes: 11 additions & 3 deletions dist/js/parsers/parser-globalize.min.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Parser: jQuery Globalize - updated 10/31/2015 (v2.24.0) */
/*! Parser: jQuery Globalize - updated 11/2/2015 (v2.24.1) */
/* Extract localized data using jQuery's Globalize parsers; set
Globalize.locale( 'xx' ) in the globalize settings */
/*jshint jquery:true, newcap: false */
Expand All @@ -7,8 +7,16 @@
a.tablesorter.addParser({id:"globalize-date",is:function(){return!1},format:function(a,b,c,d){var e,f,g=b.config,
// add options to 'config.globalize' for all columns --> globalize : { skeleton: 'GyMMMd' }
// or per column by using the column index --> globalize : { 0 : { datetime: 'medium' } }
h=g.globalize&&(g.globalize[d]||g.globalize)||{};return Globalize&&(e="object"==typeof h.lang?h.lang:Globalize(h.lang||"en")),f=e&&e.dateParser?e.dateParser(h)(a):a?new Date(a):a,f instanceof Date&&isFinite(f)?f.getTime():a},type:"numeric"}),/*! jQuery Globalize number parser (https://github.com/jquery/globalize#number-module) */
h=g.globalize&&(g.globalize[d]||g.globalize)||{};
// initialized Globalize object
// Globalize initialized from "lang" option
// cache the object
return Globalize&&(e="object"==typeof h.Globalize?h.Globalize:Globalize(h.lang||"en"),h.Globalize||(h.Globalize=e)),f=e&&e.dateParser?e.dateParser(h)(a):a?new Date(a):a,f instanceof Date&&isFinite(f)?f.getTime():a},type:"numeric"}),/*! jQuery Globalize number parser (https://github.com/jquery/globalize#number-module) */
a.tablesorter.addParser({id:"globalize-number",is:function(){return!1},format:function(b,c,d,e){var f,g,h=c.config,
// add options to 'config.globalize' for all columns --> globalize : { skeleton: 'GyMMMd' }
// or per column by using the column index --> globalize : { 0 : { datetime: 'medium' } }
i=h.globalize&&(h.globalize[e]||h.globalize)||{};return Globalize&&(f="object"==typeof i.lang?i.lang:Globalize(i.lang||"en")),g=f&&f.numberParser?f.numberParser(i)(b):b?a.tablesorter.formatFloat((b||"").replace(/[^\w,. \-()]/g,""),c):b,b&&"number"==typeof g?g:b},type:"numeric"})}(jQuery);
i=h.globalize&&(h.globalize[e]||h.globalize)||{};
// initialized Globalize object
// Globalize initialized from "lang" option
// cache the object
return Globalize&&(f="object"==typeof i.Globalize?i.Globalize:Globalize(i.lang||"en"),i.Globalize||(i.Globalize=f)),g=f&&f.numberParser?f.numberParser(i)(b):b?a.tablesorter.formatFloat((b||"").replace(/[^\w,. \-()]/g,""),c):b,b&&"number"==typeof g?g:b},type:"numeric"})}(jQuery);
74 changes: 63 additions & 11 deletions docs/example-parsers-dates.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<script src="js/prettify.js"></script>
<script src="js/docs.js"></script>
<style>
th { width: 20%; }
th { width: 14%; }
</style>

<link href="../css/theme.blue.css" rel="stylesheet">
Expand All @@ -30,9 +30,53 @@

<script id="js">$(function() {

// See http://mottie.github.io/tablesorter/docs/example-widget-grouping.html
// for details on how to use CLDR data for a locale to add data for this parser
// CLDR returns { sun: "Sun", mon: "Mon", tue: "Tue", wed: "Wed", thu: "Thu", ... }

// or copy the data directly as it is shown here:
// ************************
// French localization modified from days "abbreviated" format
// https://github.com/unicode-cldr/cldr-dates-modern/blob/master/main/fr/ca-gregorian.json
// It does *not* include the periods
$.tablesorter.dates.weekdays.fr = {
"sun" : "Dim",
"mon" : "Lun",
"tue" : "Mar",
"wed" : "Mer",
"thu" : "Jeu",
"fri" : "Ven",
"sat" : "Sam"
};

// CLDR returns an object { 1: "Jan", 2: "Feb", 3: "Mar", ..., 12: "Dec" }
// French localization modified from months "abbreviated" format
// https://github.com/unicode-cldr/cldr-dates-modern/blob/master/main/fr/ca-gregorian.json
// It does *not* include the periods
$.tablesorter.dates.months.fr = {
"1" : "Janv",
"2" : "Févr",
"3" : "Mars",
"4" : "Avr",
"5" : "Mai",
"6" : "Juin",
"7" : "Juil",
"8" : "Août",
"9" : "Sept",
"10" : "Oct",
"11" : "Nov",
"12" : "Déc"
};

// call the tablesorter plugin
$("table").tablesorter({
theme : 'blue',
// option used by weekday, month & globalize parsers (v2.24.1)
globalize : {
// columns that use French data
2 : { lang: 'fr' },
4 : { lang: 'fr' }
},
widgets : ["zebra"],
// date range used by the two-digit year parser (added v2.14.0)
dateRange : 30
Expand All @@ -52,6 +96,12 @@ <h3>Flexible client-side table sorting</h3>
<p class="tip">
<em>NOTE!</em>
<ul>
<li>In <span class="version">v2.24.1</span>,
<ul>
<li>Updated weekday &amp; month parsers to better integrate with <a href="https://github.com/jquery/globalize">jQuery Globalize</a>.</li>
<li>This demo now includes a column for French named weekdays &amp; months.</li>
</ul>
</li>
<li>Parse Dates with these parsers
<ul>
<li>The "Date" column is using the <a href="http://sugarjs.com/dates#comparing_dates">sugar</a> library to parse dates. Make sure to include the sugar library and the sugar-date-parser.</li>
Expand Down Expand Up @@ -79,22 +129,24 @@ <h1>Demo</h1>
<tr>
<th class="sorter-sugar">Date</th>
<th class="sorter-weekday">Weekday</th>
<th class="sorter-weekday">Weekday (French)</th>
<th class="sorter-month">Month</th>
<th class="sorter-month">Month (French)</th>
<th class="sorter-mmddyy">MM/DD/YY</th> <!-- "sorter-ddmmyy" also available -->
<th class="sorter-time">Time</th>
</tr>
</thead>
<tbody>
<tr><td>next friday</td><td>Friday</td><td>Aug</td><td>02/1/16</td><td>12:00 PM</td></tr>
<tr><td>today</td><td>Thurs</td><td>September</td><td>1/2/16</td><td>00:00</td></tr>
<tr><td>last Tuesday</td><td>Fri</td><td>Mar</td><td>11/1/15</td><td>18:00</td></tr>
<tr><td>the day after tomorrow</td><td>Wed</td><td>July</td><td>01/1/16</td><td>13:00</td></tr>
<tr><td>2010-05-25T12:30:40.299+01:00</td><td>Monday</td><td>Jan</td><td>1/11/15</td><td>1:30 PM</td></tr>
<tr><td>May 25th of next year</td><td>Tues</td><td>Nov</td><td>1/1/15</td><td>14:00</td></tr>
<tr><td>25 May 2010</td><td>Tuesday</td><td>November</td><td>3/1/15</td><td>1:58 PM</td></tr>
<tr><td>the last day of March</td><td>Mon</td><td>December</td><td>1/15/15</td><td>2:10 PM</td></tr>
<tr><td>last month</td><td>Wednesday</td><td>April</td><td>11/11/16</td><td>13:50</td></tr>
<tr><td>one day before yesterday</td><td>Thursday</td><td>Feb</td><td>5/1/15</td><td>4:00 AM</td></tr>
<tr><td>next friday</td><td>Friday</td><td>Vendredi</td><td>Aug</td><td>Août</td><td>02/1/16</td><td>12:00 PM</td></tr>
<tr><td>today</td><td>Sunday</td><td>Dim</td><td>September</td><td>Septembre</td><td>1/2/16</td><td>00:00</td></tr>
<tr><td>last Tuesday</td><td>Fri</td><td>Ven</td><td>Mar</td><td>Mars</td><td>11/1/15</td><td>18:00</td></tr>
<tr><td>the day after tomorrow</td><td>Wed</td><td>Mer</td><td>July</td><td>Juillet</td><td>01/1/16</td><td>13:00</td></tr>
<tr><td>2010-05-25T12:30:40.299+01:00</td><td>Monday</td><td>Lun</td><td>Jan</td><td>Janvier</td><td>1/11/15</td><td>1:30 PM</td></tr>
<tr><td>May 25th of next year</td><td>Tues</td><td>Mardi</td><td>Nov</td><td>Novembre</td><td>1/1/15</td><td>14:00</td></tr>
<tr><td>25 May 2010</td><td>Tuesday</td><td>Mar</td><td>November</td><td>Nov</td><td>3/1/15</td><td>1:58 PM</td></tr>
<tr><td>the last day of March</td><td>Sat</td><td>Sam</td><td>December</td><td>Décembre</td><td>1/15/15</td><td>2:10 PM</td></tr>
<tr><td>last month</td><td>Wednesday</td><td>Mercredi</td><td>April</td><td>Avr</td><td>11/11/16</td><td>13:50</td></tr>
<tr><td>one day before yesterday</td><td>Thursday</td><td>Jeudi</td><td>Feb</td><td>Février</td><td>5/1/15</td><td>4:00 AM</td></tr>
</tbody>
</table></div>

Expand Down
15 changes: 12 additions & 3 deletions docs/example-parsers-globalize.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<!-- Tablesorter: required -->
<link rel="stylesheet" href="../css/theme.blue.css">
<style>th { width: 25%; }</style>
<script src="../js/jquery.tablesorter.js"></script>
<script src="../js/parsers/parser-globalize.js"></script>

Expand All @@ -38,8 +39,9 @@
widthFixed: true,
// globalize options
globalize: {
1: { lang : en },
3: { lang : en, raw: 'MMM d, y G' }
// in v2.24.1, use Globalize to cache the object; lang should only contain the string
1: { lang : 'en', Globalize : en },
3: { lang : 'en', Globalize : en, raw: 'MMM d, y G' }
},
headers: {
1: { sorter: 'globalize-number' },
Expand Down Expand Up @@ -254,8 +256,15 @@ <h3>Flexible client-side table sorting</h3>
<p class="tip">
<em>NOTE!</em>
<ul>
<li>In <span class="version updated">v2.24.1</span>,
<ul>
<li>An internal change made it necessary to change the <code>lang</code> option to only hold a string of the set language.</li>
<li>To cache the Globalize object, save it as a <code>Globalize</code> option (see code example below).</li>
</ul>
</li>
<li><a href="https://github.com/jquery/globalize">jQuery Globalize</a> supports the parsing of locale specific data, including <a href="https://github.com/jquery/globalize/blob/master/doc/api/number/number-parser.md">numerous number formats</a> (including Arabic, Chinese &amp; Japanese) and <a href="https://github.com/jquery/globalize/blob/master/doc/api/date/date-parser.md">custom date formats</a>.</li>
<li>This is a very basic example using English only. For more details on how to set up Globalize, see the <a href="example-widget-grouping.html#globalization">Grouping widget documentation about Globalization</a>.</li>
<li>This is a very basic example using English only. For another example, see the <a href="example-parsers-dates.html">weekday &amp; month parsers</a>.</li>
<li>For more details on how to set up Globalize, see the <a href="example-widget-grouping.html#globalization">Grouping widget documentation about Globalization</a>.</li>
</ul>
<p>

Expand Down
29 changes: 27 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,14 @@ <h4 id="extras">Plugins / Widgets<br>
<h4 id="custom-parsers">Custom Parsers</h4>
<ul>
<li><a href="example-parsers-duration.html">Countdown parser</a> (<span class="version">v2.19.0</span>).</li>
<li><a href="example-parsers-dates.html">Date parsers</a> (<span class="version">v2.8</span>; <span class="version updated">v2.18.0</span>; includes weekday, month, two-digit year &amp; <a href="http://sugarjs.com/dates">sugar.js</a> date parsers).</li>
<li><a href="example-parsers-dates.html">Date parsers</a> (<span class="version">v2.8</span>; <span class="version updated">v2.24.1</span>; includes weekday, month, two-digit year &amp; <a href="http://sugarjs.com/dates">sugar.js</a> date parsers).</li>
<li><a href="example-parsers-date-range.html">Date range parsers</a> (<span class="version">v2.21.0</span>); if filters, include the <a href="example-widget-filter-custom-search2.html">insideRange</a> filter search type.</li>
<li><a href="example-parsers-duration.html">Duration parser</a> (<span class="version">v2.17.8</span>).</li>
<li><a href="example-parsers-feet-inch-fraction.html">Feet-inch-fraction parser</a> (<span class="version">v2.8</span>).</li>
<li><a href="example-parsers-file-type.html">File type parser</a> (<span class="version">v2.13</span>).</li>
<li><a href="example-parsers-ignore-articles.html">Ignore leading articles parser</a> (Ignore &quot;A&quot;, &quot;An&quot; and &quot;The&quot; in titles) (<span class="version">v2.8</span>).</li>
<li><a href="example-widget-grouping.html">Input/select parsers</a> (used by Grouping rows widget) (<span class="version">v2.8</span>; <span class="version updated">v2.24.0</span>).</li>
<li><a href="example-parsers-globalize.html">jQuery Globalize</a> (number &amp; date parsers; <span class="version">v2.22.0</span>; <span class="version updated">v2.24.0</span>).</li>
<li><a href="example-parsers-globalize.html">jQuery Globalize</a> (number &amp; date parsers; <span class="version">v2.22.0</span>; <span class="version updated">v2.24.1</span>).</li>
<li><a href="example-parsers-metric.html">Metric parser</a> (<span class="version">v2.8</span>).</li>
<li><a href="example-parsers-named-numbers.html">Named Numbers parser</a> (<span class="version">v2.18.0</span>; <span class="version updated">v2.22.0</span>).</li>
<li><a href="example-parsers-ip-address.html">Network (IPv4, IPv6 and MAC address parser</a> (<span class="version">v2.12</span>; <span class="version updated">v2.22.0</span>).</li>
Expand Down Expand Up @@ -935,6 +935,31 @@ <h1>Configuration</h1>
<td><a href="example-option-sort-empty.html">Example</a></td>
</tr>

<tr id="globalize">
<td><a href="#" class="permalink">globalize</a></td>
<td>object</td>
<td>null</td>
<td>
This option is used by parsers to localize the language (<span class="version">v2.24.0</span>; <span class="version updated">v2.24.1</span>).
<div class="collapsible"><br>
This option is meant to be used with the <a href="https://github.com/jquery/globalize">jQuery Globalize</a> library along with CLDR data.
<p>See the <a href="example-widget-grouping.html#globalization">Globalization</a> section in the group widget demo for details on how to set it up.</p>
<p>Currently, the globalize, month &amp; weekday parsers utilize this option.</p>
<pre class="prettyprint lang-js">$(function(){
$('table').tablesorter({
// globalize : { lang: 'en' } // for ALL columns
// or, per column by using the column index
globalize : {
0 : { lang: 'fr', Globalize : Globalize('fr'), raw: 'MMM d, y G' },
2 : { lang: 'fr' }
}
});
});</pre>
</div>
</td>
<td><a href="example-parsers-globalize.html">2</a> <a href="example-parsers-dates.html">2</a></td>
</tr>

<tr id="headers">
<td><a href="#" class="permalink">headers</a></td>
<td>Object</td>
Expand Down
Loading

0 comments on commit 4fc923e

Please sign in to comment.