Kebab file names

This commit is contained in:
Angelos Chalaris
2023-04-27 21:58:35 +03:00
parent 1d189c709a
commit 61200d90c4
440 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,29 @@
---
title: Truncate string at whitespace
tags: string
cover: cloudy-mountaintop-2
firstSeen: 2020-10-19T11:11:16+03:00
lastUpdated: 2020-10-21T21:17:45+03:00
---
Truncates a string up to specified length, respecting whitespace when possible.
- Determine if `String.prototype.length` is greater or equal to `lim`. If not, return it as-is.
- Use `String.prototype.slice()` and `String.prototype.lastIndexOf()` to find the index of the last space below the desired `lim`.
- Use `String.prototype.slice()` to appropriately truncate `str` based on `lastSpace`, respecting whitespace if possible and appending `ending` at the end.
- Omit the third argument, `ending`, to use the default ending of `'...'`.
```js
const truncateStringAtWhitespace = (str, lim, ending = '...') => {
if (str.length <= lim) return str;
const lastSpace = str.slice(0, lim - ending.length + 1).lastIndexOf(' ');
return str.slice(0, lastSpace > 0 ? lastSpace : lim - ending.length) + ending;
};
```
```js
truncateStringAtWhitespace('short', 10); // 'short'
truncateStringAtWhitespace('not so short', 10); // 'not so...'
truncateStringAtWhitespace('trying a thing', 10); // 'trying...'
truncateStringAtWhitespace('javascripting', 10); // 'javascr...'
```