Skip to content

Commit 9e2ec54

Browse files
authored
Merge pull request #64 from aladin002dz/GetFileExtension
Get file extension
2 parents 5bafef3 + 16e30ac commit 9e2ec54

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function extractFileExtension(fileName) {
2+
// Split the file name into an array of strings at the dot character.
3+
const splitFileName = fileName.split('.');
4+
5+
// Check if the split file name array has more than one element.
6+
if (splitFileName.length > 1) {
7+
// Get the last element in the array, which is the file extension.
8+
const fileExtension = splitFileName.pop();
9+
10+
// Return the file extension.
11+
return fileExtension;
12+
}
13+
// The file name does not have an extension, so return an empty string.
14+
return '';
15+
}
16+
17+
module.exports = extractFileExtension;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
const extractFileExtension = require('./FileExtension')
3+
4+
describe('extractFileExtension', () => {
5+
it('should return the file extension when given a valid file name', () => {
6+
expect(extractFileExtension('example.txt')).toBe('txt');
7+
expect(extractFileExtension('document.docx')).toBe('docx');
8+
expect(extractFileExtension('image.jpg')).toBe('jpg');
9+
});
10+
11+
it('should return an empty string when given a file name without an extension', () => {
12+
expect(extractFileExtension('README')).toBe('');
13+
expect(extractFileExtension('LICENSE')).toBe('');
14+
});
15+
16+
it('should return an empty string when given an empty string', () => {
17+
expect(extractFileExtension('')).toBe('');
18+
});
19+
});
20+
21+
/*
22+
test output:
23+
PASS ./FileExtension.test.js
24+
extractFileExtension
25+
√ should return the file extension when given a valid file name (4 ms)
26+
√ should return an empty string when given a file name without an extension (1 ms)
27+
√ should return an empty string when given an empty string
28+
29+
Test Suites: 1 passed, 1 total
30+
Tests: 3 passed, 3 total
31+
Snapshots: 0 total
32+
Time: 0.958 s, estimated 1 s
33+
Ran all test suites.
34+
*/
35+
36+
/*
37+
Testing with Jest global installation:
38+
> npm install -g jest
39+
> jest
40+
*/

L-B/0026 FileExtension/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Get the file extension
2+
3+
## Problem
4+
5+
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.
6+
7+
## Solution
8+
9+
```javascript
10+
function extractFileExtension(fileName) {
11+
// Split the file name into an array of strings at the dot character.
12+
const splitFileName = fileName.split(".");
13+
14+
// Check if the split file name array has more than one element.
15+
if (splitFileName.length > 1) {
16+
// Get the last element in the array, which is the file extension.
17+
const fileExtension = splitFileName.pop();
18+
19+
// Return the file extension.
20+
return fileExtension;
21+
}
22+
// The file name does not have an extension, so return an empty string.
23+
return "";
24+
}
25+
```
26+
27+
## How it works
28+
29+
- The `extractFileExtension` function takes a string as an argument.
30+
- The `split` method is used to split the string into an array of strings at the dot character.
31+
- The `pop` method is used to get the last element in the array, which is the file extension.
32+
- The file extension is returned.
33+
34+
## References
35+
36+
- [String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
37+
38+
## Problem Added By
39+
40+
- [GitHub](https://github.com/aladin002dz)
41+
42+
## Contributing
43+
44+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
45+
46+
Please make sure to update tests as appropriate.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)