← run

js-01-slugify

1.000
9/9 tests· basic

Proposed solution

```js
export function slugify(input) {
  // Convert to lowercase
  let result = input.toLowerCase();
  
  // Replace any character that is not an ASCII letter or digit with a hyphen
  result = result.replace(/[^a-z0-9]/g, '-');
  
  // Replace runs of hyphens with a single hyphen
  result = result.replace(/-+/g, '-');
  
  // Remove leading and trailing hyphens
  result = result.replace(/^-|-$/g, '');
  
  return result;
}
```

Test output (stdout)

TAP version 13
# Subtest: spaces become single hyphens
ok 1 - spaces become single hyphens
  ---
  duration_ms: 0.423803
  type: 'test'
  ...
# Subtest: punctuation collapses and ends are trimmed
ok 2 - punctuation collapses and ends are trimmed
  ---
  duration_ms: 0.063021
  type: 'test'
  ...
# Subtest: already-clean input is unchanged
ok 3 - already-clean input is unchanged
  ---
  duration_ms: 0.04419
  type: 'test'
  ...
# Subtest: digits are preserved
ok 4 - digits are preserved
  ---
  duration_ms: 0.040681
  type: 'test'
  ...
# Subtest: runs of mixed separators collapse to one hyphen
ok 5 - runs of mixed separators collapse to one hyphen
  ---
  duration_ms: 0.07926
  type: 'test'
  ...
# Subtest: leading and trailing junk is trimmed
ok 6 - leading and trailing junk is trimmed
  ---
  duration_ms: 0.04182
  type: 'test'
  ...
# Subtest: all-separator input becomes empty string
ok 7 - all-separator input becomes empty string
  ---
  duration_ms: 0.039251
  type: 'test'
  ...
# Subtest: empty string stays empty
ok 8 - empty string stays empty
  ---
  duration_ms: 0.03702
  type: 'test'
  ...
# Subtest: non-ASCII letters act as separators
ok 9 - non-ASCII letters act as separators
  ---
  duration_ms: 0.05237
  type: 'test'
  ...
1..9
# tests 9
# suites 0
# pass 9
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 30.366994
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.