New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

sels

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sels

Safe & expirable localStorage

latest
Source
npmnpm
Version
2.5.1
Version published
Maintainers
1
Created
Source

sels 🍪➡️🗄

sels - safe expirable localStorage

npm Tests LICENSE Package size

Using cookies for client-only purposes is irrational and unsafe. There is no point in using cookies if the data is not intended to be sent to the server. In this case, you need to use localStorage. However, localStorage may not be available (then an error will be generated), and there is no expiry mechanism in localStorage.

This library solves both problems

Installing and usage

npm i sels
import sels from 'sels';

sels.set('key', 'value');

или

<script src="https://cdn.jsdelivr.net/npm/sels@latest/index.min.js"></script>
Sels.set('key', 'value');

Breaking changes ⚠️

  • The method get version 1.x.x has been renamed to asyncGet (see description below)

Types & interfaces

interface RecordOptions {
  maxAge?: number
  expires?: string | Date
}

type RecordValue = string | boolean | number;

Methods

set(key: string, value: RecordValue, options?: RecordOptions): boolean - adds or modifies a record in localStorage. value will be cast to string. Before write, the ability to write will be checked, if the recording failed, it will return false, else true.

asyncGet(key: string): Promise - reads a record from localStorage. Checks readability before reading. If it fails, the Promise will be rejected with an error value, otherwise the Promise will be resolved with the read value. Promise will resolve with value null, if the specified key is not found.

get(key: string): string | null - reads a record from localStorage. Checks readability before reading. If the read failed, it will return null, otherwise the key value will be returned. If the key is not found, it will also return null.

remove(key: string) - removes record from localStorage. Before deleting, it checks if deletion is possible. It will return true, if deletion is successful, else false. If the key is not found, but there is no error, it will return true anyway.

clear() - completely cleans localStorage, it will return true, if successful, else false.

Fields

Sels also exports value isAvailable: boolean - availability of localStorage.

Options

The set method takes a third (optional) parameter - options. If nothing is passed, the record will be eternal.

  • maxAge is needed to indicate the number of seconds - the lifetime of the record. After this time expires, the record will no longer be available.
  • expires is needed to specify a date string or object Date, after which the record should become inaccessible. The date can be passed as a string (e.g. ISO string or 12-31-2021). Date.parse() will be used for parsing. You can also specify a Jira-like period, for example, 1w 2d 3h, so the record will stop being read after 1 week (7 days) + 2 days + 3 hours. Supported units: y, m, w, d, h (year, month, week, day, hour).

Translations

Keywords

localstorage

FAQs

Package last updated on 09 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts