github.com/outbrain/consul@v1.4.5/ui-v2/app/utils/storage/local-storage.js (about)

     1  export default function(
     2    scheme = '',
     3    storage = window.localStorage,
     4    encode = JSON.stringify,
     5    decode = JSON.parse
     6  ) {
     7    const prefix = `${scheme}:`;
     8    return {
     9      getValue: function(path) {
    10        let value = storage.getItem(`${prefix}${path}`);
    11        if (typeof value !== 'string') {
    12          value = '""';
    13        }
    14        try {
    15          value = decode(value);
    16        } catch (e) {
    17          value = '';
    18        }
    19        return value;
    20      },
    21      setValue: function(path, value) {
    22        if (value === null) {
    23          return this.removeValue(path);
    24        }
    25        try {
    26          value = encode(value);
    27        } catch (e) {
    28          value = '""';
    29        }
    30        return storage.setItem(`${prefix}${path}`, value);
    31      },
    32      removeValue: function(path) {
    33        return storage.removeItem(`${prefix}${path}`);
    34      },
    35      all: function() {
    36        return Object.keys(storage).reduce((prev, item, i, arr) => {
    37          if (item.indexOf(`${prefix}`) === 0) {
    38            const key = item.substr(prefix.length);
    39            prev[key] = this.getValue(key);
    40          }
    41          return prev;
    42        }, {});
    43      },
    44    };
    45  }