-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXIII.js
192 lines (156 loc) · 5.23 KB
/
XIII.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*!* @preserve
*
* https://github.com/8HoLoN/XIII
* @version: 0.21.00 ( April 2015 )
* @author 8HoLoN / https://github.com/8HoLoN/
* < 8holon [at] gmail.com >
* Copyright (c) 2011-2015 Alexandre REMY
*/
;(function(_g){
'use strict';
function XIII(_v,_args){
if (typeof this === 'undefined' || this === window ) {
return new XIII(_v,_args).getOppositeNumerals();
};
_args = _args || {};
if( typeof _v === 'object' ){
_args = _v;
_v = (typeof _args.value!=='undefined'?_args.value:'');
}else{
_v = (typeof _v!=='undefined'?_v:'');
}
this.u = ['I','V','X','L','C','D','M'];
this.extendedMode = _args.extendedMode || false;
this.largeNumberNotation = _args.largeNumberNotation || false;
this.forceDoubleBarUse = _args.forceDoubleBarUse || false;
this.forceSideBarsUse = (typeof _args.forceSideBarsUse==='boolean'?_args.forceSideBarsUse:false);
this.strictMode = (typeof _args.strictMode==='boolean'?_args.strictMode:false);
if( (_v+'').match(/^[0-9]+$/) ){
this.o = 0;
this.aN = _v;
this.rN = this.a2r(_v);
}else if( _v.match(/^[IVXLCDM]+$/) ){
this.o = 1;
this.rN = _v;
this.aN = this.r2a(_v);
}else{
this.rN = 'I';
this.aN = 1;
}
}
_g.XIII = XIII;
XIII.prototype.getArabicNumerals = function() {
return this.aN;
};
XIII.prototype.getRomanNumerals = function() {
return this.rN;
};
XIII.prototype.getOppositeNumerals = function() {
if( this.strictMode ){
if( this.a2r(this.aN) !== this.rN ){
return false;
}
}
return this.o?this.aN:this.rN;
};
XIII.prototype.or2a = function(_v) {
var _ret = 1;
for( var i = 0 ; i < 7 ; i++ ){
if( _v == this.u[i]){
_ret=(Math.pow(10,i-(i>>>1))*((~i&0x1)+1))>>>1;
}
}
return _ret;
};
XIII.prototype.r2a = function(_v) {
var _ret = 1;
var _last = 0;
var _tot = 0;
_ret = this.or2a(_v[0]);
_last = _ret;
for( var i=1,l=_v.length ; i < l ; i++ ){
_ret = this.or2a(_v[i]);
if( _ret <= _last ){
_tot += _last;
}else{
_tot += _ret-_last;
_ret=0;
if( i+1<_v.length ){_ret=this.or2a(_v[++i]);}
}
_last = _ret;
}
_tot += _last;
return _tot;
};
XIII.prototype.a2r = function(_v) {
var _ret='';
var _nTmp=0;
if( _v <= 3999 || (_v <= 4999 && this.extendedMode) ){
for( var i=0 ; Math.floor(_v/(Math.pow(10,i))) > 0 ; i++ ){
_nTmp=Math.floor(_v/(Math.pow(10,i)))-Math.floor(_v/(Math.pow(10,i+1)))*10;
if( _nTmp>3 ){
if( _nTmp>4 && _nTmp<9 ){
for( var j=0 ; j<_nTmp-5 ; j++ ){
_ret=this.u[i*2]+_ret;
}
_ret=this.u[i*2+1]+_ret;
}else{
if( i<3 ){
if( _nTmp==4 ){_ret=this.u[i*2]+this.u[i*2+1]+_ret;}
else if( _nTmp==9 ){_ret=this.u[i*2]+this.u[i*2+2]+_ret;}
}else{
for(var j=0 ; j<_nTmp ; j++){
_ret=this.u[i*2]+_ret;
}
}
}
}else{
for(var j=0 ; j<_nTmp ; j++){
_ret=this.u[i*2]+_ret;
}
}
}
}else{// [5000;4999999]
var _arr=[];
var _u = _v - Math.floor(_v/1e3)*1e3;
_arr.unshift(this.a2r(_u));
var _m = Math.floor(_v/1e3);
if( _v <= 3999999 || (_v <= 4999999 && this.extendedMode) ){
_arr.unshift(this.a2r(_m).replace(/(.)/g,'$1\u0305'));
}else{
_m -= Math.floor(_v/1e6)*1e3; // only if > 4 999 999 => not MMMM bar
_arr.unshift(this.a2r(_m).replace(/(.)/g,'$1\u0305'));
//_m = this.a2r(_m)+'\u00B7M';// •M [5000;4999999]
var _mm = Math.floor(_v/1e6);
if( _v <= 3999999999 || (_v <= 4999999999 && this.extendedMode) ){
_arr.unshift(this.a2r(_mm).replace(/(.)/g,'$1\u033F'));
}else{
if( this.forceSideBarsUse && _v <= 39999999999 || (_v <= 49999999999 && this.extendedMode) ){
/*
_mm -= Math.floor(_v/1e9)*1e3;// only if > 4 999 999 999 => not MMMM bar
_arr.unshift(this.a2r(_mm).replace(/(.)/g,'$1\u033F'));
// use 1e5 previous level
var _mmm = Math.floor(_v/1e9);// max number 499,999,999,999
_mmm = this.a2r(_mmm*10).replace(/(.)/g,'$1\u033F');
_mmm = (_mmm!=''?'|'+_mmm+'|':_mmm);
_arr.unshift(_mmm);
/*/
_mm -= Math.floor(_v/1e8)*1e2;// only if > 4 999 999 999 => not MMMM bar
_arr.unshift(this.a2r(_mm).replace(/(.)/g,'$1\u033F'));
// use 1e5 previous level
var _mmm = Math.floor(_v/1e8);// max number 499,999,999,999
_mmm = this.a2r(_mmm*10).replace(/(.)/g,'$1\u033F');
_mmm = (_mmm!=''?'|'+_mmm+'|':_mmm);
_arr.unshift(_mmm);
//*/
}else{
throw new RangeError('Parameter must be between ' + 1 + ' and ' + 3999999999);
}
}
}
return _arr.join('');
//return 'max exceed';
}
return _ret;
};
})(window);