summaryrefslogtreecommitdiff
path: root/node_modules/filenamify/filenamify.js
diff options
context:
space:
mode:
authorLinuxWizard42 <computerwizard@linuxmail.org>2022-10-12 22:54:37 +0300
committerLinuxWizard42 <computerwizard@linuxmail.org>2022-10-12 22:54:37 +0300
commit703e03aba33f234712206769f57717ba7d92d23d (patch)
tree0041f04ccb75bd5379c764e9fe42249fffe75fc3 /node_modules/filenamify/filenamify.js
parentab6e257e6e9d9a483d7e86f220d8b209a2cd7753 (diff)
downloadFlashRunner-703e03aba33f234712206769f57717ba7d92d23d.tar.gz
FlashRunner-703e03aba33f234712206769f57717ba7d92d23d.tar.zst
Added export_allowed file to make repository visible in cgit
Diffstat (limited to 'node_modules/filenamify/filenamify.js')
-rw-r--r--node_modules/filenamify/filenamify.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/node_modules/filenamify/filenamify.js b/node_modules/filenamify/filenamify.js
new file mode 100644
index 0000000..a548430
--- /dev/null
+++ b/node_modules/filenamify/filenamify.js
@@ -0,0 +1,40 @@
+'use strict';
+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 reTrailingPeriods = /\.+$/;
+
+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);
+ string = string.replace(reTrailingPeriods, '');
+
+ 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;
+};
+
+module.exports = filenamify;