-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 新增isMobile方法 * feat: 新增isEmail和isElement方法 * feat: 新增isHttp方法 * feat: 新增isUpperCase方法 * feat: 新增isEmptyObject方法 * feat: 新增isEmptyArray方法 * feat: 新增isIDCard方法 * feat: 新增isNumberString方法
- Loading branch information
Showing
50 changed files
with
936 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
* @returns 是否为空对象 | ||
*/ | ||
export function isEmptyObject(obj: unknown): boolean { | ||
return isObject(obj) && Reflect.ownKeys(obj as object).length === 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.