Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const validFilename = string => {
if (!string || string.length > 255) {
return false;
}
if (filenameReservedRegex().test(string) || filenameReservedRegex.windowsNames().test(string)) {
return false;
}
if (/^\.\.?$/.test(string)) {
return false;
}
return true;
};
const replacement = options.replacement === undefined ? '!' : options.replacement;
if (filenameReservedRegex().test(replacement) && reControlChars.test(replacement)) {
throw new Error('Replacement string cannot contain reserved filename characters');
}
string = string.replace(filenameReservedRegex(), replacement);
string = string.replace(reControlChars, replacement);
string = string.replace(reRelativePath, replacement);
if (replacement.length > 0) {
string = trimRepeated(string, replacement);
string = string.length > 1 ? stripOuter(string, replacement) : string;
}
string = filenameReservedRegex.windowsNames().test(string) ? string + replacement : string;
string = string.slice(0, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH);
return string;
};