Git Product home page Git Product logo

js-helpers's Introduction

js-helpers

My helper collections

Usage

Hooks

useEvent

Register an event listener, by default event will attatched to window object.

import { useEvent } from 'js-helpers'

useEvent({
    event: 'load',
    handler() {
        // do some thing
    }
})

Register an event listener for document or DOMElement.

import { useEvent } from 'js-helpers'

useEvent({
    event: 'load',
    handler() {
        // do something
    }
}, document)

// or
const button = document.getElementById('btn')
useEvent({
    event: 'onclick',
    handler() {
        // do something
    }
}, button)

useEvent returns a function that stops firing the callback:

import { useEvent } from 'js-helpers'

// Register an event listener
const event = useEvent({
    event: 'load',
    handler() {
        // do something
    }
}, document)

// stop firing the callback
event()

useInterval

Repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.

import { useInterval } from 'js-helpers'

// execute callback every 3s
useInterval(function() {
    // do something
}, 3000)

useInterval returns a function that stops firing the callback:

import { useInterval } from 'js-helpers'

// execute callback every 3s
const poll = useInterval(function() {
    // do something
}, 3000)

// stop firing the callback
poll()

useTimeout

Sets a timer which executes a function or specified piece of code once the timer expires.

import { useTimeout } from 'js-helpers'

useTimeout(function() {
    // do something
}, 3000)

useTimeout returns a function that stops firing the callback:

import { useTimeout } from 'js-helpers'

// execute callback every 3s
const timer = useTimeout(function() {
    // do something
}, 3000)

// stop firing the callback
timer()

Utils

noop

Perform no operation.

Why whould you need to use this? Here an example:

import { noop } from 'js-helpers'

export let warn = noop

if (process.env.NODE_ENV !== 'production') {
    warn = (msg) => {
        console.error(`[QuickProp warn]: ${msg}`)
    }
}

Now when you call warn function, it only show log output during development. This mean that in production, the warn function will not be overwritten.

tap

Invokes interceptor with the object, and then returns object.

import { tap } from 'js-helpers'

const value = {}

const newValue = tap(value, function(payload) {
    // do something with the payload
})

when

Apply the callback if the given "value" is true.

import { when } from 'js-helpers'

const value = {}

when(value, function(payload) {
    // do something with the payload
    return payload
})

// you can provide a third callback to be execute if value if falsy
when(value, function(payload) {
    // do something with the payload
    return payload
}, function(payload) {
    // Execute when value is falsy
    // do something with the payload
    return payload
})

sleep

Async Sleep Function

Note: please use sleep is asynchronous

import { sleep } from 'js-helpers'

async function doSomething() {
    // Sleep for 3s
    await sleep(3000)
    // Then do something
}

doSomething()

License

License under MIT

js-helpers's People

Contributors

socheatsok78 avatar

Watchers

James Cloos avatar  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.