Skip to content

Commit 418bda9

Browse files
adriengibratAdrien Gibrat
andauthored
feat: add alphaLower and basicDomain regex (#600)
Co-authored-by: Adrien Gibrat <[email protected]>
1 parent cb16d67 commit 418bda9

File tree

2 files changed

+78
-25
lines changed

2 files changed

+78
-25
lines changed

packages/regex/src/__tests__/index.ts

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
absoluteLinuxPath,
33
alpha,
4+
alphaLower,
45
alphanum,
56
alphanumLowercase,
67
alphanumSpacesDotsUnderscoreDash,
@@ -15,6 +16,7 @@ import {
1516
alphanumdots,
1617
ascii,
1718
backupKey,
19+
basicDomain,
1820
cron,
1921
digits,
2022
email,
@@ -33,6 +35,9 @@ const asciiLetters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
3335
const asciiLowercase = 'abcdefghijklmnopqrstuvwxyz'
3436
const asciiUppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
3537
const backupKeyTest = '123456789ABCEDFGHIJIKLMNOPQRSTUV'
38+
const domain = 'another-example.com'
39+
const subDomain = 'sub.another-example.com'
40+
const longTldDomain = 'sub.another-example.verylongtld'
3641
const cronTest = '0/15*-'
3742
const digitsTest = '0123456789'
3843
const emailTest = '[email protected]'
@@ -77,11 +82,32 @@ describe('@regex', () => {
7782
[whitespace, false],
7883
[cronTest, false],
7984
[macAddress1, false],
80-
])('should match regex %s to be %s)', (string, expected) => {
85+
])('should match regex %s to be %s', (string, expected) => {
8186
expect(alpha.test(string)).toBe(expected)
8287
})
8388
})
8489

