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

Refactor parseDate #487

Merged
merged 3 commits into from
Feb 18, 2019
Merged

Refactor parseDate #487

merged 3 commits into from
Feb 18, 2019

Conversation

suspectpart
Copy link
Contributor

See #486

@codecov-io
Copy link

codecov-io commented Feb 11, 2019

Codecov Report

Merging #487 into dev will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@         Coverage Diff         @@
##            dev   #487   +/-   ##
===================================
  Coverage   100%   100%           
===================================
  Files        58     58           
  Lines       509    511    +2     
  Branches     83     85    +2     
===================================
+ Hits        509    511    +2
Impacted Files Coverage Δ
src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 41eb641...6669985. Read the comment docs.

src/index.js Outdated
reg[1], reg[2] - 1, reg[3] || 1,
reg[4] || 0, reg[5] || 0, reg[6] || 0, reg[7] || 0
)
if (typeof date === 'string' && !date.toLowerCase().endsWith('z')) {
Copy link
Owner

Choose a reason for hiding this comment

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

Just noticed that endsWith is a new added method in js, with cost some more bytes than /.*[^Z]$/i.test in the final es5 code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but the concept is clearer. I also saved some letters on renaming reg[...] to d[...] multiple times and removing the let reg, wouldn't that even it out?

Copy link
Owner

Choose a reason for hiding this comment

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

reg will be minified in building I think, and endsWith becomes the added bytes.

Copy link
Contributor Author

@suspectpart suspectpart Feb 11, 2019

Choose a reason for hiding this comment

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

Funnily, this was never needed at all, there is a bug in the regex.

If you look at it

export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/

you wonder: Why does it even match a z or Z it in the end? It is because the last dot in the regex is not escaped (the dot in part ?.?(\d{1,3})?), so that matches any character and therefore matches a z / Z.

If you change it to

export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?\.?(\d{1,3})?$/i

(note the backlash to escape the dot and the i flag), all tests are still green if we just do

  if (typeof date === 'string') {
    const d = date.match(C.REGEX_PARSE)
    if (d) {
      return new Date(d[1], d[2] - 1, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)
    }
  }

So we even save a lot of bytes :-).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed that fix to PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I did, and even shortened it as /z$/i does the same (asserting it ends with a z, case insensitive). .*[^Z]$ was doing too much, no need to match any character 0-n times.

Copy link
Owner

Choose a reason for hiding this comment

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

/z$/i.test('z') doesn't make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I fixed it. I could just use the web view as I do not have the solution checked out right now, will crush those commits, sorry.

Copy link
Contributor Author

@suspectpart suspectpart Feb 13, 2019

Choose a reason for hiding this comment

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

Needs to be /[^Z]$/i.test(date), getting rid of .*.

Copy link
Contributor Author

@suspectpart suspectpart Feb 13, 2019

Choose a reason for hiding this comment

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

Ok, I crushed those commits and put the regex back. I tried to keep the Regex as short as possible, so I no went for

!/Z$/i.test(date) rather then /[^Z]$i.test(date)`, as it is

  • less bytes (not many)
  • easier to comprehend the negation outside of the regex instead of with the [^Z] negation syntax. Is that ok now?

@@ -46,6 +46,40 @@ describe('Parse', () => {
expect(dayjs(time).valueOf()).toBe(moment(time).valueOf())
})

it('String RFC 2822, time and zone', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

The added test suit here seems just test Date object rather dayjs here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just wanted to make that functionality explicit, because right now it is neither documented nor explicitly handled in the tests. Whenever we change our internal implementation we now get feedback whether our assumptions are still valid, right? Will tell you about breaking changes then.

@ghost ghost mentioned this pull request Feb 12, 2019
@suspectpart suspectpart force-pushed the 486-refactor-parse-date branch from c6134e6 to 4fa7ddf Compare February 12, 2019 17:02
@suspectpart suspectpart force-pushed the 486-refactor-parse-date branch from a106c24 to 6669985 Compare February 13, 2019 09:28
@suspectpart
Copy link
Contributor Author

Are we good to merge this and focus on the other PR (shortening the regex)? The other one is ready to be checked with the CI build and cross-browser testing.

@iamkun iamkun merged commit d4cd3ad into iamkun:dev Feb 18, 2019
@iamkun
Copy link
Owner

iamkun commented Feb 24, 2019

🎉 This PR is included in version 1.8.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants