Giter Club home page Giter Club logo

object-in-session-storage's Introduction

ObjectInSessionStorage

A TypeScript/JavaScript class representing an object or array stored in the
browser's sessionStorage. The item is identified by a unique string this.key
and stored as a key:value pair.
When you call the constructor, if the key argument is a string that isn't
empty and the value argument is not undefined or null, the item will be
stored immediately. Else, the item won't be stored until you call
this.set(value).

Note: this only works when run in a browser environment.

Constructor

view constructor
constructor(
    key? = '',
        // assigned to this.key

    value?: Object | any[]  = undefined
)
    // If `key` is not an empty string and `value` is defined, the item 
    // is stored immediately.

Properties

view properties
key: string // the unique ID for the stored object or array.
    
className: string // read-only

Methods

view methods
set(value: Object | any[]): void
    // Saves item `value` in storage.  Replaces previous value, if any.

get(): Object | any[]
    // Returns the stored object or array.

getAsJSON(): string
    // Returns stored object or array as JSON.

modify(changes: Object | any[]): void
    // `changes` does not replace the current value.  It is merged into the current value.

remove(): void
    // After calling this, both the key and value are no longer in
    // storage.  You can store the item again by calling this.set(value)

The methods below are not important to know about in order to use this
class. They're inherited from BaseClass .

protected   _createGetterAndOrSetterForEach(
		propertyNames: string[],
		configuration: IGetterSetterConfiguration
	   ) : void
    /*********************
    Use this method when you have a bunch of properties that need getter and/or 
    setter functions that all do the same thing. You pass in an array of string 
    names of those properties, and the method attaches the same getter and/or 
    setter function to each property.
    IGetterSetterConfiguration is this object:
    {
        get_setterFunction?: (
             propertyName: string, index?: number, propertyNames?: string[]
        ) => Function,
	    // get_setterFunction takes the property name as first argument and 
	    // returns the setter function.  The setter function must take one 
	    // parameter and return void.
	    
        get_getterFunction?: (
             propertyName: string, index?: number, propertyNames?: string[]
        ) => Function
	    // get_getterFunction takes the property name as first argument and 
	    // returns the getter function.  The getter function must return something.
    }
    *********************/ 
	   
	   
protected   _returnThis_after(voidExpression: any) : this
    // voidExpression is executed, then function returns this.
    // Even if voidExpression returns something, the returned data isn't used.


protected   _errorIfPropertyHasNoValue(
                property: string, // can contain dot-notation, i.e., 'property.subproperty'
                propertyNameInError? = ''
            ) : void
    // If value of this[property] is undefined or null, it triggers fatal error:
    // `The property "${propertyNameInError}" has no value.`

Usage Example

view example
// It might be a good idea to name each class instance after its key.
// After instantiation, you wouldn't modify its `key` property.

let user1 = new ObjectInSessionStorage(
    'user1',
    {username: 'papasmurf', password: 'i_love_smurfette'}
);

let user2 = new ObjectInSessionStorage(
    'user2',
    {username: 'smurfette', password: 'i_love_papa'}
);

// Or, you could create a singleton instance to handle all stored objects,
// and change its `key` when you want to change what specific object to handle.

let objInSessionStorage = new ObjectInSessionStorage();
objInSessionStorage.key = 'user1';
objInSessionStorage.set({username: 'papasmurf', password: 'i_love_smurfette'});

objInSessionStorage.key = 'user2';
objInSessionStorage.set({username: 'smurfette', password: 'i_love_papa'});

Inheritance Chain

ObjectInSessionStorage<--ObjectInBrowserStorage<--ItemInBrowserStorage<--BaseClass

Installation

npm i  @writetome51/object-in-session-storage

Loading

// If using TypeScript:
import {ObjectInSessionStorage} from '@writetome51/object-in-session-storage';
// If using ES5 JavaScript:
var ObjectInSessionStorage = 
    require('@writetome51/object-in-session-storage').ObjectInSessionStorage;

object-in-session-storage's People

Contributors

writetome51 avatar

Watchers

 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.