Skip to content

Commit

Permalink
Added file-extension utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathuria committed Jan 5, 2025
1 parent 471f570 commit 47485c0
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 19 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ This package includes various categorised validators which can be helppful for v
13. **Social Insurance Number (SIN)**: For Canada or equivalent in other countries. (Upcoming)

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

### Additional Utility:
15. **Boolean Strings**: Validate "true", "false", "1", "0", etc. (Upcoming)
16. **Hex Color Codes**: Validate `#RRGGBB` or `#RGB` color formats.
17. **EAN/UPC**: Validate international product codes. (Upcoming)
15. **Hex Color Codes**: Validate `#RRGGBB` or `#RGB` color formats.
16. **EAN/UPC**: Validate international product codes. (Upcoming)
11 changes: 11 additions & 0 deletions USAGE_DETAILS.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,14 @@ import { isValid_URL } from 'validate-functions/url';
const urlWithQuery = 'http://example.com?name=value';
console.log(isValid_URL(urlWithQuery)); // Output: true
```

- **isValid_File_Extension**

```typescript
import { isValid_File_Extension } from 'validate-functions/file-extension';

// Example of checking a file extension
const fileName = 'document.pdf';
const allowedExtensions = ['pdf', 'doc', 'txt'];
console.log(isValid_File_Extension(fileName, allowedExtensions)); // Output: true if the extension is valid, false otherwise
```
39 changes: 39 additions & 0 deletions src/__tests__/file-extension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { isValid_File_Extension } from '../file-extension';

describe('File Extension Validation', () => {
it('should return true for a valid file extension', () => {
const fileName = 'document.pdf';
const allowedExtensions = ['pdf', 'doc', 'txt'];
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(true);
});

it('should return false for an invalid file extension', () => {
const fileName = 'image.jpeg';
const allowedExtensions = ['pdf', 'doc', 'txt'];
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(false);
});

it('should return false for a file with no extension', () => {
const fileName = 'file';
const allowedExtensions = ['pdf', 'doc', 'txt'];
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(false);
});

it('should return true for a valid file extension with mixed case', () => {
const fileName = 'presentation.PPT';
const allowedExtensions = ['ppt', 'doc', 'txt'];
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(true);
});

it('should return false for an empty file name', () => {
const fileName = '';
const allowedExtensions = ['pdf', 'doc', 'txt'];
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(false);
});

it('should return false for an empty allowed extensions list', () => {
const fileName = 'document.pdf';
const allowedExtensions: string[] = [];
expect(isValid_File_Extension(fileName, allowedExtensions)).toBe(false);
});
});
8 changes: 4 additions & 4 deletions src/__tests__/postal-zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ describe('Postal Code Validation', () => {
});

describe('Invalid Country Code', () => {
it('should return false for an unsupported country code', () => {
expect(isValid_Postal_Code('awea23s', 'UN' as CountryCode)).toBe(false);
});
});
it('should return false for an unsupported country code', () => {
expect(isValid_Postal_Code('awea23s', 'UN' as CountryCode)).toBe(false);
});
});
});
13 changes: 13 additions & 0 deletions src/file-extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
*
* @param fileName
* @param allowedExtensions
* @returns
*/
export const isValid_File_Extension = (
fileName: string,
allowedExtensions: string[]
): boolean => {
const extension = fileName.split('.').pop()?.toLowerCase() || '';
return allowedExtensions.includes(extension);
};
11 changes: 5 additions & 6 deletions src/hex-color.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* A hex code, short for hexadecimal code, is a way of representing numbers using a base-16 numeral system.
* @param color
* @returns
* @param color
* @returns
*/
export const isValid_Hex_Color = (color: string): boolean => {
const hexRegex = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
return hexRegex.test(color);
}

const hexRegex = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
return hexRegex.test(color);
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from './aadhaar';
export * from './credit-card';
export * from './email';
export * from './file-extension';
export * from './ip';
export * from './hex-color';
export * from './postal-zip'
export * from './postal-zip';
export * from './ssn';
export * from './url';
9 changes: 6 additions & 3 deletions src/postal-zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export enum CountryCode {
FR = 'FR', // France
IT = 'IT', // Italy
TZ = 'TZ', // Tanzania
ZA = 'ZA' // South Africa
ZA = 'ZA', // South Africa
}

/**
Expand All @@ -33,7 +33,10 @@ export enum CountryCode {
* @param countryCode - The country code as an enum.
* @returns True if the postal code is valid for the given country, otherwise false.
*/
export const isValid_Postal_Code = (postalCode: string, countryCode: CountryCode): boolean => {
export const isValid_Postal_Code = (
postalCode: string,
countryCode: CountryCode
): boolean => {
const fiveDigitRegex = /^\d{5}$/;
const sixDigitRegex = /^\d{6}$/;
const fourDigitRegex = /^\d{4}$/;
Expand Down Expand Up @@ -63,7 +66,7 @@ export const isValid_Postal_Code = (postalCode: string, countryCode: CountryCode
[CountryCode.FR]: fiveDigitRegex,
[CountryCode.IT]: fiveDigitRegex,
[CountryCode.TZ]: fiveDigitRegex,
[CountryCode.ZA]: fourDigitRegex
[CountryCode.ZA]: fourDigitRegex,
};

const regex = postalCodeRegexes[countryCode];
Expand Down
3 changes: 2 additions & 1 deletion src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @returns True if the URL is valid, otherwise false.
*/
export const isValid_URL = (url: string): boolean => {
const urlRegex = /^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[^\s<>]*)?(\?[^\s<>]*)?(#[^\s<>]*)?$/i;
const urlRegex =
/^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[^\s<>]*)?(\?[^\s<>]*)?(#[^\s<>]*)?$/i;
return urlRegex.test(url);
};

0 comments on commit 47485c0

Please sign in to comment.