From 683a09a261a0fa4967532c27b65ea4cc010cf174 Mon Sep 17 00:00:00 2001 From: qjuly <1719483930@qq.com> Date: Fri, 8 Jul 2022 13:23:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20#27=20=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1.=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E8=BD=AC?= =?UTF-8?q?=E4=B9=89=E5=92=8C=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/canvas/index.ts | 9 -------- src/string/index.ts | 52 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/canvas/index.ts b/src/canvas/index.ts index ee7012a..b8a90b5 100644 --- a/src/canvas/index.ts +++ b/src/canvas/index.ts @@ -1,12 +1,3 @@ -/** - * @func isSupportWebP - * @returns {boolean} - * @desc 判断浏览器是否支持webP格式图片 - */ -export function isSupportWebP() { - return !![].map && document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') == 0; -} - /** * @func getImageBase64Url * @param {HTMLImageElement} image diff --git a/src/string/index.ts b/src/string/index.ts index 231daea..2ed256c 100644 --- a/src/string/index.ts +++ b/src/string/index.ts @@ -21,7 +21,7 @@ export const randomString = (len: number) => { */ export const firstUpperCase = (str: string) => { return str.charAt(0).toUpperCase() + str.slice(1); -} +}; /** * @func telEncrypt @@ -31,7 +31,7 @@ export const firstUpperCase = (str: string) => { */ export const telEncrypt = (tel: number) => { return tel.toString().replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); -} +}; /** * @func getKebabCase @@ -40,8 +40,8 @@ export const telEncrypt = (tel: number) => { * @desc 转换成短横线命名 */ export const getKebabCase = (str: string) => { - return str.replace(/[A-Z]/g, (item) => '-' + item.toLowerCase()) -} + return str.replace(/[A-Z]/g, item => '-' + item.toLowerCase()); +}; /** * @func getCamelCase * @param {string} str @@ -49,9 +49,45 @@ export const getKebabCase = (str: string) => { * @desc 转换成驼峰命名 */ export const getCamelCase = (str: string) => { - return str.replace(/-([a-z])/g, (i, item) => item.toUpperCase()) -} - - + return str.replace(/-([a-z])/g, (i, item) => item.toUpperCase()); +}; +/** + * @func getEscapeString + * @param {string} str + * @returns {string} + * @desc 字符串的转义,将`&`, `<`, `>`, `"`, `'`分别转义为`&`, `<`, `>`, `"`, `'` + */ +export const getEscapeString = (str: string): string => { + const ESCAPE = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''', + }; + const ESCAPE_EXPR = /(&|<|>|"|')/g; + return ('' + str).replace(ESCAPE_EXPR, match => { + return ESCAPE[match]; + }); +}; +/** + * @func getUnEscapeString + * @param {string} str + * @returns {string} + * @desc 字符串的反转义,将`&`, `<`, `>`, `"`, `'`替换为转义前的符号 + */ +export const getUnEscapeString = (str: string): string => { + const UN_ESCAPE = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + ''': "'", + }; + const UN_ESCAPE_EXPR = /(&|<|>|"|')/g; + return ('' + str).replace(UN_ESCAPE_EXPR, match => { + return UN_ESCAPE[match]; + }); +};