forked from agrublev/angularLocalStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangularLocalStorage.js
More file actions
181 lines (180 loc) · 5.88 KB
/
Copy pathangularLocalStorage.js
File metadata and controls
181 lines (180 loc) · 5.88 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function() {
angular.module('angularLocalStorage', []).factory('storage', [
'$parse', '$window', function($parse, $window) {
var privateMethods, publicMethods, storage, supported;
storage = $window.localStorage != null ? $window.localStorage : null;
supported = storage != null;
privateMethods = {
getPairs: function(predicate) {
var i, result, _fn, _i, _ref;
result = [];
_fn = function(i) {
var key, pair;
key = storage.key(i);
pair = {
key: key,
value: publicMethods.get(key)
};
if (predicate(pair)) {
return result.push(pair);
}
};
for (i = _i = 0, _ref = storage.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
_fn(i);
}
return result;
},
parseValue: function(res) {
var error, float, val, _ref;
try {
val = (_ref = angular.fromJson(res)) != null ? _ref : res;
val = (function() {
switch (val) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
default:
return val;
}
})();
float = $window.parseFloat(val);
if (float === val && !angular.isObject(val)) {
val = float;
}
} catch (_error) {
error = _error;
val = res;
}
return val;
},
removePairs: function(predicate) {
var pair, pairs, _fn, _i, _len;
pairs = privateMethods.getPairs(predicate);
_fn = function(pair) {
return publicMethods.remove(pair.key);
};
for (_i = 0, _len = pairs.length; _i < _len; _i++) {
pair = pairs[_i];
_fn(pair);
}
return pairs.length;
}
};
publicMethods = {
isSupported: function() {
return supported;
},
size: function() {
return storage.length;
},
set: function(key, value) {
var saver;
if (key == null) {
throw 'Null keys are not permitted.';
}
saver = angular.toJson(value);
storage.setItem(key, saver);
return privateMethods.parseValue(saver);
},
get: function(keyOrFunction, defaultValue) {
var item, pairs, _ref;
if (defaultValue == null) {
defaultValue = null;
}
if (typeof keyOrFunction === 'function') {
pairs = privateMethods.getPairs(keyOrFunction);
if (pairs.length === 0 && (defaultValue != null)) {
return defaultValue;
}
return pairs;
}
item = storage.getItem(keyOrFunction);
return (_ref = privateMethods.parseValue(item)) != null ? _ref : defaultValue;
},
initialize: function(key, value) {
var currentValue;
currentValue = publicMethods.get(key);
if (currentValue != null) {
return currentValue;
}
return publicMethods.set(key, value);
},
increment: function(key, defaultValue, incrementBy) {
var value;
if (defaultValue == null) {
defaultValue = 1;
}
if (incrementBy == null) {
incrementBy = 1;
}
value = publicMethods.get(key);
if (value == null) {
return storage.setItem(key, defaultValue);
} else {
if (typeof value !== 'number' && toString.call(value) !== '[object Number]') {
throw 'Existing value is not a number.';
}
return storage.setItem(key, value + incrementBy);
}
},
decrement: function(key, defaultValue, decrementBy) {
if (defaultValue == null) {
defaultValue = 0;
}
if (decrementBy == null) {
decrementBy = 1;
}
return publicMethods.increment(key, defaultValue, -decrementBy);
},
remove: function(keyOrFunction) {
if (typeof keyOrFunction === 'function') {
return privateMethods.removePairs(keyOrFunction);
}
storage.removeItem(keyOrFunction);
return true;
},
bind: function($scope, key, opts) {
var defaultOpts, storeName, _ref;
defaultOpts = {
defaultValue: '',
storeName: null
};
opts = (function() {
switch (false) {
case !angular.isString(opts):
return angular.extend({}, defaultOpts, {
defaultValue: opts
});
case !angular.isUndefined(opts):
return defaultOpts;
default:
return angular.extend(defaultOpts, opts);
}
})();
storeName = (_ref = opts.storeName) != null ? _ref : key;
publicMethods.initialize(storeName, opts.defaultValue);
$parse(key).assign($scope, publicMethods.get(storeName));
$scope.$watch(key, function(val) {
if (angular.isDefined(val)) {
return publicMethods.set(storeName, val);
}
}, true);
return publicMethods.get(storeName);
},
unbind: function($scope, key, storeName) {
storeName = storeName != null ? storeName : key;
$parse(key).assign($scope, null);
$scope.$watch(key, function() {});
return publicMethods.remove(storeName);
},
clearAll: function() {
return storage.clear();
}
};
return publicMethods;
}
]);
}).call(this);