Comparing version
{ | ||
"name": "slug", | ||
"description": "slugifies even utf-8 chars!", | ||
"version": "11.0.0-beta.1", | ||
"version": "11.0.0", | ||
"type": "module", | ||
@@ -17,5 +17,2 @@ "homepage": "https://github.com/Trott/slug", | ||
}, | ||
"files": [ | ||
"slug.js" | ||
], | ||
"keywords": [ | ||
@@ -38,5 +35,5 @@ "slugify", | ||
"@semantic-release/git": "^10.0.1", | ||
"@web/test-runner": "^0.19.0", | ||
"@web/test-runner": "^0.20.0", | ||
"c8": "^10.1.2", | ||
"mocha": "^10.0.0", | ||
"mocha": "^11.0.1", | ||
"semantic-release": "^24.0.0", | ||
@@ -43,0 +40,0 @@ "standard": "^17.1.0" |
@@ -129,8 +129,5 @@ # [slug](https://github.com/Trott/slug) | ||
A (painfully minimal) web playground is available at | ||
https://trott.github.io/slug/. It doesn't allow you to specify options, so it's utility is minimal. Pull requests welcome to add the ability to | ||
specify options. | ||
A web playground is available at https://trott.github.io/slug/. | ||
There is also a (similarly minimal) CLI tool available via `npx slug`. | ||
As with the web playground, it doesn't allow you to specify options, so | ||
it's utility is minimal. | ||
There is also a CLI tool available via `npx slug`. It doesn't allow you to | ||
specify options, so it's utility is minimal. |
59
slug.js
/* global btoa */ | ||
let base64 | ||
// This function's sole purpose is to help us ignore lone surrogates so that | ||
// malformed strings don't throw in the browser while being processed | ||
// permissively in Node.js. If we didn't care about parity, we could get rid | ||
// of it. | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt | ||
function getWholeCharAndI (str, i) { | ||
const code = str.charCodeAt(i) | ||
// This is a coherence check. `code` should never be `NaN`. | ||
/* c8 ignore next 3 */ | ||
if (isNaN(code)) { | ||
throw new RangeError('Index ' + i + ' out of range for string "' + str + '"; please open an issue at https://github.com/Trott/slug/issues/new') | ||
} | ||
if (code < 0xD800 || code > 0xDFFF) { | ||
return [str.charAt(i), i] // Non-surrogate character, keeping 'i' the same | ||
} | ||
// High surrogate | ||
if (code >= 0xD800 && code <= 0xDBFF) { | ||
if (str.length <= (i + 1)) { | ||
// High surrogate without following low surrogate | ||
return [' ', i] | ||
} | ||
const next = str.charCodeAt(i + 1) | ||
if (next < 0xDC00 || next > 0xDFFF) { | ||
// High surrogate without following low surrogate | ||
return [' ', i] | ||
} | ||
return [str.charAt(i) + str.charAt(i + 1), i + 1] | ||
} | ||
// Low surrogate (0xDC00 <= code && code <= 0xDFFF) | ||
if (i === 0) { | ||
// Low surrogate without preceding high surrogate | ||
return [' ', i] | ||
} | ||
const prev = str.charCodeAt(i - 1) | ||
if (prev < 0xD800 || prev > 0xDBFF) { | ||
// Low surrogate without preceding high surrogate | ||
return [' ', i] | ||
} | ||
/* c8 ignore next */ | ||
throw new Error('String "' + str + '" reaches code believed to be unreachable; please open an issue at https://github.com/Trott/slug/issues/new') | ||
} | ||
if (typeof window !== 'undefined') { | ||
@@ -90,10 +43,3 @@ if (window.btoa) { | ||
if (fallback === true && result === '') { | ||
// Get rid of lone surrogates. | ||
let input = '' | ||
for (let i = 0; i < string.length; i++) { | ||
const charAndI = getWholeCharAndI(string, i) | ||
i = charAndI[1] | ||
input += charAndI[0] | ||
} | ||
result = slugify(base64(input), opts) | ||
result = slugify(base64(string), opts) | ||
} | ||
@@ -120,2 +66,5 @@ return result | ||
} | ||
if (!string.isWellFormed()) { | ||
throw new Error('slug() received a malformed string with lone surrogates') | ||
} | ||
if (typeof opts === 'string') { opts = { replacement: opts } } | ||
@@ -122,0 +71,0 @@ opts = opts ? Object.assign({}, opts) : {} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
32986
41.34%7
40%0
-100%851
-1.85%133
-2.21%