Skip to content

Commit

Permalink
fix: spec update - blocklist filtering in uppercase-only alphabet
Browse files Browse the repository at this point in the history
  • Loading branch information
4kimov committed Aug 30, 2023
1 parent a034f37 commit 8103161
Show file tree
Hide file tree
Showing 8 changed files with 1,291 additions and 1,079 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

**v0.1.3:**
- Bug fix: spec update: blocklist filtering in uppercase-only alphabet [[PR #7](https://github.com/sqids/sqids-spec/pull/7)]
- Dev dependencies updated

**v0.1.2:**
- Bug fix: cjs import error [[PR #5](https://github.com/sqids/sqids-javascript/pull/5)]

Expand Down
7 changes: 4 additions & 3 deletions cjs/sqids.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/sqids.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions esm/sqids.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sqids",
"version": "0.1.2",
"version": "0.1.3",
"description": "Generate YouTube-like ids from numbers.",
"keywords": [
"sqids",
Expand Down Expand Up @@ -80,7 +80,7 @@
"not dead"
],
"devDependencies": {
"@niieani/scaffold": "^1.7.3",
"@niieani/scaffold": "^1.7.8",
"nodemark": "^0.3.0",
"require-from-web": "^1.2.0",
"ts-node": "^10.9.1"
Expand Down
7 changes: 4 additions & 3 deletions src/sqids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,14 @@ export default class Sqids {
}

const filteredBlocklist = new Set<string>()
const alphabetChars = alphabet.split('')
const alphabetChars = alphabet.toLowerCase().split('')
for (const word of blocklist) {
if (word.length >= 3) {
const wordChars = word.split('')
const wordLowercased = word.toLowerCase()
const wordChars = wordLowercased.split('')
const intersection = wordChars.filter((c) => alphabetChars.includes(c))
if (intersection.length === wordChars.length) {
filteredBlocklist.add(word.toLowerCase())
filteredBlocklist.add(wordLowercased)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/tests/blocklist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,17 @@ describe('blocklist', () => {

expect(sqids.decode(sqids.encode([1_000]))).toEqual([1_000])
})

it('blocklist filtering in constructor', () => {
const sqids = new Sqids({
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
blocklist: new Set(['sqnmpn']), // lowercase blocklist in only-uppercase alphabet
})

const id = sqids.encode([1, 2, 3])
const numbers = sqids.decode(id)

expect(id).toBe('ULPBZGBM') // without blocklist, would've been "SQNMPN"
expect(numbers).toEqual([1, 2, 3])
})
})
Loading

0 comments on commit 8103161

Please sign in to comment.