@hapi/topo
Advanced tools
Comparing version 3.1.3 to 3.1.4
@@ -33,4 +33,7 @@ 'use strict'; | ||
([].concat(nodes)).forEach((node, i) => { | ||
if (!Array.isArray(nodes)) { | ||
nodes = [nodes]; | ||
} | ||
for (const node of nodes) { | ||
const item = { | ||
@@ -46,8 +49,8 @@ seq: this._items.length, | ||
this._items.push(item); | ||
}); | ||
} | ||
// Insert event | ||
const error = this._sort(); | ||
Assert(!error, 'item', (group !== '?' ? `added into group ${group}` : ''), 'created a dependencies error'); | ||
const valid = this._sort(); | ||
Assert(valid, 'item', group !== '?' ? `added into group ${group}` : '', 'created a dependencies error'); | ||
@@ -59,9 +62,10 @@ return this.nodes; | ||
others = [].concat(others); | ||
for (let i = 0; i < others.length; ++i) { | ||
const other = others[i]; | ||
if (!Array.isArray(others)) { | ||
others = [others]; | ||
} | ||
for (const other of others) { | ||
if (other) { | ||
for (let j = 0; j < other._items.length; ++j) { | ||
const item = Object.assign({}, other._items[j]); // Shallow cloned | ||
this._items.push(item); | ||
for (const item of other._items) { | ||
this._items.push(Object.assign({}, item)); // Shallow cloned | ||
} | ||
@@ -78,4 +82,4 @@ } | ||
const error = this._sort(); | ||
Assert(!error, 'merge created a dependencies error'); | ||
const valid = this._sort(); | ||
Assert(valid, 'merge created a dependencies error'); | ||
@@ -90,8 +94,7 @@ return this.nodes; | ||
const graph = {}; | ||
const graphAfters = Object.create(null); // A prototype can bungle lookups w/ false positives | ||
const graphAfters = Object.create(null); // A prototype can bungle lookups w/ false positives | ||
const groups = Object.create(null); | ||
for (let i = 0; i < this._items.length; ++i) { | ||
const item = this._items[i]; | ||
const seq = item.seq; // Unique across all items | ||
for (const item of this._items) { | ||
const seq = item.seq; // Unique across all items | ||
const group = item.group; | ||
@@ -110,5 +113,5 @@ | ||
const after = item.after; | ||
for (let j = 0; j < after.length; ++j) { | ||
graphAfters[after[j]] = (graphAfters[after[j]] || []).concat(seq); | ||
for (const after of item.after) { | ||
graphAfters[after] = graphAfters[after] || []; | ||
graphAfters[after].push(seq); | ||
} | ||
@@ -119,15 +122,9 @@ } | ||
let graphNodes = Object.keys(graph); | ||
for (let i = 0; i < graphNodes.length; ++i) { | ||
const node = graphNodes[i]; | ||
for (const node in graph) { | ||
const expandedGroups = []; | ||
const graphNodeItems = Object.keys(graph[node]); | ||
for (let j = 0; j < graphNodeItems.length; ++j) { | ||
const group = graph[node][graphNodeItems[j]]; | ||
for (const graphNodeItem in graph[node]) { | ||
const group = graph[node][graphNodeItem]; | ||
groups[group] = groups[group] || []; | ||
for (let k = 0; k < groups[group].length; ++k) { | ||
expandedGroups.push(groups[group][k]); | ||
} | ||
expandedGroups.push(...groups[group]); | ||
} | ||
@@ -140,10 +137,6 @@ | ||
const afterNodes = Object.keys(graphAfters); | ||
for (let i = 0; i < afterNodes.length; ++i) { | ||
const group = afterNodes[i]; | ||
for (const group in graphAfters) { | ||
if (groups[group]) { | ||
for (let j = 0; j < groups[group].length; ++j) { | ||
const node = groups[group][j]; | ||
graph[node] = graph[node].concat(graphAfters[group]); | ||
for (const node of groups[group]) { | ||
graph[node].push(...graphAfters[group]); | ||
} | ||
@@ -155,11 +148,8 @@ } | ||
let children; | ||
const ancestors = {}; | ||
graphNodes = Object.keys(graph); | ||
for (let i = 0; i < graphNodes.length; ++i) { | ||
const node = graphNodes[i]; | ||
children = graph[node]; | ||
for (let j = 0; j < children.length; ++j) { | ||
ancestors[children[j]] = (ancestors[children[j]] || []).concat(node); | ||
for (const node in graph) { | ||
const children = graph[node]; | ||
for (const child of children) { | ||
ancestors[child] = ancestors[child] || []; | ||
ancestors[child].push(node); | ||
} | ||
@@ -173,3 +163,3 @@ } | ||
for (let i = 0; i < this._items.length; ++i) { // Really looping thru item.seq values out of order | ||
for (let i = 0; i < this._items.length; ++i) { // Looping through item.seq values out of order | ||
let next = i; | ||
@@ -210,23 +200,24 @@ | ||
if (sorted.length !== this._items.length) { | ||
return new Error('Invalid dependencies'); | ||
return false; | ||
} | ||
const seqIndex = {}; | ||
for (let i = 0; i < this._items.length; ++i) { | ||
const item = this._items[i]; | ||
for (const item of this._items) { | ||
seqIndex[item.seq] = item; | ||
} | ||
const sortedNodes = []; | ||
this._items = sorted.map((value) => { | ||
this._items = []; | ||
this.nodes = []; | ||
for (const value of sorted) { | ||
const sortedItem = seqIndex[value]; | ||
sortedNodes.push(sortedItem.node); | ||
return sortedItem; | ||
}); | ||
this.nodes.push(sortedItem.node); | ||
this._items.push(sortedItem); | ||
} | ||
this.nodes = sortedNodes; | ||
return true; | ||
} | ||
}; | ||
internals.mergeSort = (a, b) => { | ||
@@ -233,0 +224,0 @@ |
{ | ||
"name": "@hapi/topo", | ||
"description": "Topological sorting with grouping support", | ||
"version": "3.1.3", | ||
"version": "3.1.4", | ||
"repository": "git://github.com/hapijs/topo", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"keywords": [ | ||
@@ -13,2 +14,5 @@ "topological", | ||
], | ||
"files": [ | ||
"lib" | ||
], | ||
"dependencies": { | ||
@@ -22,3 +26,3 @@ "@hapi/hoek": "8.x.x" | ||
"scripts": { | ||
"test": "lab -a @hapi/code -t 100 -L", | ||
"test": "lab -a @hapi/code -t 100 -L -Y", | ||
"test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html" | ||
@@ -25,0 +29,0 @@ }, |
<a href="http://hapijs.com"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a> | ||
# topo | ||
# @hapi/topo | ||
@@ -5,0 +5,0 @@ Topological sorting with grouping support. |
10215
6
193