-
Notifications
You must be signed in to change notification settings - Fork 17
/
fragment.js
executable file
·39 lines (36 loc) · 1.4 KB
/
fragment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {ens_normalize, ens_normalize_fragment} from '../src/lib.js';
// examples where fragment (2nd argument)
// would error with ens_normalize()
test('1234--abcd', '34--A'); // error: invalid label extension
test('e\u{303}', '\u{303}'); // error: illegal placement: leading combining mark
test('oooooooooooo', 'OΟО'); // error: illegal mixture: Latin + Greek "ο" {3BF}
test('__a-b-c___', 'a_b_c'); // error: underscore allowed only at start
function name_contains_fragment(name, frag) {
// convert the name to NFD because:
// è = E8 => 65 300
// note that:
// "è".includes("e") => false
// "è".includes("◌̀") => false
// name_contains_fragment("è", "e") => true
// name_contains_fragment("è", "◌̀") => true
// if instead, you want exact codepoint matching:
// use ens_normalize_fragment() without the second argument
try {
// suggestion: name should be cached if testing many fragments
return ens_normalize_fragment(name, true).includes(ens_normalize_fragment(frag, true));
} catch (ignored) {
}
}
function test(name, frag) {
// check frag fails normalize
let error;
try {
ens_normalize(frag);
} catch (err) {
error = err.message;
}
if (!error) throw new Error(`expected frag shouldn't normalize: ${frag}`);
// check containment
let contains = name_contains_fragment(name, frag);
console.log({name, frag, contains, error});
}