90+
describe('alphaLower', () => {
91+
test.each([
92+
[alphanumdashText, false],
93+
[alphanumdashdotsText, false],
94+
[asciiLetters, false],
95+
[asciiLowercase, true],
96+
[asciiUppercase, false],
97+
[digitsTest, false],
98+
[emailTest, false],
99+
[octdigits, false],
100+
[hexdigits, false],
101+
[printable, false],
102+
[punctuation, false],
103+
[whitespace, false],
104+
[cronTest, false],
105+
[macAddress1, false],
106+
])('should match regex %s to be %s', (string, expected) => {
107+
expect(alphaLower.test(string)).toBe(expected)
108+
})
109+
})
110+
85111
describe('alphanum', () => {
86112
test.each([
87113
[alphanumdashText, false],
@@ -98,7 +124,7 @@ describe('@regex', () => {
98124
[whitespace, false],
99125
[cronTest, false],
100126
[macAddress1, false],
101-
])('should match regex %s to be %s)', (string, expected) => {
127+
])('should match regex %s to be %s', (string, expected) => {
102128
expect(alphanum.test(string)).toBe(expected)
103129
})
104130
})
@@ -119,7 +145,7 @@ describe('@regex', () => {
119145
[whitespace, false],
120146
[cronTest, false],
121147
[macAddress1, false],
122-
])('should match regex %s to be %s)', (string, expected) => {
148+
])('should match regex %s to be %s', (string, expected) => {
123149
expect(alphanumdash.test(string)).toBe(expected)
124150
})
125151
})
@@ -140,7 +166,7 @@ describe('@regex', () => {
140166
[whitespace, false],
141167
[cronTest, false],
142168
[macAddress1, false],
143-
])('should match regex %s to be %s)', (string, expected) => {
169+
])('should match regex %s to be %s', (string, expected) => {
144170
expect(alphanumdashdots.test(string)).toBe(expected)
145171
})
146172
})
@@ -161,7 +187,7 @@ describe('@regex', () => {
161187
[whitespace, false],
162188
[cronTest, false],
163189
[macAddress1, false],
164-
])('should match regex %s to be %s)', (string, expected) => {
190+
])('should match regex %s to be %s', (string, expected) => {
165191
expect(alphanumdashdotsorempty.test(string)).toBe(expected)
166192
})
167193
})
@@ -182,7 +208,7 @@ describe('@regex', () => {
182208
[whitespace, true],
183209
[cronTest, false],
184210
[macAddress1, false],
185-
])('should match regex %s to be %s)', (string, expected) => {
211+
])('should match regex %s to be %s', (string, expected) => {
186212
expect(alphanumdashdotsspaces.test(string)).toBe(expected)
187213
})
188214
})
@@ -203,7 +229,7 @@ describe('@regex', () => {
203229
[whitespace, false],
204230
[cronTest, false],
205231
[macAddress1, false],
206-
])('should match regex %s to be %s)', (string, expected) => {
232+
])('should match regex %s to be %s', (string, expected) => {
207233
expect(alphanumdashorempty.test(string)).toBe(expected)
208234
})
209235
})
@@ -224,7 +250,7 @@ describe('@regex', () => {
224250
[whitespace, true],
225251
[cronTest, false],
226252
[macAddress1, false],
227-
])('should match regex %s to be %s)', (string, expected) => {
253+
])('should match regex %s to be %s', (string, expected) => {
228254
expect(alphanumdashspaces.test(string)).toBe(expected)
229255
})
230256
})
@@ -245,7 +271,7 @@ describe('@regex', () => {
245271
[whitespace, false],
246272
[cronTest, false],
247273
[macAddress1, false],
248-
])('should match regex %s to be %s)', (string, expected) => {
274+
])('should match regex %s to be %s', (string, expected) => {
249275
expect(alphanumdots.test(string)).toBe(expected)
250276
})
251277
})
@@ -264,7 +290,7 @@ describe('@regex', () => {
264290
[whitespace, false],
265291
[cronTest, false],
266292
[macAddress1, false],
267-
])('should match regex %s to be %s)', (string, expected) => {
293+
])('should match regex %s to be %s', (string, expected) => {
268294
expect(alphanumLowercase.test(string)).toBe(expected)
269295
})
270296
})
@@ -283,7 +309,7 @@ describe('@regex', () => {
283309
[whitespace, true],
284310
[cronTest, false],
285311
[macAddress1, false],
286-
])('should match regex %s to be %s)', (string, expected) => {
312+
])('should match regex %s to be %s', (string, expected) => {
287313
expect(alphanumSpacesDotsUnderscoreDash.test(string)).toBe(expected)
288314
})
289315
})
@@ -302,7 +328,7 @@ describe('@regex', () => {
302328
[whitespace, false],
303329
[cronTest, false],
304330
[macAddress1, false],
305-
])('should match regex %s to be %s)', (string, expected) => {
331+
])('should match regex %s to be %s', (string, expected) => {
306332
expect(alphanumUnderscoreDash.test(string)).toBe(expected)
307333
})
308334
})
@@ -321,7 +347,7 @@ describe('@regex', () => {
321347
[whitespace, false],
322348
[cronTest, false],
323349
[macAddress1, false],
324-
])('should match regex %s to be %s)', (string, expected) => {
350+
])('should match regex %s to be %s', (string, expected) => {
325351
expect(alphanumUnderscoreDollarDash.test(string)).toBe(expected)
326352
})
327353
})
@@ -352,7 +378,7 @@ describe('@regex', () => {
352378
[punctuation, true],
353379
[whitespace, true],
354380
[cronTest, true],
355-
])('should match regex %s to be %s)', (string, expected) => {
381+
])('should match regex %s to be %s', (string, expected) => {
356382
expect(ascii.test(string)).toBe(expected)
357383
})
358384
})
@@ -372,10 +398,35 @@ describe('@regex', () => {
372398
[whitespace, false],
373399
[cronTest, false],
374400
[macAddress1, false],
375-
])('should match regex %s to be %s)', (string, expected) => {
401+
])('should match regex %s to be %s', (string, expected) => {
376402
expect(backupKey.test(string)).toBe(expected)
377403
})
378404
})
405+
describe('basicDomain', () => {
406+
test.each([
407+
[asciiLetters, false],
408+
[asciiLowercase, false],
409+
[asciiUppercase, false],
410+
[backupKeyTest, false],
411+
[domain, true],
412+
[subDomain, true],
413+
[longTldDomain, true],
414+
[digitsTest, false],
415+
[emailTest, false],
416+
[octdigits, false],
417+
[hexdigits, false],
418+
[printable, false],
419+
[punctuation, false],
420+
[whitespace, false],
421+
[cronTest, false],
422+
[macAddress1, false],
423+
[url1, false],
424+
[url2, false],
425+
])('should match regex %s to be %s', (string, expected) => {
426+
expect(basicDomain.test(string)).toBe(expected)
427+
})
428+
})
429+
379430

