diff --git a/spec/suites/core/UtilSpec.js b/spec/suites/core/UtilSpec.js index cadf79bbf89..ea1211f4b29 100644 --- a/spec/suites/core/UtilSpec.js +++ b/spec/suites/core/UtilSpec.js @@ -45,4 +45,23 @@ describe('L.Util', function() { expect(fn2()).toEqual(5); }); }); + + describe('#stamp', function() { + it('should set a unique id on the given object and return it', function() { + var a = {}, + id = L.Util.stamp(a); + + expect(typeof a.id).toEqual('number'); + expect(a.id).toEqual(id); + + L.Util.stamp(a); + + expect(a.id).toEqual(id); + + var b = {}, + id2 = L.Util.stamp(b); + + expect(id2).not.toEqual(id); + }); + }); }); \ No newline at end of file diff --git a/src/core/Util.js b/src/core/Util.js index bdda7e470d8..c4a52fa3a49 100644 --- a/src/core/Util.js +++ b/src/core/Util.js @@ -5,9 +5,8 @@ L.Util = {}; L.Util.extend = function(/*Object*/ dest) /*-> Object*/ { // merge src properties into dest - var sources = Array.prototype.slice.call(arguments, 1), - src; - for (var j = 0, len = sources.length; j < len; j++) { + var sources = Array.prototype.slice.call(arguments, 1); + for (var j = 0, len = sources.length, src; j < len; j++) { src = sources[j] || {}; for (var i in src) { if (src.hasOwnProperty(i)) { @@ -22,4 +21,12 @@ L.Util.bind = function(/*Function*/ fn, /*Object*/ obj) /*-> Object*/ { return function() { return fn.apply(obj, arguments); }; -}; \ No newline at end of file +}; + +L.Util.stamp = (function() { + var lastId = 0; + return function(/*Object*/ obj) { + obj.id = obj.id || ++lastId; + return obj.id; + }; +})(); \ No newline at end of file