index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict';
  2. const Assert = require('@hapi/hoek/lib/assert');
  3. const internals = {};
  4. exports.Sorter = class {
  5. constructor() {
  6. this._items = [];
  7. this.nodes = [];
  8. }
  9. add(nodes, options) {
  10. options = options || {};
  11. // Validate rules
  12. const before = [].concat(options.before || []);
  13. const after = [].concat(options.after || []);
  14. const group = options.group || '?';
  15. const sort = options.sort || 0; // Used for merging only
  16. Assert(!before.includes(group), `Item cannot come before itself: ${group}`);
  17. Assert(!before.includes('?'), 'Item cannot come before unassociated items');
  18. Assert(!after.includes(group), `Item cannot come after itself: ${group}`);
  19. Assert(!after.includes('?'), 'Item cannot come after unassociated items');
  20. if (!Array.isArray(nodes)) {
  21. nodes = [nodes];
  22. }
  23. for (const node of nodes) {
  24. const item = {
  25. seq: this._items.length,
  26. sort,
  27. before,
  28. after,
  29. group,
  30. node
  31. };
  32. this._items.push(item);
  33. }
  34. // Insert event
  35. if (!options.manual) {
  36. const valid = this._sort();
  37. Assert(valid, 'item', group !== '?' ? `added into group ${group}` : '', 'created a dependencies error');
  38. }
  39. return this.nodes;
  40. }
  41. merge(others) {
  42. if (!Array.isArray(others)) {
  43. others = [others];
  44. }
  45. for (const other of others) {
  46. if (other) {
  47. for (const item of other._items) {
  48. this._items.push(Object.assign({}, item)); // Shallow cloned
  49. }
  50. }
  51. }
  52. // Sort items
  53. this._items.sort(internals.mergeSort);
  54. for (let i = 0; i < this._items.length; ++i) {
  55. this._items[i].seq = i;
  56. }
  57. const valid = this._sort();
  58. Assert(valid, 'merge created a dependencies error');
  59. return this.nodes;
  60. }
  61. sort() {
  62. const valid = this._sort();
  63. Assert(valid, 'sort created a dependencies error');
  64. return this.nodes;
  65. }
  66. _sort() {
  67. // Construct graph
  68. const graph = {};
  69. const graphAfters = Object.create(null); // A prototype can bungle lookups w/ false positives
  70. const groups = Object.create(null);
  71. for (const item of this._items) {
  72. const seq = item.seq; // Unique across all items
  73. const group = item.group;
  74. // Determine Groups
  75. groups[group] = groups[group] || [];
  76. groups[group].push(seq);
  77. // Build intermediary graph using 'before'
  78. graph[seq] = item.before;
  79. // Build second intermediary graph with 'after'
  80. for (const after of item.after) {
  81. graphAfters[after] = graphAfters[after] || [];
  82. graphAfters[after].push(seq);
  83. }
  84. }
  85. // Expand intermediary graph
  86. for (const node in graph) {
  87. const expandedGroups = [];
  88. for (const graphNodeItem in graph[node]) {
  89. const group = graph[node][graphNodeItem];
  90. groups[group] = groups[group] || [];
  91. expandedGroups.push(...groups[group]);
  92. }
  93. graph[node] = expandedGroups;
  94. }
  95. // Merge intermediary graph using graphAfters into final graph
  96. for (const group in graphAfters) {
  97. if (groups[group]) {
  98. for (const node of groups[group]) {
  99. graph[node].push(...graphAfters[group]);
  100. }
  101. }
  102. }
  103. // Compile ancestors
  104. const ancestors = {};
  105. for (const node in graph) {
  106. const children = graph[node];
  107. for (const child of children) {
  108. ancestors[child] = ancestors[child] || [];
  109. ancestors[child].push(node);
  110. }
  111. }
  112. // Topo sort
  113. const visited = {};
  114. const sorted = [];
  115. for (let i = 0; i < this._items.length; ++i) { // Looping through item.seq values out of order
  116. let next = i;
  117. if (ancestors[i]) {
  118. next = null;
  119. for (let j = 0; j < this._items.length; ++j) { // As above, these are item.seq values
  120. if (visited[j] === true) {
  121. continue;
  122. }
  123. if (!ancestors[j]) {
  124. ancestors[j] = [];
  125. }
  126. const shouldSeeCount = ancestors[j].length;
  127. let seenCount = 0;
  128. for (let k = 0; k < shouldSeeCount; ++k) {
  129. if (visited[ancestors[j][k]]) {
  130. ++seenCount;
  131. }
  132. }
  133. if (seenCount === shouldSeeCount) {
  134. next = j;
  135. break;
  136. }
  137. }
  138. }
  139. if (next !== null) {
  140. visited[next] = true;
  141. sorted.push(next);
  142. }
  143. }
  144. if (sorted.length !== this._items.length) {
  145. return false;
  146. }
  147. const seqIndex = {};
  148. for (const item of this._items) {
  149. seqIndex[item.seq] = item;
  150. }
  151. this._items = [];
  152. this.nodes = [];
  153. for (const value of sorted) {
  154. const sortedItem = seqIndex[value];
  155. this.nodes.push(sortedItem.node);
  156. this._items.push(sortedItem);
  157. }
  158. return true;
  159. }
  160. };
  161. internals.mergeSort = (a, b) => {
  162. return a.sort === b.sort ? 0 : (a.sort < b.sort ? -1 : 1);
  163. };