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

Merged
merged 22 commits into from
Nov 24, 2024

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 Difference
A src/typed/isClass.ts 148 +148

Footnotes

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

@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 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

aleclarson commented Nov 9, 2024

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.

@aleclarson aleclarson added the prerelease Publish to NPM under the "beta" or "next" tag label Nov 11, 2024
radashi-bot pushed a commit that referenced this pull request Nov 11, 2024
@MarlonPassos-git
Copy link
Contributor Author

@aleclarson i fix the next-minor.md but now this PR broken the Benchmark CI, and i don't know why. when i try to run the command pnpm bench isClass or pnpm bench in my device work well.

image

@radashi-bot
Copy link

radashi-bot commented Nov 23, 2024

Benchmark Results

Name Current
isClass: with class 579,482 ops/sec ±6.26%
isClass: with non-class 3,905,610.91 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.

@aleclarson aleclarson merged commit 2d1b3b2 into radashi-org:main Nov 24, 2024
9 checks passed
@radashi-bot
Copy link

A stable release 12.3.0 has been published to NPM. 🚀

To install:

See the changes

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 prerelease Publish to NPM under the "beta" or "next" tag
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants