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

yunjing-utils

v1.0.0

Published

Common tool library

Downloads

4

Readme

parseTime 时间格式化

parseTime(date,fmt)

| 参数 | 类型 | 说明 | | ---- | ------------------ | ---------------------------------- | | date | string/Date/number | 日期字符串、new Date()类型或时间戳 | | fmt | string | 默认值:yyyy-MM-dd hh:mm:ss |

返回值说明: 返回 fmt 格式化时间

const time = parseTime("2023-01-01 08:00:00", "yyyy-MM-dd hh:mm:ss");
const time2 = parseTime(new Date().getTime());

formatTime 时间格式化为文字

formatTime(time,fmt)

| 参数 | 类型 | 说明 | | ---- | ------ | --------------------------- | | time | number | 时间戳 | | fmt | string | 默认值:yyyy 年 MM 月 dd 日 |

返回值说明: 距当前时间<30s 返回"刚刚" 距当前时间<1h 返回"xx 分钟前" 距当前时间<1d 返回"xx 小时前" 距当前时间<2d 返回"1 天前" 其他时间返回 fmt 格式化时间

numberTime 返回几天前、几天后的时间

numberTime(date,day,fmt) | 参数 | 类型 | 说明 | | ---- | ------------------ | ------------------------- | | date | string/Date/number | 日期字符串、new Date()类型或时间戳 | | day | number | 默认值:0 | | fmt | string | 默认值:yyyy-MM-dd hh:mm:ss |

返回值说明: 返回 fmt 格式化时间

numberTime(new Date("2023-10-11 00:00:00"), -7); //返回7天前 2023-10-04 00:00:00

getUrlQuery 获取 url 参数

返回值说明: 返回对象结构

//示例url:http://xxx.xx.com?a=123&b=456
console.log(getUrlQuery()); //{a:'123',b:'456'}

devicePlatform 获取设备平台

devicePlatform()

返回值说明:

| 字段名 | 值 | 说明 | | ------- | ---------- | ----------------- | | trident | true/false | 是否为 ie 内核 | | webKit | true/false | 是否为谷歌内核 | | presto | true/false | 是否为 opera 内核 | | gecko | true/false | 是否为火狐内核 | | mobile | true/false | 是否为移动终端 | | ios | true/false | 是否为 ios 系统 | | android | true/false | 是否为安卓系统 | | iphone | true/false | 是否为 iphone | | iPad | true/false | 是否为 ipad | | wechat | true/false | 是否为微信 |

//示例在pc端运行
console.log(utils.devicePlatform().ios); //false
console.log(utils.devicePlatform().webKit); //true

removeNullObj 删除对象中的空字段

removeNullObj(obj)

参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | obj | object | 传入一个对象 |

返回说明: 返回处理之后的对象

const obj = {
  a: null,
  b: "",
  c: undefined,
  d: "123",
};
console.log(removeNullObj(obj)); //{d: '123'}

throttle 节流

参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | fn | function | 执行的方法 | | delay | number | 延迟的时长 默认 1000ms |

const func = throttle(function () {
  console.log("throttle click");
}, 5000);

debounce 防抖

参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | fn | function | 执行的方法 | | delay | number | 延迟的时长 默认 1000ms |

const func = debounce(function () {
  console.log("debounce click");
}, 1000);

flattenArray 数组扁平化

flattenArray(arr: any[], key?: string)

参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | arr | any[] | 任意的数组类型 | | key | string | 可选,对象数组的 key |

返回说明: 返回扁平化之后的数组

const flattenNumberArray = [1, [2, [3, 4], 5], 6];
const flattenedNumberArray = flattenArray(flattenNumberArray);
console.log(flattenedNumberArray); //[1,2,3,4,5,6]

const flattenObjArray = [{ a: 1 }, [{ b: 1 }, [{ c: 1 }]]];
const flattenedObjArray = flattenArray(flattenObjArray);
console.log(flattenedObjArray); //{a:1,b:1,c:1}

const nestChildObj = [
  {
    name: "11",
    children: [
      {
        name: "111",
      },
      {
        name: "112",
      },
    ],
  },
  {
    name: "12",
    children: [
      {
        name: "121",
      },
      {
        name: "122",
      },
    ],
  },
];
const flattenedChildObj = flattenArray(nestChildObj, "children");
console.log(flattenedChildObj); //[{name:'11'},{name:'111'},{name:'112'},{name:'12'},{name:'121'},{name:'122'}]