Skip to content

Commit

Permalink
Merge pull request #2 from wgbx/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
wgbx authored Dec 4, 2024
2 parents bacdd8e + e5023d5 commit 50d230e
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/constant/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const ERROR_MUST_STRING = 'Input must be a string'

export const ERROR_MUST_NUMBER = 'Input must be a number'
1 change: 1 addition & 0 deletions packages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './utils/Browser/index'
export * from './utils/String/index'
export * from './utils/Time/index'
export * from './utils/Object/index'
export * from './utils/Type/index'
4 changes: 0 additions & 4 deletions packages/utils/Regex/isChines.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packages/utils/Regex/isChinese.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isString } from '../../index'

/**
* 判断给定的字符串是否全部由中文汉字组成
* @param {string} str - 要进行判断的字符串
* @returns {boolean} - 如果输入的字符串全部由中文汉字组成,返回 `true`;否则返回 `false`
* @example
* console.log(isChinese("你好")); // 输出 `true`
* console.log(isChinese("hello你好")); // 输出 `false`
* console.log(isChinese("")); // 输出 `false`
*/

export const isChinese = (str: string): boolean => {
if (isString(str)) {
return /^[\u4E00-\u9FA5]+$/.test(str)
}
return false
}
2 changes: 1 addition & 1 deletion packages/utils/Regex/isEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @param {string} value - 要验证的字符串
* @returns {boolean} 如果字符串是有效的电子邮件地址则返回 true,否则返回 false
*
*
* @example
* // 示例 1: 验证有效的电子邮件地址
* const isValid1 = isEmail("[email protected]");
Expand Down
12 changes: 10 additions & 2 deletions packages/utils/String/firstUpperCase.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { ERROR_MUST_STRING } from '../../constant/index'
import { isString } from '../../index'

/**
* 将字符串的首字母大写
* @param {string} str - 输入的字符串
* @returns {string} 首字母大写的字符串
*
* @throws {Error} - 当输入的参数不是字符串类型时,抛出错误,错误消息提示参数必须为字符串类型
* @example
* console.log(capitalizeFirstLetter("hello")); // 输出 "Hello"
* console.log(capitalizeFirstLetter("world")); // 输出 "World"
*/
export const firstUpperCase = (str: string) => str.charAt(0).toUpperCase() + str.slice(1)
export function firstUpperCase(str: string) {
if (isString(str)) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
throw new Error(ERROR_MUST_STRING)
}
10 changes: 8 additions & 2 deletions packages/utils/String/getCamelCase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { ERROR_MUST_STRING } from '../../constant/index'
import { isString } from '../../index'

/**
* 将字符串转换为 camel case 格式
*
* @param {string} str - 需要转换的字符串
* @returns {string} - 转换后的 camel case 格式字符串
* @throws {Error} - 当输入的参数不是字符串类型时,抛出错误,错误消息提示参数必须为字符串类型
* @example
* const camelCaseStr = getCamelCase("hello-world");
* console.log(camelCaseStr); // 输出: "helloWorld"
*/
export function getCamelCase(str: string) {
return str.replace(/-([a-z])/g, (i, item) => item.toUpperCase())
if (isString(str)) {
return str.replace(/-([a-z])/g, (i, item) => item.toUpperCase())
}
throw new Error(ERROR_MUST_STRING)
}
10 changes: 8 additions & 2 deletions packages/utils/String/getKebabCase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { ERROR_MUST_STRING } from '../../constant/index'
import { isString } from '../../index'

/**
* 将字符串转换为 kebab case 格式
*
* @param {string} str - 需要转换的字符串
* @returns {string} - 转换后的 kebab case 格式字符串
* @throws {Error} - 当输入的参数不是字符串类型时,抛出错误,错误消息提示参数必须为字符串类型
* @example
* const kebabCaseStr = getKebabCase("HelloWorld");
* console.log(kebabCaseStr); // 输出: "hello-world"
*/
export function getKebabCase(str) {
return str.replace(/[A-Z]/g, item => '-' + item.toLowerCase())
if (isString(str)) {
return str.replace(/[A-Z]/g, item => '-' + item.toLowerCase())
}
throw new Error(ERROR_MUST_STRING)
}
14 changes: 10 additions & 4 deletions packages/utils/String/telEncrypt.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { isNumber } from '../../index'
import { ERROR_MUST_NUMBER } from '../../constant/index'

/**
* 对手机号进行加密处理
*
* @param {number} tel - 需要加密的手机号
* @returns {string} - 加密后的手机号
*
* @throws {Error} - 当输入的参数不是数值类型时,抛出错误,错误消息提示参数必须为数值类型
* @example
* const encryptedTel = telEncrypt(1234567890);
* console.log(encryptedTel); // 输出: "123****7890"
*/
export function telEncrypt(tel) {
return tel.toString().replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')

export function telEncrypt(tel: number): string {
if (isNumber(tel)) {
return tel.toString().replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}
throw new Error(ERROR_MUST_NUMBER)
}
2 changes: 2 additions & 0 deletions packages/utils/Type/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './isString'
export * from './isNumber'
13 changes: 13 additions & 0 deletions packages/utils/Type/isNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getTypeOf } from '../Function/getTypeOf'

/**
* 判断给定的值是否为数值类型
* @param {*} val - 需要进行类型判断的任意值
* @returns {boolean} - 如果传入值是数值类型,则返回 `true`,否则返回 `false`
* @example
* console.log(isString('hello')); // 输出 `true`
* console.log(isString(123)); // 输出 `false`
*/
export function isNumber(val) {
return getTypeOf(val) === 'number'
}
13 changes: 13 additions & 0 deletions packages/utils/Type/isString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getTypeOf } from '../Function/getTypeOf'

/**
* 判断给定的值是否为字符串类型
* @param {*} val - 需要进行类型判断的任意值
* @returns {boolean} - 如果传入值是字符串类型,则返回 `true`,否则返回 `false`
* @example
* console.log(isString('hello')); // 输出 `true`
* console.log(isString(123)); // 输出 `false`
*/
export function isString(val) {
return getTypeOf(val) === 'string'
}

0 comments on commit 50d230e

Please sign in to comment.