Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom invalid characters (#57) #58

Closed
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ Sanitize `inputString` by removing or replacing invalid characters.

Options:

* `options.replacement`: *optional, string/function, default: `""`*. If passed
* `options.replacement`: *optional, string/function, default: `""`*. If passed
as a string, it's used as the replacement for invalid characters. If passed as
a function, the function will be called with the invalid characters and it's
return value will be used as the replacement. See [`String.prototype.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)
for more info.
* `options.additionalInvalids`: *optional, array of strings, default:* `[]`
Any additional strings or characters that should be considered invalid and be replaced. This will not override the default invalid characters, only specify additional ones.
If a string containing a mix of valid and invalid characters is supplied, all occurences of the whole string will be replaced.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare function sanitize(
input: string,
options?: {
replacement?: string | ((substring: string) => string);
additionalInvalids?: string[];
}
): string;

Expand Down
27 changes: 22 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Replaces characters in strings that are illegal/unsafe for filenames.
* Unsafe characters are either removed or replaced by a substitute set
* in the optional `options` object.
* Additionally, more invalid characters can be passed using the `additionalInvalids` attribute of the optional `options` object.
*
* Illegal Characters on Various Operating Systems
* / ? < > \ : * | "
Expand All @@ -24,7 +25,7 @@
* http://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs
*
* @param {String} input Original filename
* @param {Object} options {replacement: String | Function }
* @param {Object} options {replacement: String | Function, additionalInvalids: String[] }
* @return {String} Sanitized filename
*/

Expand All @@ -36,11 +37,26 @@ var reservedRe = /^\.+$/;
var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
var windowsTrailingRe = /[\. ]+$/;

function sanitize(input, replacement) {
function sanitize(input, replacement, invalids) {
if (typeof input !== 'string') {
throw new Error('Input must be string');
}
var sanitized = input
if (typeof replacement === 'string' && invalids.indexOf(replacement) !== -1) {
throw new Error(`The replacement string can't be part of options.additionalInvalids or contain substrings which are part of options.additionalInvalids!`);
}
var sanitized = input;
while (invalids.some(invalid => sanitized.includes(invalid))) {
let counter = 0;
for (const invalid of invalids) {
sanitized = sanitized.split(invalid).join(replacement);
}
if (counter > 1000) {
throw new Error(`Illeagal replacement function. Make sure that replacements generated by your function do not contain any of the strings specified in options.additionalInvalids!`);
parshap marked this conversation as resolved.
Show resolved Hide resolved
} else {
counter++;
}
}
sanitized = sanitized
.replace(illegalRe, replacement)
.replace(controlRe, replacement)
.replace(reservedRe, replacement)
Expand All @@ -51,9 +67,10 @@ function sanitize(input, replacement) {

module.exports = function (input, options) {
var replacement = (options && options.replacement) || '';
var output = sanitize(input, replacement);
var invalids = (options && options.additionalInvalids) || [];
var output = sanitize(input, replacement, invalids);
if (replacement === '') {
return output;
}
return sanitize(output, '');
return sanitize(output, '', []);
};
Loading