utils.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. if (payload.method == "POST" || payload.type == "POST"){
  28. //csrf token is required
  29. let csrfToken = document.getElementsByTagName("meta")["zoraxy.csrf.Token"].getAttribute("content");
  30. payload.headers = {
  31. "X-CSRF-Token": csrfToken,
  32. }
  33. }
  34. $.ajax(payload);
  35. }