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

@arquetipo28/localstorage-handler

v1.1.2

Published

Wrapper to save returned data of javascript functions into localStorage

Downloads

5

Readme

@arquetipo28/localstorage-handler

localstorage-handler is a javascript function wrapper which allows you to save any kind of data into localStorage passing a serie of arguments to make it more useful. It:

  • Is bases on JavaScript
  • Allow to save JSON and primitive values
  • Provides a way to configure localStorage data saving
  • Allow force data override
  • Allow set data expiration
  • Allow set data key by function name or specific string
  • Allow to retrieve data once and then destroy it

Installation and Usage

localStorage-handler requires Node.js v12 to run.

$ npm install @arquetipo28/localStorage-handler

Import it into your project or file

// handle is the principal function that will allow you
// to save your data into localStorage
import { handle, stored } from '@arquetipo28/localstorage-handler'

Arguments

Here are described all the arguments you can pass to configure the store of your data with the handle function.

[Function: handle] (@block, @options)
@block = callback
@options = { force: boolean, identifier: string, expire: @expire }
@expire -> { months: integer, hours: integer, minutes: integer } | dateString

And in the other side we have the stored method to retrieve saved information. It also allows you to modify its behavior trough the options.cleanAfter property which specifies if the getted information is going to be deleted after taking it. You can look for some examples below.

[Function: stored] (@options)
@options {object | string} = { identifier: string, cleanAfter: boolean } | 'identifier'

It is important to define that as default each @expire argument will be equal to 0

Examples (handle)

We provide you some useful examples to know how to implement this package.

Using function name as key with array result:

import { handle } from "@arquetipo28/localstorage-handle"
const data = handle(function usersData () {
    // This data will be stored using 'usersData' as key
    return [{}]
})

Passing identifier name as key with array result:

import { handle } from "@arquetipo28/localstorage-handle"
const data = handle(() => {
    // This data will be stored using 'usersData' as key
    return [{}]
}, { identifier: 'usersData' })

Passing identifier name as key with primitive result, including expiration time:

import { handle } from "@arquetipo28/localstorage-handle"
const data = handle(() => {
    // This data will be stored using 'usersData' as key
    return 'Hello World :D'
}, { identifier: 'usersData', expire: { minutes: 30, hours: 0 } })

Implicit identifier name, including expiration time as string:

import { handle } from "@arquetipo28/localstorage-handle"

const expire = new Date()
// Data expires in 10 seconds
expire.setSeconds(expire.getSeconds() + 10)
const data = handle(function expirationDateString () {
    // This data will be stored using 'usersData' as key
    return 'Hello World :D'
}, { expire })

Examples (stored)

stored method provides a way to take or retrieve some of the saved data and then apply certain behaviors to it, like destroy the the stored state by key

Taking stored information once and remove it from localStorage.

import { stored } from "@arquetipo28/localstorage-handle"

// here we assume that you have been handling some data with a 'state'
// implicit or explicit identifier
const data = stored({ identifier: 'state', cleanAfter: true })
// 'state' data was removed from localStorage but is was saved in `data` before
console.log(data) // Output: { state: { message: 'Hello again' } }
console.log(localStorage) // It should not contain the 'state' data

But you can also retrieve it in a safest way, without destroying it, just removing the cleanAfter key from the argument, or passing false instead.

import { stored } from "@arquetipo28/localstorage-handle"

// here we assume that you have been handling some data with a 'state'
// implicit or explicit identifier
const data = stored({ identifier: 'state', cleanAfter: false })
// 'state' data was removed from localStorage but is was saved in `data` before
console.log(data) // Output: { state: { message: 'Hello again' } }
console.log(localStorage) // It should not contain the 'state' data

1.1.0 Update

stored function now can accept a string argument as @options this will be taked as the identifier of the already stored data

import { stored } from '@arquetipo28/localstorage-handler'

// This will do the same as stored({ identifier: 'state', cleanAfter: false })
const data = stored('state')
console.log(data) // Output : { stateData: 'This is fucking awesome!' }

Todos

  • Write Tests
  • Add Examples
  • Test asynchronous integration
  • Test importation cases

License

MIT

Open Software, Hell Yeah!