helper.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. String.prototype.capitalize = function()
  19. {
  20. return this.charAt(0).toUpperCase() + this.slice(1);
  21. }
  22. Em.CoreObject.reopen({
  23. t: function(key, attrs){
  24. return Em.I18n.t(key, attrs)
  25. }
  26. });
  27. Handlebars.registerHelper('log', function(variable) {
  28. console.log(variable);
  29. });
  30. Handlebars.registerHelper('warn', function(variable) {
  31. console.warn(variable);
  32. });
  33. String.prototype.format = function() {
  34. var args = arguments;
  35. return this.replace(/{(\d+)}/g, function(match, number) {
  36. return typeof args[number] != 'undefined' ? args[number] : match;
  37. });
  38. };
  39. Number.prototype.bytesToSize = function(precision) {
  40. var value = this;
  41. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  42. var posttxt = 0;
  43. if (this == 0) return 'n/a';
  44. while( value >= 1024 ) {
  45. posttxt++;
  46. value = value / 1024;
  47. }
  48. return parseInt(value).toFixed(precision) + " " + sizes[posttxt];
  49. }
  50. Number.prototype.toDaysHoursMinutes = function() {
  51. var formatted = {},
  52. dateDiff = this,
  53. minK = 60, // sec
  54. hourK = 60 * minK, // sec
  55. dayK = 24 * hourK;
  56. dateDiff = parseInt(dateDiff/1000);
  57. formatted.d = Math.floor(dateDiff/dayK);
  58. dateDiff -= formatted.d * dayK;
  59. formatted.h = Math.floor(dateDiff/hourK);
  60. dateDiff -= formatted.h * hourK;
  61. formatted.m = Math.floor(dateDiff/minK);
  62. dateDiff -= formatted.m * minK;
  63. return formatted;
  64. }