Skip to content

fbarrailla/lstorage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💾 LStorage.

A simple browser localStorage helper, no more. Try it!

  • ✅ Store/retrieve string, number, array, object or boolean (json stringified/parsed)
  • ✅ Update stored value
  • ✅ Watch changes from other tabs
  • ✅ IE8+ compatibility
  • ✅ Small (<2Ko minified)
  • ❌ No polyfills
  • ❌ No sessionstorage, IndexedDB, WebSQL...

Installation & usage

npm install lstorage
import storage from 'lstorage'

API Reference

. set ( key, value )

storage.set('username', 'Elliot')
storage.set('user', { name: 'Elliot', age: 28 })

. get ( key, [defaultValue] )

storage.get('username') // 'Elliot'
storage.get('user') // Object{ name: 'Elliot', age: 28 }
storage.get('counter', 0) // 0 (+implicit storage.set('counter', 0))

. update ( key, modifier )

storage.update('counter', c => c+1)
// ...  
storage.update('letters', letters => letters.filter(l => l !== 'b'))
// ...
storage.update('user', user => {
  user.lastname = 'Alderson'
  return user
})

. remove ( key )

storage.remove('username')
// storage.get('username') eq. null

. watch ( key, handler )

storage.watch('counter', ({ prev, next }) => {
  console.info('counter modified from another tab')
  console.info('previous value', prev) // 2
  console.info('next value', next) // 3
})

. unwatch ( key, handler )

storage.unwatch('counter', counterChangeHandler)

. all ( )

// returns an array containing all key/values persisted by lstorage
storage.all().forEach(({ key, value }) => {
  console.info(`storage[ ${key} ]`, value)
})

. removeAll ( )

storage.removeAll() // removes all elements persisted by lstorage

Enjoy !

.watch

About

A simple browser localStorage helper, no more.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published