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

use-fns

v1.0.27

Published

Some utility functions prefixed with use

Downloads

24

Readme

use-fns

Some utility functions prefixed with use

NPM version

Install

$ npm i use-fns

Usage

import fns from "use-fns";

API

useTypeOf

Determine the type of variable

declare function useTypeOf(v: any): string;
import { useTypeOf } from 'use-fns'
console.log(useTypeOf('abc')) // 'string'
console.log(useTypeOf(123)) // 'number'
console.log(useTypeOf(true)) // 'boolean'
console.log(useTypeOf(null)) // 'null'
console.log(useTypeOf(undefined)) // 'undefined'
console.log(useTypeOf(Symbol())) // 'symbol'
console.log(useTypeOf(BigInt())) // 'bigint'
console.log(useTypeOf([])) // 'array'
console.log(useTypeOf({})) // 'object'
console.log(useTypeOf(/.+/)) // 'regexp'
console.log(useTypeOf(new Date())) // 'date'
...

useDebounce

Calling a function multiple times only takes the last one as the result

declare function useDebounce(cb: Function, wait: number = 500): Function;
import { useDebounce } from "use-fns";
const cb = () => console.log("useDebounce");
const newCb = useDebounce(cb, 1000);
for (let i = 0; i < 10; i++) newCb(); // only call once after one second

useThrottle,

Call the function at regular intervals

declare function useThrottle(cb: Function, wait: number = 500): Function;
import { useThrottle } from "use-fns";
const cb = () => console.log("useDebounce");
const newCb = useThrottle(cb, 1000);
for (let i = 0; i < 10; i++) newCb(); // call the function per second

useHideMobile

Desensitization of mobile phone number(not strict)

declare function useHideMobile(s: string): string;
import { useHideMobile } from "use-fns";
const phoneNumber = useHideMobile("12345678911"); // 1*********1

useLaunchFullscreen

Launch full screen

declare function useLaunchFullscreen<T extends Element>(ele: T): void;
import { useLaunchFullscreen } from "use-fns";
useLaunchFullscreen(document.body);

useExitFullscreen

Exit full screen

declare function useExitFullscreen(): void;
import { useExitFullscreen } from "use-fns";
useExitFullscreen();

useTurnCase

Convert string case

declare function useTurnCase(str: string, type: number): void;
import { useTurnCase } from "use-fns";
const upper = useTurnCase("aBc", 1); // ABC
const lower = useTurnCase("aBc", 2); // abc
const capital = useTurnCase("aBc", 3); // ABc
const original = useTurnCase("aBc", 4); // aBc

useSearchParams

Get url search params

declare function useSearchParams(): Record<string, string>;
import { useSearchParams } from "use-fns";
// if location.search = '?a=1&b=2'
const params = useSearchParams(); // {a: 1,b: 2}

useSysType

Get current terminal type

declare function useSysType(): void;
import { useSysType } from "use-fns";
const sys = useSysType(); // 'ios' | 'android' | ''

useUniqueArrObj

Array objects are deduplicated according to fields

declare function useUniqueArrObj<T extends Record<string, any>, U extends T[]>(
  arr: U,
  k: keyof T
): void;
import { useUniqueArrObj } from "use-fns";
const uniqueArr = useUniqueArrObj(
  [
    { name: "Joruno", age: 22 },
    { name: "hanchen", age: 22 },
    { name: "old man", age: 21 },
  ],
  "age"
);

/**
 * [
 *    {name: 'Joruno', age: 22},
 *    {name: 'old man', age: 21},
 * ]
 */

useScrollToTop

Scroll to top of page

declare function useScrollToTop(): void;
import { useScrollToTop } from "use-fns";
useScrollToTop();

useSmoothScroll

Scroll to element position

declare function useSmoothScroll(selector: string = "body"): void;
import { useSmoothScroll } from "use-fns";
useSmoothScroll("#form");

useUUID

Generate uuid

declare function useUUID(): void;
import { useUUID } from "use-fns";
const uuid = useUUID(); // 'e728a1e4-dd9c-4e3f-a747-8ca67985e293'

useMoneyFormat

Money format

declare function useMoneyFormat(): void;
import { useMoneyFormat } from "use-fns";
const res = useMoneyFormat();

useLocalCache

automatic serialization localStorage

declare function useLocalCache(): void;
import { useLocalCache } from "use-fns";
const local = useLocalCache();
local.setItem("person", { name: "Joruno", age: 22 });
local.getItem("person");
local.removeItem("person");
local.clear();
local.key(0);
local.length();

useSessionCache

automatic serialization sessionStorage

declare function useSessionCache(): void;
import { useSessionCache } from "use-fns";
const session = useSessionCache();
session.setItem("person", { name: "Joruno", age: 22 });
session.getItem("person");
session.removeItem("person");
session.clear();
session.key(0);
session.length();

useFuzzyQuery

Fuzz query

declare function useFuzzyQuery<T extends Record<string, any>, U extends T[]>(
  list: U,
  keyWord: string,
  attr: keyof T
): void;
import { useFuzzyQuery } from "use-fns";
useFuzzyQuery(
  [
    { name: "Joruno", age: 22 },
    { name: "hanchen", age: 22 },
    { name: "old man", age: 21 },
  ],
  "o",
  "name"
);

/**
 * [
 *    {name: 'Joruno', age: 22},
 *    {name: 'old man', age: 21},
 * ]
 */

useForeachTree

foreach all tree node

declare function useForeachTree<T extends Record<string, any>>(data: T[],cb: Function, childrenName:keyof T): void;
import { useForeachTree } from "use-fns";
useForeachTree(
  [
    { 
      name: "Joruno", 
      age: 22,
      children: [
          { 
            name: "hanchen",
            age: 22
          },
          { 
            name: "old man", 
            age: 21
          }
      ]
    }
  ],
  console.log,
  'children'
);

/**
 * 
 * {
 *    name: 'Joruno',
 *    age: 22,
 *    children: [ 
 *      { name: 'hanchen', age: 22 }, 
 *      { name: 'old  man', age: 21 } 
 *    ]
 * },
 *
 * { name: 'hanchen', age: 22 },
 *
 * { name: 'old man', age: 21 }
 * 
 */

useCharacterCount

Get the number of characters in a string

declare function useCharacterCount(s: string, char: string): number;
import { useCharacterCount } from "use-fns";
const count = useCharacterCount('Joruno','o') // 2

useIsEmptyObj

Check if object is empty

declare function useIsEmptyObj<T extends Record<string, any>(obj: T): boolean;
import { useIsEmptyObj } from "use-fns";
useIsEmptyObj({}) // true
useIsEmptyObj({a: 1}) // false

useDelay

Wait for a while before calling function

declare function useDelay(ms: number): Promise<void>;
import { useDelay } from "use-fns";
await useDelay(1000) // Wait for one second

useDaysBetween

Get the day difference between two dates

declare function useDaysBetween(d1:number, d2: number): number;
import { useDaysBetween } from "use-fns";
const timeStamp = Date.now()
useDaysBetween(timeStamp,timeStamp + 3600 * 1000 * 24 * 7) // 7

useRedirect

Redirect to another URL

declare function useRedirect(url: string): string;
import { useRedirect } from "use-fns";
useRedirect('https://github.com') // Redirect to github

useTouchSupported

Check for touch support on your device

declare function useTouchSupported(): boolean;
import { useTouchSupported } from "use-fns";
useTouchSupported() // true or false

useInsertHTMLAfter

Insert HTML string after element

declare function useInsertHTMLAfter(html: string, el: Element): void;
import { useInsertHTMLAfter } from "use-fns";
useInsertHTMLAfter('<div>useInsertHTMLAfter</div>',document.body)

useShuffle

Shuffle array

declare function useShuffle(arr: any[]): any[];
import { useShuffle } from "use-fns";
useShuffle([1,2]) // [1,2] or [2,1]

useGetSelectedText

Get selected text on webpage

declare function useGetSelectedText(): string;
import { useGetSelectedText } from "use-fns";
useGetSelectedText()

useGetRandomBoolean

Get random boolean

declare function useGetRandomBoolean(): boolean;
import { useGetRandomBoolean } from "use-fns";
useGetRandomBoolean() // true or false

useSum

Calculate the sum of an array

declare function useSum(arr: any[]): number;
import { useSum } from "use-fns";
useSum([]) // 0
useSum([1,2]) // 3
useSum([1,2,3]) // 6

useAverage

Calculate the average of an array

declare function useAverage(arr: any[]): number;
import { useAverage } from "use-fns";
useAverage([]) // 0
useAverage([1,2]) // 1.5
useAverage([1,2,3]) // 2

useIsUrl

Determine if a string is a valid URL

declare function useIsUrl(arr: any[],opts: {readonly lenient?: boolean} = {lenient: false}): boolean;
import { useIsUrl } from "use-fns";
useIsUrl('https://github.com') // true
useIsUrl('github.com') // false;
useIsUrl('github.com',{lenient: true}) // true

useGithubUrlFromGit

Convert git address to github address

declare function useGithubUrlFromGit(url: string,opts: Record<string,any> = {}): string;
import { useGithubUrlFromGit } from "use-fns";
useGithubUrlFromGit('git+https://github.com/Joruno-w/use-fns.git') // https://github.com/Joruno-w/use-fns

useIsScoped

Check if a string is npm scoped

declare function useIsScoped(s: string): boolean;
import { useIsScoped } from "use-fns";
useIsScoped('@joruno/use-fns') // true
useIsScoped('joruno/use-fns') // false

useArrayMoveMutable

Swap two elements of an array (mutable)

declare function useArrayMoveMutable(arr: unknow[],fromIndex: number,toIndex: number): void;
import { useArrayMoveMutable } from "use-fns";
useArrayMoveMutable([1,2,3],0,2) // [3,2,1]
useArrayMoveMutable([1,2,3],0,-2) // [2,1,3]

useArrayMoveImmutable

Swap two elements of an array (immutable)

declare function useArrayMoveImmutable(arr: unknow[],fromIndex: number,toIndex: number): void;
import { useArrayMoveImmutable } from "use-fns";
useArrayMoveImmutable([1,2,3],0,2) // [3,2,1]
useArrayMoveImmutable([1,2,3],0,-2) // [2,1,3]

License

MIT License © 2022 Joruno-w