validate
ts
/**
* 判断url是否是http或https
*
* @param {string} url 待检查的URL字符串。
* @returns 如果URL包含'http://'或'https://',返回true;否则返回false。
*/
export function isHttp_RY(url: string) {
if (typeof url !== 'string') {
return false
}
return url.includes('http://') || url.includes('https://')
}
/**
* 判断path是否为外链
*
* @param {string} path 要检查的路径字符串。
* @returns 如果路径是外部链接,则返回true;否则返回false。
*/
export function isExternal_RY(path: string) {
if (typeof path !== 'string') {
return false
}
return /^(https?:|mailto:|tel:)/.test(path)
}
/**
* 检查给定的URL是否有效。
*
* @param {string} url 待验证的URL字符串
* @returns 如果URL有效,则返回true;否则返回false。
*/
export function validURL_RY(url: string) {
const reg =
/^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*(?<!\.)$/
// 添加了长度限制
return reg.test(url) && url.length <= 2048
}
/**
* 检查字符串是否全部为小写字母。
*
* @param {string} str 待检查的字符串
* @returns 如果字符串全部为小写字母,则返回true;否则返回false。
*/
export function validLowerCase_RY(str: string) {
if (typeof str !== 'string') {
return false
}
const reg = /^[a-z]+$/
return reg.test(str)
}
/**
* 检查字符串是否由一个或多个大写字母组成。
*
* @param {string} str 待检查的字符串
* @returns 如果字符串仅包含大写字母,则返回true;否则返回false。
*/
export function validUpperCase_RY(str: string) {
if (typeof str !== 'string') {
return false
}
const reg = /^[A-Z]+$/
return reg.test(str)
}
/**
* 检查字符串是否只包含字母字符。
*
* @param {string} str 待验证的字符串
* @returns 如果字符串只包含字母字符,则返回true;否则返回false。
*/
export function validAlphabets_RY(str: string) {
if (typeof str !== 'string') {
return false
}
const reg = /^[A-Za-z]+$/
return reg.test(str)
}
/**
* 验证电子邮件地址的格式是否符合要求。
*
* @param {string} email 待验证的电子邮件地址
* @returns 如果电子邮件地址格式正确,则返回true;否则返回false。
*/
export function validEmail_RY(email: string) {
if (typeof email !== 'string') {
return false
}
const reg =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return reg.test(email)
}ts
import { describe, it, expect } from 'vitest'
import { isHttp_RY } from '../index'
describe('isHttp_RY function testing', () => {
it('1. 应对包含 http:// 的 URL 返回 true', () => {
const url = 'http://www.example.com'
expect(isHttp_RY(url)).toBe(true)
})
it('2. 应对包含 https:// 的 URL 返回 true', () => {
const url = 'https://www.example.com'
expect(isHttp_RY(url)).toBe(true)
})
it('3. 应对不包含 http:// 或 https:// 的 URL 返回 false', () => {
const url = 'ftp://www.example.com'
expect(isHttp_RY(url)).toBe(false)
})
it('4. 应对仅包含 http:// 中间有空格的 URL 返回 false', () => {
const url = 'http ://www.example.com'
expect(isHttp_RY(url)).toBe(false)
})
it('5. 应对包含 https:// 但没有域名的 URL 返回 true', () => {
const url = 'https://'
expect(isHttp_RY(url)).toBe(true)
})
it('6. 应对包含 http:// 在字符串中间的 URL 返回 true', () => {
const url = 'ftp://example.com/http://'
expect(isHttp_RY(url)).toBe(true)
})
it('7. 应对空字符串返回 false', () => {
const url = ''
expect(isHttp_RY(url)).toBe(false)
})
it('8. 应对 null 或 undefined 返回 false', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(isHttp_RY(null)).toBe(false)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(isHttp_RY(undefined)).toBe(false)
})
})