Skip to content

Commit

Permalink
feat: optimize remove file name extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Jul 20, 2023
1 parent 18cacd9 commit fb6edfb
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/utils/file-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,34 @@
* view the LICENSE file that was distributed with this source code.
*/

import path from 'node:path';

export function getFileNameExtension(
input: string,
allowed?: string[],
) : string | undefined {
const extension = path.extname(input);
if (extension === '' || extension === '.') {
return undefined;
}

if (
typeof allowed === 'undefined' ||
allowed.indexOf(extension) !== -1
) {
return extension;
}

return undefined;
}

export function removeFileNameExtension(
input: string,
extensions?: string[],
) {
if (input.includes('.')) {
const position = input.lastIndexOf('.');
const extension = input.substring(
position,
input.length,
);

if (
typeof extensions === 'undefined' ||
extensions.indexOf(extension) !== -1
) {
input = input.substring(0, position);
}
const extension = getFileNameExtension(input, extensions);
if (extension) {
return input.substring(0, input.length - extension.length);
}

return input;
Expand Down

0 comments on commit fb6edfb

Please sign in to comment.