Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions L-B/0026 FileExtension/FileExtension.js
Original file line number Diff line number Diff line change
@@ -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;
40 changes: 40 additions & 0 deletions L-B/0026 FileExtension/FileExtension.test.js
Original file line number Diff line number Diff line change
@@ -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
*/
46 changes: 46 additions & 0 deletions L-B/0026 FileExtension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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 empty string.

## Solution

```javascript
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 "";
}
```

## 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.
1 change: 1 addition & 0 deletions L-B/0026 FileExtension/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}