Skip to content

Commit

Permalink
feat: add _.isBigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
heathcliff-hu committed Sep 11, 2024
1 parent 069b26c commit 72201ac
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/typed/is-bigint.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: isBigInt
description: 'Determine if a value is a bigint'
group: Typed
---

## Basic usage

Pass in a value and get a boolean telling you if the value is a bigint.

```ts
import { isBigInt } from 'radash'

isBigInt('hello') // => false
isBigInt(['hello']) // => false
isBigInt(12) // => false
isBigInt(0n) // => true
```
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export {
} from './string'
export {
isArray,
isBigInt,
isDate,
isEmpty,
isEqual,
Expand Down
56 changes: 56 additions & 0 deletions src/tests/typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,62 @@ describe('typed module', () => {
})
})

describe('isBigInt function', () => {
test('returns false for null', () => {
const result = _.isBigInt(null)
assert.isFalse(result)
})
test('returns false for undefined', () => {
const result = _.isBigInt(undefined)
assert.isFalse(result)
})
test('returns false for boolean', () => {
const result = _.isBigInt(false)
assert.isFalse(result)
})
test('returns false for class instance', () => {
class Data {}
const result = _.isBigInt(new Data())
assert.isFalse(result)
})
test('returns false for int', () => {
const result = _.isBigInt(22)
assert.isFalse(result)
})
test('returns false for float', () => {
const result = _.isBigInt(22.0567)
assert.isFalse(result)
})
test('returns false for NaN', () => {
const result = _.isBigInt(NaN)
assert.isFalse(result)
})
test('returns false for array', () => {
const result = _.isBigInt([1, 2, 3])
assert.isFalse(result)
})
test('returns false for object', () => {
const result = _.isBigInt({})
assert.isFalse(result)
})
test('returns false for string', () => {
const result = _.isBigInt('abc')
assert.isFalse(result)
})
test('returns false for string class', () => {
const result = _.isBigInt(String('abc'))
assert.isFalse(result)
})
test('returns true for bigint', () => {
const result = _.isBigInt(22n)
assert.isTrue(result)
})
test('returns true for bigint class', () => {
const result = _.isBigInt(BigInt(22))
assert.isTrue(result)
})
})

describe('isInt function', () => {
class Data {}
test('returns false for non-number values', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export const isNumber = (value: any): value is number => {
}
}

export const isBigInt = (value: any): value is bigint => {
return typeof value === 'bigint'
}

export const isDate = (value: any): value is Date => {
return Object.prototype.toString.call(value) === '[object Date]'
}
Expand Down

0 comments on commit 72201ac

Please sign in to comment.