-
-
Notifications
You must be signed in to change notification settings - Fork 681
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
Improve keep extension #857
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d3a05e9
add failing test case
GrosSacASac 15afa8a
docs: add comment
GrosSacASac 703bec4
tests: add comment
GrosSacASac d2bd18d
tests: add a test case that proves that the regex was always bad
GrosSacASac 5fdb2d0
fix: replace regex with reliable filtering
GrosSacASac 78de849
feat: stop at first invalid char
GrosSacASac 67c6a3f
feat: allow numbers in file extensions
GrosSacASac 5103d09
feat: stop extension from being '.'
GrosSacASac 9969c25
refactor: code style
GrosSacASac 2f553b4
docs: use slugify in the example
GrosSacASac 143e473
chore: prepare release
GrosSacASac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
---|---|---|
|
@@ -42,6 +42,16 @@ function hasOwnProp(obj, key) { | |
return Object.prototype.hasOwnProperty.call(obj, key); | ||
} | ||
|
||
const invalidExtensionChar = (c) => { | ||
const code = c.charCodeAt(0); | ||
return !( | ||
code === 46 || // . | ||
(code >= 48 && code <= 57) || | ||
(code >= 65 && code <= 90) || | ||
(code >= 97 && code <= 122) | ||
); | ||
}; | ||
|
||
class IncomingForm extends EventEmitter { | ||
constructor(options = {}) { | ||
super(); | ||
|
@@ -497,6 +507,9 @@ class IncomingForm extends EventEmitter { | |
return originalFilename; | ||
} | ||
|
||
// able to get composed extension with multiple dots | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i remember pretty clearly there was some reason for that, some report or feature or issue.. but i don't remember the reason haha, so we can switch back to just getting the thing after the last dot with Node's builtin and all the drama goes to the trash. |
||
// "a.b.c" -> ".b.c" | ||
// as opposed to path.extname -> ".c" | ||
_getExtension(str) { | ||
if (!str) { | ||
return ''; | ||
|
@@ -505,13 +518,23 @@ class IncomingForm extends EventEmitter { | |
const basename = path.basename(str); | ||
const firstDot = basename.indexOf('.'); | ||
const lastDot = basename.lastIndexOf('.'); | ||
const extname = path.extname(basename).replace(/(\.[a-z0-9]+).*/i, '$1'); | ||
let rawExtname = path.extname(basename); | ||
|
||
if (firstDot === lastDot) { | ||
return extname; | ||
if (firstDot !== lastDot) { | ||
rawExtname = basename.slice(firstDot); | ||
} | ||
|
||
return basename.slice(firstDot, lastDot) + extname; | ||
let filtered; | ||
const firstInvalidIndex = Array.from(rawExtname).findIndex(invalidExtensionChar); | ||
if (firstInvalidIndex === -1) { | ||
filtered = rawExtname; | ||
} else { | ||
filtered = rawExtname.substring(0, firstInvalidIndex); | ||
} | ||
if (filtered === '.') { | ||
return ''; | ||
} | ||
return filtered; | ||
} | ||
|
||
_joinDirectoryName(name) { | ||
|
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
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.
we better use
filenamify
instead.there will not be needed for
ext
, because we can just returnpath.extname
, as per the other comments.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.
ooh, that's from the examples dir, lol