denoland / deno_std

The Deno Standard Library
https://jsr.io/@std
MIT License
2.83k stars 581 forks source link

BREAKING(semver): remove `canParse()` #5179

Closed iuioiua closed 2 days ago

iuioiua commented 4 days ago

What's changed

canParse() has been removed from @std/semver in favor of tryParse().

Motivation

This change was made because tryParse() provides extended functionality over canParse(), as it allows the user to determine whether a string is able to be parsed into a SemVer version, but returns that SemVer object too.

Migration

To migrate, use tryParse() instead of canParse(), and compare the return value to null.

- import { canParse, parse } from "@std/semver/can-parse";
+ import { tryParse } from "@std/semver/try-parse";

  const str = "1.2.3";
- if (canParse(str)) {
- const semver = parse(str);
+ const semver = tryParse(str);
+ if (semver !== null) {
    console.log(`The SemVer is {semver}`);
  } else {
    console.log("The string is not a valid SemVer");
  }

CC @timreichen

codecov[bot] commented 4 days ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 94.15%. Comparing base (26914ff) to head (d379578).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #5179 +/- ## ======================================= Coverage 94.15% 94.15% ======================================= Files 468 467 -1 Lines 37873 37856 -17 Branches 5469 5467 -2 ======================================= - Hits 35658 35644 -14 + Misses 2176 2173 -3 Partials 39 39 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

timreichen commented 4 days ago

I think it would be better the other way around. canParse() is borrowed from web APIs like URL.prototype.canParse while tryParse() was rejected as a web API (ref https://github.com/denoland/deno_std/pull/4049)

iuioiua commented 4 days ago

The motivation for this PR (see initial comment), I think, is a good counterargument for doing that. This is what Yoshiya previously explained. I didn't agree with it then, but I do now.

kt3k commented 4 days ago

now URL has URL.parse which returns URL | null https://github.com/whatwg/url/pull/825 that is mostly aligned to semver.tryParse.

kt3k commented 3 days ago

Some deno projects (deployctl, publish-on-tag) seem using canParse.

https://github.com/search?q=semver+canParse+language%3ATypeScript&type=code&l=TypeScript

Do we need to remove this?

How about keeping all three APIs (parse, tryParse, canParse)?

iuioiua commented 2 days ago

My opinion on this isn't strong. Existing use is a sufficient reason to abandon the idea. Fine with me. Closing.