Giter Club home page Giter Club logo

localstorage-helper's Introduction

Local Storage Helper

Publish Package to NPM

NPM: https://www.npmjs.com/package/localstorage-helpr

  • 🇧🇷 Lide de forma mais segura com o window.localStorage através deste Helper, que aplica algumas validações de objeto e parse de objetos para string (e vice-versa).

  • 🇬🇧 Handle window.localStorage more securely through this Helper, which applies some object validations and object parse to string (and vice versa).

  • GitHub

  • Stackblitz - Live Demo [JS vanilla]

  • Stackblitz - Live Demo [React Web]


🇧🇷 Como usar | 🇬🇧 How to use

  • 🇧🇷 Rode npm i --save localstorage-helpr ou yarn add localstorage-helpr

  • 🇬🇧 Run npm i --save localstorage-helpr or yarn add localstorage-helpr

const { create, update, remove, read, clear } = require('localstorage-helpr')

const KEY = 'CHAVE'
const VALUE = 'VALOR'

// 🇧🇷 Cria um item no LocalStorage | 🇬🇧 Creates an item in LocalStorage
create(KEY, VALUE)

// 🇧🇷 Atualiza um item no LocalStorage | 🇬🇧 Updates an item in LocalStorage
update(KEY, VALUE)

// 🇧🇷 Remove um item no LocalStorage | 🇬🇧 Removes an item in LocalStorage
remove(KEY, VALUE)

// 🇧🇷 Lê o valor de um item no LocalStorage | 🇬🇧 Reads the value of an item in LocalStorage
read(KEY)

// 🇧🇷 Limpa o LocalStorage | 🇬🇧 Clean LocalStorage
clear()

// -----------------------------------------------------

// > BONUS

// 🇧🇷 Você tem acesso aos métodos usados internamente para manipular os dados
// 🇬🇧 You have access to the methods used internally to manipulate the data

const { isObject, objectDeserialize, objectSerialize, } = require('localstorage-helper')

let obj = { a: 1, b: 2 }
let arrayIsObj = [ 1, 3, 5, 7, 9]
let number = 33

isObject(obj) // true
isObject(number) // false
isObject(arrayIsObj) // true

objectSerialize(obj) // "{"a":1,"b":2}"
objectSerialize(arrayIsObj) // "[ 1, 3, 5, 7, 9]"

objectDeserialize(obj) // { a: 1, b: 2 }
objectDeserialize(arrayIsObj) // [ 1, 3, 5, 7, 9]
  • Imagem de demonstração/Demo image

🇧🇷 Detalhes | 🇬🇧 Details

  • 🇧🇷 Por baixo dos panos ao criar ou atualizar um item, se for um objeto ele irá serializar para string, assim não corremos o risco de perder os dados ou corrompe-los, e na hora de recuperar com o read ele irá desserializar para que você tenha seu objeto intacto.

  • 🇬🇧 Under the hood when creating or updating an item, if it is an object it will serialize to string, so we don't run the risk of losing data or corrupting it, and when recovering with read it will deserialize for you to have your object intact.

  • 🇧🇷 Caso sinta necessidade de entender melhor sobre os métodos, você pode conferir os testes de unidade aqui dentro de cada pasta tem seu respectivo arquivo de teste.

  • 🇬🇧 If you feel the need to better understand the methods, you can check the unit tests here inside each folder has its respective test file.

localstorage-helper's People

Contributors

dependabot[bot] avatar ipetinate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

localstorage-helper's Issues

Criar React Hooks para outras operações com LS

Criar hooks customizados do React para trabalhar de uma maneira mais flexível com a lib, por exemplo, obter dados do localstorage, gravar dados ao iniciar o componente, gravar dados após alteração de alguma determinada variável, disparar ações após atualizações de uma determinada chave, etc

Criar React Hooks para obter dados de uma determinada Key

Criar um hook customizado do react para obter a Key do localstorage, semelhante a esse:

import { useEffect } from 'react'

import { read } from 'localstorage-helpr'

/**
 * Hook for get LS data and set to a component state, this hook call the `read` method from `localstorage-helpr` lib on component mount and execution.
 * @param {string} key LS Key for get Data
 * @param {Function} setState Function to set LS data to componente state (ex: reference for `setState` or `setMyData` on component)
 */
export const useGetLocalStorageData = (key, setState) => {
  useEffect(() => {
    setState(read(key))
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
}

export default useGetLocalStorageData

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.