Skip to content

Commit

Permalink
v1.0.1: improved register method for names conflicts checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Antonov committed Feb 21, 2016
1 parent f82c97c commit cad36bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ NanoGroup.register = function (types, constr) {
var create = function createNanoNode(id, a, b, c) {
return this.add(new constr(type, cls.uid(id), a, b, c));
};
for (var i = 0, n = hs.length; i < n; ++i)
hs[i].prototype[type] = create;
for (var i = 0, n = hs.length; i < n; ++i) {
var c = hs[i],
p = c.prototype;
if (type in p)
throw Error(c.type+'.'+type+'() method conflicts with registering constructor');
p[type] = create;
}
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nano-tree",
"version": "1.0.0",
"version": "1.0.1",
"description": "Very convenient creating of objects hieararhies library",
"main": "index.js",
"keywords": [
Expand Down
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,33 @@ suite('API', function () {
done()
});

test('Group.register() for two same constructors', function (done) {
function defnode(name, ancestor, cutoff) {
var c = new Function (name, 'type,id', 'nano.Node.call(this, type, id);');
c.prototype = {};
ancestor.expand(c, cutoff);
return c;
}
function defgroup(name, ancestor, cutoff) {
var c = new Function (name, 'type,id', 'nano.Group.call(this, type, id);');
c.prototype = {};
ancestor.expand(c, cutoff);
return c;
}

var Bow = defgroup('Bow', nano.Group);
var Arrow = defnode('Arrow', nano.Node);

Bow.register('arrow', Arrow);

try {
Bow.register('arrow', nano.Node);
} catch (e) {
return done();
}
done(Error('error not thrown'))
});

});

suite('creating', function () {
Expand Down

0 comments on commit cad36bd

Please sign in to comment.