blob: 0f7bbd67f56ddcbace9cd4bbc890c180adc2ffb0 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const path = require('path');
3const trimRepeated = require('trim-repeated');
4const filenameReservedRegex = require('filename-reserved-regex');
5const stripOuter = require('strip-outer');
6
7// Doesn't make sense to have longer filenames
8const MAX_FILENAME_LENGTH = 100;
9
10const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; // eslint-disable-line no-control-regex
11const reRelativePath = /^\.+/;
12
13const fn = (string, options) => {
14 if (typeof string !== 'string') {
15 throw new TypeError('Expected a string');
16 }
17
18 options = options || {};
19
20 const replacement = options.replacement === undefined ? '!' : options.replacement;
21
22 if (filenameReservedRegex().test(replacement) && reControlChars.test(replacement)) {
23 throw new Error('Replacement string cannot contain reserved filename characters');
24 }
25
26 string = string.replace(filenameReservedRegex(), replacement);
27 string = string.replace(reControlChars, replacement);
28 string = string.replace(reRelativePath, replacement);
29
30 if (replacement.length > 0) {
31 string = trimRepeated(string, replacement);
32 string = string.length > 1 ? stripOuter(string, replacement) : string;
33 }
34
35 string = filenameReservedRegex.windowsNames().test(string) ? string + replacement : string;
36 string = string.slice(0, MAX_FILENAME_LENGTH);
37
38 return string;
39};
40
41fn.path = (pth, options) => {
42 pth = path.resolve(pth);
43 return path.join(path.dirname(pth), fn(path.basename(pth), options));
44};
45
46module.exports = fn;