Comparing version 0.3.3 to 0.3.4
@@ -188,6 +188,7 @@ var Q = require('q'), | ||
*/ | ||
removeNode: function(id) { | ||
removeNode: function(id, leaveInChildren) { | ||
id = U.getNodeId(id); | ||
U.removeNode(this.children, this.parents, id); | ||
U.removeNode(this.children, this.parents, id, leaveInChildren); | ||
delete this.nodes[id]; | ||
@@ -197,3 +198,3 @@ | ||
var p = this.plans[k]; | ||
if (p.hasNode(id)) p.removeNode(id); | ||
if (p.hasNode(id)) p.removeTree(id); | ||
} | ||
@@ -377,8 +378,16 @@ | ||
var children = this.children[id]; | ||
var children = this.children[id], | ||
treeIds = {}, | ||
cId; | ||
children.forEach(function(cId) { | ||
this._removeTree(cId, forced); | ||
}, this); | ||
treeIds[id] = 1; | ||
U.addArrayToObject(children, treeIds); | ||
for (var i = 0; i < children.length; ) { | ||
this._removeTree(children[i], treeIds, forced); | ||
if (cId === children[i]) i++; | ||
cId = children[i]; | ||
} | ||
this.removeNode(id); | ||
@@ -389,10 +398,15 @@ | ||
_removeTree: function(id, forced) { | ||
if (this.parents[id].length === 1 || forced) { | ||
var children = this.children[id]; | ||
_removeTree: function(id, treeIds, forced) { | ||
if (this.parents[id].length === 1 || forced || U.isArrayInObject(this.parents[id], treeIds)) { | ||
var children = this.children[id], | ||
cId; | ||
children.forEach(function(cId) { | ||
this._removeTree(cId, forced); | ||
}, this); | ||
U.addArrayToObject(children, treeIds); | ||
for (var i = 0; i < children.length; ) { | ||
this._removeTree(children[i], treeIds, forced); | ||
if (cId === children[i]) i++; | ||
cId = children[i]; | ||
} | ||
this.removeNode(id); | ||
@@ -399,0 +413,0 @@ } |
var INHERIT = require('inherit'), | ||
ASSERTS = require('./asserts'), | ||
util = require('./util'), | ||
U = require('./util'), | ||
EventEmitter = require('events').EventEmitter, | ||
@@ -46,3 +46,3 @@ | ||
init: function(targets) { | ||
var ids = this._getIDs(util.toArray(targets)); | ||
var ids = this._getIDs(U.toArray(targets)); | ||
for (var id in ids) { | ||
@@ -78,3 +78,3 @@ this.link(id); | ||
if (Array.isArray(args[0])) { | ||
return util.arrayToObject(args[0]); | ||
return U.arrayToObject(args[0]); | ||
} else { | ||
@@ -116,3 +116,3 @@ var ids = {}; | ||
rescanJobs: function(ids) { | ||
util.toArray(ids).forEach(function(id) { | ||
U.toArray(ids).forEach(function(id) { | ||
this.removeJobs(this.parents[id]); | ||
@@ -324,5 +324,5 @@ this.children[id].length || this.addJob(id); | ||
link: function(children, parents) { | ||
parents = util.toArray(parents || [this.root]); | ||
parents = U.toArray(parents || [this.root]); | ||
util.toArray(children).forEach(function(child) { | ||
U.toArray(children).forEach(function(child) { | ||
parents.forEach(function(parent) { | ||
@@ -408,4 +408,4 @@ this.injectSubArch(child, parent); | ||
if (parents1 && parents2) { | ||
util.unlink(parents1, this.children[id1], id2); | ||
util.unlink(parents2, this.children[id2], id1); | ||
U.unlink(parents1, this.children[id1], id2); | ||
U.unlink(parents2, this.children[id2], id1); | ||
@@ -442,3 +442,3 @@ if (!parents1.length) this.link(id1, this.root); | ||
return util.hasLink(id, this, 'parents', parents); | ||
return U.hasLink(id, this, 'parents', parents); | ||
}, | ||
@@ -457,3 +457,3 @@ | ||
return util.hasLink(id, this, 'children', children); | ||
return U.hasLink(id, this, 'children', children); | ||
}, | ||
@@ -474,3 +474,3 @@ | ||
util.removeNode(this.children, this.parents, id); | ||
U.removeNode(this.children, this.parents, id); | ||
@@ -497,8 +497,16 @@ this.removeJob(id); | ||
var children = this.children[id]; | ||
children.forEach(function(cId) { | ||
this._removeTree(cId, forced); | ||
}, this); | ||
var children = this.children[id], | ||
treeIds = {}, | ||
cId; | ||
treeIds[id] = 1; | ||
U.addArrayToObject(children, treeIds); | ||
for (var i = 0; i < children.length; ) { | ||
this._removeTree(children[i], treeIds, forced); | ||
if (cId === children[i]) i++; | ||
cId = children[i]; | ||
} | ||
this.removeNode(id); | ||
@@ -509,10 +517,17 @@ | ||
_removeTree: function(id, forced) { | ||
if (this.parents[id].length === 1 || forced) { | ||
var children = this.children[id]; | ||
_removeTree: function(id, treeIds, forced) { | ||
ASSERTS.notActiveJob(id, this, 'Can\'t remove active job(' + id + ') from plan(' + this.getId() + ').'); | ||
children.forEach(function(cId) { | ||
this._removeTree(cId, forced); | ||
}, this); | ||
if (this.parents[id].length === 1 || forced || U.isArrayInObject(this.parents[id], treeIds)) { | ||
var children = this.children[id], | ||
cId; | ||
U.addArrayToObject(children, treeIds); | ||
for (var i = 0; i < children.length; ) { | ||
this._removeTree(children[i], treeIds, forced); | ||
if (cId === children[i]) i++; | ||
cId = children[i]; | ||
} | ||
this.removeNode(id); | ||
@@ -519,0 +534,0 @@ } |
@@ -111,2 +111,31 @@ /** | ||
/** | ||
* Check if all array elements are covered by object keys. | ||
* | ||
* @param {Array} a Array to check. | ||
* @param {Object} o Object to use. | ||
* @returns {Boolean} True if yes, otherwise false. | ||
*/ | ||
exports.isArrayInObject = function(a, o) { | ||
for (var i = 0; i < a.length; i++) { | ||
if (!(a[i] in o)) return false; | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Add array elements to object as keys. | ||
* | ||
* @param {Array} a Array to add from. | ||
* @param {Object} o Object to add to. | ||
* @returns {Object} Same object as in argument. | ||
*/ | ||
exports.addArrayToObject = function(a, o) { | ||
for (var i = 0; i < a.length; i++) { | ||
o[a[i]] = 1; | ||
} | ||
return o; | ||
}; | ||
/** | ||
* Adopted from jquery's extend method. Under the terms of MIT License. | ||
@@ -113,0 +142,0 @@ * |
{ | ||
"name": "apw", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"homepage": "http://github.com/bem/apw", | ||
@@ -5,0 +5,0 @@ "author": "Sergey Kryzhanovsky <skryzhanovsky@ya.ru> (http://github.com/afelix)", |
@@ -328,2 +328,21 @@ var APW = require(process.env.COVER? '../lib-cov/apw' : '../lib/apw'), | ||
describe('Remove tree (rhombus arch)', function() { | ||
beforeEach(function() { | ||
arch = getEmptyArch() | ||
.addNode(createNode('A')) | ||
.addNode(createNode('B'), 'A') | ||
.addNode(createNode('C'), 'A') | ||
.addNode(createNode('D'), ['B', 'C']); | ||
}); | ||
it('removeTree() A unforced', function() { | ||
arch.removeTree('A'); | ||
ASSERT.equal(arch.hasNode('A'), false); | ||
ASSERT.equal(arch.hasNode('B'), false); | ||
ASSERT.equal(arch.hasNode('C'), false); | ||
ASSERT.equal(arch.hasNode('D'), false); | ||
}); | ||
}); | ||
describe('Lock', function() { | ||
@@ -330,0 +349,0 @@ beforeEach(function() { |
91519
2349