Upgrade web assets (#219)
* Update assets * Upgrade linting and other improvments * Correct linting * Correction and type check improvements * Correct type check lib * Fix lint pathing for VSCode * Remove duplicate babel config * Remove editorconfig root attribute from web subdir * Use double quotes around message * Simplify ESLint config * Update web assets * Allow AMD loader in WebPack * Bump web dependencies * Only include FA icons in-use
This commit is contained in:
42
web/js/lib/text.mjs
Normal file
42
web/js/lib/text.mjs
Normal file
@ -0,0 +1,42 @@
|
||||
import {validateString, validateNumber} from './type.mjs';
|
||||
|
||||
/**
|
||||
* Truncate string length by characters.
|
||||
*
|
||||
* @param {string} text String to format.
|
||||
* @param {number} limit Maximum number of characters in resulting string.
|
||||
* @param {string} ending Ending to use if string is trucated.
|
||||
*
|
||||
* @returns {string} Formatted string.
|
||||
*/
|
||||
export function limitChars(text, limit = 50, ending = '...') {
|
||||
validateString(text);
|
||||
validateNumber(limit);
|
||||
validateString(ending);
|
||||
|
||||
// Check if string is already below limit
|
||||
if (text.length <= limit) {
|
||||
return text;
|
||||
}
|
||||
|
||||
// Limit string length by characters
|
||||
return text.substring(0, limit - ending.length) + ending;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate string length by words.
|
||||
*
|
||||
* @param {string} text String to format.
|
||||
* @param {number} limit Maximum number of words in resulting string.
|
||||
* @param {string} ending Ending to use if string is trucated.
|
||||
*
|
||||
* @returns {string} Formatted string.
|
||||
*/
|
||||
export function limitWords(text, limit = 10, ending = '...') {
|
||||
validateString(text);
|
||||
validateNumber(limit);
|
||||
validateString(ending);
|
||||
|
||||
// Limit string length by words
|
||||
return text.split(' ').splice(0, limit).join(' ') + ending;
|
||||
}
|
Reference in New Issue
Block a user