Skip to content

Commit

Permalink
ESM only + move to webtestrunnner (#467)
Browse files Browse the repository at this point in the history
* chore: remove karma

* chore: update esm.test.mjs to use chai

* chore: specify with extension whether tests are ESM or CJS

* mv fuzz.test to mjs, mv esm.test to chai.assert

* chore: move slug.test to ESM

* chore: move to ESM only

BREAKING CHANGE: This module only supports ESM. CommonJS and non-ESM
script tags are no longer supported.

* chore: add web-test-runner

* chore: add coverage reporting for CLI tests

* chore: fail CLI tests if coverage is not 100%
  • Loading branch information
Trott authored Oct 17, 2024
1 parent cf5321d commit a8db3ed
Show file tree
Hide file tree
Showing 13 changed files with 5,721 additions and 3,893 deletions.
23 changes: 0 additions & 23 deletions .karma.config.js

This file was deleted.

9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Make strings URL-safe.

- Respects [RFC 3986](https://tools.ietf.org/html/rfc3986)
- No dependencies
- Works in browser (`window.slug`) and AMD/CommonJS-flavoured module loaders
- Works in the browser or in Node.js

```
npm install slug
Expand All @@ -21,7 +21,7 @@ npm install --save-dev @types/slug
## Example

```javascript
var slug = require('slug')
import slug from 'slug'
var print = console.log.bind(console, '>')

print(slug('i love unicode'))
Expand Down Expand Up @@ -114,16 +114,15 @@ slug.defaults.modes['pretty'] = {

Here are some key differences between this package and [`slugify`](https://github.com/simov/slugify).

- **Stability:** `slug` is ESM-only.
`slugify` supports CommonJS and ESM.
- **Defaults:** `slug` has the `lower` option enabled by default, lowercasing all slugs
(`'On SALE'` becomes `'on-sale'`).
`slugify` has the `lower` option disabled by default (`'On SALE'` becomes `'On-SALE'`).
- **Symbols:** `slug` removes unrecognized symbols (`'$100'` becomes `'100'`, `'<5'` becomes `'5'`, etc.).
`slugify` maps them to words (`'$100'` becomes `'dollar100'`, `'<5'` becomes `'less5'`, etc.).
- **Empty Output:** `slug` will return a short, predictable hash (`' '` becomes `'icag'` and `'🎉'` becomes `'8joiq'`).
`slugify` will return an empty string (`' '` and `'🎉'` become `''`).
- **Stability:** `slug` is planning [a new release](https://github.com/Trott/slug/blob/beta/CHANGELOG.md) that will drop support for CommonJS
and only support ESM modules.
`slugify` will continue to support CommonJS and is likely to remain stable for the foreseeable future.

## Playground

Expand Down
2 changes: 1 addition & 1 deletion benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const slug = require('../slug')
import slug from '../slug.js'

const BENCHMARK_RUNS = 2048
const MAX_WORD_LENGTH = 16
Expand Down
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const slug = require('./slug.js')
import slug from './slug.js'

if (process.argv.length < 3) {
console.log('Usage: slug <string>')
Expand Down
46 changes: 44 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,49 @@ <h1>Playground for slug module</h1>
</fieldset>
</form>
</main>
<script src="./slug.js"></script>
<script src="./playground.js"></script>
<script type="module">
import slug from './slug.js';

const form = document.getElementById('form')

form.addEventListener('submit', function (e) {
e.preventDefault()
if (form === null) {
console.error('form element not found')
return
}

const fd = new FormData(form)

const replacement = fd.get('replacement')
const lowercase = fd.get('lowercase')
const trim = fd.get('trim')
const fallback = fd.get('fallback')

const remove = fd.get('remove')
const regexG = fd.get('regex_g') === null ? '' : 'g'
const regexI = fd.get('regex_i') === null ? '' : 'i'

const opts = {}

if (replacement.length > 0) {
opts.replacement = replacement
}

if (remove !== null && remove.length > 0) {
const regex = new RegExp(`/${remove}/${regexG}${regexI}`)
opts.remove = regex
}

opts.lower = lowercase !== null

opts.trim = trim !== null

opts.fallback = fallback !== null

const output = slug(document.getElementById('input').value, opts)
document.getElementById('slugOutput').innerText = output
})
</script>
</body>
</html>
Loading

0 comments on commit a8db3ed

Please sign in to comment.