Add getDate for parsing date strings to Date objects#62
Merged
Conversation
Introduces getDate(key, defaultVal) and the date() alias, which parse values via JavaScript's Date constructor — ISO 8601 strings are the recommended input. Defaults may be ISO strings or Date instances (Date instances are returned as-is). Numbers and other types are rejected; use a Date instance if you want to pass a timestamp. Returns null when neither the env value nor the default resolves to a valid date. Also: - TypeScript declarations for getDate and date. - Test fixtures and tape tests covering ISO datetime, ISO date-only, invalid values, default fallback, string/Date defaults, unparseable defaults (garbage string, invalid Date, number, object), and the alias. - README: new example in the Type Conversion section. - Bumps minor to 7.8.0 (additive API). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
getDate(key, defaultVal)(and thedate()alias) for parsing ISO 8601 date strings into nativeDateobjects. This covers the common env-var pattern of release/deploy times, expiration dates, and schedule anchors. Additive and non-breaking — bumps minor to 7.8.0.Changed Files
src/index.js— newparseDatehelper plusgetDate/datemethods. UsesDateconstructor semantics, validates viaNumber.isFinite(d.getTime()). Default-fallback behavior mirrorsgetIPandgetDuration.src/index.d.ts— type declarations forgetDateanddatewith aDate | nullreturn.test/test.env— fixtures for a valid ISO datetime, a valid date-only string, and an invalid value.test/test.js— seven new test blocks (14 assertions) covering ISO datetime parsing, date-only parsing, invalid env values, default fallback, missing-key cases (string default, Date instance default, no default), unparseable defaults (garbage string, invalidDate, number, object), and the alias.README.md— new example in the Type Conversion section.package.json— 7.7.0 → 7.8.0.Design Notes
new Date(input), so anything the Date constructor accepts will work. Strings without a timezone are interpreted as local time per ECMA-262 — consumers should prefer ISO strings withZor+HH:MMfor portability.getDateis ambiguous (seconds vs. milliseconds) and mixes well-typed and stringly-typed behavior. Consumers who need that canenv.getNumber('TS')and pass tonew Date()themselves, or supply aDateinstance as the default.Steps to Verify
npm run ci— all 130 tests pass and 100% line coverage still holds.env.getDate('RELEASE_AT')withRELEASE_AT=2024-01-15T10:30:00Zreturns a validDateat that instant.env.getDate('UNSET', '2024-01-01')returns aDateat 2024-01-01 UTC.env.getDate('UNSET', new Date())returns thatDateinstance unchanged.env.getDate('UNSET')returnsnull.env.getDate('INVALID')whereINVALID=nopereturnsnull; same call with a valid default returns the parsed default.env.date('RELEASE_AT')matchesenv.getDate('RELEASE_AT').