forked from prsousa/UnreadTopics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
98 lines (84 loc) · 2.28 KB
/
Copy pathutils.js
File metadata and controls
98 lines (84 loc) · 2.28 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
"use strict"
let Utils = {};
/**
* Extends an object with the key/values of another one
* @param {Object} container
* @param {Object} newObject
* @return {Bool} was container modified
*/
Utils.extend = function (container, newObject) {
let containerChanged = false;
for (let k in newObject) {
if (container[k] !== newObject[k]) {
container[k] = newObject[k];
containerChanged = true;
}
}
return containerChanged;
}
/**
* Determines whether an array contains values
* @param {Array} container
* @param {Array} values
* @return {Bool} containsValues
*/
Utils.containsValues = function (array, values) {
for (let v of values) {
if (array.indexOf(v) === -1) {
return false;
}
}
return true;
}
/**
* Converts jQuery's ajax returning promise to the ES6 promise standard
* @param {Object} ajax options
* @return {Promise} ES6 Promise of $.ajax
*/
Utils.ajax = function (options) {
return new Promise(function (resolve, reject) {
$.ajax(options).done(resolve).fail(reject);
});
}
/**
* Delays Promise resolvement
* @param {Integer} delay time in minutes
* @return {Promise} ES6 Promise of a future event
*/
Utils.delay = function (minutes) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, minutes * 60 * 1000);
});
}
/**
* Loads presisted data to an object
* @param {Object} destination object
* @param {Object} database resource
* @return {Promise} ES6 Promise of resource's get
*/
Utils.load = function (dest, resource) {
return resource.get(Object.keys(dest)).then(items => {
Object.keys(items).forEach(k => dest[k] = items[k]);
});
}
Utils.loadLocally = function (dest) {
return Utils.load(dest, chromep.storage.local);
}
Utils.loadRemotely = function (dest) {
return Utils.load(dest, chromep.storage.sync);
}
/**
* Presists properties of an object
* @param {Object} source object
* @param {Object} database resource
* @return {Promise} ES6 Promise of resource's set
*/
Utils.save = function (src, resource) {
return resource.set(src);
}
Utils.saveLocally = function (src) {
return Utils.save(src, chromep.storage.local);
}
Utils.saveRemotely = function (src) {
return Utils.save(src, chromep.storage.sync);
}