HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

localStorage setExpire code to add time expires

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
timelocalstoragesetexpireexpirescodeadd

Problem

Storage.prototype.setExpire = function (arrObj) {
    var date,
        now,
        days,
        deletes,
        items,
        fa, ta,
        newItems = [],
        storage = localStorage.getItem('limitStorage');
    items = JSON.parse(storage || "[]");
    date = new Date();
    date = date.toString().split(' ');
    date = Number(date[2]);
    for (var key in arrObj) {
        for (i = 0; i = ta.limit) {
            localStorage.removeItem(ta.value);
        }
        if(localStorage.getItem(ta.value) !== null) newItems.push(ta);
    }
    localStorage.setItem('limitStorage', JSON.stringify(newItems));
};


This code runs by simply calling the Storage method and then adding method .setExpire.

Example:

window.localStorage.setExpire({"item1":3,"item2":10,"item3":3});


I have to add more to the code to take into account day 31 to day 1. But as of now, if the item is added on day 20 and you want it to expire in 7 days on day 27, the localStorage item will be removed.

My question is: does anyone see anything they can review for me? Maybe a cross-browser compatibility issue? I already have a cross-browser localStorage that falls back to cookies if need be.

Also let me know if you may think of a way for the code to take into account the ending to the start of a months, since the code technically only takes the new date and then subtracts the stored date, which gets our days that have passed.

Any and all suggestions are welcomed.

Solution

I think this code could use a lot of polishing.

Getting the localStorage

storage =localStorage.getItem('limitStorage');
 getItems = storage !== null && storage !== "[]" ? localStorage.getItem('limitStorage') : null;
 getItems = getItems !== null ? JSON.parse(getItems) : null;


can easily be replaced with:

items = JSON.parse( localStorage.getItem('limitStorage') || "[]" );


I renamed getItems to items ( getItems is a name better suited for a function ), the code also defaults items to an empty array if nothing was found.

Setting an expiry date

The easiest way to deal with that is to get now in milliseconds, add 1000 60 60 24 days to it, and set that as the expiry date. Then you only have to compare (new Date()).getTime() with the expiry date.

So you have something like

var now = (new Date()).getTime();
var day = 1000 * 60 * 60 * 24;

for (var key in arrObj) {
  newItems.push( { value : key , expires : now + day * arrObj[key] } );


Now, we are facing 2 problems:

  • If you want to change the expiry date of an item, you cant..



  • Much worse, once you set some items to expire, you can never add extra items to expire since you will always fall into the else branch.



At this point, I think the whole code needs a re-write and re-think.

Code Snippets

storage =localStorage.getItem('limitStorage');
 getItems = storage !== null && storage !== "[]" ? localStorage.getItem('limitStorage') : null;
 getItems = getItems !== null ? JSON.parse(getItems) : null;
items = JSON.parse( localStorage.getItem('limitStorage') || "[]" );
var now = (new Date()).getTime();
var day = 1000 * 60 * 60 * 24;

for (var key in arrObj) {
  newItems.push( { value : key , expires : now + day * arrObj[key] } );

Context

StackExchange Code Review Q#38159, answer score: 4

Revisions (0)

No revisions yet.