380431
describe('cron', () => {
381432
test.each([
@@ -390,7 +441,7 @@ describe('@regex', () => {
390441
[punctuation, false],
391442
[whitespace, false],
392443
[cronTest, true],
393-
])('should match regex %s to be %s)', (string, expected) => {
444+
])('should match regex %s to be %s', (string, expected) => {
394445
expect(cron.test(string)).toBe(expected)
395446
})
396447
})
@@ -409,7 +460,7 @@ describe('@regex', () => {
409460
[whitespace, false],
410461
[cronTest, false],
411462
[macAddress1, false],
412-
])('should match regex %s to be %s)', (string, expected) => {
463+
])('should match regex %s to be %s', (string, expected) => {
413464
expect(digits.test(string)).toBe(expected)
414465
})
415466
})
@@ -428,7 +479,7 @@ describe('@regex', () => {
428479
[whitespace, false],
429480
[cronTest, false],
430481
[macAddress1, false],
431-
])('should match regex %s to be %s)', (string, expected) => {
482+
])('should match regex %s to be %s', (string, expected) => {
432483
expect(email.test(string)).toBe(expected)
433484
})
434485
})
@@ -448,7 +499,7 @@ describe('@regex', () => {
448499
[whitespace, false],
449500
[cronTest, false],
450501
[macAddress1, false],
451-
])('should match regex %s to be %s)', (string, expected) => {
502+
])('should match regex %s to be %s', (string, expected) => {
452503
expect(fourDigitsCode.test(string)).toBe(expected)
453504
})
454505
})
@@ -468,7 +519,7 @@ describe('@regex', () => {
468519
[whitespace, false],
469520
[cronTest, false],
470521
[macAddress1, true],
471-
])('should match regex %s to be %s)', (string, expected) => {
522+
])('should match regex %s to be %s', (string, expected) => {
472523
expect(macAddress.test(string)).toBe(expected)
473524
})
474525
})
@@ -489,7 +540,7 @@ describe('@regex', () => {
489540
[whitespace, false],
490541
[cronTest, false],
491542
[macAddress1, false],
492-
])('should match regex %s to be %s)', (string, expected) => {
543+
])('should match regex %s to be %s', (string, expected) => {
493544
expect(phone.test(string)).toBe(expected)
494545
})
495546
})
@@ -510,7 +561,7 @@ describe('@regex', () => {
510561
[whitespace, true],
511562
[cronTest, false],
512563
[macAddress1, false],
513-
])('should match regex %s to be %s)', (string, expected) => {
564+
])('should match regex %s to be %s', (string, expected) => {
514565
expect(spaces.test(string)).toBe(expected)
515566
})
516567
})
@@ -532,7 +583,7 @@ describe('@regex', () => {
532583
[whitespace, false],
533584
[cronTest, false],
534585
[macAddress1, false],
535-
])('should match regex %s to be %s)', (string, expected) => {
586+
])('should match regex %s to be %s', (string, expected) => {
536587
expect(sixDigitsCode.test(string)).toBe(expected)
537588
})
538589
})
@@ -556,7 +607,7 @@ describe('@regex', () => {
556607
[macAddress1, false],
557608
[url1, true],
558609
[url2, true],
559-
])('should match regex %s to be %s)', (string, expected) => {
610+
])('should match regex %s to be %s', (string, expected) => {
560611
expect(url.test(string)).toBe(expected)
561612
})
562613
})
@@ -577,7 +628,7 @@ describe('@regex', () => {
577628
[whitespace, false],
578629
[cronTest, false],
579630
[hexdigits, true],
580-
])('should match regex %s to be %s)', (string, expected) => {
631+
])('should match regex %s to be %s', (string, expected) => {
581632
expect(hexadecimal.test(string)).toBe(expected)
582633
})
583634
})

packages/regex/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const alpha = /^[a-zA-Z]*$/
2+
export const alphaLower = /^[a-z]+$/
23
export const alphanum = /^[a-zA-Z0-9]*$/
34
export const alphanumdash = /^[a-zA-Z0-9-]*$/
45
export const alphanumdashdots = /^[a-zA-Z0-9-.]*$/
@@ -16,6 +17,7 @@ export const absoluteLinuxPath = /(^\/$|^(\/[a-zA-Z0-9_]+)*$)/
1617
// eslint-disable-next-line no-control-regex
1718
export const ascii = /^[\x00-\x7F]+$/
1819
export const backupKey = /^[A-Z0-9]{32}$/
20+
export const basicDomain = /^[a-z0-9-]+(\.[a-z0-9-]{1,63})+$/
1921
export const cron = /^[0-9,/*-]+$/
2022
export const digits = /^[0-9]*$/
2123
export const macAddress =

0 commit comments

Comments
 (0)