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
Provide a browser-only export #17
Merged
sindresorhus
merged 6 commits into
sindresorhus:master
from
marcelgerber:browser-only-export
Sep 24, 2020
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2376b73
Split up filenamify and filenamify-path into different files
marcelgerber f393bc6
provide type definitions for all exports
marcelgerber 6ea01d8
remove `filenamify-path` export
marcelgerber 7a798d1
add note about `filenamify/browser` export to readme
marcelgerber e948a8c
Update readme.md
sindresorhus 028af24
include new files in package.json `files`
marcelgerber File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import filenamify = require('./filenamify'); | ||
|
||
/** | ||
Convert the filename in a path a valid filename and return the augmented path. | ||
*/ | ||
declare const filenamifyPath: (path: string, options?: filenamify.Options) => string; | ||
|
||
export = filenamifyPath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const filenamify = require('./filenamify'); | ||
|
||
const filenamifyPath = (filePath, options) => { | ||
filePath = path.resolve(filePath); | ||
return path.join(path.dirname(filePath), filenamify(path.basename(filePath), options)); | ||
}; | ||
|
||
module.exports = filenamifyPath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
declare namespace filenamify { | ||
interface Options { | ||
/** | ||
String to use as replacement for reserved filename characters. | ||
|
||
Cannot contain: `<` `>` `:` `"` `/` `\` `|` `?` `*` | ||
|
||
@default '!' | ||
*/ | ||
readonly replacement?: string; | ||
|
||
/** | ||
Truncate the filename to the given length. | ||
|
||
Systems generally allow up to 255 characters, but we default to 100 for usability reasons. | ||
|
||
@default 100 | ||
*/ | ||
readonly maxLength?: number; | ||
} | ||
} | ||
|
||
/** | ||
Convert a string to a valid filename. | ||
|
||
@example | ||
``` | ||
import filenamify = require('filenamify'); | ||
|
||
filenamify('<foo/bar>'); | ||
//=> 'foo!bar' | ||
|
||
filenamify('foo:"bar"', {replacement: '🐴'}); | ||
//=> 'foo🐴bar' | ||
``` | ||
*/ | ||
declare const filenamify: (string: string, options?: filenamify.Options) => string; | ||
|
||
export = filenamify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'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 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, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH); | ||
|
||
return string; | ||
}; | ||
|
||
module.exports = filenamify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,8 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const trimRepeated = require('trim-repeated'); | ||
const filenameReservedRegex = require('filename-reserved-regex'); | ||
const stripOuter = require('strip-outer'); | ||
const filenamify = require('./filenamify'); | ||
const filenamifyPath = require('./filenamify-path'); | ||
|
||
// 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, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH); | ||
|
||
return string; | ||
}; | ||
|
||
filenamify.path = (filePath, options) => { | ||
filePath = path.resolve(filePath); | ||
return path.join(path.dirname(filePath), filenamify(path.basename(filePath), options)); | ||
}; | ||
const filenamifyCombined = filenamify; | ||
filenamifyCombined.path = filenamifyPath; | ||
|
||
module.exports = filenamify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not what we discussed in the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure what you mean here, but if you meant that you don't want to provide the
filenamify-path
export then I've just pushed a commit that removes it.I've also changed the name of the
filenamify
export tobrowser
, as suggested by you in the issue. Again, it's always easy to change it.And if there's anything else you want changed, please feel free to tell me and I'll change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's what I meant. Sorry if that was not clear.