@manufosela/local-storage-helper

@manufosela/local-storage-helper

localStorage wrapper with TTL support, JSON serialization, and automatic expiration

local-storage-helper Demo

Quick usage

import { LocalStorageHelper } from '@manufosela/local-storage-helper';

const storage = new LocalStorageHelper({ prefix: 'demo_' });
storage.set('user', { name: 'Ada' });

Basic Operations

Results will appear here...
import { LocalStorageHelper } from '@manufosela/local-storage-helper';

const storage = new LocalStorageHelper({ prefix: 'demo_' });
storage.set('myKey', 'Hello World');
storage.get('myKey');
storage.remove('myKey');

With TTL

Set a value with TTL to see expiration...
storage.set('tempData', 'expiring', 10_000);
storage.getTTL('tempData');

Storage Info

Click a button to see storage info...
storage.keys();
storage.getSize();
storage.clear();
Demo code (CodePen-ready HTML, CSS, JS)
HTML (html)
<div class="top-links">
    <a href="https://manufosela.dev/utils/">Utils catalog</a>
    <a href="https://manufosela.dev/">Main catalog</a>
    <a href="https://github.com/manufosela/utils/tree/main/packages/local-storage-helper">Source</a>
  </div>
        <demo-theme-toggle></demo-theme-toggle>
  <h1>local-storage-helper Demo</h1>

  <div class="demo-section">
    <h2>Quick usage</h2>
    <pre class="code-block"><code>import { LocalStorageHelper } from '@manufosela/local-storage-helper';

const storage = new LocalStorageHelper({ prefix: 'demo_' });
storage.set('user', { name: 'Ada' });
</code></pre>
  </div>

  <div class="demo-section">
    <h2>Basic Operations</h2>
    <div class="row">
      <input type="text" id="key" placeholder="Key" value="myKey">
      <input type="text" id="value" placeholder="Value" value="Hello World">
      <button id="set">Set</button>
      <button id="get">Get</button>
      <button id="remove" class="danger">Remove</button>
    </div>
    <div class="output" id="output">Results will appear here...</div>
    <pre class="code-block"><code>import { LocalStorageHelper } from '@manufosela/local-storage-helper';

const storage = new LocalStorageHelper({ prefix: 'demo_' });
storage.set('myKey', 'Hello World');
storage.get('myKey');
storage.remove('myKey');
</code></pre>
  </div>

  <div class="demo-section">
    <h2>With TTL</h2>
    <div class="row">
      <input type="text" id="ttl-key" placeholder="Key" value="tempData">
      <input type="number" id="ttl-seconds" placeholder="TTL (seconds)" value="10">
      <button id="set-ttl">Set with TTL</button>
      <button id="check-ttl">Check TTL</button>
    </div>
    <div class="output" id="ttl-output">Set a value with TTL to see expiration...</div>
    <pre class="code-block"><code>storage.set('tempData', 'expiring', 10_000);
storage.getTTL('tempData');</code></pre>
  </div>

  <div class="demo-section">
    <h2>Storage Info</h2>
    <button id="show-keys">Show All Keys</button>
    <button id="show-size">Show Size</button>
    <button id="clear-all" class="danger">Clear All</button>
    <div class="output" id="info-output">Click a button to see storage info...</div>
    <pre class="code-block"><code>storage.keys();
storage.getSize();
storage.clear();</code></pre>
  </div>
CSS (css)
:root {
  --bg: #0c0f14;
  --bg-elevated: #141923;
  --bg-panel: #171d28;
  --border: #262f3f;
  --text: #f4f6fb;
  --text-muted: #a7b0c2;
  --text-dim: #7d879b;
  --accent: #ff8a3d;
  --accent-strong: #ff6a00;
  --accent-soft: rgba(255, 138, 61, 0.16);
  --shadow: 0 20px 50px rgba(5, 8, 14, 0.45);
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --max-width: 1160px;
}

