Add getDuration for parsing time strings to milliseconds#61
Merged
Conversation
Introduces getDuration(key, defaultVal) and the duration() alias, which parse values like "500ms", "30s", "5m", "2h", "1d", "1w" into milliseconds. Units are case-insensitive, decimals are supported, and whitespace between the number and unit is allowed. When the env value is missing or unparseable the default is parsed (numeric defaults are treated as ms). Returns null when neither resolves. Also: - TypeScript declarations for getDuration and duration. - Test fixtures and tape tests covering every unit, decimals, case insensitivity, whitespace, invalid values, default fallback, and the alias. - README: new example in the Type Conversion section. - Bumps minor to 7.7.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
getDuration(key, defaultVal)(and theduration()alias) for parsing human-friendly time strings like"500ms","30s","5m","2h","1d","1w"into milliseconds. This covers a very common env-var pattern (timeouts, TTLs, intervals) that today requires hand-rolled parsing on top ofgetNumber. Additive and non-breaking — bumps minor to 7.7.0.Changed Files
src/index.js— newDURATION_UNITS,DURATION_RE, andparseDurationhelpers plusgetDuration/durationmethods. Default-fallback behavior mirrorsgetIP(invalid env value → fall back to parsing the default).src/index.d.ts— type declarations forgetDurationanddurationwith anumber | nullreturn.test/test.env— fixtures covering every unit, decimals, uppercase unit, and whitespace between number and unit.test/test.js— seven new test blocks (18 assertions) covering every unit, decimals/case/whitespace, invalid values, default fallback, missing-key cases (string default, numeric default, no default), unparseable defaults (string,NaN,{}), and the alias.README.md— new example in the Type Conversion section.package.json— 7.6.3 → 7.7.0.Steps to Verify
npm run ci— all 116 tests pass and 100% line coverage still holds.env.getDuration('CACHE_TTL')withCACHE_TTL=5mreturns300000.env.getDuration('UNSET', '30s')returns30000.env.getDuration('UNSET', 1000)returns1000(numeric default treated as ms).env.getDuration('UNSET')returnsnull.env.getDuration('INVALID')whereINVALID=nopereturnsnull; same call with a valid string default returns the parsed default.env.duration('CACHE_TTL')matchesenv.getDuration('CACHE_TTL').