aty-utils
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

aty-utils.js

Api 列表

	objIsNull()  //判断对象是否为空或者null
	debounce() //函数防抖
	throttle() //函数节流
	formatFileSize() //格式化文件大小
	isValidKey() //判断对象中是否含有该key字段
	sortChinese() //汉字中文排序
	randomString() //生成随机字符串
	addZero() //补零
	msFormat2DateStr() //毫秒数返回年、月、日、时、分、秒
	makeTree() //根据parentId将扁平数据转树
	setToken() //设置token
	getToken() //获取token
	removeToken() //移除token
	setLocal() //设置永久缓存
	getLocal() //获取永久缓存
	removeLocal() //移除永久缓存
	clearLocal() //移除全部永久缓存
	setSession() //设置临时缓存
	getSession() //获取临时缓存
	removeSession() //移除临时缓存
	clearSession() //移除全部临时缓存
  • objIsNull 判断对象是否为空或者null

使用方式

 let a = {}
 console.log(objIsNull(a))  //true
  • debounce 函数防抖

使用方式

const fn = ()=>{
 	console.log("333")
}
const debou = debounce(fn,500,false)
  • throttle 函数节流

    使用方式

const thro = throttle(() => {
	console.log(state.value)
}, 1000)
const testThrottle = () => {
	thro()
}
  • formatFileSize 格式化文件大小

使用方式

console.log(formatFileSize(15468202))
  • isValidKey 判断对象中是否含有该key字段

使用方式

let o = { a:1, b:2 }
console.log(isValidKey("a",o))
  • sortChinese 汉字中文排序

使用方式

let arr = [{ name: "掌声" }, { name: "里斯" }, { name: "百度" }]
console.log(sortChinese(arr, "name"))
  • randomString 生成随机字符串

使用方式

console.log(randomString(64)) //长度为64位的随机字符串
  • addZero 补零

使用方式

console.log(addZero(9)) //09
  • msFormat2DateStr 毫秒数返回年、月、日、时、分、秒

使用方式

console.log(msFormat2DateStr(Date.now()))
  • makeTree 根据parentId将扁平数据转树

使用方式

const list = [
   { id: 1, name: "1", parentId: -1 },
   { id: 2, name: "11", parentId: 1 },
   { id: 3, name: "12", parentId: 1 },
   { id: 4, name: "2", parentId: -1 },
   { id: 5, name: "21", parentId: 4 },
   { id: 6, name: "22", parentId: 4 },
   { id: 7, name: "221", parentId: 6 },
   { id: 8, name: "2221", parentId: 7 }
]
console.log(makeTree(list))

Cookies 设置

  • setToken 设置token

    使用方式

setToken("123131313");
  • getToken 获取token

使用方式

console.log(getToken())
  • removeToken 移除token

使用方式

removeToken()

LocalStorage 设置

  • setLocal 设置永久缓存

使用方式

setLocal("obj", {a:1,b:2}) // 对象类型
setLocal("key", "12312313131") // 字符串类型
setLocal("list", [{a:1,b:2}]) // 数组类型
  • getLocal 获取永久缓存

使用方式

   console.log(getLocal("obj"))
   console.log(getLocal("key"))
  • removeLocal 移除永久缓存

使用方式

   removeLocal("obj")
   removeLocal("key")
  • clearLocal 移除全部永久缓存

使用方式

   clearLocal()

SessionStorage 设置

  • setSession 设置临时缓存

使用方式

setSession("obj", {a:1,b:2}) // 对象类型
  • getSession 获取临时缓存

使用方式

console.log(getSession("obj"))
  • removeSession 移除临时缓存

使用方式

removeSession("obj")
  • clearSession 移除全部临时缓存

使用方式

clearSession()

Readme

Keywords

none

Package Sidebar

Install

npm i aty-utils

Weekly Downloads

1

Version

0.0.5

License

MIT

Unpacked Size

18.9 kB

Total Files

10

Last publish

Collaborators

  • aty1990