Gathering detailed insights and metrics for localstorage-slim
Gathering detailed insights and metrics for localstorage-slim
Gathering detailed insights and metrics for localstorage-slim
Gathering detailed insights and metrics for localstorage-slim
An ultra slim localstorage wrapper with optional support for ttl and encryption
npm install localstorage-slim
Typescript
Module System
Node Version
NPM Version
JavaScript (81.7%)
TypeScript (16.7%)
Makefile (1.61%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
152 Stars
124 Commits
13 Forks
2 Watchers
4 Branches
5 Contributors
Updated on Jun 21, 2025
Latest Version
2.7.1
Package Id
localstorage-slim@2.7.1
Unpacked Size
28.71 kB
Size
10.87 kB
File Count
8
NPM Version
10.2.0
Node Version
18.18.1
Published on
Apr 15, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
22
An ultra slim localstorage wrapper with optional support for ttl and encryption
🌟 Highlights 🌟
1# you can install typeahead with npm 2$ npm install --save localstorage-slim 3 4# Alternatively you can use Yarn 5$ yarn add localstorage-slim
Then include the library in your App/Page.
As a module,
1// using ES6 modules 2import ls from 'localstorage-slim'; 3 4// using CommonJS modules 5var ls = require('localstorage-slim');
In the browser context,
1<!-- Include the library --> 2<script src="./node_modules/localstorage-slim/dist/localstorage-slim.js"></script> 3 4<!-- Alternatively, you can use a CDN with jsdelivr --> 5<script src="https://cdn.jsdelivr.net/npm/localstorage-slim"></script> 6<!-- or with unpkg.com --> 7<script src="https://unpkg.com/localstorage-slim@2.7.0/dist/localstorage-slim.js"></script>
The library will be available as a global object at window.ls
Typical usage of localstorage-slim is as follows:
1/*** Store in localstorage ***/ 2const value = { 3 a: new Date(), 4 b: null, 5 c: false, 6 d: 'superman', 7 e: 1234 8} 9 10ls.set('key1', value); // value can be anything (object, array, string, number, ...) 11ls.get('key1'); // { a: "currentdate", b: "null", c: false, d: 'superman', e: 1234 } 12 13/* with optional ttl in seconds */ 14ls.set('key2', value, { ttl: 5 }); 15ls.get('key2'); // within 5 secs => { a: "currentdate", b: "null", c: false, d: 'superman', e: 1234 } 16ls.get('key2'); // after 5 secs => null 17 18/* with optional encryption */ 19ls.set('key3', value, { encrypt: true }); // "mÆk¬ k§m®À½½°¹¿¯..." 20ls.get('key3', { decrypt: true }); // { a: "currentdate", b: "null", c: false, d: 'superman', e: 1234 }
LocalStorage-slim
provides you a config object (ls.config
) which can be modified to suit your needs. The available config parameters are as follows and all of them are completely OPTIONAL
Parameter | Description | Default |
---|---|---|
ttl?: number|null | Allows you to set a global TTL(time to live) in seconds which will be used for every item stored in the localStorage. Global ttl can be overriden with the ls.set()/ls.get() API. | null |
encrypt?: boolean | Allows you to setup global encryption of the data stored in localStorage Details. It can be overriden with the ls.set()/ls.get() API | false |
decrypt?: boolean | Allows you to decrypt encrypted data stored in localStorage. Used only by the ls.get() API | undefined |
encrypter?: (data: unknown, secret: string): string | The encryption function to be used. A default implementation only obfuscates the value. This function can be overriden with the ls.set()/ls.get() API. | Obfuscation |
decrypter?: (encryptedString: string, secret: string): unknown | A decryption function to be used. A default implementation only performs deobfuscation. This function can be overriden with the ls.set()/ls.get() API. | deobfuscation |
secret?: unknown | Allows you to set a secret key that will be passed to the encrypter/decrypter functions as a parameter. The default implementation accepts a number. Global secret can be overriden with the ls.set()/ls.get() API. | |
storage?: Storage | Allows you to define the Storage to use: localStorage , sessionStorage or even a custom store that implements the Storage interface. By default, localStorage is used and if localStorage is unavailable, then a fallback in-memory store is used | localStorage |
LocalStorage-slim allows you to encrypt the data that will be stored in your localStorage.
1// enable encryption globally 2ls.config.encrypt = true; 3 4// optionally use a different secret key 5ls.config.secret = 89;
Enabling encryption ensures that the data stored in your localStorage will be unreadable by majority of the users. Be aware of the fact that default implementation is not a true encryption but a mere obfuscation to keep the library light in weight. You can customize the encrypter
/decrypter
functions to write your own algorithm or to use a secure encryption algorithm like AES, TDES, RC4 or rabbit via CryptoJS to suit your needs.
To use a library like CryptoJS, update the following config options -
1// enable encryption 2ls.config.encrypt = true; 3// set a global secret 4ls.config.secret = 'secret-password'; 5 6// override encrypter function 7ls.config.encrypter = (data: unknown, secret: string): string => 'encrypted string'; 8// override decrypter function 9ls.config.decrypter = (encryptedString: string, secret: string): unknown => 'original data';
As seen, you can easily override the encrypter
and decrypter
functions with your own implementation of encryption/decryption logic to secure your data. Some examples can be found here.
1/* After updating the config, use ls as you normally would */ 2ls.set(...); // internally calls ls.config.encrypter(...); 3ls.get(...); // internally calls ls.config.decrypter(...); 4 5/* you can encrypt a particular LS item by providing a different secret as well. */ 6ls.set("key", "value", { secret: 'xyz'}); 7ls.get("key", { secret: 'xyz'});
⚠️ Note: It is recommended that you do not save user passwords or credit card details in LocalStorage (whether they be encrypted or not).
By configuring the storage
config option, you can easily use another storage instead of the default localStorage
.
1/* use sessionStorage */ 2ls.config.storage = sessionStorage; 3 4/* OR a custom store/storage via an IIFE */ 5ls.config.storage = (() => { 6 const store = { 7 // your storage's implementation... 8 }; 9 10 return store; 11})()
Note: If you use custom storage, it must implement the Storage interface.
The Api is very similar to that of the native LocalStorage API
.
ls.set(key, value, config = {})
Sets an item in the LocalStorage. It can accept 3 arguments
key: string
[Required] - The key with which the value should be associatedvalue: string|Date|Number|Object|Boolean|Null
[Required] - The value to be storedconfig: Config
[Optional] - This argument accepts the same properties (except storage
property) as the global config object. Defaults to an empty objectReturns false
if there was an error, else returns undefined
.
1const res = ls.set('key', 'value'); 2console.log('Value =>', res); // returns undefined if successful or false if there was a problem 3 4// with ttl 5ls.config.ttl = 3; // global ttl set to 3 seconds 6ls.set('key', 'value'); // value expires after 3s 7ls.set('key', 'value', { ttl: 5 }); // value expires after 5s (overrides global ttl) 8 9// with encryption (to encrypt particular fields) 10ls.set('key', 'value', { encrypt: true });
ls.get(key, config = {})
Retrieves the Data associated with the key stored in the LocalStorage. It accepts 2 arguments -
key: string
[Required] - The key with which the value is associatedconfig: Config
[Optional] - This argument accepts the same properties (except storage
property) as the global config object. Defaults to an empty objectIf the passed key does not exist, it returns null
.
1const value = ls.get('key'); 2console.log('Value =>', value); // value retrieved from LS 3 4// if ttl was set 5ls.get('key'); // returns the value if ttl has not expired, else returns null 6 7// when a particular field is encrypted, and it needs decryption 8ls.get('key', { decrypt: true }); 9 10// get decrypted value when global encryption is enabled 11ls.config.encrypt = true; 12ls.get('key'); // returns decrypted value
ls.flush(force = false)
Flushes expired items in the localStorage. This function is called once automatically on initialization. It can accept an optional argument force: boolean
that defaults to false
. If set to true
, it force-flushes all items including the ones that haven't expired yet. Note that doing flush(true)
only affects items that were due to expire sometime in future (i.e. they had a TTL set on them). To remove data, whether or not it has a TTL, use remove()
or clear()
.
1// removes all expired data (i.e. ttl has expired) 2ls.flush(); 3// removes all data that has a ttl (i.e. even if the ttl has not expired yet) 4ls.flush(true);
ls.remove(key)
Accepts the key: string
as an argument to remove the data associated with it.
1// delete data from the LS 2ls.remove('key'); // returns undefined if successful, false otherwise
ls.clear()
Clears the entire localstorage linked to the current domain.
1// removes all data from the LS 2ls.clear(); // returns undefined if successful, false otherwise
When localStorage is not supported by a browser, we fallback to MemoryStorage
. For websites that don't do full page loads, like SPA's, this is a perfect fallback. But for MPA's, though page crashes get prevented, data is not persisted between page loads.
Interested in contributing features and fixes?
See the Changelog
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 2/18 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
19 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More