npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zpy-tools

v1.0.8

Published

提供了日期格式化、日期计算、文件格式化、本地缓存处理、Base64转File、File转Base64、节流防抖、数据校验、数据结构转化、获取路由参数、将参数拼接到路由等相关功能

Downloads

10

Readme

安装

npm install zpy-tools

导入

const zpyTools = require('zpy-tools')

格式化时间

/**
 * 格式化时间功能
 * 
 * @param {Date} date 时间对象
 * @param {String} format 格式化字符串,默认为 "YYYY-MM-DD HH:mm:ss"
*/
const date1 = zpyTools.dateFormat(new Date(), 'yyyy-MM-dd')
const date2 = zpyTools.dateFormat(new Date(), 'hh:mm:ss')
const date3 = zpyTools.dateFormat(new Date())
console.log(date1)   // 输出 "2024-05-20"
console.log(date2)   // 输出 "13:14:52"
console.log(date3)   // 输出 "2024-05-20 13:14:52"
/**
 * 获取两个日期之间的时间差
 * 
 * @param {Date} date1 日期对象1
 * @param {Date} date2 日期对象2
 * @param {String} unit 时间单位,可选值:'year', 'month', 'day', 'hour', 'minute', 'second'(默认)
 * @param {Number} decimal 保留小数位数,默认为 2
*/
const date1 = new Date('2024-01-01')
const date2 = new Date('2024-01-10')
const diff1 = zpyTools.dateDiff(date1, date2)
const diff2 = zpyTools.dateDiff(date1, date2, 'minute')
const diff3 = zpyTools.dateDiff(date1, date2, 'hour')
const diff4 = zpyTools.dateDiff(date1, date2, 'day')
const diff5 = zpyTools.dateDiff(date1, date2, 'week')
const diff6 = zpyTools.dateDiff(date1, date2, 'week', 0)
console.log(diff1)   // 输出 777600
console.log(diff2)   // 输出 12960
console.log(diff3)   // 输出 216
console.log(diff4)   // 输出 9
console.log(diff5)   // 输出 1.29
console.log(diff6)   // 输出 1
/**
 * 日期浮动
 * 
 * @param {Date} date 日期对象
 * @param {String} unit 浮动的单位,可选值:'year', 'month', 'day', 'hour', 'minute', 'second'
 * @param {Number} value 浮动的值
*/
const date = new Date("2023-04-01");
const float1 = zpyTools.dateFloat(date, 'day', 5);
const float2 = zpyTools.dateFloat(date, 'month', 3);
const float3 = zpyTools.dateFloat(date, 'year', -1);
const float4 = zpyTools.dateFloat(date, 'hour', 10);
const float5 = zpyTools.dateFloat(date, 'minute', 30);
const float6 = zpyTools.dateFloat(date, 'second', 60);
console.log(float1);  // 输出 Thu Apr 06 2023 08:00:00 GMT+0800 (中国标准时间)
console.log(float2);  // 输出 Sat Jul 01 2023 08:00:00 GMT+0800 (中国标准时间)
console.log(float3);  // 输出 Fri Apr 01 2022 08:00:00 GMT+0800 (中国标准时间)
console.log(float4);  // 输出 Sat Apr 01 2023 18:00:00 GMT+0800 (中国标准时间)
console.log(float5);  // 输出 Sat Apr 01 2023 08:30:00 GMT+0800 (中国标准时间)
console.log(float6);  // 输出 Sat Apr 01 2023 08:01:00 GMT+0800 (中国标准时间)

格式化文件大小

