简体   繁体   English

从本地存储中检索数据时如何将数据转换为不同类型

[英]How to convert the data into different types when retrieving from local storage

I am writing a little library to manipulate the data in localStorage. 我正在写一个小库来处理localStorage中的数据。

Below is the code snippet : 下面是代码片段:

function AppStorage (appName) {
    "use strict";

    var prefix  = appName;

    var saveItem = function (key, value) {

        if (!key || !value) {

            console.error("Missing parameters \"key\" and \"value\"");    

            return false;
        }

        if (window.localStorage && window['localStorage']) {

            try {
                if (typeof value === 'Object') localStorage.setItem(prefix + '_' + key, JSON.stringify(value));
                if (typeof value === 'string') localStorage.setItem(prefix + '_' + key, value);

                return true;

            } catch (e) {

                return false;

            }

        } else  {

            return false;

        }

    }

    var getItem = function (key) {

        if (!key) {

            console.error("Missing parameter \"key\"");    

            return false;
        }

        if (window.localStorage && window['localStorage']) {
            try {

                return localStorage.getItem(prefix + '_' + key);                

            } catch (e) {

                return false;

            }

        } else  {

            return false;

        }

    }

    var removeItem = function (key) {

        if (!key) {

            console.error("Missing parameter \"key\"");

            return false;
        }

        if (window.localStorage && window['localStorage']) {

            try {
                localStorage.removeItem(prefix + '_' + key);
                return true;

            } catch (e) {
                return false;
            }

        } else  {

            console.log("Browser does not support HTML5 Web Storage");

        }

    }

    return {
        set: function (key, value) {
            return saveItem(key, value);
        },
        get: function (key) {
            return getItem(key);
        },
        remove: function (key) {
            return removeItem(key);
        }
    }

}

var as = new AppStorage ('MyApp');

How ever im stuck with the following two issues. 我如何坚持以下两个问题。

1) When data is retrieved through get() the stored information is returned as a string . 1)通过get()检索数据时,存储的信息以string形式返回。 I need this information to be received in the same format before it was passed. 在传递之前,我需要以相同的格式接收此信息。

2) Can the following code snippet be further improved. 2)是否可以进一步完善以下代码段。

Hope helps! 希望有帮助!

 var ClientStorageService = (function(persistent, session) { if(!persistent && !session) { throw new Error('ClientStorage Not Suppoerted'); } function ClientStorage() {}; ClientStorage.prototype.driver = persistent || session; ClientStorage.prototype.prefix = 'HELLOWORLD_'; ClientStorage.prototype.usePersistent = function() { if(persistent) { this.driver = persistent; return true; } return false; }; ClientStorage.prototype.useSession = function() { if(session) { this.driver = session; return true; } return false; }; ClientStorage.prototype.read = function(key) { key = this.prefix + key; var data = this.driver.getItem(key); if(data) { try { data = JSON.parse(data); } catch(e) {} return data; } return null; }; ClientStorage.prototype.store = function(key, val) { key = this.prefix + key.toString(); try { val = JSON.stringify(val); } catch(e) {} this.driver.setItem(key, val); return this; }; ClientStorage.prototype.clear = function(key) { var task = 'clear'; if(key) { key = this.prefix + key; task = 'removeItem'; } this.driver[task](key); }; return new ClientStorage(); })(window.localStorage, window.sessionStorage); ClientStorageService.store('FOO', { greetings: 'Hello World' }); var result = ClientStorageService.read('FOO'); console.log('result is', result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM