Skip to content

sem-ver implemented #566

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

Closed
wants to merge 4 commits into from
Closed

sem-ver implemented #566

wants to merge 4 commits into from

Conversation

msohailhussain
Copy link
Contributor

@msohailhussain msohailhussain commented Sep 15, 2020

Summary

  • Semantic version added
  • Greater than or equal (ge) and less than or equal (le) evaluator added

Test plan

  • All relevant test cases added

@coveralls
Copy link

coveralls commented Sep 15, 2020

Coverage Status

Coverage decreased (-0.3%) to 96.424% when pulling 8dc6368 on uzair/semver-ts into 28260a2 on master.

Copy link
Contributor

@mnoman09 mnoman09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. Added some comments.

const userValueType = typeof userValue;
const conditionValue = condition.value;

if (conditionValue === null || !isSafeInteger(conditionValue)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of repeating all these checks in every function put it in one function and use it from there see C# for reference. the only evaluation logic should be kept inside every evaluator.

Copy link
Contributor

@ozayr-zaviar ozayr-zaviar Sep 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency with previous written evaluators like greaterThanEvaluator, lessThanEvaluator etc will be lost.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @mnoman09. If there is repeated logic for checking user inputs for numeric comparison conditions, let's use a shared helper function. If the logic is not exactly the same or it is awkward to share, don't worry about it.

['2.9.9-beta', '2.9.9-beta'],
['2.1', '2.1.0'],
['2', '2.12'],
['2.9', '2.9.1']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this test where targetSV = 2.1.3 and userver= 2.1.3+build and it should return 0 see this test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.1.3 > 2.1.3+build

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is a corner case that we covered in all other sdks. you can see java link I shared above.

Copy link
Contributor

@mnoman09 mnoman09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@msohailhussain msohailhussain removed their assignment Sep 16, 2020
@msohailhussain msohailhussain removed the request for review from ozayr-zaviar September 16, 2020 18:30
const userValueType = typeof userValue;
const conditionValue = condition.value;

if (conditionValue === null || !isSafeInteger(conditionValue)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @mnoman09. If there is repeated logic for checking user inputs for numeric comparison conditions, let's use a shared helper function. If the logic is not exactly the same or it is awkward to share, don't worry about it.

Comment on lines +233 to +236
export const VERSION_TYPE = {
IS_PRE_RELEASE: '-',
IS_BUILD: '+'
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript has enum and const enum declarations. Some can be compiled into constant values leading to smaller code size. Please check the documentation for enums. Let's not use an object with long property names unless necessary, to avoid bloating the code.


}

export function compareVersion(conditionsVersion: string, userProvidedVersion: string): number | null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a documentation comment

Comment on lines +149 to +150
if (!userVersionParts || !conditionsVersionParts)
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style preference is to always use brackets with if statements.

Comment on lines +155 to +172
if (userVersionPartsLen <= idx)
return isPreReleaseInconditionsVersion || isBuildInconditionsVersion ? 1 : -1
else if (!isNumber(userVersionParts[idx])) {
if (userVersionParts[idx] < conditionsVersionParts[idx]) {
return isPreReleaseInconditionsVersion && !isPreReleaseInuserProvidedVersion ? 1 : -1;
}
else if (userVersionParts[idx] > conditionsVersionParts[idx]) {
return !isPreReleaseInconditionsVersion && isPreReleaseInuserProvidedVersion ? -1 : 1;
}
}
else {
const userVersionPart = parseInt(userVersionParts[idx])
const conditionsVersionPart = parseInt(conditionsVersionParts[idx])
if (userVersionPart > conditionsVersionPart)
return 1;
else if (userVersionPart < conditionsVersionPart)
return -1;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to simplify the control flow, there are a lot of nested conditionals.

*
*/
function isNumber(content: string): boolean {
return content.match(/^[0-9]+$/) != null ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this equivalent?

Suggested change
return content.match(/^[0-9]+$/) != null ? true : false;
return /^\d+$/.test(content);

(see: Regular Expression docs for JS)

Also: I suggest declaring regular expression once higher up in module scope, then reference here (DIGIT_STRING_REGEXP.test(content)).

*
*/
function hasWhiteSpaces(version: string): boolean {
return version.includes(' ');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about other space characters (tab, newline, etc.)? Should we use space regexp character class?

Suggested change
return version.includes(' ');
return /\s/.test(version);
  • Minor concern: String.prototype.includes is not supported in very old JS environments.
  • Is there any performance difference between String.prototype.includes and regular expression test method?

//check for pre release e.g. 1.0.0-alpha where 'alpha' is a pre release
//otherwise check for build e.g. 1.0.0+001 where 001 is a build metadata
if (isPreReleaseVersion(version)) {
targetPrefix = version.substring(0, version.indexOf(VERSION_TYPE.IS_PRE_RELEASE));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IS_PRE_RELEASE sounds like a boolean. I prefer PRE_RELEASE_VERSION_DELIMITER.

targetSuffix = version.substring(version.indexOf(VERSION_TYPE.IS_PRE_RELEASE) + 1);
}
else if (isBuildVersion(version)) {
targetPrefix = version.substring(0, version.indexOf(VERSION_TYPE.IS_BUILD));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUILD_VERSION_DELIMITER

Comment on lines +87 to +88
* @return {boolean} The array of version split into smaller parts i.e major, minor, patch etc
* null if given version is in invalid format
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would consumer code be clearer if we had an interface for semantic version components?

interface SemanticVersionComponents {
  major: number;
  minor: number;
  patch: number;
  preRelease?: number
  build?: number
}

Then:

function splitVersion(version: string): SemanticVersionComponents | null {

@raju-opti raju-opti deleted the uzair/semver-ts branch October 4, 2023 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants