PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Mirros/cdnjs
JavaScript | 472 lines | 434 code | 34 blank | 4 comment | 141 complexity | 68072ad0e64c6f8a44fd4edf626f9db3 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, smallTimes, _ref;
  218. if (overrides == null) {
  219. overrides = {};
  220. }
  221. if (isFinite(value) && value >= 0) {
  222. number = parseFloat(value);
  223. smallTimes = {
  224. 0: 'never',
  225. 1: 'once',
  226. 2: 'twice'
  227. };
  228. if (overrides[number] != null) {
  229. return "" + overrides[number] + " times";
  230. } else {
  231. return "" + (((_ref = smallTimes[number]) != null ? _ref.toString() : void 0) || number.toString() + ' times');
  232. }
  233. }
  234. };
  235. this.Humanize.pluralize = function(number, singular, plural) {
  236. if (!((number != null) && (singular != null))) {
  237. return;
  238. }
  239. if (plural == null) {
  240. plural = singular + "s";
  241. }
  242. if (parseInt(number, 10) === 1) {
  243. return singular;
  244. } else {
  245. return plural;
  246. }
  247. };
  248. this.Humanize.truncate = function(str, length, ending) {
  249. if (length == null) {
  250. length = 100;
  251. }
  252. if (ending == null) {
  253. ending = "...";
  254. }
  255. if (str.length > length) {
  256. return str.substring(0, length - ending.length) + ending;
  257. } else {
  258. return str;
  259. }
  260. };
  261. this.Humanize.truncatewords = function(string, length) {
  262. var array, i, result;
  263. array = string.split(" ");
  264. result = "";
  265. i = 0;
  266. while (i < length) {
  267. if (array[i] != null) {
  268. result += array[i] + " ";
  269. }
  270. i++;
  271. }
  272. if (array.length > length) {
  273. return result += "...";
  274. }
  275. };
  276. this.Humanize.truncatenumber = function(num, bound, ending) {
  277. var result;
  278. if (bound == null) {
  279. bound = 100;
  280. }
  281. if (ending == null) {
  282. ending = "+";
  283. }
  284. result = null;
  285. if (isFinite(num) && isFinite(bound)) {
  286. if (num > bound) {
  287. result = bound + ending;
  288. }
  289. }
  290. return (result || num).toString();
  291. };
  292. this.Humanize.oxford = function(items, limit, limitStr) {
  293. var extra, limitIndex, numItems;
  294. numItems = items.length;
  295. if (numItems < 2) {
  296. return "" + items;
  297. } else if (numItems === 2) {
  298. return items.join(' and ');
  299. } else if ((limit != null) && numItems > limit) {
  300. extra = numItems - limit;
  301. limitIndex = limit;
  302. if (limitStr == null) {
  303. limitStr = ", and " + extra + " " + (this.pluralize(extra, 'other'));
  304. }
  305. } else {
  306. limitIndex = -1;
  307. limitStr = ", and " + items[numItems - 1];
  308. }
  309. return items.slice(0, limitIndex).join(', ') + limitStr;
  310. };
  311. this.Humanize.dictionary = function(object, joiner, separator) {
  312. var defs, k, result, v;
  313. if (joiner == null) {
  314. joiner = ' is ';
  315. }
  316. if (separator == null) {
  317. separator = ', ';
  318. }
  319. result = '';
  320. if ((object != null) && typeof object === 'object' && Object.prototype.toString.call(object) !== '[object Array]') {
  321. defs = [];
  322. for (k in object) {
  323. v = object[k];
  324. defs.push(k + joiner + v);
  325. }
  326. result = defs.join(separator);
  327. }
  328. return result;
  329. };
  330. this.Humanize.frequency = function(list, verb) {
  331. var len, str, times;
  332. if (!isArray(list)) {
  333. return;
  334. }
  335. len = list.length;
  336. times = this.times(len);
  337. if (len === 0) {
  338. str = "" + times + " " + verb;
  339. } else {
  340. str = "" + verb + " " + times;
  341. }
  342. return str;
  343. };
  344. this.Humanize.pace = function(value, intervalMs, unit) {
  345. var f, prefix, rate, relativePace, roundedPace, timeUnit, _i, _len;
  346. if (unit == null) {
  347. unit = 'time';
  348. }
  349. if (value === 0 || intervalMs === 0) {
  350. return "No " + (this.pluralize(unit));
  351. }
  352. prefix = 'Approximately';
  353. timeUnit = null;
  354. rate = value / intervalMs;
  355. for (_i = 0, _len = timeFormats.length; _i < _len; _i++) {
  356. f = timeFormats[_i];
  357. relativePace = rate * f.value;
  358. if (relativePace > 1) {
  359. timeUnit = f.name;
  360. break;
  361. }
  362. }
  363. if (!timeUnit) {
  364. prefix = 'Less than';
  365. relativePace = 1;
  366. timeUnit = timeFormats[timeFormats.length - 1].name;
  367. }
  368. roundedPace = Math.round(relativePace);
  369. unit = this.pluralize(roundedPace, unit);
  370. return "" + prefix + " " + roundedPace + " " + unit + " per " + timeUnit;
  371. };
  372. this.Humanize.nl2br = function(string, replacement) {
  373. if (replacement == null) {
  374. replacement = '<br/>';
  375. }
  376. return string.replace(/\n/g, replacement);
  377. };
  378. this.Humanize.br2nl = function(string, replacement) {
  379. if (replacement == null) {
  380. replacement = '\r\n';
  381. }
  382. return string.replace(/\<br\s*\/?\>/g, replacement);
  383. };
  384. this.Humanize.capitalize = function(string) {
  385. return string.charAt(0).toUpperCase() + string.slice(1);
  386. };
  387. this.Humanize.capitalizeAll = function(string) {
  388. return string.replace(/(?:^|\s)\S/g, function(a) {
  389. return a.toUpperCase();
  390. });
  391. };
  392. this.Humanize.titlecase = function(string) {
  393. var doTitlecase, internalCaps, smallWords, splitOnHyphensRegex, splitOnWhiteSpaceRegex,
  394. _this = this;
  395. smallWords = /\b(a|an|and|at|but|by|de|en|for|if|in|of|on|or|the|to|via|vs?\.?)\b/i;
  396. internalCaps = /\S+[A-Z]+\S*/;
  397. splitOnWhiteSpaceRegex = /\s+/;
  398. splitOnHyphensRegex = /-/;
  399. doTitlecase = function(_string, hyphenated, firstOrLast) {
  400. var index, stringArray, titleCasedArray, word, _i, _len;
  401. if (hyphenated == null) {
  402. hyphenated = false;
  403. }
  404. if (firstOrLast == null) {
  405. firstOrLast = true;
  406. }
  407. titleCasedArray = [];
  408. stringArray = _string.split(hyphenated ? splitOnHyphensRegex : splitOnWhiteSpaceRegex);
  409. for (index = _i = 0, _len = stringArray.length; _i < _len; index = ++_i) {
  410. word = stringArray[index];
  411. if (word.indexOf("-") !== -1) {
  412. if (index === 0 || index === stringArray.length - 1) {
  413. titleCasedArray.push(doTitlecase(word, true, true));
  414. } else {
  415. titleCasedArray.push(doTitlecase(word, true, false));
  416. }
  417. } else {
  418. if ((index === 0 || index === stringArray.length - 1) && firstOrLast) {
  419. titleCasedArray.push(internalCaps.test(word) ? word : _this.capitalize(word));
  420. } else {
  421. if (internalCaps.test(word)) {
  422. titleCasedArray.push(word);
  423. } else if (smallWords.test(word)) {
  424. titleCasedArray.push(word.toLowerCase());
  425. } else {
  426. titleCasedArray.push(_this.capitalize(word));
  427. }
  428. }
  429. }
  430. }
  431. return titleCasedArray.join(hyphenated ? '-' : " ");
  432. };
  433. return doTitlecase(string);
  434. };
  435. if (typeof module !== "undefined" && module !== null) {
  436. module.exports = this.Humanize;
  437. }
  438. }).call(this);