Git Product home page Git Product logo

storage-typed's Introduction

storage-typed

Web Storage only accepts string value so you should write this verbose code everytime:

// get
try {
  const value = window.localStorage.getItem(key);
  return value ? JSON.parse(value) : null;
} catch (e) {
  /* ... */
}

// set
window.localStorage.setItem(key, JSON.stringify(value));

And it does not provide any type-specific operation. (e.g. increasing number value, push to array)

// increasing count
const count = JSON.parse(window.localStorage.getItem(key));
window.localStorage.setItem(key, JSON.stringify(count + 1));

// push to array
const arr = JSON.parse(window.localStorage.getItem(key));
window.localStorage.setItem(key, JSON.stringify([...arr, value]));

So storage-typed resolves all things above.

const count = TypedStorageFactory.create("count", 0); // NumberTypedStorage
count.increase();
count.get(); // 1

const arr = TypedStorageFactory.create("array", ["foo"]); // ArrayTypedStorage
arr.pop(); // "foo"
arr.push("bar");
arr.get(); // ["bar"]

/* and any other types... */

API References

TypedStorageFactory

Creates TypedStorage by type of passed initial value. Note test code.

TypedStorageFactory.create<T>(key, initialValue, options);
  • create: (key, initialValue, options) => TypedStorage<T> | NumberTypedStorage | BooleanTypedStorage | ArrayTypedStorage<T[number]>
    • returns instanceof TypedStorage by type of passed initial value
    • parameters
      • key: string
        • required
        • unique key for value
      • initialValue: T
        • required
        • any value which TypedStorage will be initialized with
      • options: TypedStorageOptions<T>

TypedStorage

Provides JSON parsing/stringifying. Note test code.

const storage = new TypedStorage<T>(key, initialValue, options);
storage.get();
storage.set(value);
  • constructor: (key, initialValue, options) => TypedStorage<T>

    • returns instanceof TypedStorage by type of passed initial value
    • parameters
      • key: string
        • required
        • unique key for value
      • initialValue: T
        • required
        • any value which TypedStorage will be initialized with
      • options: TypedStorageOptions<T>
  • get: () => T

    • returns current value
  • set: (next) => void

    • sets current value to passed value
    • parameters
      • next: T
        • required
        • next value

TypedStorageOptions

interface TypedStorageOptions<T> {
  storage?: Storage;
}
  • storage: Storage
    • Storage which TypedStorage will use

NumberTypedStorage

Extends number-specific methods based on TypedStorage API. Note test code.

const storage = new NumberTypedStorage(key, initialValue, options);
storage.increase();
storage.decrease();
  • constructor: (key, initialValue, options) => TypedStorage<number>

    • returns instanceof TypedStorage by type of passed initial value
    • parameters
      • key: string
        • required
        • unique key for value
      • initialValue: number
        • required
        • any value which NumberTypedStorage will be initialized with
      • options: TypedStorageOptions<number>
  • increase: () => void

    • adds 1 to current value
  • decrease: () => void

    • subtracts 1 from current value

BooleanTypedStorage

Extends boolean-specific methods based on TypedStorage API. Note test code.

const storage = new BooleanTypedStorage(key, initialValue, options);
storage.toggle();
storage.true();
storage.false();
  • constructor: (key, initialValue, options) => TypedStorage<boolean>

    • returns instanceof TypedStorage by type of passed initial value
    • parameters
      • key: string
        • required
        • unique key for value
      • initialValue: boolean
        • required
        • any value which BooleanTypedStorage will be initialized with
      • options: TypedStorageOptions<boolean>
  • toggle: () => void

    • reverses current value
  • true: () => void

    • sets current value to true
  • false: () => void

    • sets current value to false

ArrayTypedStorage

Extends number-specific methods based on TypedStorage API. Note test code.

const storage = new ArrayTypedStorage<T>(key, initialValue, options);
storage.push(value);
storage.pop();
  • constructor: (key, initialValue, options) => TypedStorage<T[]>

    • returns instanceof TypedStorage by type of passed initial value
    • parameters
      • key: string
        • required
        • unique key for value
      • initialValue: T[]
        • required
        • any value which NumberTypedStorage will be initialized with
      • options: TypedStorageOptions<T[]>
  • push: (value: T) => void

    • appends value to the end of current array
  • pop: () => T | null

    • removes last value of current array. if it is empty, pop returns null.

storage-typed's People

Contributors

hoseungme avatar

Stargazers

Jonghyeon Ko avatar

Watchers

James Cloos avatar  avatar

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.