-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from wgbx/release
Release
- Loading branch information
Showing
12 changed files
with
87 additions
and
15 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
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' |
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 was deleted.
Oops, something went wrong.
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,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 | ||
} |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* | ||
* @param {string} value - 要验证的字符串 | ||
* @returns {boolean} 如果字符串是有效的电子邮件地址则返回 true,否则返回 false | ||
* | ||
* | ||
* @example | ||
* // 示例 1: 验证有效的电子邮件地址 | ||
* const isValid1 = isEmail("[email protected]"); | ||
|
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 |
---|---|---|
@@ -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) | ||
} |
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 |
---|---|---|
@@ -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) | ||
} |
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 |
---|---|---|
@@ -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) | ||
} |
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 |
---|---|---|
@@ -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) | ||
} |
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,2 @@ | ||
export * from './isString' | ||
export * from './isNumber' |
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,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' | ||
} |
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,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' | ||
} |