utils.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. Utils.js
  3. This script holds utilities function that is commonly use
  4. across components
  5. */
  6. function abbreviateNumber(value) {
  7. var newValue = value;
  8. var suffixes = ["", "k", "m", "b", "t"];
  9. var suffixNum = 0;
  10. while (newValue >= 1000 && suffixNum < suffixes.length - 1) {
  11. newValue /= 1000;
  12. suffixNum++;
  13. }
  14. if (value > 1000){
  15. newValue = newValue.toFixed(2);
  16. }
  17. return newValue + suffixes[suffixNum];
  18. }
  19. Object.defineProperty(String.prototype, 'capitalize', {
  20. value: function() {
  21. return this.charAt(0).toUpperCase() + this.slice(1);
  22. },
  23. enumerable: false
  24. });
  25. //Add a new function to jquery for ajax override with csrf token injected
  26. $.cjax = function(payload){
  27. let requireTokenMethod = ["POST", "PUT", "DELETE"];
  28. if (requireTokenMethod.includes(payload.method) || requireTokenMethod.includes(payload.type)){
  29. //csrf token is required
  30. let csrfToken = document.getElementsByTagName("meta")["zoraxy.csrf.Token"].getAttribute("content");
  31. payload.headers = {
  32. "X-CSRF-Token": csrfToken,
  33. }
  34. }
  35. $.ajax(payload);
  36. }