Coder Social home page Coder Social logo

vitorluizc / persistence Goto Github PK

View Code? Open in Web Editor NEW
19.0 3.0 1.0 627 KB

๐Ÿ’พ Persistence provides a pretty easy API to handle Storage's implementations.

License: MIT License

TypeScript 100.00%
localstorage sessionstorage storage storage-engine indexeddb typescript javascript ava bili

persistence's Introduction

@vitorluizc/persistence

Build Status License Library minified size Library minified + gzipped size

Persistence provides a pretty easy API to handle Storage's implementations.

  • โšก Pretty fast and smaller than 0.3KB (minified + gzipped ESM);
  • ๐Ÿท๏ธ Type definitions to TS developers and IDE/Editors autocomplete/intellisense;
  • ๐Ÿ“ฆ ESM, CommonJS and UMD distributions (CDN uses UMD as default);

Installation

This library is published in the NPM registry and can be installed using any compatible package manager.

npm install @vitorluizc/persistence --save

# For Yarn, use the command below.
yarn add @vitorluizc/persistence

Installation from CDN

This module has an UMD bundle available through JSDelivr and Unpkg CDNs.

<script src="https://cdn.jsdelivr.net/npm/@vitorluizc/persistence"></script>

<script>
  // module will be available through `persistence` function.

  var name = persistence('name', { placeholder: 'Unknown' });

  name.get();
  //=> 'Unknown'
</script>

Usage

It exports a factory function to create persistence. A persistence can easily set, get and delete JSON parseable values to any Storage implementation (ex. SessionStorage and LocalStorage).

import createPersistence from '@vitorluizc/persistence';

const persistence = createPersistence<string>('Name', {
  timeout: 1000 * 60 * 60 * 24, // A day in milliseconds
  storage: window.sessionStorage,
  placeholder: ''
});

// Setups field's value as a persistent state on session.
const field = document.querySelector('input[name="name"]');
field.value = persistence.get();
field.addEventListener('input', () => persistence.set(field.value));

API

  • createPersistence

    The default exported factory function. It receives storage's key and, optionally, PersistenceOptions as arguments and returns a Persistence.

    import createPersistence from '@vitorluizc/persistence';
    
    const persistence = createPersistence('Storage\'s key', { placeholder: 'None' });
    TypeScript type definitions.
    declare const createPersistence: {
      <T = any, U = T> (
        name: string,
        options: PersistenceOptions & {
          placeholder: U;
        }
      ): Persistence<T, U>;
    
      <T = any, U = T | undefined> (
        name: string,
        options?: PersistenceOptions<U>
      ): Persistence<T, U>;
    };
    
    export default createPersistence;
  • PersistenceOptions

    Options used as second argument on createPersistence to set timeout, storage and placeholder value to Persistence.

    import createPersistence, { PersistenceOptions } from '@vitorluizc/persistence';
    
    const options: PersistenceOptions<boolean> = {
      timeout: 1000 * 60 * 60, // 1 hour in milliseconds.
      placeholder: false
    };
    
    const persistence = createPersistence<boolean>('isSignedIn', options);
    TypeScript type definitions.
    export interface PersistenceOptions <T = any> {
      storage?: IStorage;
      timeout?: number;
      placeholder?: T;
    }
  • Persistence

    An object with a pretty easy API to handle a Storage implementation.

    • get: Get value from persistent storage.

    • set: Set a value to a persistent storage.

    • delete: Delete value from persistent storage.

    import createPersistence, { Persistence } from '@vitorluizc/persistence';
    
    // ...
    
    type SignUpFormState = { nickname: string, };
    
    const state: Persistence<SignUpFormState> = createPersistence('state@name', {
      timeout: 1000 * 60 * 60 * 24, // A day in milliseconds
      placeholder: {
        nickname: ''
      },
    });
    
    
    $nickname.value = state.get().nickname;
    $nickname.on('keydown', (e) => state.set({ nickname: e.target.value }));
    
    $signUpForm.on('submit', (e) => {
      services.signUp(state.get());
    
      state.delete();
    });
    TypeScript type definitions.
    export interface Persistence <T = any, U = (T | undefined)> {
      get (): T | U;
      set (value: T): void;
      delete (): void;
    }

License

Released under MIT License.

persistence's People

Contributors

vitorluizc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mechamobau

persistence's Issues

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.