js-01-slugify
0.000
Challenge · difficulty 1/5
# Slugify a title
Implement an ES module **`solution.js`** exporting a single function:
```js
export function slugify(input) { /* ... */ }
```
Convert a human title string into a URL slug:
- Lowercase the whole string.
- Treat **any character that is not an ASCII letter (`a–z`) or digit (`0–9`) as a separator** —
this includes spaces, punctuation, underscores, and non-ASCII letters.
- Replace each **run** of one or more separators with a **single** hyphen (`-`).
- Strip any leading or trailing hyphens from the result.
Return the resulting slug (a string). An input made entirely of separators returns `""`.
Examples:
```js
slugify("Hello World") // => "hello-world"
slugify(" Hello, World! ") // => "hello-world"
slugify("already-clean") // => "already-clean"
slugify("Top 10 Tips") // => "top-10-tips"
slugify("a---b__c") // => "a-b-c"
slugify("@#$%") // => ""
```
tests/solution.test.js
import { test } from "node:test";
import { strict as assert } from "node:assert";
import { slugify } from "./solution.js";
test("spaces become single hyphens", () => {
assert.equal(slugify("Hello World"), "hello-world");
});
test("punctuation collapses and ends are trimmed", () => {
assert.equal(slugify(" Hello, World! "), "hello-world");
});
test("already-clean input is unchanged", () => {
assert.equal(slugify("already-clean"), "already-clean");
});
test("digits are preserved", () => {
assert.equal(slugify("Top 10 Tips"), "top-10-tips");
});
test("runs of mixed separators collapse to one hyphen", () => {
assert.equal(slugify("a---b__c"), "a-b-c");
});
test("leading and trailing junk is trimmed", () => {
assert.equal(slugify("--Foo Bar--"), "foo-bar");
});
test("all-separator input becomes empty string", () => {
assert.equal(slugify("@#$%"), "");
});
test("empty string stays empty", () => {
assert.equal(slugify(""), "");
});
test("non-ASCII letters act as separators", () => {
assert.equal(slugify("café crème"), "caf-cr-me");
});
Proposed solution
```js
export function slugify(input) {
console.log(input);
console.log('-' + '-'.repeat -1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Errors (stderr)
no code extracted from response