aboutsummaryrefslogtreecommitdiff
path: root/node_modules/filenamify/index.js
diff options
context:
space:
mode:
authorMinteck <nekostarfan@gmail.com>2021-08-24 14:41:48 +0200
committerMinteck <nekostarfan@gmail.com>2021-08-24 14:41:48 +0200
commitd25e11bee6ca5ca523884da132d18e1400e077b9 (patch)
tree8af39fde19f7ed640a60fb397c7edd647dff1c4c /node_modules/filenamify/index.js
downloadkartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.gz
kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.bz2
kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.zip
Initial commit
Diffstat (limited to 'node_modules/filenamify/index.js')
-rw-r--r--node_modules/filenamify/index.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/node_modules/filenamify/index.js b/node_modules/filenamify/index.js
new file mode 100644
index 0000000..7993d46
--- /dev/null
+++ b/node_modules/filenamify/index.js
@@ -0,0 +1,45 @@
+'use strict';
+const path = require('path');
+const trimRepeated = require('trim-repeated');
+const filenameReservedRegex = require('filename-reserved-regex');
+const stripOuter = require('strip-outer');
+
+// Doesn't make sense to have longer filenames
+const MAX_FILENAME_LENGTH = 100;
+
+const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; // eslint-disable-line no-control-regex
+const reRelativePath = /^\.+/;
+
+const filenamify = (string, options = {}) => {
+ if (typeof string !== 'string') {
+ throw new TypeError('Expected a string');
+ }
+
+ 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, MAX_FILENAME_LENGTH);
+
+ return string;
+};
+
+filenamify.path = (filePath, options) => {
+ filePath = path.resolve(filePath);
+ return path.join(path.dirname(filePath), filenamify(path.basename(filePath), options));
+};
+
+module.exports = filenamify;
+module.exports.default = filenamify;