Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function callAPI(data, method, callback, addurl) {
- data['format'] = 'json';
- $.ajax({
- data: data,
- dataType: 'json',
- url: wgScriptPath + '/api.php' + (addurl?addurl:''),
- type: method,
- cache: false,
- success: function(response) {
- if (response.error)
- alert('API error: ' + response.error.info);
- else
- callback(response);
- },
- error: function(xhr, error) { alert('AJAX error: ' + error) }
- });
- }
- function addCommas(nStr) {
- nStr += '';
- x = nStr.split('.');
- x1 = x[0];
- x2 = x.length > 1 ? '.' + x[1] : '';
- var rgx = /(\d+)(\d{3})/;
- while (rgx.test(x1)) {
- x1 = x1.replace(rgx, '$1' + ',' + '$2');
- }
- return x1 + x2;
- }
- function cloneObj(a,b) {
- if (typeof(a)!='object') return '';
- if (typeof(b)!='object') b = {};
- for (var key in a) {
- b[key] = a[key];
- }
- return b;
- }
- function sum(arr) {
- var tot = 0;
- for (var i=0;i<arr.length;i++) {
- tot += parseFloat(arr[i])||0;
- }
- return tot
- }
- //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.
- //Note: uses the above sum() function.
- function avg(arr,amt,round) {
- amt = amt||arr.length;
- round = Math.pow(10,round||0)
- var avgs = [], list = [];
- for (var i=0;i<arr.length;i++) {
- list[i % amt] = arr[i][1]
- if (i>=amt) {
- avgs.push([arr[i][0], Math.round((sum(list)/list.length) * round)/round]);
- }
- }
- return avgs;
- }
- //OMG WHY JS NO HAVE THIS ALREADY
- function isInt(n) {
- return !isNaN(parseInt(n));
- }
- //Syntax: massReplace(string, [replace,with], [replace,with], [replace,with], etc, regexFlags);
- //Applies regexFlags (optional) to all replaces.
- massReplace = function() {
- var str = arguments[0];
- var args = arguments.slice(1)
- var looplen = args.length;
- if (typeof args[args.length-1] == 'string' && args[args.length-1].match(/^[gixsm]{0,5}$/)) {
- var flags = arguments[arguments.length-1];
- looplen = looplen-1;
- } else { var flags = ''; }
- for (var i=0;i<looplen;i++) {
- str = str.replace(new RegExp(args[i][0], flags), args[i][1]);
- }
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment