PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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