Comparing version 0.4.1 to 0.5.0
"use strict"; | ||
exports.__esModule = true; | ||
exports.UIDMock = void 0; | ||
var _dag = require("../dag"); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var UIDMock = /*#__PURE__*/function () { | ||
function UIDMock() { | ||
_defineProperty(this, "_id", void 0); | ||
this._id = UIDMock._counter++; | ||
} | ||
var _proto = UIDMock.prototype; | ||
_proto.newUID = function newUID() { | ||
return new UIDMock(); | ||
}; | ||
_proto.equals = function equals(uid) { | ||
return uid instanceof UIDMock && this._id === uid._id; | ||
}; | ||
return UIDMock; | ||
}(); | ||
exports.UIDMock = UIDMock; | ||
_defineProperty(UIDMock, "_counter", 0); | ||
describe('Dag with UIDMock', function () { | ||
describe('Dag', function () { | ||
var dag; | ||
beforeEach(function () { | ||
dag = new _dag.Dag(UIDMock); | ||
dag = new _dag.Dag(); | ||
}); | ||
@@ -42,3 +13,3 @@ describe('constructor', function () { | ||
expect(function () { | ||
return new _dag.Dag(UIDMock); | ||
return new _dag.Dag(); | ||
}).not.toThrow(); | ||
@@ -123,7 +94,2 @@ }); | ||
}); | ||
it('Should throw an error in case of orphan given node', function () { | ||
expect(function () { | ||
return dag.getParents(new dag.uid()); | ||
}).toThrowError("node doesn't belong to this graph"); | ||
}); | ||
}); | ||
@@ -137,17 +103,2 @@ describe('setParenthood', function () { | ||
}); | ||
it('Should throw an error in case of both orphan UIDs', function () { | ||
expect(function () { | ||
return dag.setParenthood(new dag.uid(), new dag.uid()); | ||
}).toThrowError(); | ||
}); | ||
it('Should throw an Error in case of orphan currentNode', function () { | ||
expect(function () { | ||
return dag.setParenthood(new dag.uid(), parent); | ||
}).toThrowError("Child node doesn't belong to this graph"); | ||
}); | ||
it('Should throw an Error in case of orphan parent', function () { | ||
expect(function () { | ||
return dag.setParenthood(child, new dag.uid()); | ||
}).toThrowError("Parent node doesn't belong to this graph"); | ||
}); | ||
it('Should return this dag', function () { | ||
@@ -194,17 +145,2 @@ expect(dag.setParenthood(child, parent)).toBe(dag); | ||
}); | ||
it('Should throw an error in case of both orphan UIDs', function () { | ||
expect(function () { | ||
return dag.removeParenthood(new dag.uid(), new dag.uid()); | ||
}).toThrowError(); | ||
}); | ||
it('Should throw an Error in case of orphan currentNode', function () { | ||
expect(function () { | ||
return dag.removeParenthood(new dag.uid(), parent); | ||
}).toThrowError("Child node doesn't belong to this graph"); | ||
}); | ||
it('Should throw an Error in case of orphan parent', function () { | ||
expect(function () { | ||
return dag.removeParenthood(child, new dag.uid()); | ||
}).toThrowError("Parent node doesn't belong to this graph"); | ||
}); | ||
it('Should return this dag', function () { | ||
@@ -238,7 +174,2 @@ expect(dag.removeParenthood(child, parent)).toBe(dag); | ||
}); | ||
it('Should throw an error in case of orphan given node', function () { | ||
expect(function () { | ||
return dag.getChildren(new dag.uid()); | ||
}).toThrowError("node doesn't belong to this graph"); | ||
}); | ||
}); | ||
@@ -245,0 +176,0 @@ describe('isDescendant', function () { |
@@ -6,73 +6,21 @@ "use strict"; | ||
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); } | ||
var _dagBase = require("@dags/dag-base"); | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
var _uidUuid = require("@dags/uid-uuid"); | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
/** | ||
* Required interface of the UID Constructor | ||
*/ | ||
/** | ||
* Required interface of the Dag | ||
*/ | ||
/** | ||
* Provides DAG - Directed Acyclic Graph functionality. | ||
*/ | ||
var Dag = /*#__PURE__*/function () { | ||
/** | ||
* Set of nodes of the graph | ||
* @type {Set} | ||
* @private | ||
*/ | ||
function Dag() { | ||
_defineProperty(this, "_dagBase", new _dagBase.DagBase(_uidUuid.UUID)); | ||
} | ||
/** | ||
* Map of nodes to their children. | ||
* @type {Map} | ||
* @private | ||
*/ | ||
var _proto = Dag.prototype; | ||
/** | ||
* Map of nodes to their parents. | ||
* @type {Map} | ||
* @private | ||
*/ | ||
/** | ||
* Creates a DAG. | ||
* @class a Directed Acyclic Graph. | ||
* @constructor | ||
* @param uid constructor for UIDs | ||
*/ | ||
function Dag(uid) { | ||
this.uid = uid; | ||
_defineProperty(this, "_nodes", new Set()); | ||
_defineProperty(this, "_childMap", new Map()); | ||
_defineProperty(this, "_parentMap", new Map()); | ||
} | ||
/** | ||
* Create new node of this graph. | ||
* @return {UID} uid of the new node | ||
*/ | ||
var _proto = Dag.prototype; | ||
_proto.newNode = function newNode() { | ||
var nodeUID = new this.uid(); | ||
this._nodes.add(nodeUID); | ||
this._parentMap.set(nodeUID, new Set()); | ||
this._childMap.set(nodeUID, new Set()); | ||
return nodeUID; | ||
return this._dagBase.newNode(); | ||
} | ||
@@ -87,14 +35,4 @@ /** | ||
_proto.deleteNode = function deleteNode(node) { | ||
for (var _iterator = _createForOfIteratorHelperLoose(this.getParents(node)), _step; !(_step = _iterator()).done;) { | ||
var parent = _step.value; | ||
this.removeParenthood(node, parent); | ||
} | ||
this._dagBase.deleteNode(node); | ||
for (var _iterator2 = _createForOfIteratorHelperLoose(this.getChildren(node)), _step2; !(_step2 = _iterator2()).done;) { | ||
var child = _step2.value; | ||
this.removeParenthood(child, node); | ||
} | ||
this._nodes["delete"](node); | ||
return this; | ||
@@ -108,3 +46,3 @@ } | ||
_proto.getNodes = function getNodes() { | ||
return this._nodes; | ||
return this._dagBase.getNodes(); | ||
} | ||
@@ -119,5 +57,3 @@ /** | ||
_proto.getParents = function getParents(node) { | ||
if (!this._nodes.has(node)) throw new Error("node doesn't belong to this graph"); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this._parentMap.get(node); | ||
return this._dagBase.getParents(node); | ||
} | ||
@@ -132,5 +68,3 @@ /** | ||
_proto.getChildren = function getChildren(node) { | ||
if (!this._nodes.has(node)) throw new Error("node doesn't belong to this graph"); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this._childMap.get(node); | ||
return this._dagBase.getChildren(node); | ||
} | ||
@@ -144,7 +78,4 @@ /** | ||
_proto.setParenthood = function setParenthood(child, parent) { | ||
if (!this._nodes.has(child)) throw new Error("Child node doesn't belong to this graph"); | ||
if (!this._nodes.has(parent)) throw new Error("Parent node doesn't belong to this graph"); | ||
this.checkCycle(child, parent); | ||
this.getParents(child).add(parent); | ||
this.getChildren(parent).add(child); | ||
this._dagBase.setParenthood(child, parent); | ||
return this; | ||
@@ -160,6 +91,4 @@ } | ||
_proto.removeParenthood = function removeParenthood(child, parent) { | ||
if (!this._nodes.has(child)) throw new Error("Child node doesn't belong to this graph"); | ||
if (!this._nodes.has(parent)) throw new Error("Parent node doesn't belong to this graph"); | ||
this.getParents(child)["delete"](parent); | ||
this.getChildren(parent)["delete"](child); | ||
this._dagBase.removeParenthood(child, parent); | ||
return this; | ||
@@ -175,19 +104,5 @@ } | ||
_proto.isDescendant = function isDescendant(current, tested) { | ||
if (current.equals(tested)) return true; | ||
for (var _iterator3 = _createForOfIteratorHelperLoose(this.getChildren(current)), _step3; !(_step3 = _iterator3()).done;) { | ||
var child = _step3.value; | ||
if (this.isDescendant(child, tested)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
return this._dagBase.isDescendant(current, tested); | ||
}; | ||
_proto.checkCycle = function checkCycle(child, parent) { | ||
if (this.isDescendant(child, parent)) throw new Error('The Parent-child relationship is not possible: this parenthood establishing leads to a cycle'); | ||
}; | ||
return Dag; | ||
@@ -194,0 +109,0 @@ }(); |
@@ -9,4 +9,5 @@ "use strict"; | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _dag[key]) return; | ||
exports[key] = _dag[key]; | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -1,32 +0,11 @@ | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import { Dag } from '../dag'; // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import { Dag } from '../dag'; | ||
export class UIDMock { | ||
constructor() { | ||
_defineProperty(this, "_id", void 0); | ||
this._id = UIDMock._counter++; | ||
} | ||
newUID() { | ||
return new UIDMock(); | ||
} | ||
equals(uid) { | ||
return uid instanceof UIDMock && this._id === uid._id; | ||
} | ||
} | ||
_defineProperty(UIDMock, "_counter", 0); | ||
describe('Dag with UIDMock', () => { | ||
describe('Dag', () => { | ||
var dag; | ||
beforeEach(function () { | ||
dag = new Dag(UIDMock); | ||
dag = new Dag(); | ||
}); | ||
describe('constructor', () => { | ||
it('Should execute without any problem', () => { | ||
expect(() => new Dag(UIDMock)).not.toThrow(); | ||
expect(() => new Dag()).not.toThrow(); | ||
}); | ||
@@ -110,7 +89,2 @@ it('Should return an empty nodeset', () => { | ||
}); | ||
it('Should throw an error in case of orphan given node', () => { | ||
expect(() => { | ||
return dag.getParents(new dag.uid()); | ||
}).toThrowError("node doesn't belong to this graph"); | ||
}); | ||
}); | ||
@@ -124,11 +98,2 @@ describe('setParenthood', () => { | ||
}); | ||
it('Should throw an error in case of both orphan UIDs', () => { | ||
expect(() => dag.setParenthood(new dag.uid(), new dag.uid())).toThrowError(); | ||
}); | ||
it('Should throw an Error in case of orphan currentNode', () => { | ||
expect(() => dag.setParenthood(new dag.uid(), parent)).toThrowError("Child node doesn't belong to this graph"); | ||
}); | ||
it('Should throw an Error in case of orphan parent', () => { | ||
expect(() => dag.setParenthood(child, new dag.uid())).toThrowError("Parent node doesn't belong to this graph"); | ||
}); | ||
it('Should return this dag', () => { | ||
@@ -169,11 +134,2 @@ expect(dag.setParenthood(child, parent)).toBe(dag); | ||
}); | ||
it('Should throw an error in case of both orphan UIDs', () => { | ||
expect(() => dag.removeParenthood(new dag.uid(), new dag.uid())).toThrowError(); | ||
}); | ||
it('Should throw an Error in case of orphan currentNode', () => { | ||
expect(() => dag.removeParenthood(new dag.uid(), parent)).toThrowError("Child node doesn't belong to this graph"); | ||
}); | ||
it('Should throw an Error in case of orphan parent', () => { | ||
expect(() => dag.removeParenthood(child, new dag.uid())).toThrowError("Parent node doesn't belong to this graph"); | ||
}); | ||
it('Should return this dag', () => { | ||
@@ -207,5 +163,2 @@ expect(dag.removeParenthood(child, parent)).toBe(dag); | ||
}); | ||
it('Should throw an error in case of orphan given node', () => { | ||
expect(() => dag.getChildren(new dag.uid())).toThrowError("node doesn't belong to this graph"); | ||
}); | ||
}); | ||
@@ -212,0 +165,0 @@ describe('isDescendant', () => { |
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
/** | ||
* Required interface of the UID Constructor | ||
*/ | ||
/** | ||
* Required interface of the Dag | ||
*/ | ||
/** | ||
* Provides DAG - Directed Acyclic Graph functionality. | ||
*/ | ||
import { DagBase } from '@dags/dag-base'; | ||
import { UUID } from '@dags/uid-uuid'; | ||
export class Dag { | ||
/** | ||
* Set of nodes of the graph | ||
* @type {Set} | ||
* @private | ||
*/ | ||
constructor() { | ||
_defineProperty(this, "_dagBase", new DagBase(UUID)); | ||
} | ||
/** | ||
* Map of nodes to their children. | ||
* @type {Map} | ||
* @private | ||
*/ | ||
/** | ||
* Map of nodes to their parents. | ||
* @type {Map} | ||
* @private | ||
*/ | ||
/** | ||
* Creates a DAG. | ||
* @class a Directed Acyclic Graph. | ||
* @constructor | ||
* @param uid constructor for UIDs | ||
*/ | ||
constructor(uid) { | ||
this.uid = uid; | ||
_defineProperty(this, "_nodes", new Set()); | ||
_defineProperty(this, "_childMap", new Map()); | ||
_defineProperty(this, "_parentMap", new Map()); | ||
} | ||
/** | ||
* Create new node of this graph. | ||
* @return {UID} uid of the new node | ||
*/ | ||
newNode() { | ||
var nodeUID = new this.uid(); | ||
this._nodes.add(nodeUID); | ||
this._parentMap.set(nodeUID, new Set()); | ||
this._childMap.set(nodeUID, new Set()); | ||
return nodeUID; | ||
return this._dagBase.newNode(); | ||
} | ||
@@ -73,12 +28,4 @@ /** | ||
deleteNode(node) { | ||
for (var parent of this.getParents(node)) { | ||
this.removeParenthood(node, parent); | ||
} | ||
this._dagBase.deleteNode(node); | ||
for (var child of this.getChildren(node)) { | ||
this.removeParenthood(child, node); | ||
} | ||
this._nodes.delete(node); | ||
return this; | ||
@@ -92,3 +39,3 @@ } | ||
getNodes() { | ||
return this._nodes; | ||
return this._dagBase.getNodes(); | ||
} | ||
@@ -103,5 +50,3 @@ /** | ||
getParents(node) { | ||
if (!this._nodes.has(node)) throw new Error("node doesn't belong to this graph"); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this._parentMap.get(node); | ||
return this._dagBase.getParents(node); | ||
} | ||
@@ -116,5 +61,3 @@ /** | ||
getChildren(node) { | ||
if (!this._nodes.has(node)) throw new Error("node doesn't belong to this graph"); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this._childMap.get(node); | ||
return this._dagBase.getChildren(node); | ||
} | ||
@@ -128,7 +71,4 @@ /** | ||
setParenthood(child, parent) { | ||
if (!this._nodes.has(child)) throw new Error("Child node doesn't belong to this graph"); | ||
if (!this._nodes.has(parent)) throw new Error("Parent node doesn't belong to this graph"); | ||
this.checkCycle(child, parent); | ||
this.getParents(child).add(parent); | ||
this.getChildren(parent).add(child); | ||
this._dagBase.setParenthood(child, parent); | ||
return this; | ||
@@ -144,6 +84,4 @@ } | ||
removeParenthood(child, parent) { | ||
if (!this._nodes.has(child)) throw new Error("Child node doesn't belong to this graph"); | ||
if (!this._nodes.has(parent)) throw new Error("Parent node doesn't belong to this graph"); | ||
this.getParents(child).delete(parent); | ||
this.getChildren(parent).delete(child); | ||
this._dagBase.removeParenthood(child, parent); | ||
return this; | ||
@@ -159,18 +97,6 @@ } | ||
isDescendant(current, tested) { | ||
if (current.equals(tested)) return true; | ||
for (var child of this.getChildren(current)) { | ||
if (this.isDescendant(child, tested)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
return this._dagBase.isDescendant(current, tested); | ||
} | ||
checkCycle(child, parent) { | ||
if (this.isDescendant(child, parent)) throw new Error('The Parent-child relationship is not possible: this parenthood establishing leads to a cycle'); | ||
} | ||
} | ||
//# sourceMappingURL=dag.js.map |
@@ -1,8 +0,1 @@ | ||
import { UID } from '../dag'; | ||
export declare class UIDMock implements UID { | ||
private static _counter; | ||
private readonly _id; | ||
constructor(); | ||
newUID(): UID; | ||
equals(uid: UID): boolean; | ||
} | ||
export {}; |
/** | ||
* Required interface of the UID Constructor | ||
*/ | ||
export interface UIDConstructor { | ||
new (): UID; | ||
} | ||
/** | ||
* Required interface of the Dag | ||
*/ | ||
export interface UID { | ||
/** | ||
* Check equality to the given UID | ||
* @param uid | ||
*/ | ||
equals(uid: UID): boolean; | ||
} | ||
/** | ||
* Provides DAG - Directed Acyclic Graph functionality. | ||
*/ | ||
import { UID } from '@dags/dag-base'; | ||
export declare class Dag { | ||
uid: UIDConstructor; | ||
/** | ||
* Set of nodes of the graph | ||
* @type {Set} | ||
* Dag implementation on which this implementation is based | ||
* @private | ||
*/ | ||
private _nodes; | ||
private _dagBase; | ||
/** | ||
* Map of nodes to their children. | ||
* @type {Map} | ||
* @private | ||
*/ | ||
private _childMap; | ||
/** | ||
* Map of nodes to their parents. | ||
* @type {Map} | ||
* @private | ||
*/ | ||
private _parentMap; | ||
/** | ||
* Creates a DAG. | ||
* @class a Directed Acyclic Graph. | ||
* @constructor | ||
* @param uid constructor for UIDs | ||
*/ | ||
constructor(uid: UIDConstructor); | ||
/** | ||
* Create new node of this graph. | ||
@@ -91,3 +55,2 @@ * @return {UID} uid of the new node | ||
isDescendant(current: UID, tested: UID): boolean; | ||
private checkCycle; | ||
} |
export * from './dag'; |
{ | ||
"name": "@dags/dag", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Directed Acyclic Graph implementation", | ||
@@ -30,2 +30,6 @@ "main": "dist/cjs/index.js", | ||
}, | ||
"dependencies": { | ||
"@dags/dag-base": "^0.x", | ||
"@dags/uid-uuid": "^0.x" | ||
}, | ||
"files": [ | ||
@@ -39,2 +43,3 @@ "*.js", | ||
"dag", | ||
"global", | ||
"graph", | ||
@@ -47,3 +52,3 @@ "typescript" | ||
"sideEffects": false, | ||
"gitHead": "ff43ad327ae039f0aa36d7c3fffd612f7bfc7948" | ||
"gitHead": "485537231c053f6e3ab753398c986f0e7be49278" | ||
} |
@@ -1,1 +0,1 @@ | ||
# This library implements a Direct Acyclic Graph (DAG) in TypeScript. | ||
# Directed Acyclic Graph in TypeScript |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
68826
2
617
+ Added@dags/dag-base@^0.x
+ Added@dags/uid-uuid@^0.x
+ Added@dags/dag-base@0.2.2(transitive)
+ Added@dags/uid-uuid@0.5.1(transitive)
+ Added@types/uuid@8.3.4(transitive)
+ Addeduuid@8.3.2(transitive)