From 47485c0a2ed2bed708f71bcbcd7fc48057020615 Mon Sep 17 00:00:00 2001 From: Avi Kathuria Date: Sun, 5 Jan 2025 14:38:09 -0600 Subject: [PATCH] Added file-extension utility --- README.md | 7 +++-- USAGE_DETAILS.md | 11 ++++++++ src/__tests__/file-extension.test.ts | 39 ++++++++++++++++++++++++++++ src/__tests__/postal-zip.test.ts | 8 +++--- src/file-extension.ts | 13 ++++++++++ src/hex-color.ts | 11 ++++---- src/index.ts | 3 ++- src/postal-zip.ts | 9 ++++--- src/url.ts | 3 ++- 9 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 src/__tests__/file-extension.test.ts create mode 100644 src/file-extension.ts diff --git a/README.md b/README.md index 1548874..c8f66c7 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/USAGE_DETAILS.md b/USAGE_DETAILS.md index ab2e095..094c93a 100644 --- a/USAGE_DETAILS.md +++ b/USAGE_DETAILS.md @@ -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 +``` \ No newline at end of file diff --git a/src/__tests__/file-extension.test.ts b/src/__tests__/file-extension.test.ts new file mode 100644 index 0000000..63f4c12 --- /dev/null +++ b/src/__tests__/file-extension.test.ts @@ -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); + }); +}); diff --git a/src/__tests__/postal-zip.test.ts b/src/__tests__/postal-zip.test.ts index bc133c8..aff6612 100644 --- a/src/__tests__/postal-zip.test.ts +++ b/src/__tests__/postal-zip.test.ts @@ -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); + }); + }); }); diff --git a/src/file-extension.ts b/src/file-extension.ts new file mode 100644 index 0000000..44d33dd --- /dev/null +++ b/src/file-extension.ts @@ -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); +}; diff --git a/src/hex-color.ts b/src/hex-color.ts index b817223..7f71880 100644 --- a/src/hex-color.ts +++ b/src/hex-color.ts @@ -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); - } - \ No newline at end of file + const hexRegex = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/; + return hexRegex.test(color); +}; diff --git a/src/index.ts b/src/index.ts index 1527cc6..e61c75b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/postal-zip.ts b/src/postal-zip.ts index 9782909..fe0c5e3 100644 --- a/src/postal-zip.ts +++ b/src/postal-zip.ts @@ -24,7 +24,7 @@ export enum CountryCode { FR = 'FR', // France IT = 'IT', // Italy TZ = 'TZ', // Tanzania - ZA = 'ZA' // South Africa + ZA = 'ZA', // South Africa } /** @@ -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}$/; @@ -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]; diff --git a/src/url.ts b/src/url.ts index 0464c0f..18f6084 100644 --- a/src/url.ts +++ b/src/url.ts @@ -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); };