Giter VIP home page Giter VIP logo

asynkstorage's Introduction

About

AsynkStorage is a extended and promised API to manage localStorage. AsynkStorage has native localStorage methods and two extras like has and keys.

Installation

npm i -S asynkstorage
yarn add asynkstorage

Include

Common js

const LocalStorage = require('asynkstorage')

ES6

import LocalStorage from 'asynkstorage'

setItem(key, value)

Sets a key pair value into localStorage

As normal promise

LocalStorage.setItem('foo').then(() => {
	console.log('Done')
})

No JSON stringify anymore...

const user = {id: 1}
LocalStorage.setItem('user', user).then(() => {
	
})

Async / Await

try {
	const foo = await LocalStorage.setItem('foo')
}
catch (err) {
	console.log(err) // undefined
}

getItem(key, default)

Retrieve a key pair value stored in localStorage

As normal promise

LocalStorage.getItem('foo').then(value => {
	console.log('This is foo: ', value) // This is foo: value
})

With default value

LocalStorage.getItem('foo', 'hello foo').then(value => {
	 console.log('This is foo', value) // This is foo: hello foo
})

No JSON parse anymore...

LocalStorage.getItem('user').then(user => {
	 console.log(user) // {id: 1}
})

Async / Await

try {
	const foo = await LocalStorage.getItem('foo')
}
catch (err) {
	console.log(err) // undefined
}

removeItem(key)

Removes a key pair value from localStorage

As normal promise

LocalStorage.removeItem('foo').then(() => {
	console.log('Removed')
})

Async / Await

const foo = await LocalStorage.removeItem('foo')

has(key)

Validate if a key pair value exists in localStorage

As normal promise

LocalStorage.has('foo')
	.then(() => {
		console.log('Exists')
	})
	.catch(e => {
		console.log('Does not exist')
	})

Async / Await

const foo = await LocalStorage.setItem('foo')

if (foo) {
	console.log('Exists')
}
else {
	console.log('Does not exist')
}

keys()

Retrieve all keys from localStorage

As normal promise

LocalStorage.keys().then(keys => {
	console.log('LocalStorage', keys)
})

Async / Await

const keys = await LocalStorage.keys()
if (keys.length > 0) {
	console.log(`There are ${keys.length} keys in localStorage`)
}
else {
	console.log('LocalStorage is clean ')
}

clear()

Remove all key value pairs from localStorage

As normal promise

LocalStorage.clear().then(() => {
	console.log('LocalStorage is clean')
})

Async / Await

await LocalStorage.clear()

asynkstorage's People

Contributors

fdorantesm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

asynkstorage's Issues

Fail to compile on react

Describe the bug

When I want to run the project in production it fails to compile the React Build.

Creating an optimized production build...
Failed to compile.

Failed to minify the code from this file:

./node_modules/asynkstorage/index.js:1

To reproduce

  1. Import the library
  2. Run 'npm install'
  3. Run 'npm build'
  4. Run, really run.

OS: Centos 7
Node version: v10.7.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.