Skip to content

Commit

Permalink
fix: spec update (PR #7) - blocklist filtering in uppercase-only alph…
Browse files Browse the repository at this point in the history
…abet
  • Loading branch information
4kimov committed Aug 30, 2023
1 parent 090a81b commit db0c000
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

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

**v0.3.0:**
- Bug fix: test for decoding an invalid ID with a repeating reserved character
- Removing requirement of `ext-mbstring`
Expand Down
7 changes: 4 additions & 3 deletions src/Sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,13 +617,14 @@ public function __construct(
}

$filteredBlocklist = [];
$alphabetChars = str_split($alphabet);
$alphabetChars = str_split(strtolower($alphabet));
foreach ((array) $blocklist as $word) {
if (strlen((string) $word) >= 3) {
$wordChars = str_split((string) $word);
$wordLowercased = strtolower($word);
$wordChars = str_split((string) $wordLowercased);
$intersection = array_filter($wordChars, fn ($c) => in_array($c, $alphabetChars));
if (count($intersection) == count($wordChars)) {
$filteredBlocklist[] = strtolower((string) $word);
$filteredBlocklist[] = strtolower((string) $wordLowercased);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/SqidsBlocklistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ public function testMatchAgainstAShortBlocklistWord()
$sqids = new Sqids('', 0, ['pPQ']);
$this->assertSame([1000], $sqids->decode($sqids->encode([1000])));
}

public function testBlocklistFilteringInConstructor()
{
// lowercase blocklist in only-uppercase alphabet
$sqids = new Sqids('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 0, ['sqnmpn']);

$id = $sqids->encode([1, 2, 3]);
$numbers = $sqids->decode($id);

$this->assertSame($id, 'ULPBZGBM'); // without blocklist, would've been "SQNMPN"
$this->assertSame($numbers, [1, 2, 3]);
}
}

0 comments on commit db0c000

Please sign in to comment.