Debian Codegraph @

js/parsers/parser-roman.js

raw symbols SCIP
1 /*! Parser: roman - updated 6/28/MMXIV (v2.17.3) *//*
2 * code modified from both:
3 * Steven Levithan @ http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter
4 * Jonathan Snook comment @ http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter#comment-16140
5 */
6 /*jshint jquery:true, unused:false */
7 ;(function($) {
8 'use strict';
9
10 // allow lower case roman numerals, since lists use i, ii, iii, etc.
11 var validator = /^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/i,
12 matcher = /\b([MCDLXVI]+\b)/gi,
13 lookup = { I:1, V:5, X:10, L:50, C:100, D:500, M:1000 };
14
15 $.tablesorter.addParser({
16 id: 'roman',
17 is: function() {
18 return false;
19 },
20 format: function(s) {
21 var val,
22 roman = s.toUpperCase().split(''),
23 num = 0;
24
25 // roman numerals not found!
26 if ( !(s && validator.test(s)) ) {
27 return s;
28 }
29
30 while (roman.length) {
31 val = lookup[roman.shift()];
32 num += val * (val < lookup[roman[0]] ? -1 : 1);
33 }
34
35 return num;
36 },
37 type: 'numeric'
38 });
39
40 $.tablesorter.addParser({
41 id: 'roman-ignore',
42 is: function() {
43 return false;
44 },
45 format: function(s, table, cell, column) {
46 var val, orig,
47 c = table.config,
48 ignore = $.isArray(c.roman_ignore) ? c.roman_ignore[column] : 0,
49 // find roman numerals
50 roman = ( isNaN(ignore) ?
51 // ignore can be a regex or string
52 $.trim( s.replace(ignore, '') ) :
53 // or a number to ignore the last x letters...
54 $.trim( s.substring(0, s.length - ignore) )
55 ).match(matcher),
56 v = validator.test(roman),
57 num = 0;
58
59 // roman numerals not found!
60 if ( !(v) ) {
61 return s;
62 }
63
64 // save roman numeral for replacement
65 orig = roman[0];
66 roman = orig.toUpperCase().split('');
67
68 while (roman.length) {
69 val = lookup[roman.shift()];
70 // ignore non-roman numerals
71 if (val) {
72 num += val * (val < lookup[roman[0]] ? -1 : 1);
73 }
74 }
75
76 return num ? s.replace(orig, num) : s;
77 },
78 type: 'text'
79 });
80
81 $.tablesorter.addParser({
82 id: 'roman-extract',
83 is: function() {
84 return false;
85 },
86 format: function(s) {
87 var val,
88 // find roman numerals
89 roman = $.grep(s.split(/\b/), function(v) {
90 return validator.test(v) ? v : '';
91 }).join('').match(matcher),
92
93 v = roman ? validator.test(roman) : 0,
94 num = 0;
95
96 // roman numerals not found!
97 if ( !(v) ) {
98 return s;
99 }
100
101 // save roman numeral for replacement
102 roman = roman[0].toUpperCase().split('');
103
104 while (roman.length) {
105 val = lookup[roman.shift()];
106 // ignore non-roman numerals
107 if (val) {
108 num += val * (val < lookup[roman[0]] ? -1 : 1);
109 }
110 }
111
112 return num ? num : s;
113 },
114 type: 'numeric'
115 });
116
117 })(jQuery);