工具类函数: 与业务无关, 包括且不限于: 公共方法, 正则表达式等工具类API.
- front-utils
推荐通过这种方式进行安装.
# .npmrc / .yarnrc
@financial-freedom:registry=https://registry.npmmirror.com
再执行命令行:
# pnpm
pnpm add @financial-freedom/front-utils
# yarn
yarn add @financial-freedom/front-utils
# npm
npm i @financial-freedom/front-utils
# pnpm
pnpm add --registry https://registry.npmmirror.com @financial-freedom/front-utils
# yarn
yarn add --registry https://registry.npmmirror.com @financial-freedom/front-utils
# npm
npm i --registry https://registry.npmmirror.com @financial-freedom/front-utils
<script src="path/to/dist/front_utils.umd.js"></script>
<script>
front_utils.validator.checkEmail('[email protected]');
</script>
import * as front_utils from '@financial-freedom/front-utils'
front_utils.validator.checkEmail('[email protected]');
或者,
import { validator } from '@financial-freedom/front-utils'
validator.checkEmail('[email protected]');
常量枚举
- Example
enums.FileExtMimeMap['jpg']; // image/jpeg
enums.FileExtMimeMap['ppt']; // application/vnd.ms-powerpoint
正则表达式
- Example
regexp.emailExp.test('[email protected]');
- Example
regexp.normalPlateRegExp.test('川ALC351');
- Example
regexp.newEnergyPlateRegExp.test('川AD12345');
- Example
regexp.mobileRegExp.test('13012345678');
- Example
regexp.IDCardNoRegExp.test('11010519491231002X');
- Example
regexp.strongPasswordExp.test('!qa@ws#ed%tG123');
- Example
regexp.positiveRN2DExp.test('1.23');
- Example
regexp.positiveRN3DExp.test('1.234');
- Example
regexp.fileExtExp.exec('file.jpg')[1]; // jpg
验证器函数
- Type
/**
* 校验电子邮箱地址是否合法
* @param email 电子邮箱地址
*/
function checkEmail(email: string): boolean;
- Example
validator.checkEmail('[email protected]'); // true
- Type
/**
* 校验车牌号码是否合法
* @param numberPlate 车牌号码
*/
function checkNumberPlate(numberPlate: string): boolean;
- Example
validator.checkNumberPlate('川ALC351'); // true
validator.checkNumberPlate('川A123456'); // false
- Type
/**
* 校验手机号码是否合法
* @param mobile 手机号码
*/
function checkMobile(mobile: string): boolean;
- Example
validator.checkMobile('+8613687654321'); // true
- Type
/**
* 校验身份证号码是否合法
* @param idCardNo 身份证号码
*/
function checkIDCardNo(idCardNo: string): boolean;
- Example
validator.checkIDCardNo('11010519491231002X'); // true
validator.checkIDCardNo('01010519491231002X'); // false
- Type
/**
* 校验是否为强密码
* @param password 密码
*/
function checkStrongPassword(password: string): boolean;
- Example
validator.checkStrongPassword('!qa@ws#ed%tG123'); // true
validator.checkStrongPassword('123qweQWE~!@#$%^&*'); // false
validator.checkStrongPassword('12345678'); // false
- Type
/**
* 校验是否为正有理数(最多2位小数)
* @param numberString 数字字符串
*/
function checkPositiveRN2Exp(numberString: string): boolean;
- Example
validator.checkPositiveRN2Exp('123.45'); // true
validator.checkPositiveRN2Exp('123.456'); // false
- Type
/**
* 校验是否为正有理数(最多3位小数)
* @param numberString 数字字符串
*/
function checkPositiveRN3Exp(numberString: string): boolean;
- Example
validator.checkPositiveRN3Exp('123.456'); // true
validator.checkPositiveRN3Exp('123.4567'); // false
- Type
/**
* 校验是否是数字字符串
* @param numberString 数字字符串
* @param maxLength 最大长度
*/
function checkNumberString(numberString: string, maxLength?: number): boolean;
- Example
checkNumberString('1234567890'); // true
checkNumberString('12345', 5); // true
checkNumberString('123.'); // false
- Type
/**
* 校验是否是ipv4地址
* @param ipAddress IP地址字符串
*/
function checkIPv4(ipAddress: string): boolean;
- Example
checkIPv4('253.123.61.198'); // true
checkIPv4('128.145.158.124'); // true
checkIPv4('a.b.c.100'); // false
- Type
/**
* 校验是否是ipv6地址
* @param ipAddress IP地址字符串
*/
function checkIPv6(ipAddress: string): boolean;
- Example
checkIPv6('91a7:742d:805d:03a7:f413:b182:7de9:adea'); // true
checkIPv6('::ffff:808:808'); // true
checkIPv6('230.222.192.224'); // false
- Type
/**
* 校验是否是URL地址
* @param url URL地址字符串
*/
function checkURL(url: string): boolean;
- Example
checkURL('http://foo.com/blah_blah'); // true
checkURL('http://142.42.1.1/'); // true
checkURL('foo.com'); // true
checkURL('http://../'); // false
格式化函数
- Type
/**
* 日期时间格式化
* @param datetime 日期时间值
* @param format 日期时间格式
* @returns 格式化日期字符串, 默认: YYYY-MM-DD HH:mm:ss
*
* @see {@link https://day.js.org/docs/en/display/format#list-of-all-available-formats}
*/
function formatDatetime({ datetime, format, }?: {
datetime?: dayjs.ConfigType;
format?: string;
}): string;
- Example
// 2022-07-10 22:00:10
formatter.formatDatetime({
datetime: new Date('2022/07/10 22:00:10'),
format: 'YYYY-MM-DD HH:mm:ss',
});
// 2022-07-18 00:00:00
formatter.formatDatetime({
datetime: new Date(2022, 6, 18),
});
- Type
/**
* 数字千分位表示法
*
* @description 由于本质上调用的是 Number.prototype.toLocaleString(), 所以 toLocaleString 支持的格式化都能实现
*
* @param number 数字或数字字符串
* @param options 格式化配置
* @returns 格式化千分位数字, 如: 1,234,567
*
* @see {@link https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html}
* @see {@link https://stackoverflow.com/a/17663871/2630689}
*/
function formatThousandSeparator(number?: number | string, options?: Intl.NumberFormatOptions): string;
- Example
formatter.formatThousandSeparator(123456789.01); // 123,456,789.01
formatter.formatThousandSeparator(123456789.23456, { maximumFractionDigits: 3, }); // 123,456,789.235
获取函数
- Type
/**
* 从URI获取文件名
* @param filePath 文件路径
* @returns 文件名, 如: xxx.jpg
*/
function getFilename(filePath: string): string;
- Example
getFilename(''); // ''
getFilename('https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/weather/icons/a2.png'); // a2.png
getFilename('file.name.with.dots.txt'); // file.name.with.dots.txt
getFilename('file-with-no-extention'); // file-with-no-extention
- Type
/**
* 获取文件后缀名
* @param filePath 文件路径
* @returns 文件后缀名, 如: jpg, ppt, etc. 若未获取到, 则返回 undefined
*/
function getFileExtension(filePath: string): string;
- Example
getter.getFileExtension('file.name.with.dots.txt'); // txt
getter.getFileExtension('file-with-no-extention'); // undefined
- Type
/**
* 获取文件MIME类型
* @param ext 文件后缀名
* @returns MIME
*/
function getMimeByFileExtension(ext: string): string;
- Example
getter.getMimeByFileExtension('pptx'); // application/vnd.openxmlformats-officedocument.presentationml.presentation
getter.getMimeByFileExtension('graffle'); // undefined
- Type
/**
* 将嵌套结构对象数组,转换为单层结构对象数组
* @param nestedArray 嵌套对象数组
* @param key 需要转换的对象属性名称
* @example
* [
* {
* name: 'foo',
* age: 10,
* children: [{
* name: 'bar',
* age: 20
* }]
* },
* {
* name: 'zoo',
* age: 12
* }
* ]
* 转换为:
* [
* {
* name: 'foo',
* age: 10
* },
* {
* name: 'bar',
* age: 20
* },
* {
* name: 'zoo',
* age: 12
* }
* ]
* @see {@link https://www.techighness.com/post/javascript-flatten-deeply-nested-array-of-objects-into-single-level-array/}
*/
function getFlatObjectArray(nestedArray: Record<string, any>[], key?: string): any[];
- Example
getFlatObjectArray([
{ name: 'Jisoo', age: 28, children: [{ name: 'Jennie', age: 27, }, { name: 'Rose', age: 26, }], },
{ name: 'Lisa', age: 26, }
]); // [{ name: 'Jisoo', age: 28, }, { name: 'Jennie', age: 27, }, { name: 'Rose', age: 26, }, { name: 'Lisa', age: 26, }]
运行环境检测器
- Type
/**
* 是否是Electron应用
*
* @see {@link https://github.com/electron/electron/issues/2288#issuecomment-337858978}
*/
const isElectron: boolean;
- Type
/**
* 是否是Windows操作系统
*
* @see {@link https://en.wikipedia.org/wiki/Windows_NT}
*/
const isWindows: boolean;
- Type
/**
* 是否是MacOS操作系统
*
* @see {@link https://en.wikipedia.org/wiki/Macintosh}
*/
const isMacOS: boolean;
# 单元测试
pnpm run test
# 覆盖性测试
pnpm run coverage
兼容性
All
# 完整格式请参考 https://github.com/browserslist/browserslist
defaults
ie >= 10