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

Remove global flag on timestamp regex #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wnielson
Copy link

@wnielson wnielson commented Jan 10, 2025

The global flag on the TIMESTAMP_REGEX causes the call to TIMESTAMP_REGEX.test(value) to return a different value each time it is executed (regex objects are stateful when they have the global flag set1). Consider the following code:

const TIMESTAMP_REGEX = /^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/g;

const value = "2025-01-10T20:51:45.992Z";

console.log(TIMESTAMP_REGEX.test(value)); // true
console.log(TIMESTAMP_REGEX.test(value)); // false
console.log(TIMESTAMP_REGEX.test(value)); // true
console.log(TIMESTAMP_REGEX.test(value)); // false

When run this outputs:

true
false
true
false

This results in a bug where every other timestamp value is actually saved to the database as a string instead of a Timestamp.

@wnielson wnielson mentioned this pull request Jan 10, 2025
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.

1 participant