UNPKG

@gravityforms/utils

Version:
98 lines (92 loc) 1.9 kB
/** * @module localStorage * @description A set of utils to get, set, clear and remove localStorage data. * */ /** * @function put * @description Put an entry into localStorage. * * @since 1.0.0 * * @param {string} key The local storage key. * @param {string} value The local storage value. * * @return {void} * * @example * import { localStorage } from "@gravityforms/utils"; * * function Example() { * const data = { * some: 'thing', * even: 'more', * }; * localStorage.put( 'my-key', JSON.stringify( data ) ); * }; * */ const put = ( key, value ) => { window.localStorage.setItem( key, value ); }; /** * @function get * @description Get an entry from localStorage. * * @since 1.0.0 * * @param {string} key The local storage key. * * @return {string|null} The data as string for the key, or undefined. * * @example * import { localStorage } from "@gravityforms/utils"; * * function Example() { * const data = localStorage.get( 'my-key' ); * if ( data ) { * console.log( JSON.parse( data ) ); * } * }; * */ const get = ( key ) => window.localStorage.getItem( key ); /** * @function remove * @description Remove an entry from localStorage. * * @since 1.0.0 * * @param {string} key The local storage key. * * @return {void} * * @example * import { localStorage } from "@gravityforms/utils"; * * function Example() { * localStorage.remove( 'my-key' ); * }; * */ const remove = ( key ) => window.localStorage.removeItem( key ); /** * @function clear * @description Remove all entries from localStorage. * * @since 1.0.0 * * @return {void} * * @example * import { localStorage } from "@gravityforms/utils"; * * function Example() { * localStorage.clear(); * }; * */ const clear = () => { window.localStorage.clear(); }; export { put, get, remove, clear };