Code coverage report for src/core/Util.js

Statements: 95.89% (70 / 73)      Branches: 85.37% (35 / 41)      Functions: 100% (22 / 22)      Lines: 95.89% (70 / 73)     

All files » src/core/ » Util.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166        1   450     450 752 752 2877 2877       450       6 6 4         1 1 2223 2223         7   7 4   4 2 2     2   2 2   2 1 1       2         1       6 6       613       216 216       3 3 6 6     3       4 2 2   2     2         131           1       1 3     3 12     3     1   1 4     4 4     1     1     2     1 4   4     4       1 3 2             1 1 1 1  
/*
 * L.Util contains various utility functions used throughout Leaflet code.
 */
 
L.Util = {
	extend: function (dest) { // (Object[, Object, ...]) ->
		var sources = Array.prototype.slice.call(arguments, 1),
		    i, j, len, src;
 
		for (j = 0, len = sources.length; j < len; j++) {
			src = sources[j] || {};
			for (i in src) {
				Eif (src.hasOwnProperty(i)) {
					dest[i] = src[i];
				}
			}
		}
		return dest;
	},
 
	bind: function (fn, obj) { // (Function, Object) -> Function
		var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : null;
		return function () {
			return fn.apply(obj, args || arguments);
		};
	},
 
	stamp: (function () {
		var lastId = 0, key = '_leaflet_id';
		return function (/*Object*/ obj) {
			obj[key] = obj[key] || ++lastId;
			return obj[key];
		};
	}()),
 
	limitExecByInterval: function (fn, time, context) {
		var lock, execOnUnlock;
 
		return function wrapperFn() {
			var args = arguments;
 
			if (lock) {
				execOnUnlock = true;
				return;
			}
 
			lock = true;
 
			setTimeout(function () {
				lock = false;
 
				if (execOnUnlock) {
					wrapperFn.apply(context, args);
					execOnUnlock = false;
				}
			}, time);
 
			fn.apply(context, args);
		};
	},
 
	falseFn: function () {
		return false;
	},
 
	formatNum: function (num, digits) {
		var pow = Math.pow(10, digits || 5);
		return Math.round(num * pow) / pow;
	},
 
	splitWords: function (str) {
		return str.replace(/^\s+|\s+$/g, '').split(/\s+/);
	},
 
	setOptions: function (obj, options) {
		obj.options = L.extend({}, obj.options, options);
		return obj.options;
	},
 
	getParamString: function (obj, existingUrl) {
		var params = [];
		for (var i in obj) {
			Eif (obj.hasOwnProperty(i)) {
				params.push(encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]));
			}
		}
		return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
	},
 
	template: function (str, data) {
		return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
			var value = data[key];
			Iif (!data.hasOwnProperty(key)) {
				throw new Error('No value provided for variable ' + str);
			} else Iif (typeof value === 'function') {
				value = value(data);
			}
			return value;
		});
	},
 
	isArray: function (obj) {
		return (Object.prototype.toString.call(obj) === '[object Array]');
	},
 
	emptyImageUrl: 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
};
 
(function () {
 
	// inspired by http://paulirish.com/2011/requestanimationframe-for-smart-animating/
 
	function getPrefixed(name) {
		var i, fn,
		    prefixes = ['webkit', 'moz', 'o', 'ms'];
 
		for (i = 0; i < prefixes.length && !fn; i++) {
			fn = window[prefixes[i] + name];
		}
 
		return fn;
	}
 
	var lastTime = 0;
 
	function timeoutDefer(fn) {
		var time = +new Date(),
		    timeToCall = Math.max(0, 16 - (time - lastTime));
 
		lastTime = time + timeToCall;
		return window.setTimeout(fn, timeToCall);
	}
 
	var requestFn = window.requestAnimationFrame ||
	        getPrefixed('RequestAnimationFrame') || timeoutDefer;
 
	var cancelFn = window.cancelAnimationFrame ||
	        getPrefixed('CancelAnimationFrame') ||
	        getPrefixed('CancelRequestAnimationFrame') ||
	        function (id) { window.clearTimeout(id); };
 
 
	L.Util.requestAnimFrame = function (fn, context, immediate, element) {
		fn = L.bind(fn, context);
 
		Iif (immediate && requestFn === timeoutDefer) {
			fn();
		} else {
			return requestFn.call(window, fn, element);
		}
	};
 
	L.Util.cancelAnimFrame = function (id) {
		if (id) {
			cancelFn.call(window, id);
		}
	};
 
}());
 
// shortcuts for most used utility functions
L.extend = L.Util.extend;
L.bind = L.Util.bind;
L.stamp = L.Util.stamp;
L.setOptions = L.Util.setOptions;