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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions benchmarks/typed/isClass.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as _ from 'radashi'

describe('isClass', () => {
bench('with class', () => {
_.isNumber(class CustomClass {})
MarlonPassos-git marked this conversation as resolved.
Show resolved Hide resolved
})

bench('with non-class', () => {
_.isNumber({})
MarlonPassos-git marked this conversation as resolved.
Show resolved Hide resolved
})
})
31 changes: 31 additions & 0 deletions docs/typed/isClass.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: isClass
description: Check if a value is a class defined using the `class` syntax.
MarlonPassos-git marked this conversation as resolved.
Show resolved Hide resolved
---

### Usage

The `isClass` function returns `true` if the provided value is a class defined with the new `class` syntax.
MarlonPassos-git marked this conversation as resolved.
Show resolved Hide resolved

```ts
import * as _ from 'radashi'

_.isClass(Error) // => true
class MyClass {}
_.isClass(MyClass) // => true
MarlonPassos-git marked this conversation as resolved.
Show resolved Hide resolved
_.isClass(function OldClass() {}) // => false
_.isClass('abc') // => false
_.isClass({}) // => false
_.isClass(undefined) // => false
```

:::note

Old school classes (defined using constructor functions) will return `false`.

"Native classes", like `Error`, will return `false` because they are not defined using the `class` syntax.
:::
MarlonPassos-git marked this conversation as resolved.
Show resolved Hide resolved

### Popular use cases

- Type guard would check for a function that must be called with the `new` operator
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export * from './string/trim.ts'

export * from './typed/isArray.ts'
export * from './typed/isBoolean.ts'
export * from './typed/isClass.ts'
export * from './typed/isDate.ts'
export * from './typed/isEmpty.ts'
export * from './typed/isEqual.ts'
Expand Down
18 changes: 18 additions & 0 deletions src/typed/isClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isFunction, type Class } from 'radashi'
/**
* Checks if the given value is a class. This function verifies
* if the value was defined using the `class` syntax. Old school
* classes (defined with constructor functions) will return false.
* "Native classes" like `Error` will also return false.
*
* @see https://radashi.js.org/reference/typed/isClass
* @example
* ```ts
* isClass(class CustomClass {}) // => true
* isClass('abc') // => false
* isClass({}) // => false
* ```
*/
export function isClass(value: unknown): value is Class {
return isFunction(value) && value.toString().startsWith('class ')
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export declare class Any {
private any: typeof any
}

/**
* Represents a class constructor.
*/
export type Class<T = unknown> = new (...args: any[]) => T

/**
* Extracts `T` if `T` is not `any`, otherwise `never`.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/typed/isClass.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Class } from 'radashi'
import * as _ from 'radashi'
import { expectTypeOf } from 'vitest'

describe('isClass', () => {
test('Type inference', () => {
const value = {} as unknown
console.log('value is', value)
if (_.isClass(value)) {
console.log('value is a class')
expectTypeOf(value).toMatchTypeOf<Class>()
assertType(new value())
} else {
console.log('value is not a class')
expectTypeOf(value).not.toMatchTypeOf<Class>()
}
})
})
33 changes: 33 additions & 0 deletions tests/typed/isClass.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as _ from 'radashi'

function OldSchoolClass(something: string) {
// @ts-ignore
this.something = something
}
OldSchoolClass.prototype.doSomething = function () {
return `do ${this.something}`
}

describe('isClass', () => {
class Data {}
test('returns false for non-Class values', () => {
const fn = () => {}
expect(_.isClass(OldSchoolClass)).toBeFalsy()
expect(_.isClass(fn)).toBeFalsy()
expect(_.isClass(undefined)).toBeFalsy()
expect(_.isClass(null)).toBeFalsy()
expect(_.isClass(false)).toBeFalsy()
expect(_.isClass(() => {})).toBeFalsy()
expect(_.isClass(async () => {})).toBeFalsy()
expect(_.isClass(new Data())).toBeFalsy()
expect(_.isClass(Number.NaN)).toBeFalsy()
expect(_.isClass([1, 2, 3])).toBeFalsy()
expect(_.isClass({})).toBeFalsy()
expect(_.isClass('abc')).toBeFalsy()
expect(_.isClass(String('abc'))).toBeFalsy()
})
test('returns true for class values', () => {
expect(_.isClass(class CustomError extends Error {})).toBeTruthy()
expect(_.isClass(class CustomClass {})).toBeTruthy()
})
})
Loading