2019-07-07 11:18:12 +05:30
|
|
|
import _ from 'underscore';
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
|
|
|
* Adds a , to a string composed by numbers, at every 3 chars.
|
|
|
|
*
|
|
|
|
* 2333 -> 2,333
|
|
|
|
* 232324 -> 232,324
|
|
|
|
*
|
|
|
|
* @param {String} text
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-05-09 12:01:36 +05:30
|
|
|
export const addDelimiter = text =>
|
2018-12-13 13:39:08 +05:30
|
|
|
text ? text.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') : text;
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
|
|
|
* Returns '99+' for numbers bigger than 99.
|
|
|
|
*
|
|
|
|
* @param {Number} count
|
|
|
|
* @return {Number|String}
|
|
|
|
*/
|
|
|
|
export const highCountTrim = count => (count > 99 ? '99+' : count);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
2020-03-13 15:44:24 +05:30
|
|
|
* Converts first char to uppercase and replaces the given separator with spaces
|
|
|
|
* @param {String} string - The string to humanize
|
|
|
|
* @param {String} separator - The separator used to separate words (defaults to "_")
|
2018-03-17 18:26:18 +05:30
|
|
|
* @requires {String}
|
2020-03-13 15:44:24 +05:30
|
|
|
* @returns {String}
|
2018-03-17 18:26:18 +05:30
|
|
|
*/
|
2020-03-13 15:44:24 +05:30
|
|
|
export const humanize = (string, separator = '_') => {
|
|
|
|
const replaceRegex = new RegExp(separator, 'g');
|
|
|
|
|
|
|
|
return string.charAt(0).toUpperCase() + string.replace(replaceRegex, ' ').slice(1);
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
|
|
|
* Replaces underscores with dashes
|
|
|
|
* @param {*} str
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
export const dasherize = str => str.replace(/[_\s]+/g, '-');
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
2019-12-26 22:10:19 +05:30
|
|
|
* Replaces whitespace and non-sluggish characters with a given separator
|
|
|
|
* @param {String} str - The string to slugify
|
|
|
|
* @param {String=} separator - The separator used to separate words (defaults to "-")
|
2018-03-17 18:26:18 +05:30
|
|
|
* @returns {String}
|
|
|
|
*/
|
2019-12-26 22:10:19 +05:30
|
|
|
export const slugify = (str, separator = '-') => {
|
2019-09-30 21:07:59 +05:30
|
|
|
const slug = str
|
|
|
|
.trim()
|
|
|
|
.toLowerCase()
|
2020-03-13 15:44:24 +05:30
|
|
|
.replace(/[^a-zA-Z0-9_.-]+/g, separator)
|
|
|
|
// Remove any duplicate separators or separator prefixes/suffixes
|
|
|
|
.split(separator)
|
|
|
|
.filter(Boolean)
|
|
|
|
.join(separator);
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
return slug === separator ? '' : slug;
|
2019-09-30 21:07:59 +05:30
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
/**
|
2019-12-26 22:10:19 +05:30
|
|
|
* Replaces whitespace and non-sluggish characters with underscores
|
2018-11-20 20:47:30 +05:30
|
|
|
* @param {String} str
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2019-12-26 22:10:19 +05:30
|
|
|
export const slugifyWithUnderscore = str => slugify(str, '_');
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
|
|
|
* Truncates given text
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @param {Number} maxLength
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-05-09 12:01:36 +05:30
|
|
|
export const truncate = (string, maxLength) => `${string.substr(0, maxLength - 3)}...`;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
/**
|
|
|
|
* Truncate SHA to 8 characters
|
|
|
|
*
|
|
|
|
* @param {String} sha
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2020-01-01 13:55:28 +05:30
|
|
|
export const truncateSha = sha => sha.substring(0, 8);
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
|
|
|
* Capitalizes first character
|
|
|
|
*
|
|
|
|
* @param {String} text
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
export function capitalizeFirstCharacter(text) {
|
|
|
|
return `${text[0].toUpperCase()}${text.slice(1)}`;
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
/**
|
|
|
|
* Returns the first character capitalized
|
|
|
|
*
|
|
|
|
* If falsey, returns empty string.
|
|
|
|
*
|
|
|
|
* @param {String} text
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
export function getFirstCharacterCapitalized(text) {
|
2018-12-13 13:39:08 +05:30
|
|
|
return text ? text.charAt(0).toUpperCase() : '';
|
2018-11-18 11:00:15 +05:30
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
|
|
|
* Replaces all html tags from a string with the given replacement.
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @param {*} replace
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-10-15 14:42:47 +05:30
|
|
|
export const stripHtml = (string, replace = '') => {
|
|
|
|
if (!string) return string;
|
|
|
|
|
|
|
|
return string.replace(/<[^>]*>/g, replace);
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
|
|
|
* Converts snake_case string to camelCase
|
|
|
|
*
|
|
|
|
* @param {*} string
|
|
|
|
*/
|
|
|
|
export const convertToCamelCase = string => string.replace(/(_\w)/g, s => s[1].toUpperCase());
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
/**
|
|
|
|
* Converts camelCase string to snake_case
|
|
|
|
*
|
|
|
|
* @param {*} string
|
|
|
|
*/
|
|
|
|
export const convertToSnakeCase = string =>
|
|
|
|
slugifyWithUnderscore(string.match(/([a-zA-Z][^A-Z]*)/g).join(' '));
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
/**
|
|
|
|
* 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-11-08 19:23:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
/**
|
|
|
|
* Converts a sentence to title case
|
|
|
|
* e.g. Hello world => Hello World
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
export const convertToTitleCase = string => string.replace(/\b[a-z]/g, s => s.toUpperCase());
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
/**
|
|
|
|
* Splits camelCase or PascalCase words
|
|
|
|
* e.g. HelloWorld => Hello World
|
|
|
|
*
|
|
|
|
* @param {*} string
|
2018-12-13 13:39:08 +05:30
|
|
|
*/
|
|
|
|
export const splitCamelCase = string =>
|
2018-11-08 19:23:39 +05:30
|
|
|
string
|
2018-12-13 13:39:08 +05:30
|
|
|
.replace(/([A-Z]+)([A-Z][a-z])/g, ' $1 $2')
|
|
|
|
.replace(/([a-z\d])([A-Z])/g, '$1 $2')
|
|
|
|
.trim();
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Intelligently truncates an item's namespace by doing two things:
|
|
|
|
* 1. Only include group names in path by removing the item name
|
|
|
|
* 2. Only include the first and last group names in the path
|
|
|
|
* when the namespace includes more than 2 groups
|
|
|
|
*
|
|
|
|
* @param {String} string A string namespace,
|
|
|
|
* i.e. "My Group / My Subgroup / My Project"
|
|
|
|
*/
|
|
|
|
export const truncateNamespace = (string = '') => {
|
|
|
|
if (_.isNull(string) || !_.isString(string)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const namespaceArray = string.split(' / ');
|
|
|
|
|
|
|
|
if (namespaceArray.length === 1) {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaceArray.splice(-1, 1);
|
|
|
|
let namespace = namespaceArray.join(' / ');
|
|
|
|
|
|
|
|
if (namespaceArray.length > 2) {
|
|
|
|
namespace = `${namespaceArray[0]} / ... / ${namespaceArray.pop()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return namespace;
|
|
|
|
};
|