Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isClass function #239

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

MarlonPassos-git
Copy link
Contributor

@MarlonPassos-git MarlonPassos-git commented Sep 8, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

Add the isClass function

 isClass(class CustomClass {}) // => true
 isClass('abc') // => false
 isClass({}) // => false

In the documentation, I emphasized that this function primarily validates whether a value is a custom class using the class syntax, rather than checking if the value is compatible with new. If we wanted to handle all cases of instantiable objects, including old-school function-based classes and “native classes”, we should implement separate functions for these cases. This function does not account for function constructors or native classes that can still be instantiated with new.

If we were to address this, we could consider implementing a separate utility, perhaps using an approach like this: https://www.npmjs.com/package/isclass?activeTab=code

Related issue, if any:

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed

Does this PR introduce a breaking change?

No

Bundle impact

Status File Size 1
A src/typed/isClass.ts 148

Footnotes

  1. Function size includes the import dependencies of the function.

@radashi-bot
Copy link

radashi-bot commented Sep 8, 2024

Benchmark Results

Name Current
isClass: with class 606,360.55 ops/sec ±5.08%
isClass: with non-class 4,049,967.1 ops/sec ±0.06%

Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure.

@MarlonPassos-git MarlonPassos-git changed the title feat: add isClass function feat: add isClass function Sep 8, 2024
Copy link
Member

@aleclarson aleclarson left a comment

Choose a reason for hiding this comment

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

Thank you for this 😎

I tweaked the Class type and the return type of isClass to be even more robust. Also added more “type test” cases.

benchmarks/typed/isClass.bench.ts Outdated Show resolved Hide resolved
benchmarks/typed/isClass.bench.ts Outdated Show resolved Hide resolved
docs/typed/isClass.mdx Outdated Show resolved Hide resolved
docs/typed/isClass.mdx Outdated Show resolved Hide resolved
docs/typed/isClass.mdx Outdated Show resolved Hide resolved
docs/typed/isClass.mdx Outdated Show resolved Hide resolved
MarlonPassos-git and others added 6 commits September 8, 2024 20:00
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
@MarlonPassos-git MarlonPassos-git added the new feature This PR adds a new function or extends an existing one label Sep 10, 2024
@aleclarson aleclarson added this to the v12.3.0 milestone Sep 21, 2024
@aleclarson
Copy link
Member

Hey! There's a new requirement for PRs that introduce new features. Without this requirement met, we won't be able to merge this. Note that this PR can still be included in a beta prerelease before this requirement is fulfilled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This PR adds a new function or extends an existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants