From 960e4b1e5b803a5399bceb7938e57ae18450cc32 Mon Sep 17 00:00:00 2001 From: Claude AI Date: Mon, 20 Oct 2025 13:44:21 +0200 Subject: [PATCH 1/3] feat(strings): add randomColor function for generating hex colors Implemented randomColor() function using Test-Driven Development (TDD): - Generates random hex color codes in format #rrggbb - Returns 6-digit lowercase hex values with leading zeros - Added comprehensive tests validating format and randomness - All tests pass successfully The function uses Math.random() to generate random RGB values and formats them as a standard hex color string. --- src/strings.js | 8 ++++++++ tests/strings.test.js | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/strings.js b/src/strings.js index 3873d95..570b922 100644 --- a/src/strings.js +++ b/src/strings.js @@ -76,6 +76,14 @@ export function escapeHtml(s = '') { return String(s).replace(/[&<>"']/g, (char) => map[char]); } +/** Generate a random hex color. */ +export function randomColor() { + const hex = Math.floor(Math.random() * 0xffffff) + .toString(16) + .padStart(6, '0'); + return `#${hex}`; +} + diff --git a/tests/strings.test.js b/tests/strings.test.js index ea422d5..da74e75 100644 --- a/tests/strings.test.js +++ b/tests/strings.test.js @@ -12,6 +12,16 @@ assert.equal(strings.snakeCase('HelloWorld'), 'hello_world'); assert.equal(strings.repeat('x', 3), 'xxx'); assert.equal(strings.escapeHtml('
'), '<div>'); +// randomColor() tests +const color1 = strings.randomColor(); +assert.match(color1, /^#[0-9a-f]{6}$/i, 'randomColor should return valid hex color'); + +const color2 = strings.randomColor(); +assert.match(color2, /^#[0-9a-f]{6}$/i, 'randomColor should return valid hex color'); + +// Colors should be random (statistically very unlikely to be equal) +assert.notEqual(color1, color2, 'randomColor should generate different colors'); + console.log('strings tests ok'); From a00c08f29ed15f143dd7d813df4c1ec8c5c5c073 Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 20 Oct 2025 13:56:15 +0200 Subject: [PATCH 2/3] add(test): add test case for `randomColor()` function --- tests/randomColor.test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/randomColor.test.js diff --git a/tests/randomColor.test.js b/tests/randomColor.test.js new file mode 100644 index 0000000..fcb1292 --- /dev/null +++ b/tests/randomColor.test.js @@ -0,0 +1,22 @@ +import assert from 'node:assert/strict'; +import { randomColor } from '../src/strings.js'; + +// Test 1: randomColor returns valid hex format +const color1 = randomColor(); +assert.match(color1, /^#[0-9a-f]{6}$/i, 'randomColor should return valid hex color'); + +// Test 2: randomColor returns valid hex format (second call) +const color2 = randomColor(); +assert.match(color2, /^#[0-9a-f]{6}$/i, 'randomColor should return valid hex color'); + +// Test 3: Colors should be different (statistically very unlikely to be equal) +assert.notEqual(color1, color2, 'randomColor should generate different colors'); + +// Test 4: Test multiple calls to ensure consistency +for (let i = 0; i < 10; i++) { + const color = randomColor(); + assert.match(color, /^#[0-9a-f]{6}$/i, `iteration ${i}: randomColor should return valid hex color`); + assert.equal(color.length, 7, `iteration ${i}: color should be 7 characters long`); +} + +console.log('randomColor tests passed!'); From 89b6adfe0c9429befaac114e98b5b49e604d7d1d Mon Sep 17 00:00:00 2001 From: Claude AI Date: Mon, 20 Oct 2025 13:59:19 +0200 Subject: [PATCH 3/3] docs(readme): add randomColor function to strings module documentation Add randomColor() function to the strings module documentation with usage example. This completes the documentation for the randomColor feature implementation. Refs feature/random-color-function --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dab3fc9..e67229a 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ num.median([1, 2, 3, 4, 5]); // 3 ### 📝 Strings -`capitalize`, `titleCase`, `kebabCase`, `slugify`, `truncate`, `pad`, `stripAnsi`, `reverse`, `camelCase`, `snakeCase`, `repeat`, `escapeHtml` +`capitalize`, `titleCase`, `kebabCase`, `slugify`, `truncate`, `pad`, `stripAnsi`, `reverse`, `camelCase`, `snakeCase`, `repeat`, `escapeHtml`, `randomColor`
Show examples @@ -95,6 +95,7 @@ str.titleCase('hello world'); // "Hello World" str.truncate('Long text here', 10); // "Long te..." str.camelCase('hello-world'); // "helloWorld" str.escapeHtml(''); // Safe HTML +str.randomColor(); // "#3a9f2b" (random hex color) ```