agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
31 lines (25 loc) • 1.02 kB
JavaScript
/**
* @file Path extension modification utility
* @description Single responsibility: Change file extension while preserving path structure
*/
const path = require('path');
/**
* Change the extension of a file path
* @param {string} filePath - The original file path
* @param {string} newExtension - The new extension (with or without leading dot)
* @returns {string} Path with the new extension
*/
function changePathExtension(filePath, newExtension) {
if (typeof filePath !== 'string') {
throw new TypeError('File path must be a string');
}
if (typeof newExtension !== 'string') {
throw new TypeError('Extension must be a string');
}
// Ensure extension starts with a dot
const ext = newExtension.startsWith('.') ? newExtension : '.' + newExtension;
// Get the path without extension and add new extension
const basePath = path.join(path.dirname(filePath), path.basename(filePath, path.extname(filePath)));
return basePath + ext;
}
module.exports = changePathExtension;