Skip to content

Commit

Permalink
feat: 新增issue30-43的方法 (#54)
Browse files Browse the repository at this point in the history
* feat: 新增isMobile方法

* feat: 新增isEmail和isElement方法

* feat: 新增isHttp方法

* feat: 新增isUpperCase方法

* feat: 新增isEmptyObject方法

* feat: 新增isEmptyArray方法

* feat: 新增isIDCard方法

* feat: 新增isNumberString方法
  • Loading branch information
vtrbo committed Jun 26, 2024
1 parent b3f4a76 commit f2f4ff8
Show file tree
Hide file tree
Showing 50 changed files with 936 additions and 259 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
// Enable the ESlint flat config support
// (remove this if your ESLint extension above v3.0.5)
"eslint.experimental.useFlatConfig": true,

// Disable the default formatter, use eslint instead
"prettier.enable": false,
"editor.formatOnSave": false,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"bumpp": "^9.4.1",
"eslint-plugin-format": "^0.1.1",
"esno": "^4.7.0",
"jsdom": "^24.1.0",
"lint-staged": "^15.2.7",
"magic-string": "^0.30.10",
"npm-run-all": "^4.1.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isArray.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isArray } from '../index'
import { isArray } from '../src/isArray'

describe('@xparcai-utils/is', () => {
it('isArray', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isBigInt.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isBigInt } from '../index'
import { isBigInt } from '../src/isBigInt'

describe('@xparcai-utils/is', () => {
it('isBigInt', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isBoolean.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isBoolean } from '../index'
import { isBoolean } from '../src/isBoolean'

describe('@xparcai-utils/is', () => {
it('isBoolean', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isDate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isDate } from '../index'
import { isDate } from '../src/isDate'

describe('@xparcai-utils/is', () => {
it('isDate', () => {
Expand Down
16 changes: 16 additions & 0 deletions packages/is/__test__/isElement.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @vitest-environment jsdom
*/

import { describe, expect, it } from 'vitest'
import { isElement } from '../src/isElement'

describe('@xparcai-utils/is', () => {
it('isElement', () => {
const div = document.createElement('div')
const span = document.createElement('span')
expect(isElement('abc')).toBe(false)
expect(isElement(div)).toBe(true)
expect(isElement(span)).toBe(true)
})
})
10 changes: 10 additions & 0 deletions packages/is/__test__/isEmail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest'
import { isEmail } from '../src/isEmail'

describe('@xparcai-utils/is', () => {
it('isEmail', () => {
expect(isEmail('abc')).toBe(false)
expect(isEmail('[email protected]')).toBe(true)
expect(isEmail('[email protected]')).toBe(true)
})
})
10 changes: 10 additions & 0 deletions packages/is/__test__/isEmptyArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest'
import { isEmptyArray } from '../src/isEmptyArray'

describe('@xparcai-utils/is', () => {
it('isEmptyArray', () => {
expect(isEmptyArray(undefined)).toBe(false)
expect(isEmptyArray([])).toBe(true)
expect(isEmptyArray([1])).toBe(false)
})
})
9 changes: 9 additions & 0 deletions packages/is/__test__/isEmptyObject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from 'vitest'
import { isEmptyObject } from '../src/isEmptyObject'

describe('@xparcai-utils/is', () => {
it('isEmptyObject', () => {
expect(isEmptyObject(undefined)).toBe(false)
expect(isEmptyObject({})).toBe(true)
})
})
8 changes: 8 additions & 0 deletions packages/is/__test__/isError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { describe, expect, it } from 'vitest'
import { isError } from '../src/isError'

describe('@xparcai-utils/is', () => {
it('isError', () => {
expect(isError(new Error())).toBe(true)
})
})
2 changes: 1 addition & 1 deletion packages/is/__test__/isFalse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isFalse } from '../index'
import { isFalse } from '../src/isFalse'

describe('@xparcai-utils/is', () => {
it('isFalse', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isFunction.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { isFunction } from '../index'
import { isFunction } from '../src/isFunction'

describe('@xparcai-utils/is', () => {
it('isFunction', () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/is/__test__/isHttp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from 'vitest'
import { isHttp } from '../src/isHttp'

describe('@xparcai-utils/is', () => {
it('isHttp', () => {
expect(isHttp('http://xparcai.com')).toBe(true)
expect(isHttp('https://xparcai.com', 's')).toBe(true)
expect(isHttp('http://xparcai.com', '||')).toBe(true)
expect(isHttp('https://xparcai.com', '||')).toBe(true)

// 更改默认值
isHttp.setCondition('s')
expect(isHttp('http://xparcai.com')).toBe(false)
expect(isHttp('https://xparcai.com')).toBe(true)

// 设置默认值调用
expect(isHttp.setCondition('s')('http://xparcai.com')).toBe(false)
expect(isHttp.setCondition('s')('https://xparcai.com')).toBe(true)
})
})
10 changes: 10 additions & 0 deletions packages/is/__test__/isIDCard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest'
import { isIDCard } from '../src/isIDCard'

describe('@xparcai-utils/is', () => {
it('isIDCard', () => {
expect(isIDCard('410224950512034')).toBe(true)
expect(isIDCard('4102249505120')).toBe(false)
expect(isIDCard('410224199803120318')).toBe(false)
})
})
9 changes: 9 additions & 0 deletions packages/is/__test__/isLowerCase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from 'vitest'
import { isLowerCase } from '../src/isLowerCase'

describe('@xparcai-utils/is', () => {
it('isLowerCase', () => {
expect(isLowerCase('http://xparcai.com')).toBe(false)
expect(isLowerCase('xparcai')).toBe(true)
})
})
2 changes: 1 addition & 1 deletion packages/is/__test__/isMap.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isMap } from '../index'
import { isMap } from '../src/isMap'

describe('@xparcai-utils/is', () => {
it('isMap', () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/is/__test__/isMobile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from 'vitest'
import { isMobile } from '../src/isMobile'

describe('@xparcai-utils/is', () => {
it('isMobile', () => {
expect(isMobile('12345678910')).toBe(false)
expect(isMobile('13523456789')).toBe(true)
})
})
2 changes: 1 addition & 1 deletion packages/is/__test__/isNaN.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isNaN } from '../index'
import { isNaN } from '../src/isNaN'

describe('@xparcai-utils/is', () => {
it('isNaN', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isNull.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isNull } from '../index'
import { isNull } from '../src/isNull'

describe('@xparcai-utils/is', () => {
it('isNull', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isNumber.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isNumber } from '../index'
import { isNumber } from '../src/isNumber'

describe('@xparcai-utils/is', () => {
it('isNumber', () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/is/__test__/isNumberString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest'
import { isNumberString } from '../src/isNumberString'

describe('@xparcai-utils/is', () => {
it('isNumberString', () => {
expect(isNumberString('abc')).toBe(false)
expect(isNumberString('0.95')).toBe(true)
expect(isNumberString('255')).toBe(true)
expect(isNumberString('255.323928392')).toBe(true)
expect(isNumberString('0000')).toBe(false)
expect(isNumberString('0.00')).toBe(true)
expect(isNumberString('00.00')).toBe(false)
})
})
2 changes: 1 addition & 1 deletion packages/is/__test__/isObject.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isObject } from '../index'
import { isObject } from '../src/isObject'

describe('@xparcai-utils/is', () => {
it('isObject', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isPromise.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isPromise } from '../index'
import { isPromise } from '../src/isPromise'

describe('@xparcai-utils/is', () => {
it('isPromise', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isRegExp.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isRegExp } from '../index'
import { isRegExp } from '../src/isRegExp'

describe('@xparcai-utils/is', () => {
it('isRegExp', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isSet.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isSet } from '../index'
import { isSet } from '../src/isSet'

describe('@xparcai-utils/is', () => {
it('isSet', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isString.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isString } from '../index'
import { isString } from '../src/isString'

describe('@xparcai-utils/is', () => {
it('isString', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isSymbol.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isSymbol } from '../index'
import { isSymbol } from '../src/isSymbol'

describe('@xparcai-utils/is', () => {
it('isSymbol', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isTrue.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isTrue } from '../index'
import { isTrue } from '../src/isTrue'

describe('@xparcai-utils/is', () => {
it('isTrue', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isType.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isType } from '../index'
import { isType } from '../src/isType'

describe('@xparcai-utils/is', () => {
it('isType', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/is/__test__/isUndefined.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { isUndefined } from '../index'
import { isUndefined } from '../src/isUndefined'

describe('@xparcai-utils/is', () => {
it('isUndefined', () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/is/__test__/isUpperCase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from 'vitest'
import { isUpperCase } from '../src/isUpperCase'

describe('@xparcai-utils/is', () => {
it('isUpperCase', () => {
expect(isUpperCase('http://xparcai.com')).toBe(false)
expect(isUpperCase('XPARCAI')).toBe(true)
})
})
10 changes: 10 additions & 0 deletions packages/is/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ export * from './src/isBoolean'
export * from './src/isSet'
export * from './src/isArray'
export * from './src/isObject'
export * from './src/isMobile'
export * from './src/isEmail'
export * from './src/isElement'
export * from './src/isHttp'
export * from './src/isLowerCase'
export * from './src/isEmptyObject'
export * from './src/isEmptyArray'
export * from './src/isError'
export * from './src/isIDCard'
export * from './src/isNumberString'
12 changes: 12 additions & 0 deletions packages/is/src/isElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { isUndefined } from './isUndefined'

/**
* 某个数据是否是element类型
* @param data 某个数据
* @returns 是否是element类型
*/
export function isElement(data: unknown): data is Element {
if (isUndefined(data))
return false
return data instanceof Element
}
10 changes: 10 additions & 0 deletions packages/is/src/isEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 某个数据是否为邮箱
* @param email 某个数据
* @returns 是否为邮箱
*/
export function isEmail(email: string) {
const reg
= /^(?:[^<>()[\]\\.,;:\s@"]+(?:\.[^<>()[\]\\.,;:\s@"]+)*|".+")@(?:\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]|(?:[a-z\-0-9]+\.)+[a-z]{2,})$/i
return reg.test(email)
}
10 changes: 10 additions & 0 deletions packages/is/src/isEmptyArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { isArray } from './isArray'

/**
* 某个数组是否为空数组
* @param arr 某个数组
* @returns 是否为空数组
*/
export function isEmptyArray(arr: unknown): boolean {
return isArray(arr) && !arr.length
}
10 changes: 10 additions & 0 deletions packages/is/src/isEmptyObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { isObject } from './isObject'

/**
* 某个对象是否为空对象
* @param object 某个对象

Check warning on line 5 in packages/is/src/isEmptyObject.ts

View workflow job for this annotation

GitHub Actions / lint

Expected @param names to be "obj". Got "object"
* @returns 是否为空对象
*/
export function isEmptyObject(obj: unknown): boolean {
return isObject(obj) && Reflect.ownKeys(obj as object).length === 0
}
10 changes: 10 additions & 0 deletions packages/is/src/isError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { isType } from './isType'

/**
* 某个数据是否为Error类型
* @param data 某个数据
* @returns 是否为Error类型
*/
export function isError(data: unknown): boolean {
return isType(data, 'Error')
}
27 changes: 27 additions & 0 deletions packages/is/src/isHttp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type Condition = '' | 's' | '||'

let _condition: Condition = ''

/**
* 某个地址是否是http(s)
* @param url 某个地址
* @param condition 条件, 默认''; '': 仅http; 's': 仅https; '||': http或https
* @returns 是否是http(s)
*/
export function isHttp(url: string, condition: Condition = _condition): boolean {
const conditionMap: Record<Condition, () => boolean> = {
'': () => url.includes('http://'),
's': () => url.includes('https://'),
'||': () => url.includes('http://') || url.includes('https://'),
}
return conditionMap[condition]()
}

/**
* 设置isHttp函数的默认条件
* @param condition 条件, 默认'';
*/
isHttp.setCondition = function (condition: Condition = _condition) {
_condition = condition
return isHttp
}
Loading

0 comments on commit f2f4ff8

Please sign in to comment.