Skip to content

Commit b9f19db

Browse files
authored
Use ESM
Closes GH-43. Reviewed-by: Dan Flettre <[email protected]>
1 parent 4bfc760 commit b9f19db

File tree

8 files changed

+66
-74
lines changed

8 files changed

+66
-74
lines changed

Diff for: README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ npm install github-slugger
2323
## Usage
2424

2525
```js
26-
var GithubSlugger = require('github-slugger')
27-
var slugger = new GithubSlugger()
26+
import GithubSlugger from 'github-slugger'
27+
28+
const slugger = new GithubSlugger()
2829

2930
slugger.slug('foo')
3031
// returns 'foo'
@@ -56,7 +57,7 @@ If you need, you can also use the underlying implementation which does not keep
5657
track of the previously slugged strings (not recommended):
5758

5859
```js
59-
var slug = require('github-slugger').slug;
60+
import GithubSlugger, {slug} from 'github-slugger'
6061

6162
slug('foo bar baz')
6263
// returns 'foo-bar-baz'

Diff for: index.js

+41-36
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
1-
const regex = require('./regex.js')
2-
3-
module.exports = BananaSlug
1+
import { regex } from './regex.js'
42

53
const own = Object.hasOwnProperty
64

7-
function BananaSlug () {
8-
const self = this
9-
10-
if (!(self instanceof BananaSlug)) return new BananaSlug()
11-
12-
self.reset()
13-
}
14-
155
/**
16-
* Generate a unique slug.
17-
* @param {string} value String of text to slugify
18-
* @param {boolean} [false] Keep the current case, otherwise make all lowercase
19-
* @return {string} A unique slug string
6+
* Slugger.
207
*/
21-
BananaSlug.prototype.slug = function (value, maintainCase) {
22-
const self = this
23-
let slug = slugger(value, maintainCase === true)
24-
const originalSlug = slug
25-
26-
while (own.call(self.occurrences, slug)) {
27-
self.occurrences[originalSlug]++
28-
slug = originalSlug + '-' + self.occurrences[originalSlug]
8+
export default class BananaSlug {
9+
/**
10+
* Create a new slug class.
11+
*/
12+
constructor () {
13+
this.reset()
2914
}
3015

31-
self.occurrences[slug] = 0
32-
33-
return slug
34-
}
16+
/**
17+
* Generate a unique slug.
18+
*
19+
* @param {string} value
20+
* String of text to slugify
21+
* @param {boolean} [maintainCase=false]
22+
* Keep the current case, otherwise make all lowercase
23+
* @return {string}
24+
* A unique slug string
25+
*/
26+
slug (value, maintainCase) {
27+
const self = this
28+
let result = slug(value, maintainCase === true)
29+
const originalSlug = result
30+
31+
while (own.call(self.occurrences, result)) {
32+
self.occurrences[originalSlug]++
33+
result = originalSlug + '-' + self.occurrences[originalSlug]
34+
}
35+
36+
self.occurrences[result] = 0
37+
38+
return result
39+
}
3540

36-
/**
37-
* Reset - Forget all previous slugs
38-
* @return void
39-
*/
40-
BananaSlug.prototype.reset = function () {
41-
this.occurrences = Object.create(null)
41+
/**
42+
* Reset - Forget all previous slugs
43+
*
44+
* @return void
45+
*/
46+
reset () {
47+
this.occurrences = Object.create(null)
48+
}
4249
}
4350

44-
function slugger (string, maintainCase) {
51+
export function slug (string, maintainCase) {
4552
if (typeof string !== 'string') return ''
4653
if (!maintainCase) string = string.toLowerCase()
4754
return string.replace(regex, '').replace(/ /g, '-')
4855
}
49-
50-
BananaSlug.slug = slugger

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
"bugs": {
1111
"url": "https://github.com/Flet/github-slugger/issues"
1212
},
13+
"type": "module",
1314
"files": [
1415
"index.js",
1516
"regex.js"
1617
],
1718
"devDependencies": {
1819
"@octokit/rest": "^19.0.0",
1920
"@unicode/unicode-13.0.0": "^1.0.0",
21+
"c8": "^7.0.0",
2022
"hast-util-select": "^5.0.0",
2123
"mdast-util-gfm": "^2.0.0",
2224
"mdast-util-to-markdown": "^1.0.0",
2325
"node-fetch": "^3.0.0",
24-
"nyc": "^15.0.0",
2526
"regenerate": "^1.0.0",
2627
"rehype-parse": "^8.0.0",
2728
"standard": "*",
@@ -49,7 +50,7 @@
4950
"scripts": {
5051
"format": "standard --fix",
5152
"test-api": "tape test | tap-spec",
52-
"test-coverage": "nyc --reporter lcov tape test/index.js | tap-spec",
53+
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
5354
"test": "npm run format && npm run test-coverage"
5455
},
5556
"nyc": {

Diff for: regex.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

Diff for: script/generate-regex.mjs renamed to script/generate-regex.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async function main () {
5656
await fs.writeFile('regex.js', [
5757
'// This module is generated by `script/`.',
5858
'/* eslint-disable no-control-regex, no-misleading-character-class, no-useless-escape */',
59-
'module.exports = ' + generator.toRegExp() + 'g',
59+
'export const regex = ' + generator.toRegExp() + 'g',
6060
''
6161
].join('\n'))
6262
}

Diff for: test/index.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
const test = require('tape')
2-
const GithubSlugger = require('../')
3-
const gist = require('./fixtures.json')
1+
import fs from 'node:fs'
2+
import test from 'tape'
3+
import GithubSlugger, { slug } from '../index.js'
44

5-
require('./test-static')
5+
const fixtures = JSON.parse(
6+
String(fs.readFileSync(
7+
new URL('fixtures.json', import.meta.url)
8+
))
9+
)
610

711
test('simple stuff', function (t) {
812
const slugger = new GithubSlugger()
913

10-
t.equals(GithubSlugger().slug('foo'), 'foo', 'should work without new')
1114
t.equals(slugger.slug(1), '', 'should return empty string for non-strings')
1215

1316
// Note: GH doesn’t support `maintaincase`, so the actual values are commented below.
@@ -17,10 +20,17 @@ test('simple stuff', function (t) {
1720
t.end()
1821
})
1922

23+
test('static method', function (t) {
24+
t.equals(slug('foo'), 'foo')
25+
t.equals(slug('foo bar'), 'foo-bar')
26+
t.equals(slug('foo'), 'foo') // idem potent
27+
t.end()
28+
})
29+
2030
test('fixtures', function (t) {
2131
const slugger = new GithubSlugger()
2232

23-
gist.forEach((d) => {
33+
fixtures.forEach((d) => {
2434
t.equals(slugger.slug(d.input), d.expected, d.name)
2535
})
2636

Diff for: test/test-static.js

-25
This file was deleted.

0 commit comments

Comments
 (0)