Skip to content

Commit f271d72

Browse files
feat: add absolute path regex (#514)
1 parent 434d03e commit f271d72

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

packages/regex/src/__tests__/index.ts

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
absoluteLinuxPath,
23
alpha,
34
alphanum,
45
alphanumLowercase,
@@ -47,6 +48,17 @@ const url1 = 'http://console.scaleway.com'
4748
const url2 = 'https://www.scaleway.com'
4849
const whitespace = ' \t\n\r\x0b\x0c'
4950
const macAddress1 = '1F:B5:FA:47:CD:C4'
51+
const linuxPaths = {
52+
BAD: [
53+
'/var/test@',
54+
'/var/test/',
55+
'/var/test@',
56+
'/var//test',
57+
'//',
58+
'/var/test-',
59+
],
60+
GOOD: ['/var', '/var/test', '/var/test_', '/var_/test', '/'],
61+
}
5062

5163
describe('@regex', () => {
5264
describe('alpha', () => {
@@ -87,7 +99,7 @@ describe('@regex', () => {
8799
[cronTest, false],
88100
[macAddress1, false],
89101
])('should match regex %s to be %s)', (string, expected) => {
90-
expect(alphanum.test(string)).toBe(expected)
102+
expect(alphanum.test(string)).toBe(expected)
91103
})
92104
})
93105

@@ -108,7 +120,7 @@ describe('@regex', () => {
108120
[cronTest, false],
109121
[macAddress1, false],
110122
])('should match regex %s to be %s)', (string, expected) => {
111-
expect(alphanumdash.test(string)).toBe(expected)
123+
expect(alphanumdash.test(string)).toBe(expected)
112124
})
113125
})
114126

@@ -129,7 +141,7 @@ describe('@regex', () => {
129141
[cronTest, false],
130142
[macAddress1, false],
131143
])('should match regex %s to be %s)', (string, expected) => {
132-
expect(alphanumdashdots.test(string)).toBe(expected)
144+
expect(alphanumdashdots.test(string)).toBe(expected)
133145
})
134146
})
135147

@@ -150,7 +162,7 @@ describe('@regex', () => {
150162
[cronTest, false],
151163
[macAddress1, false],
152164
])('should match regex %s to be %s)', (string, expected) => {
153-
expect(alphanumdashdotsorempty.test(string)).toBe(expected)
165+
expect(alphanumdashdotsorempty.test(string)).toBe(expected)
154166
})
155167
})
156168

@@ -171,7 +183,7 @@ describe('@regex', () => {
171183
[cronTest, false],
172184
[macAddress1, false],
173185
])('should match regex %s to be %s)', (string, expected) => {
174-
expect(alphanumdashdotsspaces.test(string)).toBe(expected)
186+
expect(alphanumdashdotsspaces.test(string)).toBe(expected)
175187
})
176188
})
177189

@@ -192,7 +204,7 @@ describe('@regex', () => {
192204
[cronTest, false],
193205
[macAddress1, false],
194206
])('should match regex %s to be %s)', (string, expected) => {
195-
expect(alphanumdashorempty.test(string)).toBe(expected)
207+
expect(alphanumdashorempty.test(string)).toBe(expected)
196208
})
197209
})
198210

@@ -213,7 +225,7 @@ describe('@regex', () => {
213225
[cronTest, false],
214226
[macAddress1, false],
215227
])('should match regex %s to be %s)', (string, expected) => {
216-
expect(alphanumdashspaces.test(string)).toBe(expected)
228+
expect(alphanumdashspaces.test(string)).toBe(expected)
217229
})
218230
})
219231

@@ -234,7 +246,7 @@ describe('@regex', () => {
234246
[cronTest, false],
235247
[macAddress1, false],
236248
])('should match regex %s to be %s)', (string, expected) => {
237-
expect(alphanumdots.test(string)).toBe(expected)
249+
expect(alphanumdots.test(string)).toBe(expected)
238250
})
239251
})
240252

@@ -253,7 +265,7 @@ describe('@regex', () => {
253265
[cronTest, false],
254266
[macAddress1, false],
255267
])('should match regex %s to be %s)', (string, expected) => {
256-
expect(alphanumLowercase.test(string)).toBe(expected)
268+
expect(alphanumLowercase.test(string)).toBe(expected)
257269
})
258270
})
259271

@@ -272,7 +284,7 @@ describe('@regex', () => {
272284
[cronTest, false],
273285
[macAddress1, false],
274286
])('should match regex %s to be %s)', (string, expected) => {
275-
expect(alphanumSpacesDotsUnderscoreDash.test(string)).toBe(expected)
287+
expect(alphanumSpacesDotsUnderscoreDash.test(string)).toBe(expected)
276288
})
277289
})
278290

@@ -291,7 +303,7 @@ describe('@regex', () => {
291303
[cronTest, false],
292304
[macAddress1, false],
293305
])('should match regex %s to be %s)', (string, expected) => {
294-
expect(alphanumUnderscoreDash.test(string)).toBe(expected)
306+
expect(alphanumUnderscoreDash.test(string)).toBe(expected)
295307
})
296308
})
297309

@@ -310,7 +322,20 @@ describe('@regex', () => {
310322
[cronTest, false],
311323
[macAddress1, false],
312324
])('should match regex %s to be %s)', (string, expected) => {
313-
expect(alphanumUnderscoreDollarDash.test(string)).toBe(expected)
325+
expect(alphanumUnderscoreDollarDash.test(string)).toBe(expected)
326+
})
327+
})
328+
329+
describe('absoluteLinuxPath', () => {
330+
test.each([
331+
...linuxPaths.GOOD.map(
332+
(testStr: string) => [testStr, true] as [string, boolean],
333+
),
334+
...linuxPaths.BAD.map(
335+
(testStr: string) => [testStr, false] as [string, boolean],
336+
),
337+
])('should match regex %s to be %s', (string, expected) => {
338+
expect(absoluteLinuxPath.test(string)).toBe(expected)
314339
})
315340
})
316341

