# 编码
# base64
定义:
declare type toaReturnType = ReturnType<typeof btoa>;
declare const zhbtoa: (data: string) => toaReturnType;
declare type tobReturnType = ReturnType<typeof atob>;
declare const zhatob: (data: string) => tobReturnType;
export { zhbtoa as btoa, zhatob as atob };
基于 window.btoa
、window.atob
封装,支持中文。
# blob
- base64ToBlob
base64 转 Blob
定义:
export declare function base64ToBlob(base64: string, contentType?: string, sliceSize?: number, zh?: boolean): Blob;
# 序列化
- stringify
序列化
定义:
export declare const stringify: <T>(value: T) => string;
用法:
import { stringify } from '@tencent/slug-function'
// name=slugteam&desc=腾讯游戏大前端
stringify({
name: 'slugteam',
desc: '腾讯游戏大前端'
})
- parse
反序列化
定义:
export declare const parse: <T>(text: string) => T;
# 颜色处理
- toRgba
hex/rgb(a) 转 rgba,支持重设透明度,对于 transparent
等特殊写法,不做处理。
定义:
export declare const toRgba: (hex?: string | undefined, a?: number | undefined) => string | undefined;
- toHex
rgb(a) 转 hex
定义:
export declare const toHex: (color: string) => string;
# unicode
- decodeUnicode
对 4 个字节字符进行解码(如 emoji 表情),需与 encodeUnicode()
配套使用
定义:
export declare function decodeUnicode(str: string): string;
用法:
import { decodeUnicode } from '@tencent/slug-function'
// 😂
decodeUnicode('[u:128514]')
- encodeUnicode
对 4 个字节字符进行编码(如 emoji 表情),需与 decodeUnicode()
配套使用
定义:
export declare function encodeUnicode(s: string): string;
用法:
import { encodeUnicode } from '@tencent/slug-function'
// [u:128514]
encodeUnicode('😂')
# parseMarkdown(str)
- 参数:
{String} str
- 需要处理的字符串
- 返回值:
{String}
- 处理后的字符串
- 用法:
import { parseMarkdown } from '@tencent/slug-function'
// <a href="https://sy.qq.com/">链接</a>
parseMarkdown('[链接](https://sy.qq.com/)')
markdown 语法解析,目前仅支持超链接
# xssFilter(str)
- 参数:
{String} str
- 需要处理的字符串
- 返回值:
{String}
- 处理后的字符串
- 用法:
import { xssFilter } from '@tencent/slug-function'
// <script>alert("123")</script>
xssFilter('<script>alert("123")</script>')
XSS 特殊字符过滤
# 对象命名转换
本系列函数有着完备的类型推导,需要 ts4.3+ 支持
定义:
// 字符串转换成大驼峰命名
export declare const pascalizeKey: <Key extends string, SA extends SeparatorArray>(key: Key, separators?: SA | undefined) => PascalizeKey<Key, SA>;
// 字符串转换成小驼峰命名
export declare const camelizeKey: <Key extends string, SA extends SeparatorArray>(key: Key, separators?: SA | undefined) => Uncapitalize<PascalizeKey<Key, SA>>;
// 字符串转换成下划线命名
export declare function underlizeKey<Key extends string>(key: Key, ignoreFirst?: false): UnderlizeKeyHelper<Key>;
export declare function underlizeKey<Key extends string>(key: Key, ignoreFirst: true): UnderlizeKey<Key>;
// 键值转换成小驼峰命名(对象、数组)
export declare const camelize: <T>(obj: T) => Camelize<T>;
// 键值转换成下划线命名(对象、数组)
export declare const underlize: <T>(obj: T) => Underlize<T>;
# URL
- fixedEncodeURIComponent
遵循 RFC 3986 的编码
- https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
- safeDecodeURIComponent
安全解码,处理 %2
此类输入时不会抛出异常
const text4 = '%21%27%28%29%2'
test(text4, () => {
expect(safeDecodeURIComponent(text4)).toBe(`!'()%2`)
})