Giter VIP home page Giter VIP logo

hacktoberfest's Introduction

utils-lazy-hook

Lib

https://www.npmjs.com/package/utils-lazy-hook

How to use

npm i utils-lazy-hook or yarn add utils-lazy-hook

useInterval

useInterval(() => {
  console.log(`Hello World~`);
}, 1000);

useLogic

const {
  isTrue,
  isFalse,
  isNull,
  isUndefined,
  isZero,
  isEven,
  isOdd
  } = useLogic();

useMediaQuery

import {useMediaQuery , breakpoints} from 'utils-lazy-hook'

const isMobile = useMediaQuery("(max-width: 768px)")
const isDesktop = useMediaQuery("(min-width: 768px)")
//or
/*
  DEFAULT breakpoints
  up: {
    mobile: '(min-width: 0px)',
    tablet: '(min-width: 768px)',
    desktop: '(min-width: 1024x)',
  },
  down: {
    mobile: '(max-width: 0px)',
    tablet: '(max-width: 768px)',
    desktop: '(max-width: 1024x)',
  },
*/
const isTablet = useMediaQuery(breakpoints.up.tablet)

useFlatMap

interface CategoryProps {
  category: string;
  movies: MoviesProps[];
}

interface MoviesProps {
  name: string;
  rating: number;
}

  const data: CategoryProps[] = [
    {
      category: "drama",
      movies: [{ name: "It's Okay to not be Okay", rating: 4.0 }]
    },
    {
      category: "romantic",
      movies: [
        { name: "Titanic", rating: 5.0 },
        { name: "Titanic", rating: 5.0 },
        { name: "Titanic", rating: 5.0 },
        { name: "Titanic", rating: 5.0 },
        { name: "Titanic", rating: 5.0 },
        { name: "Titanic", rating: 5.0 }
      ]
    }
  ];

  const res = useFlatMap<CategoryProps[], MoviesProps[]>(data, "movies");

useSortRanking

  const profiles: any[] = [
    {
      name: "Lufy",
      email: "[email protected]",
      total_score: 30
    },
    {
      name: "Zoro",
      email: "[email protected]",
      total_score: 18
    },
    {
      name: "Sanji",
      email: "[email protected]",
      total_score: 18
    }
  ];

const profileSorted = useSortRanking<any[], string>(profiles, "total_score");

example result: {
  [
  {
    name: 'Lufy',
    email: '[email protected]',
    total_score: 30,
    ranking: 1
  },
  {
    name: 'Zoro',
    email: '[email protected]',
    total_score: 18,
    ranking: 2
  },
  {
    name: 'Sanji',
    email: '[email protected]',
    total_score: 18,
    ranking: 2
  }
]
}

useRandomString

  const value = useRandomString()

useOmit

const omitResult = useOmit({a: '1', b: '2', c: '3'}, ['a']);

example omitResult = {
  b: '2',
  c: '3'
}

useMinMax

const { findMin,findMax } = useMinMax();
const minNumber = findMin([1,2,3]) // 1
const maxNumber = findMax([1,2,3]) // 3

useEmailRegex

const email = '[email protected]'
const {isEmail} = useEmailRegex()
console.log(isEmail(email))
// true

useOnReachBottom

import { useState } from 'react'
import { useOnReachBottom } from 'utils-lazy-hook';

function App() {
  const [data, setData] = useState(['Item 0']);

  const elemRef = useOnReachBottom(() => loadMore());

  const getData = (offset: number, limit: number) => {
    return Array.from(new Array(limit)).map((_, i) => `Item ${offset + i}`);
  }

  const loadMore = () => {
    setData(prevData => {
      const total = prevData.length;
      const result = getData(total, 10);
      return [
        ...prevData,
        ...result
      ]
    })
  }

  return (
    <div ref={ref => elemRef.current = ref}>
      {
        data.map((item) => (
          <p key={item}>{item}</p>
        ))
      }
    </div>
  )
}

export default App

hacktoberfest's People

Contributors

gitihm avatar sainytk avatar n4sunday avatar arimsingle avatar iffancoe avatar matsoffi-yk avatar 62theories avatar frametee29 avatar supanut1911 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.