Skip to content

Commit

Permalink
fix: enable browser compatibility for all code points
Browse files Browse the repository at this point in the history
  • Loading branch information
Trott committed May 29, 2020
1 parent dfc2b41 commit 2729c2b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 4 additions & 4 deletions slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@
if (code >= 0xD800 && code <= 0xDBFF) {
if (str.length <= (i + 1)) {
// High surrogate without following low surrogate
return ['', i + 1]
return [' ', i]
}
const next = str.charCodeAt(i + 1)
if (next < 0xDC00 || next > 0xDFFF) {
// High surrogate without following low surrogate
return ['', i + 1]
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]
return [' ', i]
}

const prev = str.charCodeAt(i - 1)

if (prev < 0xD800 || prev > 0xDBFF) {
// Low surrogate without preceding high surrogate
return ['', i + 1]
return [' ', i]
}

// Return the next character instead (and increment)
Expand Down
12 changes: 6 additions & 6 deletions test/fuzz.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ describe('fuzz-testing slug', function () {
}

for (let i = 0; i < FUZZ_TESTS; i++) {
const { fuzzyString, codePoints } = getString(MAX_BMP_CODE_POINT)
assert(slug(fuzzyString), `STRING: ${fuzzyString}\nCODEPOINTS: ${JSON.stringify(codePoints)}`)
// TODO: Fix so we don't need to skip this in the browser. Maybe see the
// "fixing" vesions of charCodeAt() in
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
if (typeof window === 'undefined') {
{
const { fuzzyString, codePoints } = getString(MAX_BMP_CODE_POINT)
assert(slug(fuzzyString), `STRING: ${fuzzyString}\nCODEPOINTS: ${JSON.stringify(codePoints)}`)
}

{
const { fuzzyString, codePoints } = getString(MAX_CODE_POINT)
assert(slug(fuzzyString), `STRING: ${fuzzyString}\nCODEPOINTS: ${JSON.stringify(codePoints)}`)
}
Expand Down
7 changes: 6 additions & 1 deletion test/slug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,12 @@ describe('slug', function () {
it('should ignore symbols if they are not in the charmap', () => {
assert.strictEqual(slug('unicode ♥ is ☢'), 'unicode-love-is')
})

it('should ignore lone surrogates', () => {
assert.strictEqual(slug(String.fromCodePoint(56714, 36991)), '6yg')
assert.strictEqual(slug(String.fromCodePoint(56714, 36991)), 'iombvw')
})

it('should handle a lone surrogate by itself', () => {
assert.strictEqual(slug(String.fromCodePoint(56714)), 'ia')
})
})

0 comments on commit 2729c2b

Please sign in to comment.