Skip to content

Commit

Permalink
L.Util.stamp & specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Sep 6, 2010
1 parent 66f44ac commit 0eb9cd8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
19 changes: 19 additions & 0 deletions spec/suites/core/UtilSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
15 changes: 11 additions & 4 deletions src/core/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -22,4 +21,12 @@ L.Util.bind = function(/*Function*/ fn, /*Object*/ obj) /*-> Object*/ {
return function() {
return fn.apply(obj, arguments);
};
};
};

L.Util.stamp = (function() {
var lastId = 0;
return function(/*Object*/ obj) {
obj.id = obj.id || ++lastId;
return obj.id;
};
})();

0 comments on commit 0eb9cd8

Please sign in to comment.