1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-03-10 07:29:20 +01:00

434 lines
13 KiB
JavaScript
Raw Normal View History

2021-07-01 11:23:58 +02:00
(function () {
2015-05-05 03:10:25 +02:00
var Humanize, isArray, isFinite, isNaN, objectRef, timeFormats, toString;
2021-07-01 11:23:58 +02:00
objectRef = new function () {}();
2015-05-05 03:10:25 +02:00
toString = objectRef.toString;
2021-07-01 11:23:58 +02:00
isNaN = function (value) {
return Number.isNaN(value);
2015-05-05 03:10:25 +02:00
};
2021-07-01 11:23:58 +02:00
isFinite = function (value) {
return ((typeof window !== 'undefined' && window !== null ? window.isFinite : undefined) || global.isFinite)(value) && !isNaN(parseFloat(value));
2015-05-05 03:10:25 +02:00
};
2021-07-01 11:23:58 +02:00
isArray = function (value) {
2015-05-05 03:10:25 +02:00
return toString.call(value) === '[object Array]';
};
timeFormats = [
{
name: 'second',
value: 1e3
}, {
name: 'minute',
value: 6e4
}, {
name: 'hour',
value: 36e5
}, {
name: 'day',
value: 864e5
}, {
name: 'week',
value: 6048e5
}
];
Humanize = {};
2021-07-01 11:23:58 +02:00
Humanize.intword = function (number, charWidth, decimals) {
2015-05-05 03:10:25 +02:00
if (decimals == null) {
decimals = 2;
}
/*
# This method is deprecated. Please use compactInteger instead.
# intword will be going away in the next major version.
*/
return Humanize.compactInteger(number, decimals);
};
2021-07-01 11:23:58 +02:00
Humanize.compactInteger = function (input, decimals) {
2015-05-05 03:10:25 +02:00
var bigNumPrefixes, decimalIndex, decimalPart, decimalPartArray, length, number, numberLength, numberLengths, output, outputNumber, signString, unsignedNumber, unsignedNumberCharacterArray, unsignedNumberString, wholePart, wholePartArray, _i, _len, _length;
if (decimals == null) {
decimals = 0;
}
decimals = Math.max(decimals, 0);
number = parseInt(input, 10);
2021-07-01 11:23:58 +02:00
signString = number < 0 ? '-' : '';
2015-05-05 03:10:25 +02:00
unsignedNumber = Math.abs(number);
2021-07-01 11:23:58 +02:00
unsignedNumberString = '' + unsignedNumber;
2015-05-05 03:10:25 +02:00
numberLength = unsignedNumberString.length;
numberLengths = [13, 10, 7, 4];
bigNumPrefixes = ['T', 'B', 'M', 'k'];
if (unsignedNumber < 1000) {
if (decimals > 0) {
2021-07-01 11:23:58 +02:00
unsignedNumberString += '.' + (Array(decimals + 1).join('0'));
2015-05-05 03:10:25 +02:00
}
2021-07-01 11:23:58 +02:00
return '' + signString + unsignedNumberString;
2015-05-05 03:10:25 +02:00
}
if (numberLength > numberLengths[0] + 3) {
return number.toExponential(decimals).replace('e+', 'x10^');
}
for (_i = 0, _len = numberLengths.length; _i < _len; _i++) {
_length = numberLengths[_i];
if (numberLength >= _length) {
length = _length;
break;
}
}
decimalIndex = numberLength - length + 1;
2021-07-01 11:23:58 +02:00
unsignedNumberCharacterArray = unsignedNumberString.split('');
2015-05-05 03:10:25 +02:00
wholePartArray = unsignedNumberCharacterArray.slice(0, decimalIndex);
decimalPartArray = unsignedNumberCharacterArray.slice(decimalIndex, decimalIndex + decimals + 1);
2021-07-01 11:23:58 +02:00
wholePart = wholePartArray.join('');
decimalPart = decimalPartArray.join('');
2015-05-05 03:10:25 +02:00
if (decimalPart.length < decimals) {
2021-07-01 11:23:58 +02:00
decimalPart += '' + (Array(decimals - decimalPart.length + 1).join('0'));
2015-05-05 03:10:25 +02:00
}
if (decimals === 0) {
2021-07-01 11:23:58 +02:00
output = '' + signString + wholePart + bigNumPrefixes[numberLengths.indexOf(length)];
2015-05-05 03:10:25 +02:00
} else {
2021-07-01 11:23:58 +02:00
outputNumber = (+('' + wholePart + '.' + decimalPart)).toFixed(decimals);
output = '' + signString + outputNumber + bigNumPrefixes[numberLengths.indexOf(length)];
2015-05-05 03:10:25 +02:00
}
return output;
};
2021-07-01 11:23:58 +02:00
Humanize.intcomma = Humanize.intComma = function (number, decimals) {
2015-05-05 03:10:25 +02:00
if (decimals == null) {
decimals = 0;
}
return Humanize.formatNumber(number, decimals);
};
2021-07-01 11:23:58 +02:00
Humanize.filesize = Humanize.fileSize = function (filesize) {
2015-05-05 03:10:25 +02:00
var sizeStr;
if (filesize >= 1073741824) {
2021-07-01 11:23:58 +02:00
sizeStr = Humanize.formatNumber(filesize / 1073741824, 2, '') + ' GB';
2015-05-05 03:10:25 +02:00
} else if (filesize >= 1048576) {
2021-07-01 11:23:58 +02:00
sizeStr = Humanize.formatNumber(filesize / 1048576, 2, '') + ' MB';
2015-05-05 03:10:25 +02:00
} else if (filesize >= 1024) {
2021-07-01 11:23:58 +02:00
sizeStr = Humanize.formatNumber(filesize / 1024, 0) + ' KB';
2015-05-05 03:10:25 +02:00
} else {
2021-07-01 11:23:58 +02:00
sizeStr = Humanize.formatNumber(filesize, 0) + Humanize.pluralize(filesize, ' byte');
2015-05-05 03:10:25 +02:00
}
return sizeStr;
};
2021-07-01 11:23:58 +02:00
Humanize.formatNumber = function (number, precision, thousand, decimal) {
var base; var commas; var decimals; var firstComma; var mod; var negative; var usePrecision;
2015-05-05 03:10:25 +02:00
if (precision == null) {
precision = 0;
}
if (thousand == null) {
2021-07-01 11:23:58 +02:00
thousand = ',';
2015-05-05 03:10:25 +02:00
}
if (decimal == null) {
2021-07-01 11:23:58 +02:00
decimal = '.';
2015-05-05 03:10:25 +02:00
}
2021-07-01 11:23:58 +02:00
firstComma = function (number, thousand, position) {
2015-05-05 03:10:25 +02:00
if (position) {
return number.substr(0, position) + thousand;
} else {
2021-07-01 11:23:58 +02:00
return '';
2015-05-05 03:10:25 +02:00
}
};
2021-07-01 11:23:58 +02:00
commas = function (number, thousand, position) {
return number.substr(position).replace(/(\d{3})(?=\d)/g, '$1' + thousand);
2015-05-05 03:10:25 +02:00
};
2021-07-01 11:23:58 +02:00
decimals = function (number, decimal, usePrecision) {
2015-05-05 03:10:25 +02:00
if (usePrecision) {
2021-07-01 11:23:58 +02:00
return decimal + Humanize.toFixed(Math.abs(number), usePrecision).split('.')[1];
2015-05-05 03:10:25 +02:00
} else {
2021-07-01 11:23:58 +02:00
return '';
2015-05-05 03:10:25 +02:00
}
};
usePrecision = Humanize.normalizePrecision(precision);
2021-07-01 11:23:58 +02:00
negative = number < 0 ? '-' : '';
base = parseInt(Humanize.toFixed(Math.abs(number || 0), usePrecision), 10) + '';
2015-05-05 03:10:25 +02:00
mod = base.length > 3 ? base.length % 3 : 0;
return negative + firstComma(base, thousand, mod) + commas(base, thousand, mod) + decimals(number, decimal, usePrecision);
};
2021-07-01 11:23:58 +02:00
Humanize.toFixed = function (value, precision) {
2015-05-05 03:10:25 +02:00
var power;
if (precision == null) {
precision = Humanize.normalizePrecision(precision, 0);
}
power = Math.pow(10, precision);
return (Math.round(value * power) / power).toFixed(precision);
};
2021-07-01 11:23:58 +02:00
Humanize.normalizePrecision = function (value, base) {
2015-05-05 03:10:25 +02:00
value = Math.round(Math.abs(value));
if (isNaN(value)) {
return base;
} else {
return value;
}
};
2021-07-01 11:23:58 +02:00
Humanize.ordinal = function (value) {
2015-05-05 03:10:25 +02:00
var end, leastSignificant, number, specialCase;
number = parseInt(value, 10);
if (number === 0) {
return value;
}
specialCase = number % 100;
if (specialCase === 11 || specialCase === 12 || specialCase === 13) {
2021-07-01 11:23:58 +02:00
return '' + number + 'th';
2015-05-05 03:10:25 +02:00
}
leastSignificant = number % 10;
switch (leastSignificant) {
case 1:
2021-07-01 11:23:58 +02:00
end = 'st';
2015-05-05 03:10:25 +02:00
break;
case 2:
2021-07-01 11:23:58 +02:00
end = 'nd';
2015-05-05 03:10:25 +02:00
break;
case 3:
2021-07-01 11:23:58 +02:00
end = 'rd';
2015-05-05 03:10:25 +02:00
break;
default:
2021-07-01 11:23:58 +02:00
end = 'th';
2015-05-05 03:10:25 +02:00
}
2021-07-01 11:23:58 +02:00
return '' + number + end;
2015-05-05 03:10:25 +02:00
};
2021-07-01 11:23:58 +02:00
Humanize.times = function (value, overrides) {
2015-05-05 03:10:25 +02:00
var number, smallTimes, _ref;
if (overrides == null) {
overrides = {};
}
if (isFinite(value) && value >= 0) {
number = parseFloat(value);
smallTimes = ['never', 'once', 'twice'];
if (overrides[number] != null) {
2021-07-01 11:23:58 +02:00
return '' + overrides[number];
2015-05-05 03:10:25 +02:00
} else {
2021-07-01 11:23:58 +02:00
return '' + (((_ref = smallTimes[number]) != null ? _ref.toString() : undefined) || number.toString() + ' times');
2015-05-05 03:10:25 +02:00
}
}
};
2021-07-01 11:23:58 +02:00
Humanize.pluralize = function (number, singular, plural) {
2015-05-05 03:10:25 +02:00
if (!((number != null) && (singular != null))) {
return;
}
if (plural == null) {
2021-07-01 11:23:58 +02:00
plural = singular + 's';
2015-05-05 03:10:25 +02:00
}
if (parseInt(number, 10) === 1) {
return singular;
} else {
return plural;
}
};
2021-07-01 11:23:58 +02:00
Humanize.truncate = function (str, length, ending) {
2015-05-05 03:10:25 +02:00
if (length == null) {
length = 100;
}
if (ending == null) {
ending = '...';
}
if (str.length > length) {
return str.substring(0, length - ending.length) + ending;
} else {
return str;
}
};
2021-07-01 11:23:58 +02:00
Humanize.truncatewords = Humanize.truncateWords = function (string, length) {
2015-05-05 03:10:25 +02:00
var array, i, result;
2021-07-01 11:23:58 +02:00
array = string.split(' ');
result = '';
2015-05-05 03:10:25 +02:00
i = 0;
while (i < length) {
if (array[i] != null) {
2021-07-01 11:23:58 +02:00
result += '' + array[i] + ' ';
2015-05-05 03:10:25 +02:00
}
i++;
}
if (array.length > length) {
2021-07-01 11:23:58 +02:00
return result + '...';
2015-05-05 03:10:25 +02:00
}
};
2021-07-01 11:23:58 +02:00
Humanize.truncatenumber = Humanize.boundedNumber = function (num, bound, ending) {
2015-05-05 03:10:25 +02:00
var result;
if (bound == null) {
bound = 100;
}
if (ending == null) {
2021-07-01 11:23:58 +02:00
ending = '+';
2015-05-05 03:10:25 +02:00
}
result = null;
if (isFinite(num) && isFinite(bound)) {
if (num > bound) {
result = bound + ending;
}
}
return (result || num).toString();
};
2021-07-01 11:23:58 +02:00
Humanize.oxford = function (items, limit, limitStr) {
2015-05-05 03:10:25 +02:00
var extra, limitIndex, numItems;
numItems = items.length;
if (numItems < 2) {
2021-07-01 11:23:58 +02:00
return '' + items;
2015-05-05 03:10:25 +02:00
} else if (numItems === 2) {
return items.join(' and ');
} else if ((limit != null) && numItems > limit) {
extra = numItems - limit;
limitIndex = limit;
if (limitStr == null) {
2021-07-01 11:23:58 +02:00
limitStr = ', and ' + extra + ' ' + (Humanize.pluralize(extra, 'other'));
2015-05-05 03:10:25 +02:00
}
} else {
limitIndex = -1;
2021-07-01 11:23:58 +02:00
limitStr = ', and ' + items[numItems - 1];
2015-05-05 03:10:25 +02:00
}
return items.slice(0, limitIndex).join(', ') + limitStr;
};
2021-07-01 11:23:58 +02:00
Humanize.dictionary = function (object, joiner, separator) {
2015-05-05 03:10:25 +02:00
var defs, key, result, val;
if (joiner == null) {
joiner = ' is ';
}
if (separator == null) {
separator = ', ';
}
result = '';
if ((object != null) && typeof object === 'object' && Object.prototype.toString.call(object) !== '[object Array]') {
defs = [];
for (key in object) {
val = object[key];
2021-07-01 11:23:58 +02:00
defs.push('' + key + joiner + val);
2015-05-05 03:10:25 +02:00
}
result = defs.join(separator);
}
return result;
};
2021-07-01 11:23:58 +02:00
Humanize.frequency = function (list, verb) {
2015-05-05 03:10:25 +02:00
var len, str, times;
if (!isArray(list)) {
return;
}
len = list.length;
times = Humanize.times(len);
if (len === 0) {
2021-07-01 11:23:58 +02:00
str = '' + times + ' ' + verb;
2015-05-05 03:10:25 +02:00
} else {
2021-07-01 11:23:58 +02:00
str = '' + verb + ' ' + times;
2015-05-05 03:10:25 +02:00
}
return str;
};
2021-07-01 11:23:58 +02:00
Humanize.pace = function (value, intervalMs, unit) {
2015-05-05 03:10:25 +02:00
var f, prefix, rate, relativePace, roundedPace, timeUnit, _i, _len;
if (unit == null) {
unit = 'time';
}
if (value === 0 || intervalMs === 0) {
2021-07-01 11:23:58 +02:00
return 'No ' + (Humanize.pluralize(unit));
2015-05-05 03:10:25 +02:00
}
prefix = 'Approximately';
timeUnit = null;
rate = value / intervalMs;
for (_i = 0, _len = timeFormats.length; _i < _len; _i++) {
f = timeFormats[_i];
relativePace = rate * f.value;
if (relativePace > 1) {
timeUnit = f.name;
break;
}
}
if (!timeUnit) {
prefix = 'Less than';
relativePace = 1;
timeUnit = timeFormats[timeFormats.length - 1].name;
}
roundedPace = Math.round(relativePace);
unit = Humanize.pluralize(roundedPace, unit);
2021-07-01 11:23:58 +02:00
return '' + prefix + ' ' + roundedPace + ' ' + unit + ' per ' + timeUnit;
2015-05-05 03:10:25 +02:00
};
2021-07-01 11:23:58 +02:00
Humanize.nl2br = function (string, replacement) {
2015-05-05 03:10:25 +02:00
if (replacement == null) {
replacement = '<br/>';
}
return string.replace(/\n/g, replacement);
};
2021-07-01 11:23:58 +02:00
Humanize.br2nl = function (string, replacement) {
2015-05-05 03:10:25 +02:00
if (replacement == null) {
replacement = '\r\n';
}
2021-07-01 11:23:58 +02:00
return string.replace(/<br\s*\/?>/g, replacement);
2015-05-05 03:10:25 +02:00
};
2021-07-01 11:23:58 +02:00
Humanize.capitalize = function (string, downCaseTail) {
2015-05-05 03:10:25 +02:00
if (downCaseTail == null) {
downCaseTail = false;
}
2021-07-01 11:23:58 +02:00
return '' + (string.charAt(0).toUpperCase()) + (downCaseTail ? string.slice(1).toLowerCase() : string.slice(1));
2015-05-05 03:10:25 +02:00
};
2021-07-01 11:23:58 +02:00
Humanize.capitalizeAll = function (string) {
return string.replace(/(?:^|\s)\S/g, function (a) {
2015-05-05 03:10:25 +02:00
return a.toUpperCase();
});
};
2021-07-01 11:23:58 +02:00
Humanize.titlecase = Humanize.titleCase = function (string) {
var doTitleCase; var internalCaps; var smallWords; var splitOnHyphensRegex; var splitOnWhiteSpaceRegex;
2015-05-05 03:10:25 +02:00
smallWords = /\b(a|an|and|at|but|by|de|en|for|if|in|of|on|or|the|to|via|vs?\.?)\b/i;
internalCaps = /\S+[A-Z]+\S*/;
splitOnWhiteSpaceRegex = /\s+/;
splitOnHyphensRegex = /-/;
2021-07-01 11:23:58 +02:00
doTitleCase = function (_string, hyphenated, firstOrLast) {
2015-05-05 03:10:25 +02:00
var index, stringArray, titleCasedArray, word, _i, _len;
if (hyphenated == null) {
hyphenated = false;
}
if (firstOrLast == null) {
firstOrLast = true;
}
titleCasedArray = [];
stringArray = _string.split(hyphenated ? splitOnHyphensRegex : splitOnWhiteSpaceRegex);
for (index = _i = 0, _len = stringArray.length; _i < _len; index = ++_i) {
word = stringArray[index];
if (word.indexOf('-') !== -1) {
titleCasedArray.push(doTitleCase(word, true, index === 0 || index === stringArray.length - 1));
continue;
}
if (firstOrLast && (index === 0 || index === stringArray.length - 1)) {
titleCasedArray.push(internalCaps.test(word) ? word : Humanize.capitalize(word));
continue;
}
if (internalCaps.test(word)) {
titleCasedArray.push(word);
} else if (smallWords.test(word)) {
titleCasedArray.push(word.toLowerCase());
} else {
titleCasedArray.push(Humanize.capitalize(word));
}
}
return titleCasedArray.join(hyphenated ? '-' : ' ');
};
return doTitleCase(string);
};
this.Humanize = Humanize;
2021-07-01 11:23:58 +02:00
if (typeof module !== 'undefined' && module !== null) {
2015-05-05 03:10:25 +02:00
module.exports = Humanize;
}
2021-07-01 11:23:58 +02:00
}).call(this);