forked from adraffy/ens-normalize.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
executable file
·40 lines (34 loc) · 1.13 KB
/
validate.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
40
import {ens_normalize, ens_tokenize, nfc} from '../src/lib.js';
//import {ens_normalize, ens_tokenize, nfc} from '../../ens-norm-tests/old-versions/1.8.9.js';
import {str_from_cps, run_tests} from '../src/utils.js';
import {readFileSync} from 'node:fs';
let file = process.argv[2];
if (!file) file = new URL('../validate/tests.json', import.meta.url).pathname;
console.log(`Testing: ${file}`);
const TESTS = JSON.parse(readFileSync(file));
test(ens_normalize);
test(ens_normalize_via_tokenize);
// proof of concept
function ens_normalize_via_tokenize(name) {
let norm = str_from_cps(nfc(ens_tokenize(name).flatMap(token => {
switch (token.type) {
case 'disallowed': throw new Error('disallowed');
case 'ignored': return [];
case 'stop': return token.cp;
default: return token.cps;
}
})));
if (ens_normalize(norm) !== norm) {
throw new Error(`wrong: ${norm}`);
}
return norm;
}
function test(fn) {
let errors = run_tests(fn, TESTS);
if (errors.length) {
console.log(errors);
console.log(`Errors: ${errors.length}`);
throw new Error(fn.name);
}
console.log(`PASS ${fn.name}`);
}