From 9362b781e9cbf84a431ec05562437c2d1f3558d4 Mon Sep 17 00:00:00 2001 From: aladin002dz Date: Tue, 10 Oct 2023 14:24:30 +0100 Subject: [PATCH 1/2] get extension from a file name --- L-B/0026 FileExtension/FileExtension.js | 17 ++++++++ L-B/0026 FileExtension/FileExtension.test.js | 40 +++++++++++++++++++ L-B/0026 FileExtension/README.md | 41 ++++++++++++++++++++ L-B/0026 FileExtension/jest.config.json | 1 + 4 files changed, 99 insertions(+) create mode 100644 L-B/0026 FileExtension/FileExtension.js create mode 100644 L-B/0026 FileExtension/FileExtension.test.js create mode 100644 L-B/0026 FileExtension/README.md create mode 100644 L-B/0026 FileExtension/jest.config.json diff --git a/L-B/0026 FileExtension/FileExtension.js b/L-B/0026 FileExtension/FileExtension.js new file mode 100644 index 0000000..6987c7f --- /dev/null +++ b/L-B/0026 FileExtension/FileExtension.js @@ -0,0 +1,17 @@ +function extractFileExtension(fileName) { + // Split the file name into an array of strings at the dot character. + const splitFileName = fileName.split('.'); + + // Check if the split file name array has more than one element. + if (splitFileName.length > 1) { + // Get the last element in the array, which is the file extension. + const fileExtension = splitFileName.pop(); + + // Return the file extension. + return fileExtension; + } + // The file name does not have an extension, so return an empty string. + return ''; +} + +module.exports = extractFileExtension; diff --git a/L-B/0026 FileExtension/FileExtension.test.js b/L-B/0026 FileExtension/FileExtension.test.js new file mode 100644 index 0000000..86bdb7f --- /dev/null +++ b/L-B/0026 FileExtension/FileExtension.test.js @@ -0,0 +1,40 @@ + +const extractFileExtension = require('./FileExtension') + +describe('extractFileExtension', () => { + it('should return the file extension when given a valid file name', () => { + expect(extractFileExtension('example.txt')).toBe('txt'); + expect(extractFileExtension('document.docx')).toBe('docx'); + expect(extractFileExtension('image.jpg')).toBe('jpg'); + }); + + it('should return an empty string when given a file name without an extension', () => { + expect(extractFileExtension('README')).toBe(''); + expect(extractFileExtension('LICENSE')).toBe(''); + }); + + it('should return an empty string when given an empty string', () => { + expect(extractFileExtension('')).toBe(''); + }); +}); + +/* +test output: + PASS ./FileExtension.test.js + extractFileExtension + √ should return the file extension when given a valid file name (4 ms) + √ should return an empty string when given a file name without an extension (1 ms) + √ should return an empty string when given an empty string + +Test Suites: 1 passed, 1 total +Tests: 3 passed, 3 total +Snapshots: 0 total +Time: 0.958 s, estimated 1 s +Ran all test suites. +*/ + +/* +Testing with Jest global installation: +> npm install -g jest +> jest +*/ diff --git a/L-B/0026 FileExtension/README.md b/L-B/0026 FileExtension/README.md new file mode 100644 index 0000000..a84b3fc --- /dev/null +++ b/L-B/0026 FileExtension/README.md @@ -0,0 +1,41 @@ +# Get the file extension + +## Problem + +Write a function that takes a string and returns the file extension, which is the string after the last dot (.) in the string. If there is no dot in the string, return undefined. + +## Solution + +```javascript +function extractFileExtension(fileName) { + // Split the file name into an array of strings at the dot character. + const splitFileName = fileName.split("."); + + // Get the last element in the array, which is the file extension. + const fileExtension = splitFileName.pop(); + + // Return the file extension. + return fileExtension; +} +``` + +## How it works + +- The `extractFileExtension` function takes a string as an argument. +- The `split` method is used to split the string into an array of strings at the dot character. +- The `pop` method is used to get the last element in the array, which is the file extension. +- The file extension is returned. + +## References + +- [String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) + +## Problem Added By + +- [GitHub](https://github.com/aladin002dz) + +## Contributing + +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. + +Please make sure to update tests as appropriate. diff --git a/L-B/0026 FileExtension/jest.config.json b/L-B/0026 FileExtension/jest.config.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/L-B/0026 FileExtension/jest.config.json @@ -0,0 +1 @@ +{} \ No newline at end of file From 16e30ac3a712e24088044002ecc97ee809b524fd Mon Sep 17 00:00:00 2001 From: aladin002dz Date: Tue, 10 Oct 2023 14:29:56 +0100 Subject: [PATCH 2/2] FileExtension readme update --- L-B/0026 FileExtension/README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/L-B/0026 FileExtension/README.md b/L-B/0026 FileExtension/README.md index a84b3fc..d69f24a 100644 --- a/L-B/0026 FileExtension/README.md +++ b/L-B/0026 FileExtension/README.md @@ -2,7 +2,7 @@ ## Problem -Write a function that takes a string and returns the file extension, which is the string after the last dot (.) in the string. If there is no dot in the string, return undefined. +Write a function that takes a string and returns the file extension, which is the string after the last dot (.) in the string. If there is no dot in the string, return empty string. ## Solution @@ -11,11 +11,16 @@ function extractFileExtension(fileName) { // Split the file name into an array of strings at the dot character. const splitFileName = fileName.split("."); - // Get the last element in the array, which is the file extension. - const fileExtension = splitFileName.pop(); + // Check if the split file name array has more than one element. + if (splitFileName.length > 1) { + // Get the last element in the array, which is the file extension. + const fileExtension = splitFileName.pop(); - // Return the file extension. - return fileExtension; + // Return the file extension. + return fileExtension; + } + // The file name does not have an extension, so return an empty string. + return ""; } ```