storage_session.js

/**
 * @module sessionStorage
 * @description A set of utils to get, set, clear and remove localStorage data.
 *
 */

/**
 * @function put
 * @description Put an entry into sessionStorage.
 *
 * @since 1.0.0
 *
 * @param {string} key   The session storage key.
 * @param {string} value The session storage value.
 *
 * @return {void}
 *
 * @example
 * import { sessionStorage } from "@gravityforms/utils";
 *
 * function Example() {
 *     const data = {
 *         some: 'thing',
 *         even: 'more',
 *     };
 *     sessionStorage.put( 'my-key', JSON.stringify( data ) );
 * };
 *
 */
const put = ( key, value ) => {
	window.sessionStorage.setItem( key, value );
};

/**
 * @function get
 * @description Get an entry from sessionStorage.
 *
 * @since 1.0.0
 *
 * @param {string} key The session storage key.
 *
 * @return {string|null} The data as string for the key, or undefined.
 *
 * @example
 * import { sessionStorage } from "@gravityforms/utils";
 *
 * function Example() {
 *     const data = sessionStorage.get( 'my-key' );
 *     if ( data ) {
 *         console.log( JSON.parse( data ) );
 *     }
 * };
 *
 */
const get = ( key ) => window.sessionStorage.getItem( key );

/**
 * @function remove
 * @description Remove an entry from sessionStorage.
 *
 * @since 1.0.0
 *
 * @param {string} key The session storage key.
 *
 * @return {void}
 *
 * @example
 * import { sessionStorage } from "@gravityforms/utils";
 *
 * function Example() {
 *     sessionStorage.remove( 'my-key' );
 * };
 *
 */
const remove = ( key ) => window.sessionStorage.removeItem( key );

/**
 * @function clear
 * @description Remove all entries from sessionStorage.
 *
 * @since 1.0.0
 *
 * @return {void}
 *
 * @example
 * import { sessionStorage } from "@gravityforms/utils";
 *
 * function Example() {
 *     sessionStorage.clear();
 * };
 *
 */
const clear = () => {
	window.sessionStorage.clear();
};

export { put, get, remove, clear };