fix: implement missing utils and correct edge-case bugs - #324
Open
stooit wants to merge 1 commit into
Open
Conversation
- calculator.divide: throw on zero divisor instead of returning Infinity - string-utils.wordCount: split on /\s+/ to handle consecutive whitespace - string-utils.truncate: implement word-boundary truncation with ellipsis counting toward maxLength, plus whitespace-led fallback - task-manager: implement remove, update, and sortBy - date-utils: use Math.round for day bucket (off-by-one fix) - validator.isEmail: widen TLD length; validator.isUrl: allow ports
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
Fixes all 16 failing tests in the TypeScript utility library. 60/60 tests now pass,
tsc --noEmitclean. No test files modified, no dependencies added.Changes
src/calculator.ts—dividereturnedInfinityon a zero divisor; now throws. (-0is also caught since-0 === 0.)src/string-utils.tswordCountsplit on the literal" ", so consecutive spaces produced empty elements. Now trims and splits on/\s+/(also handles tabs/newlines).truncatewas unimplemented. Truncates at a word boundary with"..."counting towardmaxLength; returns input unchanged when within limit. Any whitespace is treated as a boundary, with a fallback to the hard slice so whitespace-led input never returns a bare ellipsis. Length contract (result.length <= maxLength) verified via fuzzing.src/task-manager.ts— implementedremove(delegates toMap.delete),update(returnsfalsefor unknown id; applies only fields explicitly present), andsortBy(priorityhigh>medium>low, statusin_progress>pending>completed,createdAtascending; sort is stable so ties keep insertion order).src/date-utils.ts— day bucket usedMath.floor(diffHours / 24)(36h → "1 day ago"); nowMath.round, fixing the off-by-one.src/validator.ts—isEmailcapped the TLD at 4 chars (rejected.museum); widened to{2,}while keepinguser@example/a@.comrejected.isUrlrequired an empty port (rejectedhttp://localhost:3000); dropped that clause while retaining thehttp/https-only allowlist.Verification
bun test→ 60 pass / 0 failbunx tsc --noEmit→ exit 0Assumptions / notes
isEmailis a pragmatic form validator, not RFC 5322 — accepts some addresses that would bounce and rejects some exotic-but-legal ones. Fine for form validation.truncatewithmaxLength < 3returns a clipped ellipsis (e.g.truncate("abc", 2) === ".."); no test covers it and the length contract holds, so behaviour was left as-is.updatecannot clear an optional field viaundefined). Worth a follow-up if those behaviours matter.