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 🙏

© 2025 – Pkg Stats / Ryan Hefner

w-utils-js

v1.0.1

Published

js-utils

Readme

w-utils-js

JS公用函数库。

包含:

import { isEmpty } from "w-utils-js";

isEmpty(2);
=> false

debounce(func, gap=500)

创建一个防抖函数。频繁触发的情况下,只等有足够的gap空闲时间才执行。

const test1 = debounce(test);
window.onresize = function () {
  test1(3);
};

throttle(func, gap=500)

频繁触发的情况下,一定时间内只执行一次。方式同debounce。

compose(fns)

函数组合。接受多个函数作为参数,参数按从右到左的顺序连续执行,将上一个函数的执行结果传入下一个函数。实现对数据的连续处理。

相当于将需要嵌套执行的函数平铺。如:a(b(c()))变为compose(a, b, c)执行。

function fn1(x) {  
  return x * 2;  
}  
function fn2(x) {  
  return x + 1;  
}  

compose(fn1, fn2)(1);  
// => 4
compose(fn2, fn1)(1);  
// => 3

curry(func)

柯里化。将多参数的函数转换成单参数的形式。如:fn(a, b, c)变为curry(fn)(a)(b)(c)执行。 柯里化的作用:参数复用、提前返回、延迟执行。

function fn(x, y) {  
  return x + y;  
}  

curry(fn)(1)(2);
或
curry(fn)(1, 2);
// => 3

once(func)

once函数。规定函数只执行一次。可缓存执行结果。


handleCode(value, jiemi=false)

对localstorage内容进行加密、解密。默认加密,jiemi为true则解密。

encodePath(path[, sep])

对非法文件路径进行转码,如路径包含"#"导致图片读取失败。sep默认会自动用/[\/]+/分割。linux的sep为/,window为\。

encodePath("C:/asd#/")
// => C:/asd%23/

copyTxt(txt)

复制文本

isEmpty(value)

验证是否为空。字符串化的"null"、"undefined"都视为空。

getBoolean(value, def = false)

获取变量的实际布尔值,如"false"、"0"等将返回false。字符串化的"null"、"undefined"都视为空,返回def默认值。


deepExtend(target, ...sources)

对象属性深层合并。

deepExtend({ a: 1 }, { b: 2 }, { c: 3 });
// => { a: 1, b: 2, c: 3 }

isEqualObj(x, y, filterArr = [])

判断数组、对象是否改变。filterArr为不监听是否变化的对象属性组合。未变化返回true。

isEqualObj({ a: 1 }, { a: 1, b: 2 });
// => false
isEqualObj({ a: 1 }, { a: 1, b: 2 }, ["b"]);
// => true

deepClone(source)

深拷贝数组、对象。

flatToTree(data, key = 'id', parentKey = 'parentId', childrenKey = 'children')

数组转成树形结构。

treeToFlat(data, childrenKey = 'children')

树形转成平级结构。

getIndex(arr, val, key, useStrict = true)

对象数组字段查找,找不到返回-1。useStrict是否严格比较,true则2 === "2",false则2 == "2"。

getIndex([1, 2, 3], 3)
getIndex([1, 2, 3], "3", false, false)
getIndex([{ value: 1 }, { value: 2 }, { value: 3 }], 3, "value");
// => 2

$t(data, str)

i18n取值。

const data = {
  a: {
    b: {
      c: 123
    }
  }
};
$t(data, 'a.b.c')
// => 123

getBase64(source, type = 'url', crossOrigin = false)

获取图片的base64。type为url网络链接、path本地路径、file。返回Promise。

baseToFile(base64, toFile = true)

base64转Blob、File。默认file。


getRandom(max = 1, min = 0)

获取两个数之间的随机数。

calcRowsAndCols(x)

计算一个数能排多少行、列,行列要尽可能接近。

calcRowsAndCols(5)
// => [2, 3]

escapeReg(str)

正则特殊字符转义。

escapeReg("*")
// => \*