Skip to content

Add option to disable automatic date parsing#1130

Merged
Belphemur merged 2 commits intoBelphemur:developfrom
SergioChan:fix/optional-date-parsing
Mar 10, 2026
Merged

Add option to disable automatic date parsing#1130
Belphemur merged 2 commits intoBelphemur:developfrom
SergioChan:fix/optional-date-parsing

Conversation

@SergioChan
Copy link
Contributor

@SergioChan SergioChan commented Mar 6, 2026

Summary

  • add a parseDates flag to JsonAdapter/Config (default true)
  • allow disabling ISO date-string coercion to preserve string values
  • document the new Config argument

Testing

  • not run (not provided)

Fixes issue #486

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the node-json-db library by providing a new configuration option that gives users more granular control over how date-like strings are handled during data retrieval. This change addresses a common need to prevent unintended type conversions, ensuring data integrity and flexibility for applications that prefer to manage date strings explicitly.

Highlights

  • New Configuration Option: Introduced a parseDates flag in JsonAdapter and Config classes, allowing users to control automatic ISO date string parsing.
  • Date Parsing Control: The new parseDates flag, defaulting to true, enables disabling the automatic coercion of ISO date strings into Date objects, preserving them as strings if desired.
  • Documentation Update: The README.md file has been updated to document the new parseDates argument in the Config constructor.
Changelog
  • README.md
    • Updated the example JsonDB initialization to include the new parseDates argument in the Config constructor.
  • src/adapter/data/JsonAdapter.ts
    • Added a parseDates private readonly property to the JsonAdapter class.
    • Modified the JsonAdapter constructor to accept a parseDates argument with a default value of true.
    • Updated the reviver method to conditionally parse date strings into Date objects only if parseDates is true.
  • src/lib/JsonDBConfig.ts
    • Added a parseDates property to the Config class interface.
    • Modified the Config constructor to accept a parseDates argument with a default value of true.
    • Ensured the parseDates value is passed to the JsonAdapter constructor when initializing both standard and ciphered adapters.
Activity
  • No testing information was provided for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.60%. Comparing base (f376e98) to head (1573d38).
⚠️ Report is 6 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1130   +/-   ##
========================================
  Coverage    99.60%   99.60%           
========================================
  Files           12       12           
  Lines         1527     1531    +4     
  Branches       322      322           
========================================
+ Hits          1521     1525    +4     
  Misses           6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a useful option to disable automatic parsing of date strings. The implementation is straightforward. However, the pull request is missing unit tests for the new functionality, which is a significant concern, especially since the description states that testing was not performed. I've added a comment highlighting the need for tests. I also have a couple of smaller suggestions: one to improve the example in the README.md to better demonstrate the new feature, and another for a minor code improvement in JsonAdapter.ts for regex matching.

Comment on lines +11 to 15
constructor(adapter: IAdapter<string>, humanReadable: boolean = false, parseDates: boolean = true) {
this.adapter = adapter;
this.humanReadable = humanReadable;
this.parseDates = parseDates;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This change introduces new functionality, but it's missing corresponding unit tests. Please add a test case to verify that when parseDates is set to false, date strings are not converted to Date objects. The PR description also mentions 'Testing: not run (not provided)', which is concerning.

README.md Outdated
// The fourth argument is the separator. By default it's slash (/)
// The fifth argument enables sync writes (default false)
// The sixth argument controls automatic Date parsing for ISO strings (default true)
var db = new JsonDB(new Config("myDataBase", true, false, '/', false, true));
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To better illustrate the new feature, it would be more helpful to show an example where date parsing is disabled (false). The current example uses true, which is the default and doesn't showcase the new capability.

Suggested change
var db = new JsonDB(new Config("myDataBase", true, false, '/', false, true));
var db = new JsonDB(new Config("myDataBase", true, false, '/', false, false));


private reviver(key: string, value: any): any {
if (typeof value == "string" && this.dateRegex.exec(value) != null) {
if (this.parseDates && typeof value == "string" && this.dateRegex.exec(value) != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For checking if a regex matches a string, it's more idiomatic and slightly more performant to use RegExp.prototype.test() instead of RegExp.prototype.exec(). test() returns a boolean and doesn't create an array of matches.

Suggested change
if (this.parseDates && typeof value == "string" && this.dateRegex.exec(value) != null) {
if (this.parseDates && typeof value == "string" && this.dateRegex.test(value)) {

@SergioChan
Copy link
Contributor Author

Follow-up (clean version): addressed in commit 1573d38.

Changes made:

  • Added unit test coverage for parseDates=false behavior (test/adapter/adapters.test.ts).
  • Switched JsonAdapter date match check from RegExp.exec() to RegExp.test().
  • Updated README example to demonstrate disabling date parsing (6th Config arg set to false).

Validation run:

  • npm test -- test/adapter/adapters.test.ts --runInBand
  • Result: PASS (12 tests)

@SergioChan
Copy link
Contributor Author

Correction (previous comment formatting got mangled by my CLI quoting):

Addressed in follow-up commit 1573d38:

  • Added regression coverage for parseDates=false in test/adapter/adapters.test.ts
  • Switched date matching from exec(...) != null to test(...)
  • Updated README example to demonstrate disabling automatic date parsing

Validation run (pass): npm test -- --runInBand test/adapter/adapters.test.ts

@Belphemur
Copy link
Owner

Hello @SergioChan,

Thanks for your contribution, I'm adding to the lib :)

@all-contributors add @SergioChan for code

@Belphemur Belphemur merged commit 103f704 into Belphemur:develop Mar 10, 2026
10 checks passed
@allcontributors
Copy link
Contributor

@Belphemur

I've put up a pull request to add @SergioChan! 🎉

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.

2 participants