Skip to content

Commit 4a46d58

Browse files
committed
feat: add async validation on Is class
1 parent 8cb65f8 commit 4a46d58

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ Is.Uuid('not-valid-uuid') // false
178178
Is.Cep('not-valid-cep') // false
179179
Is.Cpf('not-valid-cpf') // false
180180
Is.Cnpj('not-valid-cnpj') // false
181+
Is.Async(() => {}) // false
182+
Is.Async(async () => {}) // true
183+
Is.Async(() => { new Promise((resolve => resolve())) }) // true
181184

182185
Is.String('value') // true
183186
Is.Undefined('value') // false

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/utils",
3-
"version": "1.6.2",
3+
"version": "1.6.3",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",

src/Classes/Is.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ export class Is {
123123
return isCnpj(cnpj)
124124
}
125125

126+
/**
127+
* Verify if is an async function
128+
*
129+
* @param value The value
130+
* @return true or false
131+
*/
132+
static Async(value: any) {
133+
const fnString = value.toString().trim()
134+
135+
const validation = !!(
136+
fnString.match(/^async/) || fnString.match(/return _ref[^.]*\.apply/)
137+
)
138+
139+
return validation || fnString.includes('new Promise(')
140+
}
141+
126142
/**
127143
* Verify if value is undefined
128144
*

src/utils/global.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare global {
3131
static Cep(cep: string | number): boolean
3232
static Cpf(cpf: string | number): boolean
3333
static Cpnj(cnpj: string | number): boolean
34+
static Async(value: any): boolean
3435
static Undefined(value: any): value is undefined
3536
static Null(value: any): value is null
3637
static Boolean(value: any): value is boolean

tests/Classes/is.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ describe('\n Is Class', () => {
2222
expect(Is.Cpf('529.461.090-62')).toBe(true)
2323
})
2424

25+
it('should verify if is a valid async function', async () => {
26+
expect(Is.Async(0)).toBe(false)
27+
expect(Is.Async('')).toBe(false)
28+
expect(Is.Async(() => '')).toBe(false)
29+
expect(Is.Async(async () => '')).toBe(true)
30+
expect(Is.Async(() => new Promise(resolve => resolve))).toBe(true)
31+
})
32+
2533
it('should verify if is a valid cnpj', async () => {
2634
expect(Is.Cnpj(0)).toBe(false)
2735
expect(Is.Cnpj('')).toBe(false)

0 commit comments

Comments
 (0)