Skip to content
This repository has been archived by the owner. It is now read-only.
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 

README.adoc

Resetable

Setup

npm i --save resetable

Usage

const Resetable = require('resetable')
const name = new Resetable('defaultValue')

What is this?

You all love creating chainable functions, which set global value and once to use that global, you clean it off. Okay wait! Let’s check the below example.

Without Resetable

const carStore = {}
let carModel = null
let carType = null


carStore.model = (model) => {
    carModel = model
    return carStore
}

carStore.type = (type) => {
    carType = type
    return carStore
}

carStore.search = () => {
    const model = carModel
    const type = carType
    carModel = null
    carType = null

    // perform search
}

carStore
    .model('1990')
    .type('stang')
    .search()

With Resetable

const Resetable = require('resetable')

const carStore = {}
let carModel = new Resetable(null) (1)
let carType = new Resetable(null)


carStore.model = (model) => {
    carModel.set(model) (2)
    return carStore
}

carStore.type = (type) => {
    carType.set(type)
    return carStore
}

carStore.search = () => {
    const model = carModel.pull() (3)
    const type = carType.pull()

    // perform search
}

carStore
    .model('1990')
    .type('stang')
    .search()
  1. Define the temporary variable as resetable with a default value.

  2. Make use of the set method to set the value.

  3. Calling the pull method will clear the resetable variable back to the original value.

About

A sweet to define temporary values and clear them off conditionally

Resources

Releases

No releases published

Packages

No packages published
You can’t perform that action at this time.