Skip to content

Commit 054308f

Browse files
committed
updates: tests for utils
1 parent f5949f8 commit 054308f

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

src/utils.test.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import { strict as assert } from 'node:assert'
22
import { suite, test } from 'node:test'
33

4-
import { validatedTld } from './utils.ts'
4+
import { isDomain, validatedTld } from './utils.ts'
5+
6+
suite('isDomain()', () => {
7+
test('invalid domains', function () {
8+
assert.equal(false, isDomain(''), 'empty')
9+
assert.equal(false, isDomain('c'), 'too short')
10+
assert.equal(false, isDomain('c om'), 'contains space')
11+
assert.equal(false, isDomain('domain'), 'no tld')
12+
assert.equal(false, isDomain('-example.com'), 'starts with hyphen')
13+
assert.equal(false, isDomain('example.com1'), 'tld contains number')
14+
assert.throws(() => validatedTld('clearlyinvalidtldbecasuethisistoooooooooooooloooooooooooooooooong.com'), 'too long')
15+
})
16+
17+
test('valid domain string', function () {
18+
assert.ok(isDomain('example.com'))
19+
assert.ok(isDomain('.example.com.'))
20+
assert.ok(isDomain('youtu.be'))
21+
assert.ok(isDomain('blog.google'))
22+
})
23+
})
524

625
suite('validatedTld()', () => {
7-
test('invalid TLDs', function() {
26+
test('invalid TLDs', function () {
827
assert.throws(() => validatedTld(''), 'empty')
928
assert.throws(() => validatedTld('c'), 'too short')
1029
assert.throws(() => validatedTld('c om'), 'contains space')
@@ -15,18 +34,18 @@ suite('validatedTld()', () => {
1534
assert.throws(() => validatedTld('clearlyinvalidtldbecasuethisistoooooooooooooloooooooooooooooooong'), 'too long')
1635
})
1736

18-
test('valid - TLDs', function() {
37+
test('valid - TLDs', function () {
1938
assert.equal(validatedTld('.com'), 'com')
2039
assert.equal(validatedTld('org.'), 'org')
2140
assert.equal(validatedTld('AI'), 'ai')
2241
assert.equal(validatedTld('.nyc'), 'nyc')
2342
})
2443

25-
test('valid - SLDs', function() {
44+
test('valid - SLDs', function () {
2645
assert.equal(validatedTld('.co.uk'), 'co.uk')
2746
})
2847

29-
test('valid - IDN', function() {
48+
test('valid - IDN', function () {
3049
assert.equal(validatedTld('xn--zckzah'), 'xn--zckzah')
3150
assert.equal(validatedTld('テスト'), 'テスト')
3251
assert.equal(validatedTld('.香港'), '香港')

src/utils.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { toASCII } from 'punycode-esm'
22

3+
/**
4+
* Split a string into two parts at a given index.
5+
*
6+
* @param string The string to split.
7+
* @param by The index at which to split the string.
8+
* @returns An array containing the two parts of the string.
9+
*/
310
export function splitStringBy(string: string, by: number): [string, string] {
411
return [string.slice(0, by), string.slice(by + 1)]
512
}
613

714
/**
815
* Check if a string is a valid TLD, and return it in canonical form.
9-
*
10-
* @param tld
16+
*
17+
* @param tld
1118
* @returns The normalized TLD
1219
* @throws If the TLD is invalid
1320
*/
@@ -23,17 +30,20 @@ export function validatedTld(tld: string): string {
2330
}
2431

2532
const labelTest = /^([a-z]{2,64}|xn[a-z0-9-]{5,})$/
26-
const labels = tld.split('.').map(label => toASCII(label))
33+
const labels = tld.split('.').map((label) => toASCII(label))
2734

28-
if (!labels.every(label => labelTest.test(label))) {
35+
if (!labels.every((label) => labelTest.test(label))) {
2936
throw new Error(`Invalid TLD "${tld}"`)
3037
}
3138

3239
//return labels.join('.')
3340
return tld
3441
}
3542

36-
export function isTld(tld: string): boolean {
43+
/**
44+
* Check if a string is a valid TLD format
45+
*/
46+
function isTld(tld: string): boolean {
3747
if (tld.startsWith('.')) {
3848
tld = tld.substring(1)
3949
}
@@ -42,9 +52,17 @@ export function isTld(tld: string): boolean {
4252
return /^([a-z]{2,64}|xn[a-z0-9-]{5,})$/i.test(toASCII(tld))
4353
}
4454

55+
/**
56+
* Check if a string is a valid domain format
57+
* @param domain The domain to check.
58+
*/
4559
export function isDomain(domain: string): boolean {
60+
if (domain.startsWith('.')) {
61+
domain = domain.slice(1)
62+
}
63+
4664
if (domain.endsWith('.')) {
47-
domain = domain.substring(0, domain.length - 1)
65+
domain = domain.slice(0, -1)
4866
}
4967

5068
const labels = toASCII(domain).split('.').reverse()

0 commit comments

Comments
 (0)