Skip to content

Commit 47485c0

Browse files
committed
Added file-extension utility
1 parent 471f570 commit 47485c0

File tree

9 files changed

+85
-19
lines changed

9 files changed

+85
-19
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ This package includes various categorised validators which can be helppful for v
3939
13. **Social Insurance Number (SIN)**: For Canada or equivalent in other countries. (Upcoming)
4040

4141
### Formats and Patterns:
42-
14. **File Extensions**: Validate file names/extensions (e.g., `.jpg`, `.pdf`). (Upcoming)
42+
14. **File Extensions**: Validate file names/extensions (e.g., `.jpg`, `.pdf`).
4343

4444
### Additional Utility:
45-
15. **Boolean Strings**: Validate "true", "false", "1", "0", etc. (Upcoming)
46-
16. **Hex Color Codes**: Validate `#RRGGBB` or `#RGB` color formats.
47-
17. **EAN/UPC**: Validate international product codes. (Upcoming)
45+
15. **Hex Color Codes**: Validate `#RRGGBB` or `#RGB` color formats.
46+
16. **EAN/UPC**: Validate international product codes. (Upcoming)

USAGE_DETAILS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,14 @@ import { isValid_URL } from 'validate-functions/url';
8585
const urlWithQuery = 'http://example.com?name=value';
8686
console.log(isValid_URL(urlWithQuery)); // Output: true
8787
```
88+
89+
- **isValid_File_Extension**
90+
91+
```typescript
92+
import { isValid_File_Extension } from 'validate-functions/file-extension';
93+
94+
// Example of checking a file extension
95+
const fileName = 'document.pdf';
96+
const allowedExtensions = ['pdf', 'doc', 'txt'];
97+
console.log(isValid_File_Extension(fileName, allowedExtensions)); // Output: true if the extension is valid, false otherwise
98+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { isValid_File_Extension } from '../file-extension';
2+
3+
describe('File Extension Validation', () => {
4+
it('should return true for a valid file extension', () => {
5+
const fileName = 'document.pdf';
6+
const allowedExtensions = ['pdf', 'doc', 'txt'];
7+
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(true);
8+
});
9+
10+
it('should return false for an invalid file extension', () => {
11+
const fileName = 'image.jpeg';
12+
const allowedExtensions = ['pdf', 'doc', 'txt'];
13+
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(false);
14+
});
15+
16+
it('should return false for a file with no extension', () => {
17+
const fileName = 'file';
18+
const allowedExtensions = ['pdf', 'doc', 'txt'];
19+
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(false);
20+
});
21+
22+
it('should return true for a valid file extension with mixed case', () => {
23+
const fileName = 'presentation.PPT';
24+
const allowedExtensions = ['ppt', 'doc', 'txt'];
25+
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(true);
26+
});
27+
28+
it('should return false for an empty file name', () => {
29+
const fileName = '';
30+
const allowedExtensions = ['pdf', 'doc', 'txt'];
31+
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(false);
32+
});
33+
34+
it('should return false for an empty allowed extensions list', () => {
35+
const fileName = 'document.pdf';
36+
const allowedExtensions: string[] = [];
37+
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(false);
38+
});
39+
});

src/__tests__/postal-zip.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ describe('Postal Code Validation', () => {
256256
});
257257

258258
describe('Invalid Country Code', () => {
259-
it('should return false for an unsupported country code', () => {
260-
expect(isValid_Postal_Code('awea23s', 'UN' as CountryCode)).toBe(false);
261-
});
262-
});
259+
it('should return false for an unsupported country code', () => {
260+
expect(isValid_Postal_Code('awea23s', 'UN' as CountryCode)).toBe(false);
261+
});
262+
});
263263
});

src/file-extension.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
*
3+
* @param fileName
4+
* @param allowedExtensions
5+
* @returns
6+
*/
7+
export const isValid_File_Extension = (
8+
fileName: string,
9+
allowedExtensions: string[]
10+
): boolean => {
11+
const extension = fileName.split('.').pop()?.toLowerCase() || '';
12+
return allowedExtensions.includes(extension);
13+
};

src/hex-color.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/**
22
* A hex code, short for hexadecimal code, is a way of representing numbers using a base-16 numeral system.
3-
* @param color
4-
* @returns
3+
* @param color
4+
* @returns
55
*/
66
export const isValid_Hex_Color = (color: string): boolean => {
7-
const hexRegex = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
8-
return hexRegex.test(color);
9-
}
10-
7+
const hexRegex = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
8+
return hexRegex.test(color);
9+
};

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export * from './aadhaar';
22
export * from './credit-card';
33
export * from './email';
4+
export * from './file-extension';
45
export * from './ip';
56
export * from './hex-color';
6-
export * from './postal-zip'
7+
export * from './postal-zip';
78
export * from './ssn';
89
export * from './url';

src/postal-zip.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export enum CountryCode {
2424
FR = 'FR', // France
2525
IT = 'IT', // Italy
2626
TZ = 'TZ', // Tanzania
27-
ZA = 'ZA' // South Africa
27+
ZA = 'ZA', // South Africa
2828
}
2929

3030
/**
@@ -33,7 +33,10 @@ export enum CountryCode {
3333
* @param countryCode - The country code as an enum.
3434
* @returns True if the postal code is valid for the given country, otherwise false.
3535
*/
36-
export const isValid_Postal_Code = (postalCode: string, countryCode: CountryCode): boolean => {
36+
export const isValid_Postal_Code = (
37+
postalCode: string,
38+
countryCode: CountryCode
39+
): boolean => {
3740
const fiveDigitRegex = /^\d{5}$/;
3841
const sixDigitRegex = /^\d{6}$/;
3942
const fourDigitRegex = /^\d{4}$/;
@@ -63,7 +66,7 @@ export const isValid_Postal_Code = (postalCode: string, countryCode: CountryCode
6366
[CountryCode.FR]: fiveDigitRegex,
6467
[CountryCode.IT]: fiveDigitRegex,
6568
[CountryCode.TZ]: fiveDigitRegex,
66-
[CountryCode.ZA]: fourDigitRegex
69+
[CountryCode.ZA]: fourDigitRegex,
6770
};
6871

6972
const regex = postalCodeRegexes[countryCode];

src/url.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @returns True if the URL is valid, otherwise false.
55
*/
66
export const isValid_URL = (url: string): boolean => {
7-
const urlRegex = /^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[^\s<>]*)?(\?[^\s<>]*)?(#[^\s<>]*)?$/i;
7+
const urlRegex =
8+
/^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[^\s<>]*)?(\?[^\s<>]*)?(#[^\s<>]*)?$/i;
89
return urlRegex.test(url);
910
};

0 commit comments

Comments
 (0)