lstorage
v1.0.1
Published
A browser localStorage helper, no more.
Downloads
2
Readme
:floppy_disk: LStorage.
A simple browser localStorage
helper, no more.
- :white_check_mark: Store/retrieve string, number, array, object or boolean (json stringified/parsed)
- :white_check_mark: Update stored value
- :white_check_mark: Watch changes from other tabs
- :white_check_mark: IE8+ compatibility
- :white_check_mark: Small (<2Ko minified)
- :x: No polyfills
- :x: 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