-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (81 loc) · 3.42 KB
/
Copy pathindex.js
File metadata and controls
99 lines (81 loc) · 3.42 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
/*!
* cache-funk <https://github.com/beckettkev/cache-funk>
*
* Copyright (c) 2016, Kevin Beckett (@beckettkev).
* Licensed under the MIT License.
*/
'use strict';
// Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem
// throw QuotaExceededError. We're going to detect this and just silently drop any calls to setItem
// to avoid the entire page breaking, without having to do a check at each usage of Storage.
window.isStorageAvailable = false;
if (typeof localStorage === 'object') {
try {
localStorage.setItem('localStorage', 1);
localStorage.removeItem('localStorage');
window.isStorageAvailable = true;
} catch (error) {
Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function setItem () {};
console.log('Your web browser does not support storing settings locally. In Safari, the most common cause of this is using "Private Browsing Mode". Some settings may not save or some features may not work properly for you.');
}
}
function insertIntoArrayAtPosition(src, target, position) {
var beginning = src.splice(0, position);
var end = src.splice(position);
return beginning.concat(target).concat(end);
}
function dynamicallySetObjectAttributes(source, target, exclusions) {
target = {
payload: target
};
Object.keys(source).map(function(item) {
if (exclusions.indexOf(item) === -1) {
target[item] = source[item];
}
});
// update the timestamp to now, we have just updated it...
target.timestamp = Date.now();
return target;
}
function fetchItem(key) {
var cache = localStorage.getItem(key);
var data = JSON.parse(cache);
if (data && data.payload) {
return data;
}
return undefined;
}
module.exports = {
// check to see if we have local storage available
exists: function exists() {
return window.isStorageAvailable;
},
// get local storage item if it is still valid
fetch: function fetch(key) {
return fetchItem(key);
},
// good citizen removal service function for deleting local storage entries
remove: function remove(key) {
localStorage.removeItem(key);
},
// vanilla local storage cache entry with a new key
store: function store(key, payload) {
// var data = { timestamp: Date.now(), payload: payload };
localStorage.setItem(key, JSON.stringify(payload));
},
//function for inserting into a pre-existing local storage item at a set position (to maintain the search results shape)
insert: function insert(key, items, position, exclusions) {
var data = fetchItem(key);
if (typeof exclusions === 'undefined') {
exclusions = [];
}
// if the length of the array is bigger than it's position - insert this page results to the correct position of the cache array
// else add this data to the end of the cache array (remember that position is zero based)
data = data.payload.length > (position + 1) ? insertIntoArrayAtPosition(data.payload, items.payload, position) : data.payload.concat(items.payload);
// populate any other properties within the payload before adding the combined result back to the cache
// var combined = { timestamp: Date.now(), payload: dynamicallySetObjectAttributes(items, data, ['birthday','payload'].concat(exclusions)) };
var combined = dynamicallySetObjectAttributes(items, data, ['timestamp','payload'].concat(exclusions));
localStorage.setItem(key, JSON.stringify(combined));
}
};