diff --git a/src/strings.js b/src/strings.js index 3873d95..6c6e9da 100644 --- a/src/strings.js +++ b/src/strings.js @@ -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 = ' ') { @@ -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); +} diff --git a/tests/strings.test.js b/tests/strings.test.js index ea422d5..106de0e 100644 --- a/tests/strings.test.js +++ b/tests/strings.test.js @@ -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('