/JQuery.WebKit/JQuery.WebKit/Resources/ClientScripts/JQuery/Plugins/jquery.format.js

# · JavaScript · 522 lines · 469 code · 42 blank · 11 comment · 259 complexity · f528b2243d46fb19bae1155322bdc4e6 MD5 · raw file

  1. /*
  2. * jQuery Format Plugin v${version}
  3. * http://www.asual.com/jquery/format/
  4. *
  5. * Copyright (c) 2009-2010 Rostislav Hristov
  6. * Uses code by Matt Kruse
  7. * Dual licensed under the MIT or GPL Version 2 licenses.
  8. * http://jquery.org/license
  9. *
  10. * Date: ${timestamp}
  11. */
  12. (function ($) {
  13. $.format = (function () {
  14. var UNDEFINED = 'undefined',
  15. TRUE = true,
  16. FALSE = false,
  17. _locale = {
  18. date: {
  19. format: 'dddd, MMMM dd, yyyy h:mm:ss tt',
  20. monthsFull: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  21. monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  22. daysFull: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  23. daysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  24. timeFormat: 'h:mm tt',
  25. shortDateFormat: 'M/d/yyyy',
  26. longDateFormat: 'dddd, MMMM dd, yyyy'
  27. },
  28. number: {
  29. format: '#,##0.0#',
  30. groupingSeparator: ',',
  31. decimalSeparator: '.'
  32. }
  33. };
  34. return {
  35. locale: function (value) {
  36. a = { a: 6 };
  37. if (value) {
  38. for (var p in value) {
  39. for (var v in value[p]) {
  40. _locale[p][v] = value[p][v];
  41. }
  42. }
  43. }
  44. return _locale;
  45. },
  46. date: function (value, format) {
  47. var i = 0,
  48. j = 0,
  49. l = 0,
  50. c = '',
  51. token = '',
  52. x,
  53. y;
  54. if (typeof value == 'string') {
  55. var getNumber = function (str, p, minlength, maxlength) {
  56. for (var x = maxlength; x >= minlength; x--) {
  57. var token = str.substring(p, p + x);
  58. if (token.length >= minlength && (new RegExp(/^\d+$/)).test(token)) {
  59. return token;
  60. }
  61. }
  62. return null;
  63. };
  64. if (typeof format == UNDEFINED) {
  65. format = _locale.date.format;
  66. }
  67. var _strict = false,
  68. pos = 0,
  69. now = new Date(),
  70. year = now.getYear(),
  71. month = now.getMonth() + 1,
  72. date = 1,
  73. hh = now.getHours(),
  74. mm = now.getMinutes(),
  75. ss = now.getSeconds(),
  76. SSS = now.getMilliseconds(),
  77. ampm = '',
  78. monthName,
  79. dayName;
  80. while (i < format.length) {
  81. token = '';
  82. c = format.charAt(i);
  83. while ((format.charAt(i) == c) && (i < format.length)) {
  84. token += format.charAt(i++);
  85. }
  86. if (token.indexOf('MMMM') > -1 && token.length > 4) {
  87. token = 'MMMM';
  88. }
  89. if (token.indexOf('EEEE') > -1 && token.length > 4) {
  90. token = 'EEEE';
  91. }
  92. if (token == 'yyyy' || token == 'yy' || token == 'y') {
  93. if (token == 'yyyy') {
  94. x = 4;
  95. y = 4;
  96. }
  97. if (token == 'yy') {
  98. x = 2;
  99. y = 2;
  100. }
  101. if (token == 'y') {
  102. x = 2;
  103. y = 4;
  104. }
  105. year = getNumber(value, pos, x, y);
  106. if (year === null) {
  107. return 0;
  108. }
  109. pos += year.length;
  110. if (year.length == 2) {
  111. year = parseInt(year, 10);
  112. if (year > 70) {
  113. year = 1900 + year;
  114. } else {
  115. year = 2000 + year;
  116. }
  117. }
  118. } else if (token == 'MMMM') {
  119. month = 0;
  120. for (j = 0, l = _locale.date.monthsFull.length; j < l; j++) {
  121. monthName = _locale.date.monthsFull[j];
  122. if (value.substring(pos, pos + monthName.length).toLowerCase() == monthName.toLowerCase()) {
  123. month = j + 1;
  124. pos += monthName.length;
  125. break;
  126. }
  127. }
  128. if ((month < 1) || (month > 12)) {
  129. return 0;
  130. }
  131. } else if (token == 'MMM') {
  132. month = 0;
  133. for (j = 0, l = _locale.date.monthsShort.length; j < l; j++) {
  134. monthName = _locale.date.monthsShort[j];
  135. if (value.substring(pos, pos + monthName.length).toLowerCase() == monthName.toLowerCase()) {
  136. month = j + 1;
  137. pos += monthName.length;
  138. break;
  139. }
  140. }
  141. if ((month < 1) || (month > 12)) {
  142. return 0;
  143. }
  144. } else if (token == 'EEEE') {
  145. for (j = 0, l = _locale.date.daysFull.length; j < l; j++) {
  146. dayName = _locale.date.daysFull[j];
  147. if (value.substring(pos, pos + dayName.length).toLowerCase() == dayName.toLowerCase()) {
  148. pos += dayName.length;
  149. break;
  150. }
  151. }
  152. } else if (token == 'EEE') {
  153. for (j = 0, l = _locale.date.daysShort.length; j < l; j++) {
  154. dayName = _locale.date.daysShort[j];
  155. if (value.substring(pos, pos + dayName.length).toLowerCase() == dayName.toLowerCase()) {
  156. pos += dayName.length;
  157. break;
  158. }
  159. }
  160. } else if (token == 'MM' || token == 'M') {
  161. month = getNumber(value, pos, _strict ? token.length : 1, 2);
  162. if (month === null || (month < 1) || (month > 12)) {
  163. return 0;
  164. }
  165. pos += month.length;
  166. } else if (token == 'dd' || token == 'd') {
  167. date = getNumber(value, pos, _strict ? token.length : 1, 2);
  168. if (date === null || (date < 1) || (date > 31)) {
  169. return 0;
  170. }
  171. pos += date.length;
  172. } else if (token == 'hh' || token == 'h') {
  173. hh = getNumber(value, pos, _strict ? token.length : 1, 2);
  174. if (hh === null || (hh < 1) || (hh > 12)) {
  175. return 0;
  176. }
  177. pos += hh.length;
  178. } else if (token == 'HH' || token == 'H') {
  179. hh = getNumber(value, pos, _strict ? token.length : 1, 2);
  180. if (hh === null || (hh < 0) || (hh > 23)) {
  181. return 0;
  182. }
  183. pos += hh.length;
  184. } else if (token == 'KK' || token == 'K') {
  185. hh = getNumber(value, pos, _strict ? token.length : 1, 2);
  186. if (hh === null || (hh < 0) || (hh > 11)) {
  187. return 0;
  188. }
  189. pos += hh.length;
  190. } else if (token == 'kk' || token == 'k') {
  191. hh = getNumber(value, pos, _strict ? token.length : 1, 2);
  192. if (hh === null || (hh < 1) || (hh > 24)) {
  193. return 0;
  194. }
  195. pos += hh.length;
  196. hh--;
  197. } else if (token == 'mm' || token == 'm') {
  198. mm = getNumber(value, pos, _strict ? token.length : 1, 2);
  199. if (mm === null || (mm < 0) || (mm > 59)) {
  200. return 0;
  201. }
  202. pos += mm.length;
  203. } else if (token == 'ss' || token == 's') {
  204. ss = getNumber(value, pos, _strict ? token.length : 1, 2);
  205. if (ss === null || (ss < 0) || (ss > 59)) {
  206. return 0;
  207. }
  208. pos += ss.length;
  209. } else if (token == 'SSS' || token == 'SS' || token == 'S') {
  210. SSS = getNumber(value, pos, _strict ? token.length : 1, 3);
  211. if (SSS === null || (SSS < 0) || (SSS > 999)) {
  212. return 0;
  213. }
  214. pos += SSS.length;
  215. } else if (token == 'a') {
  216. var ap = value.substring(pos, pos + 2).toLowerCase();
  217. if (ap == 'am') {
  218. ampm = 'AM';
  219. } else if (ap == 'pm') {
  220. ampm = 'PM';
  221. } else {
  222. return 0;
  223. }
  224. pos += 2;
  225. } else {
  226. if (token != value.substring(pos, pos + token.length)) {
  227. return 0;
  228. } else {
  229. pos += token.length;
  230. }
  231. }
  232. }
  233. if (pos != value.length) {
  234. return 0;
  235. }
  236. if (month == 2) {
  237. if (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)) {
  238. if (date > 29) {
  239. return 0;
  240. }
  241. } else {
  242. if (date > 28) {
  243. return 0;
  244. }
  245. }
  246. }
  247. if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
  248. if (date > 30) {
  249. return 0;
  250. }
  251. }
  252. if (hh < 12 && ampm == 'PM') {
  253. hh = hh - 0 + 12;
  254. } else if (hh > 11 && ampm == 'AM') {
  255. hh -= 12;
  256. }
  257. return (new Date(year, month - 1, date, hh, mm, ss, SSS));
  258. } else {
  259. var formatNumber = function (n, s) {
  260. if (typeof s == UNDEFINED || s == 2) {
  261. return (n >= 0 && n < 10 ? '0' : '') + n;
  262. } else {
  263. if (n >= 0 && n < 10) {
  264. return '00' + n;
  265. }
  266. if (n >= 10 && n < 100) {
  267. return '0' + n;
  268. }
  269. return n;
  270. }
  271. };
  272. if (typeof format == UNDEFINED) {
  273. format = _locale.date.format;
  274. }
  275. y = value.getYear();
  276. if (y < 1000) {
  277. y = String(y + 1900);
  278. }
  279. var M = value.getMonth() + 1,
  280. d = value.getDate(),
  281. E = value.getDay(),
  282. H = value.getHours(),
  283. m = value.getMinutes(),
  284. s = value.getSeconds(),
  285. S = value.getMilliseconds();
  286. value = {
  287. y: y,
  288. yyyy: y,
  289. yy: String(y).substring(2, 4),
  290. M: M,
  291. MM: formatNumber(M),
  292. MMM: _locale.date.monthsShort[M - 1],
  293. MMMM: _locale.date.monthsFull[M - 1],
  294. d: d,
  295. dd: formatNumber(d),
  296. EEE: _locale.date.daysShort[E],
  297. EEEE: _locale.date.daysFull[E],
  298. H: H,
  299. HH: formatNumber(H)
  300. };
  301. if (H === 0) {
  302. value.h = 12;
  303. } else if (H > 12) {
  304. value.h = H - 12;
  305. } else {
  306. value.h = H;
  307. }
  308. value.hh = formatNumber(value.h);
  309. value.k = H + 1;
  310. value.kk = formatNumber(value.k);
  311. if (H > 11) {
  312. value.K = H - 12;
  313. } else {
  314. value.K = H;
  315. }
  316. value.KK = formatNumber(value.K);
  317. if (H > 11) {
  318. value.a = 'PM';
  319. } else {
  320. value.a = 'AM';
  321. }
  322. value.m = m;
  323. value.mm = formatNumber(m);
  324. value.s = s;
  325. value.ss = formatNumber(s);
  326. value.S = S;
  327. value.SS = formatNumber(S);
  328. value.SSS = formatNumber(S, 3);
  329. var result = '';
  330. i = 0;
  331. c = '';
  332. token = '';
  333. s = false;
  334. while (i < format.length) {
  335. token = '';
  336. c = format.charAt(i);
  337. if (c == '\'') {
  338. i++;
  339. if (format.charAt(i) == c) {
  340. result = result + c;
  341. i++;
  342. } else {
  343. s = !s;
  344. }
  345. } else {
  346. while (format.charAt(i) == c) {
  347. token += format.charAt(i++);
  348. }
  349. if (token.indexOf('MMMM') != -1 && token.length > 4) {
  350. token = 'MMMM';
  351. }
  352. if (token.indexOf('EEEE') != -1 && token.length > 4) {
  353. token = 'EEEE';
  354. }
  355. if (typeof value[token] != UNDEFINED && !s) {
  356. result = result + value[token];
  357. } else {
  358. result = result + token;
  359. }
  360. }
  361. }
  362. return result;
  363. }
  364. },
  365. number: function (value, format) {
  366. var groupingSeparator,
  367. groupingIndex,
  368. decimalSeparator,
  369. decimalIndex,
  370. roundFactor,
  371. i;
  372. if (typeof value == 'string') {
  373. groupingSeparator = _locale.number.groupingSeparator;
  374. decimalSeparator = _locale.number.decimalSeparator;
  375. decimalIndex = value.indexOf(decimalSeparator);
  376. roundFactor = 1;
  377. if (decimalIndex != -1) {
  378. roundFactor = Math.pow(10, value.length - decimalIndex - 1);
  379. }
  380. value = value.replace(new RegExp('[' + groupingSeparator + ']', 'g'), '');
  381. value = value.replace(new RegExp('[' + decimalSeparator + ']'), '.');
  382. return (parseInt(value * roundFactor, 10)) / roundFactor;
  383. } else {
  384. if (typeof format == UNDEFINED || format.length < 1) {
  385. format = _locale.number.format;
  386. }
  387. var integer = '',
  388. fraction = '',
  389. negative = value < 0;
  390. value = Math.abs(value);
  391. groupingSeparator = ',';
  392. groupingIndex = format.lastIndexOf(groupingSeparator);
  393. decimalSeparator = '.';
  394. decimalIndex = format.indexOf(decimalSeparator);
  395. if (decimalIndex != -1) {
  396. fraction = _locale.number.decimalSeparator;
  397. var minFraction = format.substr(decimalIndex + 1).replace(/#/g, '').length,
  398. maxFraction = format.substr(decimalIndex + 1).length;
  399. if (maxFraction > 0) {
  400. roundFactor = 1000;
  401. var powFraction = Math.pow(10, maxFraction),
  402. tempRound = Math.round(parseInt(value * powFraction * roundFactor -
  403. Math.round(value) * powFraction * roundFactor, 10) / roundFactor),
  404. tempFraction = String(tempRound < 0 ? Math.round(parseInt(value * powFraction * roundFactor -
  405. parseInt(value, 10) * powFraction * roundFactor, 10) / roundFactor) : tempRound),
  406. parts = value.toString().split('.');
  407. if (typeof parts[1] != UNDEFINED) {
  408. for (i = 0; i < parts[1].length; i++) {
  409. if (parts[1].substr(i, 1) == '0') {
  410. tempFraction = '0' + tempFraction;
  411. } else {
  412. break;
  413. }
  414. }
  415. }
  416. for (i = 0; i < (maxFraction - fraction.length); i++) {
  417. tempFraction += '0';
  418. }
  419. var symbol,
  420. formattedFraction = '';
  421. for (i = 0; i < tempFraction.length; i++) {
  422. symbol = tempFraction.substr(i, 1);
  423. if (i >= minFraction && symbol == '0' && /^0*$/.test(tempFraction.substr(i + 1))) {
  424. break;
  425. }
  426. formattedFraction += symbol;
  427. }
  428. fraction += formattedFraction;
  429. }
  430. if (fraction == _locale.number.decimalSeparator) {
  431. fraction = '';
  432. }
  433. }
  434. if (decimalIndex !== 0) {
  435. if (fraction != '') {
  436. integer = String(Math.floor(value));
  437. } else {
  438. integer = String(Math.round(value));
  439. }
  440. var grouping = _locale.number.groupingSeparator,
  441. groupingSize = 0;
  442. if (groupingIndex != -1) {
  443. if (decimalIndex != -1) {
  444. groupingSize = decimalIndex - groupingIndex;
  445. } else {
  446. groupingSize = format.length - groupingIndex;
  447. }
  448. groupingSize--;
  449. }
  450. if (groupingSize > 0) {
  451. var count = 0,
  452. formattedInteger = '';
  453. i = integer.length;
  454. while (i--) {
  455. if (count !== 0 && count % groupingSize === 0) {
  456. formattedInteger = grouping + formattedInteger;
  457. }
  458. formattedInteger = integer.substr(i, 1) + formattedInteger;
  459. count++;
  460. }
  461. integer = formattedInteger;
  462. }
  463. var maxInteger;
  464. if (decimalIndex != -1) {
  465. maxInteger = format.substr(0, decimalIndex).replace(new RegExp('#|' + grouping, 'g'), '').length;
  466. } else {
  467. maxInteger = format.replace(new RegExp('#|' + grouping, 'g'), '').length;
  468. }
  469. var tempInteger = integer.length;
  470. for (i = tempInteger; i < maxInteger; i++) {
  471. integer = '0' + integer;
  472. }
  473. }
  474. result = integer + fraction;
  475. return (negative ? '-' : '') + result;
  476. }
  477. }
  478. };
  479. })();
  480. } (jQuery));