2017-11-13 04:11:54 -05:00
|
|
|
/**
|
|
|
|
* Adds a , to a string composed by numbers, at every 3 chars.
|
|
|
|
*
|
|
|
|
* 2333 -> 2,333
|
|
|
|
* 232324 -> 232,324
|
|
|
|
*
|
|
|
|
* @param {String} text
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-04-02 17:50:39 -04:00
|
|
|
export const addDelimiter = text =>
|
2018-10-10 02:25:43 -04:00
|
|
|
text ? text.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') : text;
|
2017-10-10 03:47:42 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns '99+' for numbers bigger than 99.
|
|
|
|
*
|
|
|
|
* @param {Number} count
|
|
|
|
* @return {Number|String}
|
|
|
|
*/
|
2017-11-13 04:11:54 -05:00
|
|
|
export const highCountTrim = count => (count > 99 ? '99+' : count);
|
2017-10-10 03:47:42 -04:00
|
|
|
|
2017-11-13 04:11:54 -05:00
|
|
|
/**
|
2017-11-17 11:53:18 -05:00
|
|
|
* Converts first char to uppercase and replaces undercores with spaces
|
2017-11-13 04:11:54 -05:00
|
|
|
* @param {String} string
|
|
|
|
* @requires {String}
|
|
|
|
*/
|
2018-04-02 17:50:39 -04:00
|
|
|
export const humanize = string =>
|
|
|
|
string.charAt(0).toUpperCase() + string.replace(/_/g, ' ').slice(1);
|
2016-12-14 08:47:16 -05:00
|
|
|
|
2017-11-13 04:11:54 -05:00
|
|
|
/**
|
|
|
|
* Adds an 's' to the end of the string when count is bigger than 0
|
|
|
|
* @param {String} str
|
|
|
|
* @param {Number} count
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
export const pluralize = (str, count) => str + (count > 1 || count === 0 ? 's' : '');
|
2016-12-19 10:26:10 -05:00
|
|
|
|
2017-11-13 04:11:54 -05:00
|
|
|
/**
|
|
|
|
* Replaces underscores with dashes
|
|
|
|
* @param {*} str
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
export const dasherize = str => str.replace(/[_\s]+/g, '-');
|
2016-12-14 08:47:16 -05:00
|
|
|
|
2017-11-13 04:11:54 -05:00
|
|
|
/**
|
|
|
|
* Removes accents and converts to lower case
|
|
|
|
* @param {String} str
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
export const slugify = str => str.trim().toLowerCase();
|
2016-12-14 08:47:16 -05:00
|
|
|
|
2018-09-08 02:03:00 -04:00
|
|
|
/**
|
|
|
|
* Replaces whitespaces with hyphens and converts to lower case
|
|
|
|
* @param {String} str
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-10-15 09:32:47 -04:00
|
|
|
export const slugifyWithHyphens = str => str.toLowerCase().replace(/\s+/g, '-');
|
2018-09-08 02:03:00 -04:00
|
|
|
|
2017-11-13 04:11:54 -05:00
|
|
|
/**
|
|
|
|
* Truncates given text
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @param {Number} maxLength
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-04-02 17:50:39 -04:00
|
|
|
export const truncate = (string, maxLength) => `${string.substr(0, maxLength - 3)}...`;
|
2016-12-14 08:47:16 -05:00
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
/**
|
|
|
|
* Truncate SHA to 8 characters
|
|
|
|
*
|
|
|
|
* @param {String} sha
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
export const truncateSha = sha => sha.substr(0, 8);
|
|
|
|
|
2019-01-08 04:31:23 -05:00
|
|
|
const ELLIPSIS_CHAR = '…';
|
|
|
|
export const truncatePathMiddleToLength = (text, maxWidth) => {
|
|
|
|
let returnText = text;
|
|
|
|
let ellipsisCount = 0;
|
|
|
|
|
|
|
|
while (returnText.length >= maxWidth) {
|
|
|
|
const textSplit = returnText.split('/').filter(s => s !== ELLIPSIS_CHAR);
|
|
|
|
const middleIndex = Math.floor(textSplit.length / 2);
|
|
|
|
|
|
|
|
returnText = textSplit
|
|
|
|
.slice(0, middleIndex)
|
|
|
|
.concat(
|
|
|
|
new Array(ellipsisCount + 1).fill().map(() => ELLIPSIS_CHAR),
|
|
|
|
textSplit.slice(middleIndex + 1),
|
|
|
|
)
|
|
|
|
.join('/');
|
|
|
|
|
|
|
|
ellipsisCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnText;
|
|
|
|
};
|
|
|
|
|
2017-11-16 14:33:35 -05:00
|
|
|
/**
|
|
|
|
* Capitalizes first character
|
|
|
|
*
|
|
|
|
* @param {String} text
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
export function capitalizeFirstCharacter(text) {
|
|
|
|
return `${text[0].toUpperCase()}${text.slice(1)}`;
|
|
|
|
}
|
2018-01-03 16:03:39 -05:00
|
|
|
|
2018-08-01 15:43:50 -04:00
|
|
|
/**
|
|
|
|
* Returns the first character capitalized
|
|
|
|
*
|
|
|
|
* If falsey, returns empty string.
|
|
|
|
*
|
|
|
|
* @param {String} text
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
export function getFirstCharacterCapitalized(text) {
|
2018-10-10 02:25:43 -04:00
|
|
|
return text ? text.charAt(0).toUpperCase() : '';
|
2018-08-01 15:43:50 -04:00
|
|
|
}
|
|
|
|
|
2018-01-03 16:03:39 -05:00
|
|
|
/**
|
|
|
|
* Replaces all html tags from a string with the given replacement.
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @param {*} replace
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-05-03 04:25:22 -04:00
|
|
|
export const stripHtml = (string, replace = '') => {
|
|
|
|
if (!string) return string;
|
|
|
|
|
|
|
|
return string.replace(/<[^>]*>/g, replace);
|
|
|
|
};
|
2018-02-07 07:57:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts snake_case string to camelCase
|
|
|
|
*
|
|
|
|
* @param {*} string
|
|
|
|
*/
|
|
|
|
export const convertToCamelCase = string => string.replace(/(_\w)/g, s => s[1].toUpperCase());
|
2018-04-02 17:50:39 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a sentence to lower case from the second word onwards
|
|
|
|
* e.g. Hello World => Hello world
|
|
|
|
*
|
|
|
|
* @param {*} string
|
|
|
|
*/
|
|
|
|
export const convertToSentenceCase = string => {
|
|
|
|
const splitWord = string.split(' ').map((word, index) => (index > 0 ? word.toLowerCase() : word));
|
|
|
|
|
|
|
|
return splitWord.join(' ');
|
|
|
|
};
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits camelCase or PascalCase words
|
|
|
|
* e.g. HelloWorld => Hello World
|
|
|
|
*
|
|
|
|
* @param {*} string
|
2018-10-10 02:25:43 -04:00
|
|
|
*/
|
|
|
|
export const splitCamelCase = string =>
|
2018-06-21 08:22:40 -04:00
|
|
|
string
|
2018-10-10 02:25:43 -04:00
|
|
|
.replace(/([A-Z]+)([A-Z][a-z])/g, ' $1 $2')
|
|
|
|
.replace(/([a-z\d])([A-Z])/g, '$1 $2')
|
|
|
|
.trim();
|