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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ num.median([1, 2, 3, 4, 5]); // 3
</details>

### 📝 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`

<details>
<summary>Show examples</summary>
Expand All @@ -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('<script>alert("xss")</script>'); // Safe HTML
str.randomColor(); // "#3a9f2b" (random hex color)
```
</details>

Expand Down
8 changes: 8 additions & 0 deletions src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}




Expand Down
22 changes: 22 additions & 0 deletions tests/randomColor.test.js
Original file line number Diff line number Diff line change
@@ -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!');
10 changes: 10 additions & 0 deletions tests/strings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ assert.equal(strings.snakeCase('HelloWorld'), 'hello_world');
assert.equal(strings.repeat('x', 3), 'xxx');
assert.equal(strings.escapeHtml('<div>'), '&lt;div&gt;');

// 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');


Expand Down