@@ -328,7 +353,7 @@ describe('@regex', () => {
328353
[whitespace, true],
329354
[cronTest, true],
330355
])('should match regex %s to be %s)', (string, expected) => {
331-
expect(ascii.test(string)).toBe(expected)
356+
expect(ascii.test(string)).toBe(expected)
332357
})
333358
})
334359

@@ -348,7 +373,7 @@ describe('@regex', () => {
348373
[cronTest, false],
349374
[macAddress1, false],
350375
])('should match regex %s to be %s)', (string, expected) => {
351-
expect(backupKey.test(string)).toBe(expected)
376+
expect(backupKey.test(string)).toBe(expected)
352377
})
353378
})
354379

@@ -366,7 +391,7 @@ describe('@regex', () => {
366391
[whitespace, false],
367392
[cronTest, true],
368393
])('should match regex %s to be %s)', (string, expected) => {
369-
expect(cron.test(string)).toBe(expected)
394+
expect(cron.test(string)).toBe(expected)
370395
})
371396
})
372397

@@ -385,7 +410,7 @@ describe('@regex', () => {
385410
[cronTest, false],
386411
[macAddress1, false],
387412
])('should match regex %s to be %s)', (string, expected) => {
388-
expect(digits.test(string)).toBe(expected)
413+
expect(digits.test(string)).toBe(expected)
389414
})
390415
})
391416

@@ -404,7 +429,7 @@ describe('@regex', () => {
404429
[cronTest, false],
405430
[macAddress1, false],
406431
])('should match regex %s to be %s)', (string, expected) => {
407-
expect(email.test(string)).toBe(expected)
432+
expect(email.test(string)).toBe(expected)
408433
})
409434
})
410435

@@ -424,7 +449,7 @@ describe('@regex', () => {
424449
[cronTest, false],
425450
[macAddress1, false],
426451
])('should match regex %s to be %s)', (string, expected) => {
427-
expect(fourDigitsCode.test(string)).toBe(expected)
452+
expect(fourDigitsCode.test(string)).toBe(expected)
428453
})
429454
})
430455

@@ -444,7 +469,7 @@ describe('@regex', () => {
444469
[cronTest, false],
445470
[macAddress1, true],
446471
])('should match regex %s to be %s)', (string, expected) => {
447-
expect(macAddress.test(string)).toBe(expected)
472+
expect(macAddress.test(string)).toBe(expected)
448473
})
449474
})
450475

@@ -465,7 +490,7 @@ describe('@regex', () => {
465490
[cronTest, false],
466491
[macAddress1, false],
467492
])('should match regex %s to be %s)', (string, expected) => {
468-
expect(phone.test(string)).toBe(expected)
493+
expect(phone.test(string)).toBe(expected)
469494
})
470495
})
471496

@@ -486,7 +511,7 @@ describe('@regex', () => {
486511
[cronTest, false],
487512
[macAddress1, false],
488513
])('should match regex %s to be %s)', (string, expected) => {
489-
expect(spaces.test(string)).toBe(expected)
514+
expect(spaces.test(string)).toBe(expected)
490515
})
491516
})
492517

@@ -508,7 +533,7 @@ describe('@regex', () => {
508533
[cronTest, false],
509534
[macAddress1, false],
510535
])('should match regex %s to be %s)', (string, expected) => {
511-
expect(sixDigitsCode.test(string)).toBe(expected)
536+
expect(sixDigitsCode.test(string)).toBe(expected)
512537
})
513538
})
514539

@@ -532,7 +557,7 @@ describe('@regex', () => {
532557
[url1, true],
533558
[url2, true],
534559
])('should match regex %s to be %s)', (string, expected) => {
535-
expect(url.test(string)).toBe(expected)
560+
expect(url.test(string)).toBe(expected)
536561
})
537562
})
538563

@@ -553,7 +578,7 @@ describe('@regex', () => {
553578
[cronTest, false],
554579
[hexdigits, true],
555580
])('should match regex %s to be %s)', (string, expected) => {
556-
expect(hexadecimal.test(string)).toBe(expected)
581+
expect(hexadecimal.test(string)).toBe(expected)
557582
})
558583
})
559584
})

packages/regex/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ export const alphanumLowercase = /^[a-z0-9]+$/
1111
export const alphanumSpacesDotsUnderscoreDash = /^[a-zA-Z0-9-.\s_]*$/
1212
export const alphanumUnderscoreDash = /^[a-zA-Z0-9_-]*$/
1313
export const alphanumUnderscoreDollarDash = /^[a-zA-Z0-9_$-]*$/
14+
export const absoluteLinuxPath = /(^\/$|^(\/[a-zA-Z0-9_]+)*$)/
15+
1416
// eslint-disable-next-line no-control-regex
1517
export const ascii = /^[\x00-\x7F]+$/
1618
export const backupKey = /^[A-Z0-9]{32}$/
1719
export const cron = /^[0-9,/*-]+$/
1820
export const digits = /^[0-9]*$/
19-
export const macAddress = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/
21+
export const macAddress =
22+
/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/
2023
// Used by W3C
21-
export const email = /^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
24+
export const email =
25+
/^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
2226
export const fourDigitsCode = /^[0-9]{4}$/
2327
export const phone = /^\+[0-9]*/
2428
export const spaces = /^\s*$/
2529
export const sixDigitsCode = /^[0-9]{6}$/
26-
export const url = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
30+
export const url =
31+
/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
2732
export const hexadecimal = /^[0-9a-fA-F]+$/

0 commit comments

Comments
 (0)