Joeytje50

all kinds of funxionz u sud add :D

Nov 30th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function callAPI(data, method, callback, addurl) {
  2.     data['format'] = 'json';
  3.     $.ajax({
  4.         data: data,
  5.         dataType: 'json',
  6.         url: wgScriptPath + '/api.php' + (addurl?addurl:''),
  7.         type: method,
  8.         cache: false,
  9.         success: function(response) {
  10.             if (response.error)
  11.                 alert('API error: ' + response.error.info);
  12.             else
  13.                 callback(response);
  14.         },
  15.         error: function(xhr, error) { alert('AJAX error: ' + error) }
  16.     });
  17. }
  18.  
  19. function addCommas(nStr) {
  20.     nStr += '';
  21.     x = nStr.split('.');
  22.     x1 = x[0];
  23.     x2 = x.length > 1 ? '.' + x[1] : '';
  24.     var rgx = /(\d+)(\d{3})/;
  25.     while (rgx.test(x1)) {
  26.         x1 = x1.replace(rgx, '$1' + ',' + '$2');
  27.     }
  28.     return x1 + x2;
  29. }
  30.  
  31. function cloneObj(a,b) {
  32.     if (typeof(a)!='object') return '';
  33.     if (typeof(b)!='object') b = {};
  34.     for (var key in a) {
  35.         b[key] = a[key];
  36.     }
  37.     return b;
  38. }
  39.  
  40. function sum(arr) {
  41.     var tot = 0;
  42.     for (var i=0;i<arr.length;i++) {
  43.         tot += parseFloat(arr[i])||0;
  44.     }
  45.     return tot
  46. }
  47.  
  48. //This returns an array with the averages of the preceding  amt  values in the array. Use avg(arr)[0] to get the total average of an array.
  49. //Note: uses the above sum() function.
  50. function avg(arr,amt,round) {
  51.     amt = amt||arr.length;
  52.     round = Math.pow(10,round||0)
  53.     var avgs = [], list = [];
  54.     for (var i=0;i<arr.length;i++) {
  55.         list[i % amt] = arr[i][1]
  56.         if (i>=amt) {
  57.             avgs.push([arr[i][0], Math.round((sum(list)/list.length) * round)/round]);
  58.         }
  59.     }
  60.     return avgs;
  61. }
  62.  
  63. //OMG WHY JS NO HAVE THIS ALREADY
  64. function isInt(n) {
  65.     return !isNaN(parseInt(n));
  66. }
  67.  
  68. //Syntax: massReplace(string, [replace,with], [replace,with], [replace,with], etc, regexFlags);
  69. //Applies regexFlags (optional) to all replaces.
  70. massReplace = function() {
  71.     var str = arguments[0];
  72.     var args = arguments.slice(1)
  73.     var looplen = args.length;
  74.     if (typeof args[args.length-1] == 'string' && args[args.length-1].match(/^[gixsm]{0,5}$/)) {
  75.         var flags = arguments[arguments.length-1];
  76.         looplen = looplen-1;
  77.     } else { var flags = ''; }
  78.     for (var i=0;i<looplen;i++) {
  79.         str = str.replace(new RegExp(args[i][0], flags), args[i][1]);
  80.     }
  81.     return str;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment