# Code coverage report for browser/Store/LocalStorage.js

## Statements: 82.54% (52 / 63) Branches: 53.49% (23 / 43) Functions: 100% (12 / 12) Lines: 82.54% (52 / 63)

All files » browser/Store/ » LocalStorage.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    1251

    1

    1
    1

    1

    1

    1

    1

    1
    1

    1

    1

    1
    2
    2

    1
    2

    2

    2

    1
    7

    1
    1
    1

    1
    6
    6

    1
    5

    1
    1
    1

    1
    1
    1
    2
    2

    1

    1
    2

    2
    4
    4

    2
    2

    1
    1
    1
    2

    1

    1

    1

     (function (root, factory) {
      // AMD.
      Iif(typeof define === 'function' && define.amd) {
        define([], factory);
      }

      // Browser Global.
      Eif(typeof root.navigator === "object") {
        Iif(typeof root.Terraformer === "undefined"){
          root.Terraformer = {};
        }
        Iif(typeof root.Terraformer.Store === "undefined"){
          root.Terraformer.Store = {};
        }
        root.Terraformer.Store.LocalStorage = factory().LocalStorage;
      }
    }(this, function() {
      var exports = { };

      var Terraformer, callback;

      // Local Reference To Browser Global
      Eif(typeof this.navigator === "object") {
        Terraformer = this.Terraformer;
      }

      // Setup Node Dependencies
      Iif(typeof module === 'object' && typeof module.exports === 'object') {
        Terraformer = require('terraformer');
      }

      // Setup AMD Dependencies
      Iif(arguments[0] && typeof define === 'function' && define.amd) {
        Terraformer = arguments[0];
      }

      // These methods get called in context of the geostore
      function LocalStorage(){
        var opts = arguments[0] || {};
        this._key = opts.key || "_terraformer";
      }

      // store the data at id returns true if stored successfully
      LocalStorage.prototype.add = function(geojson, callback){
        Iif(geojson.type === "FeatureCollection"){
          for (var i = 0; i < geojson.features.length; i++) {
            this.set(geojson.features[i]);
          }
        } else {
          this.set(geojson);
        }
        Iif ( callback ) {
          callback( null, geojson );
        }
      };

      LocalStorage.prototype.key = function(id){
        return this._key +"_"+id;
      };

      // remove the data from the index and data with id returns true if removed successfully.
      LocalStorage.prototype.remove = function( id, callback ){
        localStorage.removeItem( this.key( id ) );
        Iif ( callback ) {
          callback( null, id );
        }
      };

      // return the data stored at id
      LocalStorage.prototype.get = function(id, callback){
        Eif ( callback ) {
          callback( null, JSON.parse(localStorage.getItem(this.key(id))));
        }
      };

      LocalStorage.prototype.set = function(feature){
        Iif ( callback ) {
          localStorage.setItem(this.key(feature.id), JSON.stringify(feature));
        }
      };

      LocalStorage.prototype.update = function(geojson, callback){
        this.set(geojson);
        Iif ( callback ) {
          callback( null, geojson );
        }
      };

      LocalStorage.prototype.toJSON = function(){
        var exports = {};
        for(var feature in localStorage){
          Eif(feature.match(this._key)){
            exports[feature] = localStorage[feature];
          }
        }
        return exports;
      };

      LocalStorage.prototype.serialize = function(callback){
        var objs = [];

        for (var key in localStorage){
          Eif(key.match(this._key)){
            objs.push(localStorage.getItem(key));
          }
        }

        Eif ( callback ) {
          callback(null, JSON.stringify(objs));
        }
      };

      LocalStorage.prototype.deserialize = function(serial){
        var data = JSON.parse(serial);
        for (var i = data.length - 1; i >= 0; i--) {
          this.set(data[i]);
        }
        return this;
      };

      exports.LocalStorage = LocalStorage;

      return exports;
    }));