From 66d98a8b2649830257ed9a4ef07b4478bfd20274 Mon Sep 17 00:00:00 2001 From: atharvasail11 Date: Sat, 22 Nov 2025 14:21:26 +0530 Subject: [PATCH 1/3] Feature:Added randomColor() utility and its test case --- src/strings.js | 4 ++++ tests/strings.test.js | 1 + 2 files changed, 5 insertions(+) diff --git a/src/strings.js b/src/strings.js index 3873d95..2a76184 100644 --- a/src/strings.js +++ b/src/strings.js @@ -76,6 +76,10 @@ export function escapeHtml(s = '') { return String(s).replace(/[&<>"']/g, (char) => map[char]); } +/* Generates a random Hex color*/ +export const 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..7f44ee8 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('
'), '<div>'); +assert.match(strings.randomColor(),/^#([0-9a-f]{6})$/); console.log('strings tests ok'); From 814c6bef60da9d6d2b45a24439e6fb6dfd7314f4 Mon Sep 17 00:00:00 2001 From: atharvasail11 Date: Sat, 22 Nov 2025 15:27:48 +0530 Subject: [PATCH 2/3] Made some Improvements in slugify function and truncate function as they where not getting the expected result in test cases. --- src/strings.js | 9 +++++---- tests/strings.test.js | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/strings.js b/src/strings.js index 2a76184..ce9f0ac 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 = ' ') { diff --git a/tests/strings.test.js b/tests/strings.test.js index 7f44ee8..106de0e 100644 --- a/tests/strings.test.js +++ b/tests/strings.test.js @@ -11,7 +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>'); -assert.match(strings.randomColor(),/^#([0-9a-f]{6})$/); +assert.match(strings.randomColor(),/^#([0-9a-f]{6})$/) console.log('strings tests ok'); From ca4ea874f2f5816dfa4b6ead22055b2292199f35 Mon Sep 17 00:00:00 2001 From: atharvasail11 Date: Sat, 22 Nov 2025 15:40:50 +0530 Subject: [PATCH 3/3] Converted the randomColor function from arrow function to normal function for code consistency --- src/strings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/strings.js b/src/strings.js index ce9f0ac..6c6e9da 100644 --- a/src/strings.js +++ b/src/strings.js @@ -78,7 +78,7 @@ export function escapeHtml(s = '') { } /* Generates a random Hex color*/ -export const randomColor=()=>{ +export function randomColor() { return '#'+Math.floor(Math.random()*16777216).toString(16).padStart(6,0); }