-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
lib.js
212 lines (196 loc) · 5.79 KB
/
lib.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { check } from 'meteor/check';
const helpers = {
sanitize(str = '', max = 28, replacement = '-') {
return str.replace(/([^a-z0-9\-\_]+)/gi, replacement).substring(0, max);
},
isUndefined(obj) {
return obj === void 0;
},
isObject(obj) {
if (this.isArray(obj) || this.isFunction(obj)) {
return false;
}
return obj === Object(obj);
},
isArray(obj) {
return Array.isArray(obj);
},
isBoolean(obj) {
return obj === true || obj === false || Object.prototype.toString.call(obj) === '[object Boolean]';
},
isFunction(obj) {
return typeof obj === 'function' || false;
},
isDate(date) {
return !Number.isNaN(new Date(date).getDate());
},
isEmpty(obj) {
if (this.isDate(obj)) {
return false;
}
if (this.isObject(obj)) {
return !Object.keys(obj).length;
}
if (this.isArray(obj) || this.isString(obj)) {
return !obj.length;
}
return false;
},
clone(obj) {
if (!this.isObject(obj)) return obj;
return this.isArray(obj) ? obj.slice() : Object.assign({}, obj);
},
has(_obj, path) {
let obj = _obj;
if (!this.isObject(obj)) {
return false;
}
if (!this.isArray(path)) {
return this.isObject(obj) && Object.prototype.hasOwnProperty.call(obj, path);
}
const length = path.length;
for (let i = 0; i < length; i++) {
if (!Object.prototype.hasOwnProperty.call(obj, path[i])) {
return false;
}
obj = obj[path[i]];
}
return !!length;
},
omit(obj, ...keys) {
const clear = Object.assign({}, obj);
for (let i = keys.length - 1; i >= 0; i--) {
delete clear[keys[i]];
}
return clear;
},
now: Date.now,
throttle(func, wait, options = {}) {
let previous = 0;
let timeout = null;
let result;
const that = this;
let self;
let args;
const later = () => {
previous = options.leading === false ? 0 : that.now();
timeout = null;
result = func.apply(self, args);
if (!timeout) {
self = args = null;
}
};
const throttled = function () {
const now = that.now();
if (!previous && options.leading === false) previous = now;
const remaining = wait - (now - previous);
self = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(self, args);
if (!timeout) {
self = args = null;
}
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
throttled.cancel = () => {
clearTimeout(timeout);
previous = 0;
timeout = self = args = null;
};
return throttled;
}
};
const _helpers = ['String', 'Number', 'Date'];
for (let i = 0; i < _helpers.length; i++) {
helpers['is' + _helpers[i]] = function (obj) {
return Object.prototype.toString.call(obj) === `[object ${_helpers[i]}]`;
};
}
/*
* @const {Function} fixJSONParse - Fix issue with Date parse
*/
const fixJSONParse = function(obj) {
for (let key in obj) {
if (helpers.isString(obj[key]) && obj[key].includes('=--JSON-DATE--=')) {
obj[key] = obj[key].replace('=--JSON-DATE--=', '');
obj[key] = new Date(parseInt(obj[key]));
} else if (helpers.isObject(obj[key])) {
obj[key] = fixJSONParse(obj[key]);
} else if (helpers.isArray(obj[key])) {
let v;
for (let i = 0; i < obj[key].length; i++) {
v = obj[key][i];
if (helpers.isObject(v)) {
obj[key][i] = fixJSONParse(v);
} else if (helpers.isString(v) && v.includes('=--JSON-DATE--=')) {
v = v.replace('=--JSON-DATE--=', '');
obj[key][i] = new Date(parseInt(v));
}
}
}
}
return obj;
};
/*
* @const {Function} fixJSONStringify - Fix issue with Date stringify
*/
const fixJSONStringify = function(obj) {
for (let key in obj) {
if (helpers.isDate(obj[key])) {
obj[key] = `=--JSON-DATE--=${+obj[key]}`;
} else if (helpers.isObject(obj[key])) {
obj[key] = fixJSONStringify(obj[key]);
} else if (helpers.isArray(obj[key])) {
let v;
for (let i = 0; i < obj[key].length; i++) {
v = obj[key][i];
if (helpers.isObject(v)) {
obj[key][i] = fixJSONStringify(v);
} else if (helpers.isDate(v)) {
obj[key][i] = `=--JSON-DATE--=${+v}`;
}
}
}
}
return obj;
};
/*
* @locus Anywhere
* @private
* @name formatFleURL
* @param {Object} fileRef - File reference object
* @param {String} version - [Optional] Version of file you would like build URL for
* @param {String} uriBase - [Optional] URI base, see - https://github.com/veliovgroup/Meteor-Files/issues/626
* @summary Returns formatted URL for file
* @returns {String} Downloadable link
*/
const formatFleURL = (fileRef, version = 'original', _uriBase = (__meteor_runtime_config__ || {}).ROOT_URL) => {
check(fileRef, Object);
check(version, String);
let uriBase = _uriBase;
if (!helpers.isString(uriBase)) {
uriBase = (__meteor_runtime_config__ || {}).ROOT_URL || '/';
}
const _root = uriBase.replace(/\/+$/, '');
const vRef = (fileRef.versions && fileRef.versions[version]) || fileRef || {};
let ext;
if (helpers.isString(vRef.extension)) {
ext = `.${vRef.extension.replace(/^\./, '')}`;
} else {
ext = '';
}
if (fileRef.public === true) {
return _root + (version === 'original' ? `${fileRef._downloadRoute}/${fileRef._id}${ext}` : `${fileRef._downloadRoute}/${version}-${fileRef._id}${ext}`);
}
return `${_root}${fileRef._downloadRoute}/${fileRef._collectionName}/${fileRef._id}/${version}/${fileRef._id}${ext}`;
};
export { fixJSONParse, fixJSONStringify, formatFleURL, helpers };