| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const path = require('path'); |
| 3 | const trimRepeated = require('trim-repeated'); |
| 4 | const filenameReservedRegex = require('filename-reserved-regex'); |
| 5 | const stripOuter = require('strip-outer'); |
| 6 | |
| 7 | // Doesn't make sense to have longer filenames |
| 8 | const MAX_FILENAME_LENGTH = 100; |
| 9 | |
| 10 | const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; // eslint-disable-line no-control-regex |
| 11 | const reRelativePath = /^\.+/; |
| 12 | |
| 13 | const 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 | |
| 41 | fn.path = (pth, options) => { |
| 42 | pth = path.resolve(pth); |
| 43 | return path.join(path.dirname(pth), fn(path.basename(pth), options)); |
| 44 | }; |
| 45 | |
| 46 | module.exports = fn; |