const fetchHeaders = {
'Accept': 'text/html',
'Content-Type': 'text/html',
'X-Requested-With': 'XMLHttpRequest',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0'
};
/**
* Make an AJAX request.
*
* If error, catch return a promise error object:
*
* .catch(err => alert(`${err.url}\n\n${err.status} (${err.statusText})`));
*
* @param url URL of RESTful api to call
* @param options the fetch() options
* @returns {Promise}
*/
export function AjaxReq(url, options) {
let defaults = {headers: fetchHeaders};
// Extend default options
Object.assign(defaults, options);
return fetch(url, defaults)
.then(resp => {
if (resp.status === 200)
return resp.json();
else
return Promise.reject(resp);
});
}
Array.prototype.countOccurrences = function (column, val) {
return this.reduce((a, v) => (v[column] == val ? a + 1 : a), 0);
}
Array.prototype.uniq = function () {
return [...new Set(this)]
};