Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ export function kebabCase(s = '') {
}

export function slugify(s = '') {
return kebabCase(s)
.normalize('NFKD')
return kebabCase(
s.normalize('NFKD')
.replace(/\p{Diacritic}/gu, '')
.toLowerCase();
.toLowerCase()
);
}

export function truncate(s = '', max = 100, suffix = '…') {
const str = String(s);
if (str.length <= max) return str;
return str.slice(0, Math.max(0, max - suffix.length)) + suffix;
return str.slice(0, Math.max(0, max)) + suffix;
}

export function pad(s = '', length = 2, ch = ' ') {
Expand Down Expand Up @@ -76,6 +77,10 @@ export function escapeHtml(s = '') {
return String(s).replace(/[&<>"']/g, (char) => map[char]);
}

/* Generates a random Hex color*/
export function randomColor() {
return '#'+Math.floor(Math.random()*16777216).toString(16).padStart(6,0);
}



Expand Down
1 change: 1 addition & 0 deletions tests/strings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ assert.equal(strings.camelCase('hello-world'), 'helloWorld');
assert.equal(strings.snakeCase('HelloWorld'), 'hello_world');
assert.equal(strings.repeat('x', 3), 'xxx');
assert.equal(strings.escapeHtml('<div>'), '&lt;div&gt;');
assert.match(strings.randomColor(),/^#([0-9a-f]{6})$/)

console.log('strings tests ok');

Expand Down