/**
 * 文件大小转换功能
 * 
 * @param {Number} size 文件大小
 * @param {String} fromUnit 源单位
 * @param {String} toUnit 目标单位
 * @param {Number} decimal 保留小数位数,默认为 2
*/
const fileSize1 = zpyTools.fileSizeConvert(2000, 'KB', 'MB')
const fileSize2 = zpyTools.fileSizeConvert(1024, 'KB', 'MB', 2)
conosle.log(fileSize1)   // 输出 "1.95 MB"
conosle.log(fileSize2)   // 输出 "1.00 MB"
/**
 * 文件大小格式化功能
 * 
 * @param {Number} size 文件大小 - kb
 * @param {Number} decimal 保留小数位数,默认为 2
*/
const fileSize1 = zpyTools.fileSizeFormat(2000)
const fileSize2 = zpyTools.fileSizeFormat(1024, 2)
conosle.log(fileSize1)   // 输出 "1.95 MB"
conosle.log(fileSize2)   // 输出 "1.00 MB"
/**
 * File 文件转 Baes64
 * @param {File} file  文件
 * @returns {string}  base64 字符串
 */
const base64 = zpyTools.fileToBase64(file)
conosle.log(base64)   // 输出 "data:image/*;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
/**
 * Baes64 文件转 File
 * @param {String} base64 base64字符串
 * @returns {File}  File 对象
 */
