系列文章目录
提示:函数式编程思想
文章目录
前言
一些常用的工具类封装起来;
可供参考
如何发布npm包
发布
一、创建你的组件
- 先创建个目录,如demo
- 创建一个index.js文件,这里就以这个为例说明,即需要发布的npm包
- 初始化,在本目录下,执行npm init
{
"name": "hammer-utils",
"version": "1.0.0",
"description": "工具类数据包", // 这里填写组件描述信息
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "hammer1010",
"license": "ISC",
//"keywords": ["vue3", "vite", "ts"], // 二级搜索关键词
//"repository": { // 码云或git项目地址
//"type": "gitee",
//"url": "https://gitee.com/hammer1010_admin/vue3-vite"
//}
}
二、创建并登录npm账号
● 如果没有账号,首先要创建一个npm账号
● 创建完成,你注册邮箱会收到一个确认邮件,记得验证一下
● 然后登录你的npm账号,npm login,会让你输入用户名、密码、邮箱
三、发布操作
登录验证完成后, npm publish
四、更新操作
npm version patch // 这个操作会在原先的版本号上+1,如1.0.0 会变成1.0.1
npm publish // 重新发布
五、删除操作
删除指定的包和版本号
npm unpublish 包名@版本号
使用
npm install hammer-utils
import { scrollToTop } from 'utils-lan'
console.log(scrollToTop()) // 回到顶部
utils
npm 地址:hammer-utils
链接: https://www.npmjs.com/package/hammer-utils
// 邮箱
export const isEmail = (s) => {
return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
}
// 手机
export const isMobile = (s) => {
return /^1[0-9]{10}$/.test(s)
}
/**
* 节流
* @param {*} func 执行函数
* @param {*} delay 节流时间,毫秒
*/
export const throttle = function(func, delay) {
let timer = null
return function() {
if (!timer) {
timer = setTimeout(() => {
func.apply(this, arguments)
// 或者直接 func()
timer = null
}, delay)
}
}
}
/**
* 防抖
* @param {*} fn 执行函数
* @param {*} wait 防抖时间,毫秒
*/
export const debounce = function(fn, wait) {
let timeout = null
return function() {
if (timeout !== null) clearTimeout(timeout)// 如果多次触发将上次记录延迟清除掉
timeout = setTimeout(() => {
fn.apply(this, arguments)
// 或者直接 fn()
timeout = null
}, wait)
}
}
/**
* 时间格式转化
* @param {Date} date 时间戳
* @param type 可选,格式类型(hms,no) 默认hms时分秒,no 不带时分秒
*/
export function __num (num) {
return num < 10 ? '0' + num : num
}
export function __Time (date,type = 'hms') {
if (date === 0) return '-'
const Y = date.getFullYear() // 年
const m = __num(date.getMonth() + 1) // 月
const d = __num(date.getDate()) // 日
const h = __num(date.getHours()) // 时
const i = __num(date.getMinutes()) // 分
const s = __num(date.getSeconds()) // 秒
if (type === 'hms') {
return Y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
} else {
return Y + '-' + m + '-' + d
}
}
/**
* 获取当前时间 yyyy-mm-dd,hh:mm:ss
*/
export const getYyMmDdHhMmSs = () => {
const date = new Date()
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hours = date.getHours()
const minu = date.getMinutes()
const second = date.getSeconds()
const arr = [month, day, hours, minu, second]
arr.forEach((item,index) => {
arr[index]=item < 10 ? '0' + item : item
})
return (
year +
'-' +
arr[0] +
'-' +
arr[1] +
' ' +
arr[2] +
':' +
arr[3] +
':' +
arr[4]
)
}
/**
* 文件下载
* @param {string} url
* @returns {File}
*/
export function __DownLoad (href, name) {
const a = document.createElement('a')
if (name) a.setAttribute('download', name)
a.href = href
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
/**
* 下载二进制文件流
* @param {Blob} blob 文件流
* @param {String} contentDisposition 文件名的请求头 对应 content-disposition
*/
export function __DownLoadByBlob (blob, contentDisposition) {
const file = new Blob([blob], { type: 'application/octet-stream' })
// 提取文件名
const fileName = contentDisposition.match(
/filename=(.*)/
)[1]
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// 兼容IE,window.navigator.msSaveBlob:以本地方式保存文件
window.navigator.msSaveBlob(file, decodeURI(fileName))
} else {
const blobURL = window.URL.createObjectURL(file)
const tempLink = document.createElement('a')
tempLink.style.display = 'none'
tempLink.href = blobURL
tempLink.setAttribute('download', decodeURI(fileName))
if (typeof tempLink.download === 'undefined') {
// 兼容:某些浏览器不支持HTML5的download属性
tempLink.setAttribute('target', '_blank')
}
document.body.appendChild(tempLink)
tempLink.click()
document.body.removeChild(tempLink)
window.URL.revokeObjectURL(blobURL)
}
}
/**
* 数组取差集
* @param {Array} arr1 数组1
* @param {Array} arr2 数组2
*/
export function __Differ (arr1, arr2) {
return [...arr1, ...arr2].filter(v => arr1.includes(v) && !arr2.includes(v))
}
// 判断是终端类型,值有ios,android,iPad
export const checkIosAndroidIpad = () => {
const u = navigator.userAgent;
const obj = {
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
android: u.indexOf("Android") > -1 || u.indexOf("Linux") > -1, //android终端或者uc浏览器
iPad: u.indexOf("iPad") > -1, //是否iPad
}
return Object.keys(obj)[Object.values(obj).indexOf(true)]
}
// 判断是否是微信,qq 或 uc
export const checkWeixinQqUc = () => {
const u = navigator.userAgent;
const obj = {
weixin: u.indexOf("MicroMessenger") > -1, //是否微信
qq: u.match(/QQ/i) == "qq"&&!u.indexOf('MQQBrowser') > -1, //是否QQ
uc: u.indexOf('UCBrowser') > -1
}
return Object.keys(obj)[Object.values(obj).indexOf(true)]
}
/**
* localStorage 存贮
* 目前对象值如果是函数 、RegExp等特殊对象存贮会被忽略
* @param {String} key 属性
* @param {Object} value 值
*/
export const localStorageSet = (key, value) => {
if (typeof (value) === 'object') value = JSON.stringify(value)
localStorage.setItem(key, value)
}
/**
* localStorage 获取
* @param {String} key 属性
*/
export const localStorageGet = (key) => {
return localStorage.getItem(key)
}
/**
* localStorage 移除
* @param {String} key 属性
*/
export const localStorageRemove = (key) => {
localStorage.removeItem(key)
}
// 检测是否是对象
export const isObject = (obj) => {
return Object.prototype.toString.call(obj) === '[object Object]';
}
// 检测是否是数组
export const isArray = (arr) => {
return Object.prototype.toString.call(arr) === '[object Array]';
}
// 检测对象是否存在该属性
export const objectHasOwn = (object, key) => {
return Object.hasOwn(object, key)
}
// 获取url参数
export const getQueryString = (name) => {
const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
const search = window.location.search.split('?')[1] || '';
const r = search.match(reg) || [];
return r[2];
}
// 滚动到顶部
export const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
}
// 将阿拉伯数字翻译成中文的大写数字
export const numberToChinese = (num) => {
var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十");
var BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");
var a = ("" + num).replace(/(^0*)/g, "").split("."),
k = 0,
re = "";
for (var i = a[0].length - 1; i >= 0; i--) {
switch (k) {
case 0:
re = BB[7] + re;
break;
case 4:
if (!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$")
.test(a[0]))
re = BB[4] + re;
break;
case 8:
re = BB[5] + re;
BB[7] = BB[5];
k = 0;
break;
}
if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0)
re = AA[0] + re;
if (a[0].charAt(i) != 0)
re = AA[a[0].charAt(i)] + BB[k % 4] + re;
k++;
}
if (a.length > 1) // 加上小数部分(如果有小数部分)
{
re += BB[6];
for (var i = 0; i < a[1].length; i++)
re += AA[a[1].charAt(i)];
}
if (re == '一十')
re = "十";
if (re.match(/^一/) && re.length == 3)
re = re.replace("一", "");
return re;
}
一、简写编程写法?
js模块
let res = 1;
if(res) console.log(1111) // 1111
res && console.log(333) // 333
css模块
// bgc-1,2,3,4,5,6
@for $i from 1 to 6 {
.bgc-#{$i} {
background: url("~@/assets/image/comon-#{$i}.png")
no-repeat
100%
100%;
background-size: cover;
}
}
总结
没有特别的幸运,那么就特别的努力!!!
5537



被折叠的 条评论
为什么被折叠?



