PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/humanize-plus/1.3.3/humanize.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 476 lines | 438 code | 34 blank | 4 comment | 144 complexity | 712c94bf9717391dcce2cc9c43545c32 MD5 | raw file
  1. (function() {
  2. var arrayIndex, isArray, isFinite, isNaN, objectRef, sortedIndex, timeFormats, toString;
  3. objectRef = new function() {};
  4. toString = objectRef.toString;
  5. sortedIndex = function(array, candidate, iterator) {
  6. var high, low, mid, value;
  7. if (iterator == null) {
  8. iterator = function(value) {
  9. return value;
  10. };
  11. }
  12. value = iterator(candidate);
  13. low = 0;
  14. high = array.length;
  15. while (low < high) {
  16. mid = (low + high) >> 1;
  17. if (iterator(array[mid]) < value) {
  18. low = mid + 1;
  19. } else {
  20. high = mid;
  21. }
  22. }
  23. return low;
  24. };
  25. arrayIndex = [].indexOf || function(item) {
  26. var arr, arrItem, index, _i, _len;
  27. arr = this;
  28. for (index = _i = 0, _len = arr.length; _i < _len; index = ++_i) {
  29. arrItem = arr[index];
  30. if (arrItem === item) {
  31. return index;
  32. }
  33. }
  34. return -1;
  35. };
  36. isNaN = function(value) {
  37. return value !== value;
  38. };
  39. isFinite = function(value) {
  40. return ((typeof window !== "undefined" && window !== null ? window.isFinite : void 0) || global.isFinite)(value) && !isNaN(parseFloat(value));
  41. };
  42. isArray = function(value) {
  43. return toString.call(value) === '[object Array]';
  44. };
  45. timeFormats = [
  46. {
  47. name: 'second',
  48. value: 1e3
  49. }, {
  50. name: 'minute',
  51. value: 6e4
  52. }, {
  53. name: 'hour',
  54. value: 36e5
  55. }, {
  56. name: 'day',
  57. value: 864e5
  58. }, {
  59. name: 'week',
  60. value: 6048e5
  61. }
  62. ];
  63. this.Humanize = {};
  64. this.Humanize.intword = function(number, charWidth, decimals) {
  65. if (decimals == null) {
  66. decimals = 2;
  67. }
  68. /*
  69. # This method is deprecated. Please use compactInteger instead.
  70. # intword will be going away in the next major version.
  71. */
  72. return this.compactInteger(number, decimals);
  73. };
  74. this.Humanize.compactInteger = function(input, decimals) {
  75. var bigNumPrefixes, decimalIndex, decimalPart, decimalPartArray, length, number, numberLength, numberLengths, output, outputNumber, signString, unsignedNumber, unsignedNumberCharacterArray, unsignedNumberString, wholePart, wholePartArray, _i, _len, _length;
  76. if (decimals == null) {
  77. decimals = 0;
  78. }
  79. decimals = Math.max(decimals, 0);
  80. number = parseInt(input, 10);
  81. signString = number < 0 ? "-" : "";
  82. unsignedNumber = Math.abs(number);
  83. unsignedNumberString = "" + unsignedNumber;
  84. numberLength = unsignedNumberString.length;
  85. numberLengths = [13, 10, 7, 4];
  86. bigNumPrefixes = ['T', 'B', 'M', 'k'];
  87. if (unsignedNumber < 1000) {
  88. if (decimals > 0) {
  89. unsignedNumberString += "." + (Array(decimals + 1).join('0'));
  90. }
  91. return "" + signString + unsignedNumberString;
  92. }
  93. if (numberLength > numberLengths[0] + 3) {
  94. return number.toExponential(decimals).replace('e+', 'x10^');
  95. }
  96. for (_i = 0, _len = numberLengths.length; _i < _len; _i++) {
  97. _length = numberLengths[_i];
  98. if (numberLength >= _length) {
  99. length = _length;
  100. break;
  101. }
  102. }
  103. decimalIndex = numberLength - length + 1;
  104. unsignedNumberCharacterArray = unsignedNumberString.split("");
  105. wholePartArray = unsignedNumberCharacterArray.slice(0, decimalIndex);
  106. decimalPartArray = unsignedNumberCharacterArray.slice(decimalIndex, decimalIndex + decimals + 1);
  107. wholePart = wholePartArray.join("");
  108. decimalPart = decimalPartArray.join("");
  109. if (decimalPart.length < decimals) {
  110. decimalPart += "" + (Array(decimals - decimalPart.length + 1).join('0'));
  111. }
  112. if (decimals === 0) {
  113. output = "" + signString + wholePart + bigNumPrefixes[numberLengths.indexOf(length)];
  114. } else {
  115. outputNumber = (+("" + wholePart + "." + decimalPart)).toFixed(decimals);
  116. output = "" + signString + outputNumber + bigNumPrefixes[numberLengths.indexOf(length)];
  117. }
  118. return output;
  119. };
  120. this.Humanize.intcomma = function(number, decimals) {
  121. if (decimals == null) {
  122. decimals = 0;
  123. }
  124. return this.formatNumber(number, decimals);
  125. };
  126. this.Humanize.filesize = function(filesize) {
  127. var sizeStr;
  128. if (filesize >= 1073741824) {
  129. sizeStr = this.formatNumber(filesize / 1073741824, 2, "") + " GB";
  130. } else if (filesize >= 1048576) {
  131. sizeStr = this.formatNumber(filesize / 1048576, 2, "") + " MB";
  132. } else if (filesize >= 1024) {
  133. sizeStr = this.formatNumber(filesize / 1024, 0) + " KB";
  134. } else {
  135. sizeStr = this.formatNumber(filesize, 0) + this.pluralize(filesize, " byte");
  136. }
  137. return sizeStr;
  138. };
  139. this.Humanize.formatNumber = function(number, precision, thousand, decimal) {
  140. var base, commas, decimals, firstComma, mod, negative, usePrecision,
  141. _this = this;
  142. if (precision == null) {
  143. precision = 0;
  144. }
  145. if (thousand == null) {
  146. thousand = ",";
  147. }
  148. if (decimal == null) {
  149. decimal = ".";
  150. }
  151. firstComma = function(number, thousand, position) {
  152. if (position) {
  153. return number.substr(0, position) + thousand;
  154. } else {
  155. return "";
  156. }
  157. };
  158. commas = function(number, thousand, position) {
  159. return number.substr(position).replace(/(\d{3})(?=\d)/g, "$1" + thousand);
  160. };
  161. decimals = function(number, decimal, usePrecision) {
  162. if (usePrecision) {
  163. return decimal + _this.toFixed(Math.abs(number), usePrecision).split(".")[1];
  164. } else {
  165. return "";
  166. }
  167. };
  168. usePrecision = this.normalizePrecision(precision);
  169. negative = number < 0 && "-" || "";
  170. base = parseInt(this.toFixed(Math.abs(number || 0), usePrecision), 10) + "";
  171. mod = base.length > 3 ? base.length % 3 : 0;
  172. return negative + firstComma(base, thousand, mod) + commas(base, thousand, mod) + decimals(number, decimal, usePrecision);
  173. };
  174. this.Humanize.toFixed = function(value, precision) {
  175. var power;
  176. if (precision == null) {
  177. precision = this.normalizePrecision(precision, 0);
  178. }
  179. power = Math.pow(10, precision);
  180. return (Math.round(value * power) / power).toFixed(precision);
  181. };
  182. this.Humanize.normalizePrecision = function(value, base) {
  183. value = Math.round(Math.abs(value));
  184. if (isNaN(value)) {
  185. return base;
  186. } else {
  187. return value;
  188. }
  189. };
  190. this.Humanize.ordinal = function(value) {
  191. var end, leastSignificant, number, specialCase;
  192. number = parseInt(value, 10);
  193. if (number === 0) {
  194. return value;
  195. }
  196. specialCase = number % 100;
  197. if (specialCase === 11 || specialCase === 12 || specialCase === 13) {
  198. return number + "th";
  199. }
  200. leastSignificant = number % 10;
  201. switch (leastSignificant) {
  202. case 1:
  203. end = "st";
  204. break;
  205. case 2:
  206. end = "nd";
  207. break;
  208. case 3:
  209. end = "rd";
  210. break;
  211. default:
  212. end = "th";
  213. }
  214. return number + end;
  215. };
  216. this.Humanize.times = function(value, overrides) {
  217. var number, result;
  218. if (overrides == null) {
  219. overrides = {};
  220. }
  221. if (isFinite(value) && value >= 0) {
  222. number = parseFloat(value);
  223. switch (number) {
  224. case 0:
  225. result = (overrides[0] != null) || 'never';
  226. break;
  227. case 1:
  228. result = (overrides[1] != null) || 'once';
  229. break;
  230. case 2:
  231. result = (overrides[2] != null) || 'twice';
  232. break;
  233. default:
  234. result = (overrides[number] || number) + " times";
  235. }
  236. }
  237. return result;
  238. };
  239. this.Humanize.pluralize = function(number, singular, plural) {
  240. if (!((number != null) && (singular != null))) {
  241. return;
  242. }
  243. if (plural == null) {
  244. plural = singular + "s";
  245. }
  246. if (parseInt(number, 10) === 1) {
  247. return singular;
  248. } else {
  249. return plural;
  250. }
  251. };
  252. this.Humanize.truncate = function(str, length, ending) {
  253. if (length == null) {
  254. length = 100;
  255. }
  256. if (ending == null) {
  257. ending = "...";
  258. }
  259. if (str.length > length) {
  260. return str.substring(0, length - ending.length) + ending;
  261. } else {
  262. return str;
  263. }
  264. };
  265. this.Humanize.truncatewords = function(string, length) {
  266. var array, i, result;
  267. array = string.split(" ");
  268. result = "";
  269. i = 0;
  270. while (i < length) {
  271. if (array[i] != null) {
  272. result += array[i] + " ";
  273. }
  274. i++;
  275. }
  276. if (array.length > length) {
  277. return result += "...";
  278. }
  279. };
  280. this.Humanize.truncatenumber = function(num, bound, ending) {
  281. var result;
  282. if (bound == null) {
  283. bound = 100;
  284. }
  285. if (ending == null) {
  286. ending = "+";
  287. }
  288. result = null;
  289. if (isFinite(num) && isFinite(bound)) {
  290. if (num > bound) {
  291. result = bound + ending;
  292. }
  293. }
  294. return (result || num).toString();
  295. };
  296. this.Humanize.oxford = function(items, limit, limitStr) {
  297. var extra, limitIndex, numItems;
  298. numItems = items.length;
  299. if (numItems < 2) {
  300. return "" + items;
  301. } else if (numItems === 2) {
  302. return items.join(' and ');
  303. } else if ((limit != null) && numItems > limit) {
  304. extra = numItems - limit;
  305. limitIndex = limit;
  306. if (limitStr == null) {
  307. limitStr = ", and " + extra + " " + (this.pluralize(extra, 'other'));
  308. }
  309. } else {
  310. limitIndex = -1;
  311. limitStr = ", and " + items[numItems - 1];
  312. }
  313. return items.slice(0, limitIndex).join(', ') + limitStr;
  314. };
  315. this.Humanize.dictionary = function(object, joiner, separator) {
  316. var defs, k, result, v;
  317. if (joiner == null) {
  318. joiner = ' is ';
  319. }
  320. if (separator == null) {
  321. separator = ', ';
  322. }
  323. result = '';
  324. if ((object != null) && typeof object === 'object' && Object.prototype.toString.call(object) !== '[object Array]') {
  325. defs = [];
  326. for (k in object) {
  327. v = object[k];
  328. defs.push(k + joiner + v);
  329. }
  330. result = defs.join(separator);
  331. }
  332. return result;
  333. };
  334. this.Humanize.frequency = function(list, verb) {
  335. var len, str, times;
  336. if (!isArray(list)) {
  337. return;
  338. }
  339. len = list.length;
  340. times = this.times(len);
  341. if (len === 0) {
  342. str = "" + times + " " + verb;
  343. } else {
  344. str = "" + verb + " " + times;
  345. }
  346. return str;
  347. };
  348. this.Humanize.pace = function(value, intervalMs, unit) {
  349. var f, prefix, rate, relativePace, roundedPace, timeUnit, _i, _len;
  350. if (unit == null) {
  351. unit = 'time';
  352. }
  353. if (value === 0 || intervalMs === 0) {
  354. return "No " + (this.pluralize(unit));
  355. }
  356. prefix = 'Approximately';
  357. timeUnit = null;
  358. rate = value / intervalMs;
  359. for (_i = 0, _len = timeFormats.length; _i < _len; _i++) {
  360. f = timeFormats[_i];
  361. relativePace = rate * f.value;
  362. if (relativePace > 1) {
  363. timeUnit = f.name;
  364. break;
  365. }
  366. }
  367. if (!timeUnit) {
  368. prefix = 'Less than';
  369. relativePace = 1;
  370. timeUnit = timeFormats[timeFormats.length - 1].name;
  371. }
  372. roundedPace = Math.round(relativePace);
  373. unit = this.pluralize(roundedPace, unit);
  374. return "" + prefix + " " + roundedPace + " " + unit + " per " + timeUnit;
  375. };
  376. this.Humanize.nl2br = function(string, replacement) {
  377. if (replacement == null) {
  378. replacement = '<br/>';
  379. }
  380. return string.replace(/\n/g, replacement);
  381. };
  382. this.Humanize.br2nl = function(string, replacement) {
  383. if (replacement == null) {
  384. replacement = '\r\n';
  385. }
  386. return string.replace(/\<br\s*\/?\>/g, replacement);
  387. };
  388. this.Humanize.capitalize = function(string) {
  389. return string.charAt(0).toUpperCase() + string.slice(1);
  390. };
  391. this.Humanize.capitalizeAll = function(string) {
  392. return string.replace(/(?:^|\s)\S/g, function(a) {
  393. return a.toUpperCase();
  394. });
  395. };
  396. this.Humanize.titlecase = function(string) {
  397. var doTitlecase, internalCaps, smallWords, splitOnHyphensRegex, splitOnWhiteSpaceRegex,
  398. _this = this;
  399. smallWords = /\b(a|an|and|at|but|by|de|en|for|if|in|of|on|or|the|to|via|vs?\.?)\b/i;
  400. internalCaps = /\S+[A-Z]+\S*/;
  401. splitOnWhiteSpaceRegex = /\s+/;
  402. splitOnHyphensRegex = /-/;
  403. doTitlecase = function(_string, hyphenated, firstOrLast) {
  404. var index, stringArray, titleCasedArray, word, _i, _len;
  405. if (hyphenated == null) {
  406. hyphenated = false;
  407. }
  408. if (firstOrLast == null) {
  409. firstOrLast = true;
  410. }
  411. titleCasedArray = [];
  412. stringArray = _string.split(hyphenated ? splitOnHyphensRegex : splitOnWhiteSpaceRegex);
  413. for (index = _i = 0, _len = stringArray.length; _i < _len; index = ++_i) {
  414. word = stringArray[index];
  415. if (word.indexOf("-") !== -1) {
  416. if (index === 0 || index === stringArray.length - 1) {
  417. titleCasedArray.push(doTitlecase(word, true, true));
  418. } else {
  419. titleCasedArray.push(doTitlecase(word, true, false));
  420. }
  421. } else {
  422. if ((index === 0 || index === stringArray.length - 1) && firstOrLast) {
  423. titleCasedArray.push(internalCaps.test(word) ? word : _this.capitalize(word));
  424. } else {
  425. if (internalCaps.test(word)) {
  426. titleCasedArray.push(word);
  427. } else if (smallWords.test(word)) {
  428. titleCasedArray.push(word.toLowerCase());
  429. } else {
  430. titleCasedArray.push(_this.capitalize(word));
  431. }
  432. }
  433. }
  434. }
  435. return titleCasedArray.join(hyphenated ? '-' : " ");
  436. };
  437. return doTitlecase(string);
  438. };
  439. if (typeof module !== "undefined" && module !== null) {
  440. module.exports = this.Humanize;
  441. }
  442. }).call(this);