总结些开发中常用的函数方法
授人予鱼不如授人予渔。
如果大家还有其他开发中常用的方法可以发送邮箱:zxm7769@163.com联系我
让我们一起为前端滚出最大的轮子。
使用示例
- 1.安装 npm i zxmcommon
- 2.界面引用 import {comfuns , comtest} from "zxmcommon";
- 3.comfuns: API使用 comfuns.fromTime("1702450232") //13天前
- 4.comtest: 各种验证方法
根据时间戳计算多少分钟/小时/天之前
API | 参数 | 说明 |
---|
fromTime | 时间戳 | 根据时间戳计算多少分钟/小时/天之前 |
comfuns.fromTime("1702450232")
关键信息隐藏
API | 参数1 | 参数2 | 参数3 |
---|
encryptany | 要进行隐藏的变量 | 前面需要保留几位 | 后面需要保留几位 |
comfuns.encryptany("您好呀哈哈哈",1,1)
时间戳转 YY-mm-dd HH:ii:ss
API | 参数1 | 参数2 |
---|
toDate | 时间戳 | str/array |
comfuns.toDate("1702450232",'str');
取最大最小数之间的随机数
comfuns.random(1,10)
去除空格
API | 可选参数 |
---|
trim | all(全部) 、both(左右)、 left(左) 、right(右) |
comfuns.trim("你 好 ",'all');
数字格式化
API | 参数1 | 参数2 | 参数3 | 参数4 |
---|
priceFormat | 要格式化的数字(string\ number) | 保留几位小数 | 小数点符号 | 千分位符号 |
comfuns.priceFormat(100,2,',');
邮箱验证
comtest.email('test@example.com'); // true
comtest.email(123456) // false
手机号格式验证
API | 参数 |
---|
mobile | 要验证的字段(string \number ) |
comtest.mobile('12345678901') // true
comtest.mobile(13812345678) // true
comtest.mobile('invalid-number') // false
comtest.mobile(123456) // false
验证URL格式
comtest.url('https://www.example.com') //true
comtest.url('12341234') //false
验证日期格式
comtest.date('2023-01-01') // true
comtest.date(1640995200000) // true (时间戳)
comtest.date('invalid-date') // false
comtest.date('2023-01-32') // false (无效的日期)
验证字符串
comtest.string('Hello, World!') //true
comtest.string(42); //false
comtest.string(null); //false
验证整数
comtest.digits('12345'); // true
comtest.digits(987654); // true
comtest.digits('abc123'); // false
comtest.digits(12.345); // false
身份证号码验证
comtest.idCard('120102198807152134'); // true
comtest.idCard('123456789012345'); //false
是否是车牌号
comtest.carNo('粤BD12345'); // true (new energy vehicle plate)
comtest.carNo('川A123BC'); //false
是否是中文
comtest.chinese('你好'); //true
comtest.chinese('Hello'); // false
comtest.chinese('123'); // false
是否是字母
comtest.letter(''); // true (empty string is considered valid)
comtest.letter('abc'); // true
comtest.letter('123'); // false
comtest.letter('abc123'); // false
是否是数字或字母
comtest.enOrNum('abc123'); // true
comtest.enOrNum('ABCXYZ'); // true
comtest.enOrNum('123456'); // true
comtest.enOrNum('abc123!'); // false (contains a special character)
comtest.enOrNum(''); //true
是否是固定电话
comtest.landline('010-1234567'); // true
comtest.landline('021-12345678'); // true
comtest.landline('123-4567890'); // false (invalid area code)
comtest.landline('0512-12345678901'); // false (too many digits)
comtest.landline('12345678'); // false (missing area code)
comtest.landline(''); //false
判断是否为空
comtest.empty(undefined); // true
comtest.empty(''); // true
comtest.empty(' '); // true
comtest.empty(null); // true
comtest.empty(0); // true
comtest.empty(NaN); // true
comtest.empty(false); // true
comtest.empty([]); // true
comtest.empty({});//true
判断是json字符串
comtest.jsonString('{"key": "value"}'); // true
comtest.jsonString('{"key": "value", "nested": {"foo": "bar"}}'); // true
comtest.jsonString('invalid-json'); // false
comtest.jsonString('12345'); // false (valid JSON, but not an object)
comtest.jsonString({ key: 'value' }); // false (not a string)
判断是否是数组
comtest.array([1, 2, 3]); // true
comtest.array(['a', 'b', 'c']); // true
comtest.array({ key: 'value' }); // false (not an array)
comtest.array(123); // false (not an array)
comtest.array(null); // false (not an array)
comtest.array(undefined); // false (not an array)
是否是对象
comtest.object({ key: 'value' }); // true
comtest.object([1, 2, 3]); // false (arrays are not considered objects)
comtest.object('Hello, World!'); // false (strings are not considered objects)
comtest.object(42); // false (numbers are not considered objects)
comtest.object(null); // false (null is not considered an object)
comtest.object(undefined); // false (undefined is not considered an object)
是否是短信验证码
comtest.code('123456'); // true (default length is 6)
comtest.code('987654', 6); // true (explicit length is 6)
comtest.code('1234'); // false (length is less than 6)
comtest.code('12345678', 8); // true (explicit length is 8)
comtest.code('abc123'); // false (contains non-digit characters)
comtest.code(123456); // true (number, but still valid)
comtest.code(null); // false (not a string)
comtest.code(undefined); // false (not a string)
是否图片格式
comtest.image('path/to/image.jpg'); // true
comtest.image('another/path/picture.png'); // true
comtest.image('document.txt'); // false (non-image file)
comtest.image('no-extension'); // false (no file extension)
comtest.image(''); // false (empty string)
comtest.image(null); // false (null)
comtest.image(undefined); // false (undefined)
是否视频格式
comtest.video('path/to/video.mp4'); // true
comtest.video('another/path/movie.mkv'); // true
comtest.video('document.txt'); // false (non-video file)
comtest.video('no-extension'); // false (no file extension)
comtest.video(''); // false (empty string)
comtest.video(null); // false (null)
comtest.video(undefined); // false (undefined)