Skip to content

Commit

Permalink
feat: add file-name extension helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 3, 2022
1 parent 96a1959 commit ce1095f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@
},
"dependencies": {
"glob": "^8.0.3"
},
"config": {
"commitizen": {
"path": "@commitlint/cz-commitlint"
}
}
}
28 changes: 28 additions & 0 deletions src/utils/file-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2022.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

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);
}
}

return input;
}
24 changes: 24 additions & 0 deletions test/unit/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2022.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import { removeFileNameExtension } from "../../src/utils/file-name";

describe('src/utils/*.ts', function () {
it('should remove file name extension', () => {
let data = removeFileNameExtension('test.js', ['.js', '.ts']);
expect(data).toEqual('test');

data = removeFileNameExtension('test.mts', ['.js', '.ts']);
expect(data).toEqual('test.mts');

data = removeFileNameExtension('test.js', []);
expect(data).toEqual('test.js');

data = removeFileNameExtension('test.js');
expect(data).toEqual('test');
})
});

0 comments on commit ce1095f

Please sign in to comment.