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

@4a/helper

v0.2.0

Published

常用方法库

Downloads

6

Readme

Helper

常用方法库

Usage

npm i @4a/helper
yarn add @4a/helper
import { ... } from '@4a/helper'

Assert

function ok(condition: any, message: string): void;
function not(condition: any, message: string): void;

Array

function asyncMap(arr: Promise<any>[], fn: ArrayPromiseCallback): Promise<any[]>;
function asyncFind(arr: Promise<any>[], fn: BoolPromiseCallback): Promise<any>;
function asyncFilter(arr: Promise<any>[], fn: BoolPromiseCallback): Promise<any[]>;
function asyncSome(arr: Promise<any>[], fn: BoolPromiseCallback): Promise<boolean>;
function asyncEvery(arr: Promise<any>[], fn: BoolPromiseCallback): Promise<boolean>;
/**
 * 从数组中删除一个元素
 * @param arr
 * @param val 值或回调函数
 * @returns 修改数组本身
 */
function remove(arr: any[], val: any | BoolCallback): any[];
/**
 * 从数组中删除一个元素
 * @param arr
 * @param val 值或回调函数
 * @returns 原始数组保持不变,返回新数组
 */
function removed(arr: any[], val: any | BoolCallback): any[];
/**
 * 数组去重,使用set特性,只对值数组生效
 * @param arr
 * @description Array.from(new Set(arr))
 */
function uniqued(arr: any[]): any[];
/**
 * 数组最大值
 * @param {number[] | any[]} arr
 * @param {CompareCallback} fn arr是number[]时fn为空,arr是any[]时fn必须
 */
function max(arr: number[]): number;
function max(arr: any[], fn: CompareCallback): any;
/**
 * 数组排序,支持自定义排序,默认排序规则:>
 * @param arr
 * @param rule 自定义排序规则
 */
function sorted(arr: any[], rule?: CompareInput): any[];

Date

const date: {
    /**
     * 日期格式化,日期+时间
     * @param date
     * @returns YYYY-MM-DD HH:mm:ss
     */
    format(date: MomentInput): string;
    /**
     * 日期格式化,仅日期没有时间
     * @param date
     * @returns YYYY-MM-DD
     */
    formatDate(date: MomentInput): string;
    /**
     * 今天的日期
     * @returns YYYY-MM-DD
     */
    today(): string;
    /**
     * 今天的日期
     * @returns Moment(YYYY-MM-DD)
     */
    todayStart(): moment.Moment;
    /**
     * 明天的日期
     * @returns Moment(YYYY-MM-DD).add(1, 'days')
     */
    todayEnd(): moment.Moment;
    /**
     * 日期向后偏移
     * @param date 日期
     * @param days 向后偏移天数,默认=1
     * @returns YYYY-MM-DD
     */
    afterDay(date: MomentInput, days?: number): string;
    isToday(date: MomentInput): boolean;
    /**
     *
     * @param amount 增加数量
     * @param unit 单位,默认 = 'days'
     * @returns YYYY-MM-DD HH:mm:ss
     */
    add(amount: DurationInputArg1, unit?: DurationInputArg2): string;
    /**
     * 连续时间区间
     * @param offset 区间天数
     * @param shift 漂移天数,向前推移的天数
     */
    offsetDays(offset: number, shift?: number): string[];
};

is

function isArray(obj: any): boolean;
function isPlainObject(obj: any): boolean;
function isString(obj: any): boolean;
function isFunction(obj: any): boolean;
function isEmptyObject(obj: any): boolean;

Lib

function http(baseURL: string): string;
function https(baseURL: string): string;
function random(min: number, max: number): number;
/**
 * 概率计算,基于Math.random的随机概率
 * @d 分子
 * @m 分母
 */
function ratio(d: number, m: number): boolean;
/**
 * @array
 * 支持数组,数组使用arr.slice(0)
 * @description
 * 深度clone,基于JSON.parse(JSON.stringify(obj)),不能拷贝循环引用的对象
 */
function clone<T>(obj: T): T;
/**
 * Array => Map
 * @param arr   数组
 * @param key   数组子对象的key
 * @param fn    处理key值的回调
 */
function toMap(arr: PlainObject[], key: string, fn?: ToMapCallback): PlainObject;
/**
 * Array => Map
 * @param arr
 * @param keys
 * @returns
 */
function makeMap(arr: PlainObject[], keys: string[]): PlainObject;
/**
 * Map => Array
 * @param map Map数据
 * @param fn  处理数据的回调
 * @example toArray({a:1}, (key, value) => key) ==> ['a']
 * @example toArray({a:1}, (key, value) => value) ==> [1]
 */
function toArray(map: PlainObject, fn: (key: string, value: any) => any): any[];
/**
 * 获取一个Map的key的数组
 * @param map Map对象
 * @param fn  对key的处理回调
 */
function getKeys(map: PlainObject, fn: (key: string) => string): string[];
/**
 * 获取一个Map的value的数组
 * @param map Map对象
 * @param fn  对value的处理回调
 */
function getValues(map: PlainObject, fn: (value: any) => any): any[];

Url

function urlParse(url: string): {
    protocol: string;
    port: string;
    host: string;
    username: string;
    password: string;
    pathname: string;
    query: import("url").URLSearchParams;
};