Coder Social home page Coder Social logo

sametcelikbicak / storage-function Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 0.0 698 KB

A function to manipulate browser storages, v2023.3.19 is ready on NPM

Home Page: https://www.npmjs.com/package/storage-function

License: MIT License

TypeScript 96.79% Shell 3.21%
storage function npm typescript localstorage sessionstorage browser local-storage session-storage javascript

storage-function's Introduction

Logo

Storage Function

npm npm CodeQL Analyze Build & Test dependabot

GitHub Repo stars

A function to manipulate browser storages. Such as store a key value pair to session or local storage, getting key value pairs from local or session storage.You can check all available functions for more details.

Table of contents

Installation

npm i storage-function

Breaking Change

Before 2.0.0 version each function should be import itself like below

import { toLocalStorage, toSessionStorage } from 'storage-function';

or import them with an alias like below

 import * as storageFunction from 'storage-function';

With 2.0.0 version its enough to just import storageFunction then you can reach all helpful function under it like below.

import { storageFunction } from 'storage-function';

Usage

Local storage key and value definitions

Sample key and value definition for testing local storage functions.

const localStorageKey = 'local_storage_key';

const localStorageValue = {
  name: 'object value',
  stringValue: 'string value',
  booleanValue: true,
  numberValue: 1234567890,
  dateValue: new Date(),
};
toLocalStorage
import { storageFunction } from 'storage-function';

storageFunction.toLocalStorage(localStorageKey, localStorageValue);
fromLocalStorage
import { storageFunction } from 'storage-function';

console.log(storageFunction.fromLocalStorage(localStorageKey));

Result:
{"name":"object value","stringValue":"string value","booleanValue":true,"numberValue":1234567890,"dateValue":"2021-08-22T15:57:05.147Z"}
removeFromLocalStorage
import { storageFunction } from 'storage-function';

storageFunction.removeFromLocalStorage(localStorageKey);
clearLocalStorage
import { storageFunction } from 'storage-function';

storageFunction.clearLocalStorage(); // Remove all local storage keys

const exceptedKeys = ['some_key', 'another_key'];
storageFunction.clearLocalStorage(exceptedKeys); // Remove all local storage keys except exceptedKeys
getKeysFromLocalStorage
import { storageFunction } from 'storage-function';

console.log(storageFunction.getKeysFromLocalStorage());

Result:
(2) ["localKey2", "localKey1"]
0: "localKey2"
1: "localKey1"
getAllFromLocalStorage
import { storageFunction } from 'storage-function';

console.log(storageFunction.getAllFromLocalStorage());

Result:
(2) [{}, {}]
0: {localKey2: "\"Local storage value 2\""}
1: {localKey1: "\"Local storage value 1\""}
getKeysCountFromLocalStorage
import { storageFunction } from 'storage-function';

storageFunction.toLocalStorage(localStorageKey, localStorageValue);

console.log(storageFunction.getKeysCountFromLocalStorage());

Result:
1

Session storage key and value definitions

Sample key and value definition for testing session storage functions.

const sessionStorageKey = 'session_storage_key';

const sessionStorageValue = {
  name: 'object value',
  stringValue: 'string value',
  booleanValue: true,
  numberValue: 1234567890,
  dateValue: new Date(),
};
toSessionStorage
import { storageFunction } from 'storage-function';

storageFunction.toSessionStorage(sessionStorageKey, sessionStorageValue);
fromSessionStorage
import { storageFunction } from 'storage-function';

console.log(storageFunction.fromSessionStorage(sessionStorageKey));

Result:
{"name":"object value","stringValue":"string value","booleanValue":true,"numberValue":1234567890,"dateValue":"2021-08-22T15:58:28.646Z"}
removeFromSessionStorage
import { storageFunction } from 'storage-function';

storageFunction.removeFromSessionStorage(sessionStorageKey);
clearSessionStorage
import { storageFunction } from 'storage-function';

storageFunction.clearSessionStorage(); // Remove all session storage keys

const exceptedKeys = ['key_one', 'keyTwo'];
storageFunction.clearSessionStorage(exceptedKeys); // Remove all session storage keys except exceptedKeys
getKeysFromSessionStorage
import { storageFunction } from 'storage-function';

console.log(storageFunction.getKeysFromSessionStorage());

Result:
(2) ["sessionKey2", "sessionKey1"]
0: "sessionKey2"
1: "sessionKey1"
getAllFromSessionStorage
import { storageFunction } from 'storage-function';

console.log(storageFunction.getAllFromSessionStorage());

Result:
(2) [{}, {}]
0: {sessionKey2: "\"Session storage value 2\""}
1: {sessionKey1: "\"Session storage value 1\""}
getKeysCountFromSessionStorage
import { storageFunction } from 'storage-function';

storageFunction.toSessionStorage(sessionStorageKey, sessionStorageValue);

console.log(storageFunction.getKeysCountFromSessionStorage());

Result:
1

Storage functions definition

They work with local and session storage.

clearStorage
import { storageFunction } from 'storage-function';

storageFunction.clearStorage(); // Remove all storage keys

const exceptedKeys = ['id_token', 'access_token'];
storageFunction.clearStorage(exceptedKeys); // Remove all storage keys except exceptedKeys
getKeys
import { storageFunction } from 'storage-function';

console.log(storageFunction.getKeys());

Result:
{localStorage: Array(2), sessionStorage: Array(2)}
localStorage: (2) ["localKey2", "localKey1"]
sessionStorage: (2) ["sessionKey2", "sessionKey1"]
getAll
import { storageFunction } from 'storage-function';

console.log(storageFunction.getAll());

Result:
{localStorage: Array(2), sessionStorage: Array(2)}
localStorage: Array(2)
0: {localKey2: "\"Local storage value 2\""}
1: {localKey1: "\"Local storage value 1\""}
sessionStorage: Array(2)
0: {sessionKey2: "\"Session storage value 2\""}
1: {sessionKey1: "\"Session storage value 1\""}
getKeysCount
import { storageFunction } from 'storage-function';

storageFunction.toLocalStorage(localStorageKey, localStorageValue);
storageFunction.toSessionStorage(sessionStorageKey, sessionStorageValue);

console.log(storageFunction.getKeysCount());

Result:
{localStorage: 1, sessionStorage: 1}

Want to contribute?

You can read and follow our CONTRIBUTING.md and report it using GitHub Issues! for reporting bugs, suggesting enhancements, bugfixes, new features and extras are welcome.

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

Samet ÇELİKBIÇAK
Samet ÇELİKBIÇAK

🚇 💻 🐛 📖 💡 🚧 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

storage-function's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar sametcelikbicak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

storage-function's Issues

Add except function for all clearStorage functions

For storage
clearStorage().except(exceptedKey1).except(exceptedKey2);
For local storage
clearLocalStorage().except(exceptedKey1).except(exceptedKey2);
For session storage
clearSessionStorage().except(exceptedKey1).except(exceptedKey2);

Or another usage for similar result to except keys when remove storage option

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.