Skip to content

Commit 9362b78

Browse files
committed
get extension from a file name
1 parent a9672bd commit 9362b78

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 undefined.
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+
// Get the last element in the array, which is the file extension.
15+
const fileExtension = splitFileName.pop();
16+
17+
// Return the file extension.
18+
return fileExtension;
19+
}
20+
```
21+
22+
## How it works
23+
24+
- The `extractFileExtension` function takes a string as an argument.
25+
- The `split` method is used to split the string into an array of strings at the dot character.
26+
- The `pop` method is used to get the last element in the array, which is the file extension.
27+
- The file extension is returned.
28+
29+
## References
30+
31+
- [String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
32+
33+
## Problem Added By
34+
35+
- [GitHub](https://github.com/aladin002dz)
36+
37+
## Contributing
38+
39+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
40+
41+
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)