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

[JS] adding handling for epoch dates #6504

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,15 @@
{{/usePromises}} };

{{#emitJSDoc}} /**
* Parses an ISO-8601 string representation of a date value.
* Parses an ISO-8601 string representation or epoch representation of a date value.
* @param {String} str The date value as a string.
* @returns {Date} The parsed date object.
*/
{{/emitJSDoc}} exports.parseDate = function(str) {
return new Date(str.replace(/T/i, ' '));
if (isNaN(str)) {
Copy link
Member

Choose a reason for hiding this comment

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

The Number.isNaN() method determines whether a value is NaN (Not-A-Number).

Ref: https://www.w3schools.com/jsref/jsref_isnan_number.asp

Did you mean to check if str is a number?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No I meant to check if str is not a number. If it's not a number then we'll default to the existing logic otherwise it's a number that we can just pass into Date.

Copy link
Member

Choose a reason for hiding this comment

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

return new Date(str.replace(/T/i, ' '));
}
return new Date(+str);
};

{{#emitJSDoc}} /**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,15 @@ class ApiClient {
}

{{#emitJSDoc}}/**
* Parses an ISO-8601 string representation of a date value.
* Parses an ISO-8601 string representation or epoch representation of a date value.
* @param {String} str The date value as a string.
* @returns {Date} The parsed date object.
*/{{/emitJSDoc}}
static parseDate(str) {
return new Date(str);
if (isNaN(str)) {
return new Date(str);
}
return new Date(+str);
}

{{#emitJSDoc}}/**
Expand Down
7 changes: 5 additions & 2 deletions samples/client/petstore/javascript-es6/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,15 @@ class ApiClient {
}

/**
* Parses an ISO-8601 string representation of a date value.
* Parses an ISO-8601 string representation or epoch representation of a date value.
* @param {String} str The date value as a string.
* @returns {Date} The parsed date object.
*/
static parseDate(str) {
return new Date(str);
if (isNaN(str)) {
return new Date(str);
}
return new Date(+str);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,15 @@ class ApiClient {
}

/**
* Parses an ISO-8601 string representation of a date value.
* Parses an ISO-8601 string representation or epoch representation of a date value.
* @param {String} str The date value as a string.
* @returns {Date} The parsed date object.
*/
static parseDate(str) {
return new Date(str);
if (isNaN(str)) {
return new Date(str);
}
return new Date(+str);
}

/**
Expand Down