Debian Codegraph @

js/parsers/parser-ignore-articles.js

raw symbols SCIP
1 /*! Parser: ignoreArticles - updated 9/15/2014 (v2.17.8) *//*
2 * This parser will remove 'The', 'A' and 'An' from the beginning of a book
3 * or movie title, so it sorts by the second word or number
4 * Demo: http://jsfiddle.net/Mottie/abkNM/5/
5 */
6 /*jshint browser: true, jquery:true, unused:false */
7 ;(function($) {
8 'use strict';
9
10 var ts = $.tablesorter;
11
12 // basic list from http://en.wikipedia.org/wiki/Article_%28grammar%29
13 ts.ignoreArticles = {
14 'en' : 'the, a, an',
15 'de' : 'der, die, das, des, dem, den, ein, eine, einer, eines, einem, einen',
16 'nl' : 'de, het, de, een',
17 'es' : 'el, la, lo, los, las, un, una, unos, unas',
18 'pt' : 'o, a, os, as, um, uma, uns, umas',
19 'fr' : 'le, la, l\'_, les, un, une, des',
20 'it' : 'il, lo, la, l\'_, i, gli, le, un\', uno, una, un',
21 'hu' : 'a, az, egy'
22 };
23
24 // To add a custom parser, define:
25 // $.tablesorter.ignoreArticles['xx'] = 'A, B, C';
26 // and then set the language id 'xx' in the headers option
27 // ignoreArticles : 'xx'
28
29 ts.addParser({
30 id: 'ignoreArticles',
31 is: function() {
32 return false;
33 },
34 format: function(s, table, cell, cellIndex) {
35 var art, ignore, lang,
36 c = table.config,
37 str = s || '';
38 if ( !(c.headers && c.headers[cellIndex] && c.headers[cellIndex].ignoreArticlesRegex) ) {
39 // initialize - save regex in c.headers[cellIndex].ignoreArticlesRegex
40 if (!c.headers) { c.headers = {}; }
41 if (!c.headers[cellIndex]) { c.headers[cellIndex] = {}; }
42 lang = ts.getData( c.$headers.eq(cellIndex), ts.getColumnData( table, c.headers, cellIndex ), 'ignoreArticles' );
43 art = (ts.ignoreArticles[lang] || 'the, a, an' ) + '';
44 c.headers[cellIndex].ignoreArticlesRegex = new RegExp('^(' + $.trim( art.split(/\s*\,\s*/).join('\\s|') + '\\s' ).replace('_\\s', '') + ')', 'i');
45 // exception regex stored in c.headers[cellIndex].ignoreArticlesRegex2
46 ignore = ts.getData( c.$headers.eq(cellIndex), ts.getColumnData( table, c.headers, cellIndex ), 'ignoreArticlesExcept' );
47 c.headers[cellIndex].ignoreArticlesRegex2 = ignore !== '' ? new RegExp('^(' + ignore.replace(/\s/g, '\\s') + ')', 'i') : '';
48 }
49 art = c.headers[cellIndex].ignoreArticlesRegex;
50 if (art.test(str)) {
51 ignore = c.headers[cellIndex].ignoreArticlesRegex2;
52 if ( !(ignore && ignore.test(str)) ) {
53 return str.replace(art, '');
54 }
55 }
56 return str;
57 },
58 type: 'text'
59 });
60
61 })(jQuery);