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

moon-utils

v2.0.5

Published

##### 类型:

Readme

isType

类型:
(obj: any, type: string | string[]) => boolean
  • 类型判断
  • @param obj 校验对象
  • @param type 校验类型,可以是字符串或数组,数组为或结果。值为所有类型的实例化名。如 Object,Number...
  • @return Boolean
isType(obj, "Array");
isType(obj, ["Array", "String"]);

isType(obj, "array");
isType(obj, ["array", "string"]);

getUUID

类型:
(prefix?: string) => string
  • 得到一个 uuid
  • @param prefix 前缀
  • @return String
getUUID("moon-validate-");

setEventListenerVue2

类型:
(binder: any, vm: any, evtName: string, listener: (...arg: any[]) => void, options: any) => (() => void) | undefined
  • @description 设置 window 或 document 事件监听,同时当所在的 vue 实例销毁时自动移除监听
  • @author 闰月飞鸟
  • @param binder 指定 window 或 document 必传
  • @param vm vue 实例 必传
  • @param evtName 事件名 必传
  • @param listener 监听函数 必传
  • @param options 监听本身参数
  • @return 返回一个移除监听函数
setEventListenerVue2(window, this, "mousemove", () => {});

mergeObject

类型:
(to: object, from: object)=> object
  • @description 对象合并,相同函数合并成一个,原函数先执行,若函数有返回值:若为对象则合并,非对象的以来源函数结果为主
  • @author 闰月飞鸟
  • @param to 原对象
  • @param from 待合并的来源对象
  • @return 返回一个新的对象
mergeObject({}, {});

asyncLoadElement

类型:
(elementName: string, attrs: Recordable, appendToElement?: HTMLHeadElement) => Promise<unknown>
  • @description 异步添加 dom 元素
  • @author 闰月飞鸟
  • @param {*} el 元素名
  • @param {*} attrs 元素属性
  • @param {*} appendToElement 添加到的目标元素,默认 document.head
  • @return Boolean
await asyncLoadElement('script',{src;'',id:'xx'});

InstanceValidate

类型:
(target: Recordable, rules: Schema["rules"], validateCallback: ValidateCallback) => boolean
  • @description 校验目标对象是否符合输入的校验规则
  • @return function 返回回调函数
  • @param targetObject 必选,目标对象 object
  • target @param rules 必选,校验规则 object
  • target @param validateCallback 可选,校验回调返回函数,errors,fields,两个参数, (errors, fields)=>{}
  • target @return 校验结果
const form = {
  catalogId: "",
};
const rules = {
  catalogId: [
    {
      required: true,
      message: "请选择资源目录",
    },
  ],
};
const validate=InstanceValidate()
validate({}, rules, (e) => {
  console.log(e);
});

treeToFlat

类型:
 <T = any>({ source, id, pId, children, }: {
    source: T[];
    id?: string | undefined;
    pId?: string | undefined;
    children?: string | undefined;
}) => { id: string; data: T; pId: string }[]
  • @description 将普通的树形数据,转成扁平化的数据,
  • @description id,pId,children 为source中对应的字段名。
  • @description 不改变源数据
  • @author 闰月飞鸟
  • @param {*} source
  • @param {*} id
  • @param {*} pId
  • @param {*} children
  • @return Array
treeToFlat({ source: data, id: "id", pId: "pId", children: "children" });

treeDataFactory

类型
 type TreeFactoryItemType<T> = {
  id: string;
  pId: string;
  children?: TreeFactoryItemType<T>[];
  data: T;
  track?: string[];
  trigger?: string[];
  level?: number;
  [k: string]: any;
};

 
<T extends Recordable<any>>({ source, id, pId, }: {
    source: T[];
    id?: string | undefined;
    pId?: string | undefined;
}, customizer?: ((item: TreeFactoryItemType<T>) => void) | undefined) => {
    treeData: TreeFactoryItemType<T>[];
    leaves: TreeFactoryItemType<T>[];
    objById :{
    		[key: string]: TreeFactoryItemType<T>;
			};
    flatData: TreeFactoryItemType<T>[]
}
  • @description 树形数据格式化
  • @param {*} source
  • @param {*} id
  • @param {*} pId
  • @param customizer 自定义节点信息,可以直接对节点对象添加自定义属性。
  • @return {treeData,leaves,objById,flatData}
  • @description treeData 格式化后的树数据
  • @description leaves 所有叶子节点
  • @description objById 以 id 为 key 的对象
  • @description flatData 扁平数组,
  • @return TreeFactoryItemType 类型
  • @param {*} id
  • @param {*} pId
  • @param {*} children 子项
  • @param {*} data 源数据
  • @param {*} track 所有当前节点的父节点 id,包括自身 ID
  • @param {*} trigger 所有当前节点的子节点 id,不包含自身 ID
let { treeData } = treeDataFactory({ source: data, id: "id", pId: "pId" });

arrayRemoveItem

类型
<T = any>(arr: T[], remove: (item: T, index: number) => boolean) => void
  • @description — 在不修改当前引用的基础上 ,批量移除元素。数组依据条件函数,
  • @author — 闰月飞鸟
  • @param arr — 目标数组
  • @param remove — 移除函数,接受两个参数,当前项item,以及下标index。
  • @return — Boolean,返回true时,代表要移除该项
 arrayRemoveItem([],(item,index)=>{
  ...
  return true
});

addUrlParams

类型

(url: string, params?: Recordable, merge?: boolean) => string
  • @description 在 url 后面追加指定对象作为新的参数。
  • @author 闰月飞鸟
  • @param {url} 需要追加参数的 url
  • @param {params} 具体的参数对象
  • @param {merge} 对原有的 url 参数进行覆盖合并,还是保留合并,true 时为覆盖合并,以当前参数为主,false 则为保留合并,以原来的 url 参数为主 。默认为覆盖合并。即有相同参数的以后传的参数值为准
addUrlParams("http:", { a: "b" }); ==> "http:?a=b"

getUrlParams

(url: string, opt?: (qs.IParseOptions<qs.BooleanOptional> & {
    decoder?: undefined;
}) | undefined) => {
    rootUrl: string;
    urlParams: {};
}
  • @description 获取 url 中的参数 。
  • @author 闰月飞鸟
  • @param {url} url
  • @param {opt} qs.parse 第二个参数
  • @return {rootUrl,urlParams} 返回 rootUrl 以及 urlParams 对象
getUrlParams("http:?a=b");  ==>{rootUrl:"http:",urlParams:{a:"b"}}