-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
process: add release.compareVersion(str) functionality #20055
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,10 @@ const { | |
ERR_ASSERTION, | ||
ERR_CPU_USAGE, | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_INVALID_ARG_VALUE, | ||
ERR_INVALID_ARRAY_LENGTH, | ||
ERR_INVALID_OPT_VALUE, | ||
ERR_OUT_OF_RANGE, | ||
ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET, | ||
ERR_UNKNOWN_SIGNAL | ||
} | ||
|
@@ -291,15 +293,43 @@ function setupCompareVersion() { | |
prereleaseTag, | ||
} = process.release; | ||
|
||
process.release.compareVersion = (major, minor, patch, tag) => { | ||
if (typeof major !== 'number') | ||
throw new ERR_INVALID_ARG_TYPE('major', 'number', major); | ||
if (typeof minor !== 'number') | ||
throw new ERR_INVALID_ARG_TYPE('minor', 'number', minor); | ||
if (typeof patch !== 'number') | ||
throw new ERR_INVALID_ARG_TYPE('patch', 'number', patch); | ||
if (tag !== undefined && typeof tag !== 'string') | ||
throw new ERR_INVALID_ARG_TYPE('tag', 'string', tag); | ||
process.release.compareVersion = function(major, minor, patch, tag) { | ||
if (arguments.length === 1 && typeof major === 'string') { | ||
const [start, ...end] = major.split('-'); | ||
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. Would it be faster to just use a regexp to parse and validate the string? 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 did not benchmark this explicitly but I changed it to a reg exp nevertheless because it is also nicer to read. |
||
tag = end.join('-'); | ||
const chunks = start.split('.'); | ||
if (chunks.length !== 3) { | ||
throw new ERR_INVALID_ARG_VALUE('version', | ||
major, | ||
'is not a valid Semantic Version'); | ||
} | ||
[major, minor, patch] = chunks.map((chunk) => { | ||
const num = Number(chunk); | ||
if (Number.isNaN(num) || | ||
chunk === '' || | ||
chunk.length > 1 && chunk.startsWith('0')) { | ||
throw new ERR_INVALID_ARG_VALUE('version', | ||
major, | ||
'is not a valid Semantic Version'); | ||
} | ||
return num; | ||
}); | ||
} else { | ||
if (typeof major !== 'number') | ||
throw new ERR_INVALID_ARG_TYPE('major', 'number', major); | ||
if (typeof minor !== 'number') | ||
throw new ERR_INVALID_ARG_TYPE('minor', 'number', minor); | ||
if (typeof patch !== 'number') | ||
throw new ERR_INVALID_ARG_TYPE('patch', 'number', patch); | ||
if (tag !== undefined && typeof tag !== 'string') | ||
throw new ERR_INVALID_ARG_TYPE('tag', 'string', tag); | ||
} | ||
if (major < 0) | ||
throw new ERR_OUT_OF_RANGE('major', '>= 0', major); | ||
if (minor < 0) | ||
throw new ERR_OUT_OF_RANGE('minor', '>= 0', minor); | ||
if (patch < 0) | ||
throw new ERR_OUT_OF_RANGE('patch', '>= 0', patch); | ||
|
||
if (major > majorVersion) | ||
return -1; | ||
|
@@ -315,8 +345,11 @@ function setupCompareVersion() { | |
return -1; | ||
if (patch < patchVersion) | ||
return 1; | ||
if (prereleaseTag) | ||
return prereleaseTag === tag ? 0 : 1; | ||
if (prereleaseTag) { | ||
if (prereleaseTag === tag) | ||
return 0; | ||
return prereleaseTag < tag ? -1 : 1; | ||
} | ||
if (tag) | ||
return -1; | ||
|
||
|
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.
The description should come after the parameter list (yes, this should've also been fixed in the original PR).
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.
Done (including in the original description).