1
0
mirror of https://github.com/flarum/core.git synced 2025-01-18 06:38:25 +01:00

convert: common/utils/string

This commit is contained in:
David Sevilla Martin 2020-06-23 10:02:59 -04:00 committed by Franz Liedke
parent 4f34e326ef
commit 20baa93ca7
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4

View File

@ -1,12 +1,7 @@
/**
* Truncate a string to the given length, appending ellipses if necessary.
*
* @param {String} string
* @param {Number} length
* @param {Number} [start=0]
* @return {String}
*/
export function truncate(string, length, start = 0) {
export function truncate(string: string, length: number, start: number = 0): string {
return (start > 0 ? '...' : '') + string.substring(start, start + length) + (string.length > start + length ? '...' : '');
}
@ -17,11 +12,8 @@ export function truncate(string, length, start = 0) {
* NOTE: This method does not use the comparably sophisticated transliteration
* mechanism that is employed in the backend. Therefore, it should only be used
* to *suggest* slugs that can be overridden by the user.
*
* @param {String} string
* @return {String}
*/
export function slug(string) {
export function slug(string: string): string {
return string
.toLowerCase()
.replace(/[^a-z0-9]/gi, '-')
@ -32,11 +24,8 @@ export function slug(string) {
/**
* Strip HTML tags and quotes out of the given string, replacing them with
* meaningful punctuation.
*
* @param {String} string
* @return {String}
*/
export function getPlainContent(string) {
export function getPlainContent(string: string): string {
const html = string.replace(/(<\/p>|<br>)/g, '$1 &nbsp;').replace(/<img\b[^>]*>/gi, ' ');
const dom = $('<div/>').html(html);
@ -55,10 +44,7 @@ getPlainContent.removeSelectors = ['blockquote', 'script'];
/**
* Make a string's first character uppercase.
*
* @param {String} string
* @return {String}
*/
export function ucfirst(string) {
export function ucfirst(string: string): string {
return string.substr(0, 1).toUpperCase() + string.substr(1);
}