@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
100 lines (99 loc) • 3.17 kB
JavaScript
/**
* Converts the first character of string to upper case and the remaining to lower case.
* Returns a type-safe capitalized string.
*/
export function _capitalize(s = '') {
return (s.charAt(0).toUpperCase() + s.slice(1).toLowerCase());
}
/**
* Convert a string to a type-safe uppercase string.
*/
export function _toUpperCase(s) {
return s.toUpperCase();
}
/**
* Convert a string to a type-safe lowercase string.
*/
export function _toLowercase(s) {
return s.toLowerCase();
}
export function _upperFirst(s = '') {
return (s.charAt(0).toUpperCase() + s.slice(1));
}
export function _lowerFirst(s) {
return (s.charAt(0).toLowerCase() + s.slice(1));
}
/**
* Like String.split(), but with limit, returning the tail together with last element.
*
* @returns Returns the new array of string segments.
*/
export function _split(str, separator, limit) {
const parts = str.split(separator);
if (parts.length <= limit)
return parts;
return [...parts.slice(0, limit - 1), parts.slice(limit - 1).join(separator)];
}
export function _removeWhitespace(s) {
return s.replaceAll(/\s/g, '');
}
/**
* _.truncate('hi-diddly-ho there, neighborino')
* // => 'hi-diddly-ho there, neighbo...'
*/
export function _truncate(s, maxLen, omission = '...') {
if (!s || s.length <= maxLen)
return s;
if (maxLen <= omission.length)
return omission;
return s.slice(0, maxLen - omission.length) + omission;
}
/**
* _.truncateMiddle('abcdefghijklmnopqrstuvwxyz', 10)
* // => 'abcd...xyz'
*/
export function _truncateMiddle(s, maxLen, omission = '...') {
if (!s || s.length <= maxLen)
return s;
if (maxLen <= omission.length)
return omission;
const mark1 = Math.round((maxLen - omission.length) / 2);
const mark2 = s.length - Math.floor((maxLen - omission.length) / 2);
return s.slice(0, mark1) + omission + s.slice(mark2);
}
// These functions are modeled after Kotlin's String API
export function _substringBefore(s, delimiter) {
const pos = s.indexOf(delimiter);
return s.slice(0, pos !== -1 ? pos : undefined);
}
export function _substringBeforeLast(s, delimiter) {
const pos = s.lastIndexOf(delimiter);
return s.slice(0, pos !== -1 ? pos : undefined);
}
export function _substringAfter(s, delimiter) {
const pos = s.indexOf(delimiter);
return pos !== -1 ? s.slice(pos + delimiter.length) : s;
}
export function _substringAfterLast(s, delimiter) {
const pos = s.lastIndexOf(delimiter);
return pos !== -1 ? s.slice(pos + delimiter.length) : s;
}
/**
* Returns the substring between LAST `leftDelimiter` and then FIRST `rightDelimiter`.
*
* @example
*
* const s = '/Users/lalala/someFile.test.ts'
* _substringBetweenLast(s, '/', '.')
* // `someFile`
*/
export function _substringBetweenLast(s, leftDelimiter, rightDelimiter) {
return _substringBefore(_substringAfterLast(s, leftDelimiter), rightDelimiter);
}
/**
* Converts `\n` (aka new-line) to `<br>`, to be presented in HTML.
* Keeps `\n`, so if it's printed in non-HTML environment it still looks ok-ish.
*/
export function _nl2br(s) {
return s.replaceAll('\n', '<br>\n');
}