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 🙏

© 2026 – Pkg Stats / Ryan Hefner

colourful-utils

v1.0.2

Published

常用的工具函数

Readme

colourful-utils

记录一些使用过有用的工具函数

安装


script 标签:

<script src='https://cdn.jsdelivr.net/npm/colourful-utils/dist/colourful-utils.js'></script>
<script>
	cUtils.legalURLs("https://cdn.jsdelivr.net")
</script>

npm:

npm install colourful-utils

方法


  1. 是否是合法的URL

    import { legalURLs } from 'colourful-utils'
    legalURLs('https://cdn.jsdelivr.net')
  2. 格式化时间,秒数和时分秒的相互转换 15 -> 00:00:15

    import { formateSeconds, formateTimes } from 'colourful-utils'
    formateSeconds(1500)
    formateTimes('15:00')
  3. 比较时间,当前时间和传入时间相差多久

    // 1. 小于1分钟,显示:刚刚
    // 2. 大于等于1分钟、小于1小时,显示:x分钟前
    // 3. 大于等于1小时、小于24小时,显示:x小时前
    // 4. 大于等于24小时小于96小时,显示X天前
    // 5. 大于等于4天,显示:月-日  时-分(01-08  17:00)
    // 6. 大于等于365天,显示:年-月-日 时-分(2019-12-8  15:22)
    import { comparingTime } from 'colourful-utils'
    comparingTime(new Date('2025-2-11 17:58:36') | '毫秒数', new Date())
  4. 睡眠函数

    import { sleep } from 'colourful-utils'
    await sleep(5000); // 延迟5秒钟
  5. 移除字符串前后空格

    import { strTrim } from 'colourful-utils'
    strTrim('  123   ') // 123
  6. 阻止事件冒泡

    import { stopEvent } from 'colourful-utils'
    dom.addEventListener('click', (e) => {
        stopEvent(e)
    });
  7. 随机区间小数

    import { randomFloat } from 'colourful-utils'
    random(1,10) // 1-10之间的小数
  8. 随机区间正整数

    import { randomInt } from 'colourful-utils'
    randomInt(1,10) 
    randomInt(1.2, 10.8) // [2,10] 最小值会向上取整,最大值会向下取整
  9. 计算字符串的长度 假定ASCLL范围内0-128为单字节 其他为双字节

    import { getStrLen } from 'colourful-utils'
    getStrLen('hello') // 5
    getStrLen('hello, 世界') // 11
  10. 判断是否为移动设备

    import { isMobileDevice } from 'colourful-utils'
    isMobileDevice() // { mobile: true, ios: false }
  11. 是否是IE浏览器

    import { isIE } from 'colourful-utils'
    isIE() // false
  12. RGBA RGB颜色格式转换为16进制颜色值

    import { rgbaToHex, rbgToHex } from 'colourful-utils'
    rgbaToHex('rgba(255,125,46,0.9)') // #ff7d2ee6
    rgbToHex('rgb(255,125,46)') // #ff7d2e
  13. base64数据转换为Blob

    import { base64ToBlob } from 'colourful-utils'
    base64ToBlob('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=') // Blob对象
  14. 将数从一个范围映射到另一个范围

    import { scale } from 'colourful-utils'
    scale(10,0,100,1,10) // 0.1
  15. 函数的组合使用,当某项数据需要多个方法处理时

    import { combination } from 'colourful-utils'
    function fn1(x){
       return x * 2;
    }
    function fn2(x){
       return x + 1;
    }
    const fn = combination(fn1, fn2);
    console.log(fn(2)); // 5