| 1 |
#!/usr/bin/env node |
| 2 |
|
| 3 |
// Generates list of 2-char english tlds. |
| 4 |
// |
| 5 |
// Code is dirty, i know, but it's needed only once |
| 6 |
// |
| 7 |
'use strict'; |
| 8 |
|
| 9 |
|
| 10 |
/*eslint-disable no-console*/ |
| 11 |
/*eslint-env es6*/ |
| 12 |
|
| 13 |
function toRanges(str) { |
| 14 |
var ranges = [], i; |
| 15 |
|
| 16 |
str = str.slice(1, -1); |
| 17 |
|
| 18 |
while (str.length) { |
| 19 |
for (i = 1; ; i++) { |
| 20 |
if (str[i] !== String.fromCharCode(str[i - 1].charCodeAt(0) + 1)) { |
| 21 |
if (i < 3) { |
| 22 |
ranges.push(str.slice(0, i)); |
| 23 |
} else { |
| 24 |
ranges.push(str[0] + '-' + str[i - 1]); |
| 25 |
} |
| 26 |
str = str.slice(i); |
| 27 |
break; |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
return '[' + ranges.join('') + ']'; |
| 32 |
} |
| 33 |
|
| 34 |
var tlds = require('tlds').filter(name => /^[a-z]{2}$/.test(name)).sort(); |
| 35 |
|
| 36 |
// |
| 37 |
// group by first letter |
| 38 |
// |
| 39 |
|
| 40 |
var result = []; |
| 41 |
|
| 42 |
'abcdefghijklmnopqrstuvwxyz'.split('').forEach(letter => { |
| 43 |
var list = tlds.filter(name => name[0] === letter); |
| 44 |
|
| 45 |
if (!list.length) { return; } |
| 46 |
|
| 47 |
if (list.length < 2) { |
| 48 |
result = result.concat(list); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
result.push(letter + '[' + list.map(n => n[1]).join('') + ']'); |
| 53 |
}); |
| 54 |
|
| 55 |
result = result.join('|'); |
| 56 |
|
| 57 |
console.log(result); |
| 58 |
|
| 59 |
// |
| 60 |
// Compact ranges |
| 61 |
// |
| 62 |
|
| 63 |
result = result.replace(/\[[a-z]+\]/g, toRanges); |
| 64 |
|
| 65 |
// console.log(result); |