Skip to content

Commit 9e1545d

Browse files
committed
Prevent chars with code point > 0xffff
1 parent 302447f commit 9e1545d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/lib/chars.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ test('invalid characters', (t) => {
3838
t.regex(invalidChars('dingo\\sky'), /Invalid/)
3939
t.regex(invalidChars('dingo`sky'), /Invalid/)
4040
t.regex(invalidChars('dingo\u0088sky'), /Invalid/)
41+
t.regex(invalidChars('dîngøsky:🐕'), /Invalid/)
4142
})
4243

4344
test('non-unique character', (t) => t.regex(invalidChars('unique'), /not unique/))

src/lib/chars.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export const charsName = (chars: string): string => {
4848
}
4949

5050
const validChar = (char: string): boolean => {
51-
const codePointNonBreakSpace = 160
51+
if (char.codePointAt(1)) return false
5252

53+
const codePointNonBreakSpace = 160
5354
const codePoint = codePointOf(char)
5455

5556
if (codePoint < codePointOf('!')) return false
@@ -60,6 +61,8 @@ const validChar = (char: string): boolean => {
6061
if (codePoint <= codePointOf('~')) return true
6162
if (codePoint < codePointNonBreakSpace) return false
6263

64+
if (char.codePointAt(1)) return false
65+
6366
return true
6467
}
6568

src/lib/puid.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ test('dîngøsky chars (count power of 2 with carry)', (t) => {
183183
t.is(dingoskyUtf8Id(), 'ksk')
184184
})
185185

186+
test('dîngøsky:₩ chars', (t) => {
187+
const dingoskyDogBytes = fixedBytes([
188+
0xec, 0xf9, 0xdb, 0x7a, 0x33, 0x3d, 0x21, 0x97, 0xa0, 0xc2, 0xbf, 0x92, 0x80, 0xdd, 0x2f, 0x57, 0x12, 0xc1, 0x1a,
189+
0xef
190+
])
191+
const dingoskyDogId = puidGenerator({ bits: 24, chars: 'dîngøsky:₩', entropyBytes: dingoskyDogBytes })
192+
193+
t.is(dingoskyDogId(), '₩gî₩₩nî₩')
194+
t.is(dingoskyDogId(), 'ydkîsnsd')
195+
t.is(dingoskyDogId(), 'îøsîndøk')
196+
})
186197

187198
test('Safe32 (count non-power of 2 with carry)', (t) => {
188199
// D 2 E 3 E 9 D A 1 9 0 3 B 7 3 C

0 commit comments

Comments
 (0)