A sweet to define temporary values and clear them off conditionally
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
test
.editorconfig
.gitignore
.npmignore
README.adoc
package-lock.json
package.json
rollup.config.js

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.