h1 { color: var(--accent); }
    .demo-section {
      background: var(--bg-elevated);
      padding: 2rem;
      border-radius: 8px;
      margin-bottom: 2rem;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .demo-section h2 {
      margin-top: 0;
      color: var(--text-muted);
    }
    button {
      padding: 0.5rem 1rem;
      margin: 0.25rem;
      border: none;
      border-radius: 4px;
      background: var(--accent);
      color: white;
      cursor: pointer;
    }
    button:hover { background: var(--accent-strong); }
    button.danger { background: #dc3545; }
    button.danger:hover { background: #c82333; }
    input {
      padding: 0.5rem;
      margin: 0.25rem;
      border: 1px solid var(--border);
      border-radius: 4px;
    }
    .output {
      margin-top: 1rem;
      padding: 1rem;
      background: var(--bg-panel);
      border-radius: 4px;
      font-family: monospace;
      font-size: 0.875rem;
      white-space: pre-wrap;
    }
    .row {
      display: flex;
      gap: 0.5rem;
      margin-bottom: 0.5rem;
      align-items: center;
    }
    .top-links {
      display: flex;
      gap: 0.5rem;
      flex-wrap: wrap;
      margin-bottom: 1rem;
    }
    .top-links a {
      font-size: 0.8rem;
      color: var(--text-muted);
      text-decoration: none;
      border: 1px solid var(--border);
      padding: 0.35rem 0.75rem;
      border-radius: 999px;
    }

    .code-block {
      background: #0b0f1a;
      border: 1px solid #2b3247;
      border-radius: 8px;
      padding: 12px;
      color: #d6d9e6;
      font-family: "JetBrains Mono", "Courier New", monospace;
      font-size: 0.85rem;
      white-space: pre-wrap;
      word-break: break-word;
    }
  






:root {
  --bg: #0f1117;
  --bg-2: #151a26;
  --bg-spot-1: #1a2136;
  --bg-spot-2: #1d1b34;
  --card: #1c2233;
  --text: #f3f6ff;
  --muted: #b8c0d9;
  --line: #2b3247;
  --accent: #ffb000;
  --accent-2: #00d0ff;
  --surface: #0b0f1a;
  --code-bg: #0b0f1a;
  --code-text: #d6d9e6;
}

:root.light {
  --bg: #f5f5f7;
  --bg-2: #ffffff;
  --bg-spot-1: #f8e9d0;
  --bg-spot-2: #e8eef8;
  --card: #ffffff;
  --text: #1d1d1f;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #ffb000;
  --accent-2: #00a7d6;
  --surface: #f3f4f6;
  --code-bg: #111827;
  --code-text: #f9fafb;
}

:root.dark {
  --bg: #0f1117;
  --bg-2: #151a26;
  --bg-spot-1: #1a2136;
  --bg-spot-2: #1d1b34;
  --card: #1c2233;
  --text: #f3f6ff;
  --muted: #b8c0d9;
  --line: #2b3247;
  --accent: #ffb000;
  --accent-2: #00d0ff;
  --surface: #0b0f1a;
  --code-bg: #0b0f1a;
  --code-text: #d6d9e6;
}

h1 {
  color: var(--accent) !important;
}

.top-links a,
.topbar a {
  color: var(--muted) !important;
  border-color: var(--line) !important;
}

.top-links a:hover,
.topbar a:hover {
  color: var(--text) !important;
}

.card,
.section,
.demo-section,
.panel {
  background: var(--card) !important;
  border-color: var(--line) !important;
  color: var(--text) !important;
}

.code-block,
pre,
code,
.output,
.current-url,
.event-log,
.result-card,
.log {
  background: var(--code-bg) !important;
  color: var(--code-text) !important;
  border-color: var(--line) !important;
}

input,
select,
textarea {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-color: var(--line) !important;
}

button {
  background: linear-gradient(120deg, var(--accent), #ff6a00) !important;
  color: #111 !important;
}

footer {
  color: var(--muted) !important;
}

footer a {
  color: var(--text) !important;
}
JS (js)
import { LocalStorageHelper } from "https://esm.sh/@manufosela/local-storage-helper";

    const storage = new LocalStorageHelper({ prefix: 'demo_' });
    const output = document.getElementById('output');
    const ttlOutput = document.getElementById('ttl-output');
    const infoOutput = document.getElementById('info-output');

    // Basic operations
    document.getElementById('set').addEventListener('click', () => {
      const key = document.getElementById('key').value;
      const value = document.getElementById('value').value;
      storage.set(key, value);
      output.textContent = `Set "${key}" = "${value}"`;
    });

    document.getElementById('get').addEventListener('click', () => {
      const key = document.getElementById('key').value;
      const value = storage.get(key);
      output.textContent = value !== null
        ? `Get "${key}" = ${JSON.stringify(value)}`
        : `Key "${key}" not found`;
    });

    document.getElementById('remove').addEventListener('click', () => {
      const key = document.getElementById('key').value;
      storage.remove(key);
      output.textContent = `Removed "${key}"`;
    });

    // TTL operations
    document.getElementById('set-ttl').addEventListener('click', () => {
      const key = document.getElementById('ttl-key').value;
      const seconds = parseInt(document.getElementById('ttl-seconds').value);
      storage.set(key, `Value expires in ${seconds}s`, seconds * 1000);
      ttlOutput.textContent = `Set "${key}" with ${seconds}s TTL`;

      // Start countdown
      const interval = setInterval(() => {
        const remaining = storage.getTTL(key);
        if (remaining === null) {
          ttlOutput.textContent = `"${key}" has expired!`;
          clearInterval(interval);
        } else {
          ttlOutput.textContent = `"${key}" expires in ${Math.ceil(remaining / 1000)}s`;
        }
      }, 1000);
    });

    document.getElementById('check-ttl').addEventListener('click', () => {
      const key = document.getElementById('ttl-key').value;
      const remaining = storage.getTTL(key);
      ttlOutput.textContent = remaining !== null
        ? `"${key}" TTL: ${Math.ceil(remaining / 1000)}s remaining`
        : `"${key}" has no TTL or doesn't exist`;
    });

    // Info operations
    document.getElementById('show-keys').addEventListener('click', () => {
      const keys = storage.keys();
      infoOutput.textContent = `Keys (${keys.length}):\n${keys.join('\n') || '(none)'}`;
    });

    document.getElementById('show-size').addEventListener('click', () => {
      const { used, available, items } = storage.getSize();
      infoOutput.textContent = `Storage Info:
Items: ${items}
Used: ${(used / 1024).toFixed(2)} KB
Available: ${(available / 1024).toFixed(2)} KB`;
    });

    document.getElementById('clear-all').addEventListener('click', () => {
      storage.clear();
      infoOutput.textContent = 'All demo_ prefixed items cleared';
    });
  

class DemoThemeToggle extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.handleClick = this.handleClick.bind(this);
  }

  connectedCallback() {
    const saved = localStorage.getItem('utils-demo-theme');
    document.documentElement.classList.remove('dark');
    document.documentElement.classList.toggle('light', saved === 'light');

    this.render();
  }

  render() {
    const isLight = document.documentElement.classList.contains('light');
    this.shadowRoot.innerHTML = `
      <style>
        :host {
          display: inline-flex;
        }
        .toggle {
          display: inline-flex;
          align-items: center;
          border: 1px solid var(--line);
          border-radius: 999px;
          overflow: hidden;
          background: var(--surface);
        }
        button {
          border: none;
          background: transparent;
          color: var(--muted);
          padding: 6px 12px;
          font-size: 0.8rem;
          cursor: pointer;
          font-family: "Space Grotesk", "Trebuchet MS", Arial, sans-serif;
        }
        button.active {
          background: linear-gradient(120deg, var(--accent), #ff6a00);
          color: #111;
          font-weight: 600;
        }
      </style>
      <div class="toggle" role="group" aria-label="Theme toggle">
        <button class="${isLight ? 'active' : ''}" data-theme="light">Light</button>
        <button class="${isLight ? '' : 'active'}" data-theme="dark">Dark</button>
      </div>
    `;

    this.shadowRoot.querySelectorAll('button').forEach((btn) => {
      btn.addEventListener('click', this.handleClick);
    });
  }

  handleClick(event) {
    const theme = event.currentTarget.dataset.theme;
    const isLight = theme === 'light';
    document.documentElement.classList.toggle('light', isLight);
    document.documentElement.classList.remove('dark');
    localStorage.setItem('utils-demo-theme', isLight ? 'light' : 'dark');
    this.render();
  }
}

customElements.define('demo-theme-toggle', DemoThemeToggle);