OMGUtils.js 966 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const fetchHeaders = {
  2. 'Accept': 'text/html',
  3. 'Content-Type': 'text/html',
  4. 'X-Requested-With': 'XMLHttpRequest',
  5. 'Cache-Control': 'no-cache',
  6. 'Pragma': 'no-cache',
  7. 'Expires': '0'
  8. };
  9. /**
  10. * Make an AJAX request.
  11. *
  12. * If error, catch return a promise error object:
  13. *
  14. * .catch(err => alert(`${err.url}\n\n${err.status} (${err.statusText})`));
  15. *
  16. * @param url URL of RESTful api to call
  17. * @param options the fetch() options
  18. * @returns {Promise<Response | never>}
  19. */
  20. export function AjaxReq(url, options) {
  21. let defaults = {headers: fetchHeaders};
  22. // Extend default options
  23. Object.assign(defaults, options);
  24. return fetch(url, defaults)
  25. .then(resp => {
  26. if (resp.status === 200)
  27. return resp.json();
  28. else
  29. return Promise.reject(resp);
  30. });
  31. }
  32. Array.prototype.countOccurrences = function (column, val) {
  33. return this.reduce((a, v) => (v[column] == val ? a + 1 : a), 0);
  34. }
  35. Array.prototype.uniq = function () {
  36. return [...new Set(this)]
  37. };