const file = zpyTools.base64ToFile('data:image/*;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
conosle.log(file)   // 输出 new File()

本地存储

/**
 * 设置本地存储
 * 
 * @param {String} key 键
*/
zpyTools.setLocalStorage('key', '本地存储的值')

/**
 * 获取本地存储
 * 
 * @param {String} key 键
*/
const localStorage = zpyTools.getLocalStorage('key')
console.log(localStorage)  // 输出 "本地存储的值" 

数据验证

/**
 * 是否为空
 * 
 * @param {*} value 值
*/
const isNull1 = zpyTools.isNull("")
const isNull2 = zpyTools.isNull(123)
conosle.log(isNull1)   // 输出 true
conosle.log(isNull2)   // 输出 fasle
/**
 * 是否合法URL
 * 
 * @param {*} value 值
*/
const isURL1 = zpyTools.isURL('http://www.baidu.com')
const isURL2 = zpyTools.isURL('')
conosle.log(isURL1)   // 输出 true
conosle.log(isURL2)   // 输出 fasle
/**
 * 是否合法邮箱
 * 
 * @param {*} emiail 值
*/
const isEmail1 = zpyTools.isEmail('[email protected]')
const isEmail2 = zpyTools.isEmail('')
conosle.log(isEmail1)   // 输出 true
conosle.log(isEmail2)   // 输出 fasle
/**
 * 是否合法手机号
 * 
 * @param {*} phone 值
*/
const isPhone1 = zpyTools.isMobile(18120733333)
const isPhone2 = zpyTools.isMobile('')
conosle.log(isPhone1)   // 输出 true
conosle.log(isPhone2)   // 输出 fasle

工具包

/**
 * 获取数据类型
 * 
 * @param {*} value 值
*/
const type1 = zpyTools.getDataType("123")
const type2 = zpyTools.getDataType(123)
const type3 = zpyTools.getDataType(true)
const type4 = zpyTools.getDataType({})
const type5 = zpyTools.getDataType([])
const type6 = zpyTools.getDataType(() => {})
const type7 = zpyTools.getDataType(null)
conosle.log(type1)   // 输出 "string"
conosle.log(type2)   // 输出 "number"
conosle.log(type3)   // 输出 "boolean"
conosle.log(type4)   // 输出 "object"
conosle.log(type5)   // 输出 "array"
conosle.log(type6)   // 输出 "function"
conosle.log(type7)   // 输出 "null"
/**
 * 对象深拷贝
 * 
 * @param {*} data 值
*/
const obj1 = zpyTools.deepClone({name: "对象深拷贝"})
console.log(obj1)   // 输出 {name: "对象深拷贝"}
/**
 * 将数组拼接成字符串
 * 
 * @param {String} value 字符串
 * @param {String} separator 分隔符
*/
const str1 = zpyTools.arrayToString([1,2,3,4,5])
const str2 = zpyTools.arrayToString([1,2,3,4,5], '、')
conole.log(str1)    // 输出 "1,2,3,4,5"
conole.log(str2)    // 输出 "1、2、3、4、5"
/**
 * 将字符串拆分成数组
 * 
 * @param {String} value 字符串
 * @param {String} separator 分隔符
*/
const arr1 = zpyTools.stringToArray("1,2,3,4,5")
const arr2 = zpyTools.stringToArray("12+34+56", '+')
conole.log(arr1)    // 输出 ['1','2','3','4','5']
conole.log(arr2)    // 输出 ['12','34','56']
/**
 * 将扁平数组转换为树形结构
 * 
 * @param {Array} items 数组
*/
const tree = zpyTools.arrayToTree([
  {id: 1, parentId: null, name: '根节点'}, 
  {id: 2, parentId: 1, name: '子节点1'}, 
  {id: 3, parentId: 2, name: '子节点2'}
])
conole.log(tree)
// 输出 
// [
//   {
//     "id": 1,
//     "parentId": null,
//     "name": "根节点",
//     "children": [
//       {
//         "id": 2,
//         "parentId": 1,
//         "name": "子节点1",
//         "children": [
//           {
//             "id": 3,
//             "parentId": 2,
//             "name": "子节点2"
//           }
//         ]
//       }
//     ]
//   }
// ]
/**
 * 将树形结构转换为数组
 * 
 * @param {Array} value 树结构
*/
const arr = zpyTools.treeToArray(
  {
    "id": 1,
    "parentId": null,
    "name": "根节点",
    "children": [
      {
        "id": 2,
        "parentId": 1,
        "name": "子节点1",
        "children": [
          {
            "id": 3,
            "parentId": 2,
            "name": "子节点2"
          }
        ]
      }
    ]
  }
)
conole.log(arr)
// 输出 
// [
//   {
//     "id": 1,
//     "parentId": null,
//     "name": "根节点",
//     "children": [
//       {
//         "id": 2,
//         "parentId": 1,
//         "name": "子节点1",
//         "children": [
//           {
//             "id": 3,
//             "parentId": 2,
//             "name": "子节点2"
//           }
//         ]
//       }
//     ]
//   }
// ]
/**
 * 深度比较两个对象是否包含相同的值
 * 
 * @param {Object} obj1 对象1
 * @param {Object} obj2 对象2
*/
const obj1 = {name: "张三"}
const obj2 = {name: "李四"}
const obj3 = obj1
const isEqual1 = zpyTools.hasObjectsEqual(obj1, obj3)
const isEqual2 = zpyTools.hasObjectsEqual(obj1, obj2)
conole.log(isEqual1)   // 输出 true
conole.log(isEqual2)   // 输出 false
/**
 * 防抖函数工厂
 * @param {function} fn 需要执行的函数
 * @param {Number} delay 延迟时间,默认500ms
 */
zpyTools.debounce(() => {
  // 执行函数
  conosle.log('防抖函数')   // 输出 "防抖函数"
})
/**
 * 节流函数工厂
 * @param {function} fn 需要执行的函数
 * @param {Number} interval 间隔时间,默认1000ms
 */
zpyTools.throttle(() => {
  // 执行函数
  conosle.log('节流函数')   // 输出 "节流函数"
})
/**
 * 获取URL中的参数
 * @param {String} url URL字符串,默认为当前页面的URL
 * @returns {Object} 包含URL参数的对象
 */
const url = 'https://www.baidu.com?name=张三&age=18'
const params = zpyTools.getUrlParams(url)
conole.log(params)   // 输出 {name: '张三', age: '18'}
/**
 * 将参数添加到URL
 * @param {string} url 基础URL
 * @param {Object} params 要添加的参数对象
 * @returns {string} 添加了参数后的URL
 */
const url = 'https://www.baidu.com'
const params = {name: '张三', age: '18'}
const newUrl = zpyTools.addParamsToUrl(url, params)
conole.log(newUrl)   // 输出 "https://www.baidu.com?name=张三&age=18"

开源协议

ISC